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.plugins.bpm;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.cms.transformation.ConsistencyChecker.CHECK;
032import org.ametys.cms.transformation.URIResolver;
033import org.ametys.core.util.FilenameUtils;
034import org.ametys.core.util.URIUtils;
035import org.ametys.plugins.bpm.jcr.JCRWorkflow;
036import org.ametys.plugins.bpm.jcr.JCRWorkflowProcess;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.runtime.i18n.I18nizableText;
039import org.ametys.runtime.plugin.component.PluginAware;
040import org.ametys.web.URIPrefixHandler;
041
042/**
043 * Uri Resolver for the BPM plugin
044 */
045public class BPMUriResolver extends AbstractLogEnabled implements URIResolver, Serviceable, PluginAware, Contextualizable
046{
047    private String _pluginName;
048    private AmetysObjectResolver _resolver;
049    private URIPrefixHandler _prefixHandler;
050    private Context _context;
051    
052    public void setPluginInfo(String pluginName, String featureName, String id)
053    {
054        _pluginName = pluginName;
055    }
056
057    @Override
058    public void contextualize(Context context) throws ContextException
059    {
060        _context = context;
061    }
062    
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
066        _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
067    }
068
069    public String getType()
070    {
071        return "bpm-attachment";
072    }
073
074    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
075    {
076        try
077        {
078            String[] uriSplit = uri.split("#");
079            String processId = uriSplit[0];
080            String attachmentName = uriSplit[1];
081            JCRWorkflowProcess process = _resolver.resolveById(processId);
082            JCRWorkflow workflow = _resolver.resolveById(process.getWorkflow());
083            
084            Request request = ContextHelper.getRequest(_context);
085            String siteName = (String) request.getAttribute("site");
086            
087            StringBuilder resultPath = new StringBuilder();
088            if (internal)
089            {
090                resultPath.append("cocoon://");
091            }
092            else if (absolute)
093            {
094                resultPath.append(_prefixHandler.getAbsoluteUriPrefix(siteName));
095            }
096            else
097            {
098                resultPath.append(_prefixHandler.getUriPrefix(siteName));
099            }
100            
101            resultPath.append("/_plugins/")
102                      .append(_pluginName)
103                      .append("/process/")
104                      .append(FilenameUtils.encodeName(workflow.getName()))
105                      .append("/")
106                      .append(FilenameUtils.encodeName(process.getName()))
107                      .append("/_attachments/")
108                      .append(FilenameUtils.encodeName(attachmentName));
109              
110            Map<String, String> params = new HashMap<>();
111            if (download)
112            {
113                params.put("download", "true");
114            }
115            
116            return URIUtils.encodeURI(resultPath.toString(), params);
117        }
118        catch (Exception e)
119        {
120            throw new IllegalStateException(e);
121        }
122    }
123
124    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
125    {
126        return resolve(uri, download, absolute, internal);
127    }
128
129    public String resolveImageAsBase64(String uri, int height, int width)
130    {
131        return resolve(uri, false, false, false);
132    }
133
134    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
135    {
136        return resolve(uri, download, absolute, internal);
137    }
138
139    public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth)
140    {
141        return resolve(uri, false, false, false);
142    }
143
144    public String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal)
145    {
146        return resolve(uri, download, absolute, internal);
147    }
148
149    public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth)
150    {
151        return resolve(uri, false, false, false);
152    }
153
154    public CHECK checkLink(String uri, boolean shortTest)
155    {
156        return CHECK.SUCCESS;
157    }
158
159    public I18nizableText getLabel(String uri)
160    {
161        return new I18nizableText(uri.substring(0, uri.lastIndexOf("/") + 1));
162    }
163}