001/*
002 *  Copyright 2025 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.pwa;
017
018import java.util.List;
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.cocoon.environment.SourceResolver;
030import org.apache.commons.lang3.StringUtils;
031
032import org.ametys.core.cocoon.JSonReader;
033import org.ametys.plugins.repository.UnknownAmetysObjectException;
034import org.ametys.web.renderingcontext.RenderingContext;
035import org.ametys.web.renderingcontext.RenderingContextHandler;
036import org.ametys.web.repository.site.Site;
037import org.ametys.web.repository.site.SiteManager;
038
039/**
040 * 
041 * Generates the manifest file for the PWA for the given site
042 */
043public class ManifestAction extends AbstractAction implements Serviceable
044{
045    private SiteManager _siteManager;
046    private RenderingContextHandler _renderingContextHandler;
047
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
051        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
052    }
053    
054    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String siteName, Parameters parameters) throws Exception
055    {
056        if (_renderingContextHandler.getRenderingContext() != RenderingContext.FRONT)
057        {
058            // We only want manifest in the Front
059            return null;
060        }
061        
062        Site site;
063        try
064        {
065            site = _siteManager.getSite(siteName);
066            if (site == null)
067            {
068                return null;
069            }
070        }
071        catch (UnknownAmetysObjectException e)
072        {
073            getLogger().warn("Cannot generator the manifest for the unknown site " + siteName, e);
074            return null;
075        }
076        
077        Request request = ObjectModelHelper.getRequest(objectModel);
078            
079        Map<String, Object> manifest = Map.of(
080            "name", site.getTitle(),
081            "short_name", site.getTitle(),
082            
083            // Hard to get the site color here, since Site.getColor only return a palette index
084            "theme_color", "#ddd7eb", // Color of UI items around the window
085            "background_color", "#ffffff", // Color of background while loading the window => needs to be the background color of the page to be as smooth as possible
086            
087            "display", StringUtils.equals(request.getParameter("display"), "minimal-ui") ? "minimal-ui" : "standalone",
088            "start_url", site.getUrl() + "/",
089            "scope", site.getUrl() + "/",
090            
091            "icons", List.of(_icon(site, 48), _icon(site, 96), _icon(site, 512))
092        );
093        
094        ObjectModelHelper.getRequest(objectModel).setAttribute(JSonReader.OBJECT_TO_READ, manifest);
095        
096        return Map.of();
097    }
098    
099    private Map<String, String> _icon(Site site, int size)
100    {
101        return Map.of(
102                "src", site.getUrl() + "/plugins/web/resources/img/manifest/ametys_" + size + "x" + size + ".png",
103                "sizes", size + "x" + size,
104                "type", "image/png",
105                "purpose", "any"
106            );
107    }
108}