001/*
002 *  Copyright 2011 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 java.util.Collections;
019
020import org.ametys.cms.transformation.URIResolver;
021import org.ametys.core.util.FilenameUtils;
022import org.ametys.core.util.URIUtils;
023import org.ametys.plugins.explorer.resources.Resource;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.UnknownAmetysObjectException;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * {@link URIResolver} for type "attachment-page".<br>
031 * These links point to a file from the attachments of the current Page.
032 */
033public class PageAttachmentURIResolver extends AttachmentURIResolver
034{
035    @Override
036    public String getType()
037    {
038        return "attachment-page";
039    }
040    
041    @Override
042    public String _resolve(String uri, String uriArgument, boolean download, boolean absolute, boolean internal)
043    {
044        Page page;
045        String path;
046        
047        try
048        {
049            Resource resource = (Resource) _resolver.resolveById(uri);
050            path = resource.getResourcePath();
051            
052            AmetysObject ao = resource;
053            do
054            {
055                ao = ao.getParent();
056            }
057            while (ao != null && !(ao instanceof Page));
058            
059            if (ao == null)
060            {
061                throw new IllegalArgumentException("The given attachment URI should be a page attachment.");
062            }
063            
064            page = (Page) ao;
065        }
066        catch (UnknownAmetysObjectException e)
067        {
068            getLogger().warn("Link to unexisting resource " + uri);
069            return "";
070        }
071        catch (Exception e)
072        {
073            throw new IllegalStateException(e);
074        }
075    
076        String filename = FilenameUtils.encodePath(path);
077        String basePath = org.apache.commons.io.FilenameUtils.removeExtension(filename);
078        String extension = org.apache.commons.io.FilenameUtils.getExtension(filename);
079        
080        StringBuilder resultPath = new StringBuilder();
081        resultPath.append(getUriPrefix(page, download, absolute, internal));
082        
083        resultPath.append("/").append(page.getSitemapName())
084                  .append("/").append(page.getPathInSitemap())
085                  .append("/_attachment")
086                  .append(basePath)
087                  .append(uriArgument)
088                  .append(extension.isEmpty() ? "" : "." + extension);
089        
090        return URIUtils.encodeURI(resultPath.toString(), download ? Collections.singletonMap("download", "true") : null);
091    }
092
093    @Override
094    public I18nizableText getLabel(String uri)
095    {
096        try
097        {
098            Resource resource = (Resource) _resolver.resolveById(uri);
099            return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGEATTACHMENT_LABEL", Collections.singletonList(resource.getResourcePath()));
100        }
101        catch (UnknownAmetysObjectException e)
102        {
103            return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGEATTACHMENT_UNKNOWN");
104        }
105    }
106}