001/* 002 * Copyright 2024 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.mobileapp.action; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.environment.Request; 024 025import org.ametys.plugins.mobileapp.PostConstants; 026import org.ametys.plugins.mobileapp.SiteHelper; 027 028/** 029 * Returns the site from an URL 030 */ 031public class GetSiteAction extends AbstractPostAction 032{ 033 private SiteHelper _siteHelper; 034 035 @Override 036 public void service(ServiceManager smanager) throws ServiceException 037 { 038 super.service(smanager); 039 _siteHelper = (SiteHelper) smanager.lookup(SiteHelper.ROLE); 040 } 041 042 @Override 043 protected Map<String, Object> doAction(Request request, Map<String, Object> jsonParams) 044 { 045 Map<String, Object> result = new HashMap<>(); 046 047 String url = (String) getParameter(PostConstants.URL, jsonParams, request); 048 if (url.endsWith("/")) 049 { 050 url = url.substring(0, url.length() - 1); 051 } 052 String siteName = _siteHelper.getSiteByUrl(url); 053 054 if (siteName == null) 055 { 056 result.put("code", 500); 057 throw new RuntimeException("Unable to retrieve site with url: " + url); 058 } 059 060 result.put("site_name", siteName); 061 062 result.put("code", 200); 063 064 return result; 065 } 066}