001/*
002 *  Copyright 2022 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.web.service;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.acting.AbstractAction;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.commons.lang3.StringUtils;
030
031import org.ametys.core.util.JSONUtils;
032import org.ametys.web.WebConstants;
033import org.ametys.web.data.type.ModelItemTypeExtensionPoint;
034import org.ametys.web.repository.page.StaticZoneItem;
035import org.ametys.web.repository.page.Zone;
036import org.ametys.web.repository.page.ZoneItem;
037import org.ametys.web.repository.site.Site;
038import org.ametys.web.repository.site.SiteManager;
039import org.ametys.web.repository.sitemap.Sitemap;
040
041/**
042 * Action to get service
043 */
044public class GetServiceAction extends AbstractAction implements Serviceable
045{
046    /** The service extension point */
047    protected ServiceExtensionPoint _serviceEP;
048    
049    /** The site manager */
050    protected SiteManager _siteManager;
051    
052    /** The JSON utils */
053    protected JSONUtils _jsonUtils;
054    
055    /** The page data type extension point */
056    protected ModelItemTypeExtensionPoint _pageDataTypeExtensionPoint;
057    
058    @Override
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        _serviceEP = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
062        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
063        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
064        _pageDataTypeExtensionPoint = (ModelItemTypeExtensionPoint) manager.lookup(ModelItemTypeExtensionPoint.ROLE_PAGE_DATA);
065    }
066    
067    @Override
068    public Map<String, String> act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
069    {
070        Request request = ObjectModelHelper.getRequest(objectModel);
071        
072        String serviceId = parameters.getParameter("serviceId", null);
073        String siteName = parameters.getParameter("siteName", null);
074        String language = parameters.getParameter("lang", null);
075        
076        if (StringUtils.isBlank(serviceId) || StringUtils.isBlank(siteName) || StringUtils.isBlank(language))
077        {
078            throw new IllegalArgumentException("The service identifier, the site name and the language are mandatory to get service. Some of these parameters are missing.");
079        }
080        
081        Service service = _serviceEP.getExtension(serviceId);
082        Site site = _siteManager.getSite(siteName);
083        Sitemap sitemap = site.getSitemap(language);
084        
085        String serviceParamsAsJson = parameters.getParameter("serviceParams", null);
086        Map<String, Object> serviceParams = _jsonUtils.convertJsonToMap(serviceParamsAsJson);
087        
088        ZoneItem zoneItem = new StaticZoneItem(service, serviceParams, _pageDataTypeExtensionPoint);
089        
090        // PageGenerator emulator
091        request.setAttribute(WebConstants.REQUEST_ATTR_ZONEITEM, zoneItem); 
092        request.setAttribute(Zone.class.getName(), "default");
093
094        // PageMatcher emulator
095        request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, siteName);
096        request.setAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME, language);
097        request.setAttribute(WebConstants.REQUEST_ATTR_SITE, site);
098        request.setAttribute(WebConstants.REQUEST_ATTR_SITEMAP, sitemap);
099        request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, site.getSkinId());
100        
101        Map<String, String> result = new HashMap<>();
102        
103        result.put("site", site.getName());
104        result.put("sitemapLanguage", language);
105        result.put("skin", site.getSkinId());
106        return result;
107    }
108}