001/*
002 *  Copyright 2017 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.editor;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.components.ContextHelper;
021import org.apache.cocoon.environment.Request;
022
023import org.ametys.cms.transformation.URIResolver;
024import org.ametys.web.URIPrefixHandler;
025import org.ametys.web.renderingcontext.RenderingContext;
026import org.ametys.web.renderingcontext.RenderingContextHandler;
027import org.ametys.web.repository.site.Site;
028import org.ametys.web.repository.site.SiteManager;
029
030/**
031 * {@link URIResolver} for type "remote".<br>
032 * This resolver is a proxy to another type to be called from remote url.
033 * The resolved uri are necessary absolutes.
034 */
035public class RemoteURIResolver extends org.ametys.cms.remote.RemoteURIResolver
036{
037    private RenderingContextHandler _renderingContexthandler;
038    private SiteManager _siteManager;
039    private URIPrefixHandler _prefixHandler;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _renderingContexthandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
046        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
047        _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
048    }
049    
050    @Override
051    protected String getUriPrefix(Request request)
052    {
053        String siteName = (String) request.getAttribute("siteName");
054        
055        if (siteName == null)
056        {
057            siteName = (String) request.getAttribute("site");
058        }
059        
060        return siteName == null ? _prefixHandler.getUriPrefix() : _prefixHandler.getUriPrefix(siteName);
061    }
062    
063    @Override
064    protected String getAbsoluteRemoteUriPrefix() 
065    {
066        Request request = ContextHelper.getRequest(_context);
067        
068        String siteName = (String) request.getAttribute("siteName");
069        
070        if (siteName == null)
071        {
072            siteName = (String) request.getAttribute("site");
073        }
074        
075        if (_renderingContexthandler.getRenderingContext() == RenderingContext.FRONT)
076        {
077            Site site = _siteManager.getSite(siteName);
078            return site.getUrl();
079        }
080        
081        return super.getAbsoluteRemoteUriPrefix();
082    }
083}