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