001/* 002 * Copyright 2010 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.plugins.explorer.resources.Resource; 025import org.ametys.plugins.repository.AmetysObject; 026import org.ametys.web.URIPrefixHandler; 027import org.ametys.web.WebHelper; 028import org.ametys.web.explorer.ResourceHelper; 029import org.ametys.web.repository.site.Site; 030 031/** 032 * {@link URIResolver} for type "explorer". <br> 033 * These links point to a file from the resources explorer. 034 */ 035public class ResourceURIResolver extends org.ametys.cms.transformation.ResourceURIResolver 036{ 037 /** The URI prefix handler */ 038 protected URIPrefixHandler _webPrefixHandler; 039 040 @Override 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 super.service(manager); 044 _webPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 045 } 046 047 @Override 048 protected String getUriPrefix (AmetysObject object, boolean download, boolean absolute, boolean internal) 049 { 050 String siteName = null; 051 if (object instanceof Resource) 052 { 053 Resource resource = (Resource) object; 054 siteName = _getSiteName(resource); 055 } 056 057 String initialSiteName = (String) ContextHelper.getRequest(_context).getAttribute("initialSiteName"); 058 boolean isInitialSite = initialSiteName == null || initialSiteName.equals(siteName); 059 060 if (internal) 061 { 062 return "cocoon://" + siteName; 063 } 064 else if (absolute || !isInitialSite) 065 { 066 return _webPrefixHandler.getAbsoluteUriPrefix(siteName); 067 } 068 else 069 { 070 return _webPrefixHandler.getUriPrefix(siteName); 071 } 072 } 073 074 @Override 075 protected String getResourcePath(Resource resource) 076 { 077 String fullPath = resource.getPath(); 078 079 if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH)) 080 { 081 return fullPath.substring(ResourceHelper.SHARED_RESOURCE_PATH.length()); 082 } 083 else 084 { 085 return super.getResourcePath(resource); 086 } 087 } 088 089 @Override 090 protected String getRealPrefix (Resource resource, String prefix) 091 { 092 String fullPath = resource.getPath(); 093 if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH)) 094 { 095 // shared resource, URI should begin with "shared-" 096 // the resource path should also contain the shared collection's name 097 return "shared-" + prefix; 098 } 099 else 100 { 101 return prefix; 102 } 103 } 104 105 /** 106 * Get the site name from resource 107 * @param resource the resource 108 * @return the site name 109 */ 110 protected String _getSiteName (Resource resource) 111 { 112 Request request = ContextHelper.getRequest(_context); 113 114 String fullPath = resource.getPath(); 115 if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH)) 116 { 117 return WebHelper.getSiteName(request); 118 } 119 else 120 { 121 AmetysObject parent = resource.getParent(); 122 while (parent != null) 123 { 124 if (parent instanceof Site) 125 { 126 return ((Site) parent).getName(); 127 } 128 129 parent = parent.getParent(); 130 } 131 return null; 132 } 133 } 134}