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.workspaces.documents; 017 018import java.util.Collections; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.commons.lang.StringUtils; 023 024import org.ametys.cms.transformation.URIResolver; 025import org.ametys.core.authentication.token.AuthenticationTokenManager; 026import org.ametys.core.user.CurrentUserProvider; 027import org.ametys.core.user.UserIdentity; 028import org.ametys.core.util.URIUtils; 029import org.ametys.plugins.explorer.resources.Resource; 030import org.ametys.plugins.explorer.resources.ResourceCollection; 031import org.ametys.plugins.repository.AmetysObject; 032import org.ametys.plugins.repository.UnknownAmetysObjectException; 033import org.ametys.plugins.repository.version.VersionableAmetysObject; 034import org.ametys.plugins.workspaces.project.objects.Project; 035 036/** 037 * {@link URIResolver} for webdav "project-resource". <br> 038 * These links point to a file from the resources of a project through webdav 039 */ 040public class WebdavProjectResourceURIResolver extends ProjectResourceURIResolver 041{ 042 /** The authentication token manager */ 043 protected AuthenticationTokenManager _authenticationTokenManager; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 super.service(manager); 049 _authenticationTokenManager = (AuthenticationTokenManager) manager.lookup(AuthenticationTokenManager.ROLE); 050 } 051 052 @Override 053 public String getType() 054 { 055 return "webdav-project-resource"; 056 } 057 058 // Override to single encoded instead of twice 059 @Override 060 protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String prefix, String suffix) 061 { 062 String version = null; 063 String path; 064 AmetysObject resource = null; 065 try 066 { 067 String resourceId = uri; 068 int i = uri.indexOf(";"); 069 if (i != -1) 070 { 071 resourceId = uri.substring(0, i); 072 version = uri.substring(i + 1); 073 } 074 075 resource = _resolver.resolveById(resourceId); 076 if (resource instanceof Resource) 077 { 078 path = getResourcePath((Resource) resource); 079 } 080 else if (resource instanceof ResourceCollection) 081 { 082 path = ((ResourceCollection) resource).getExplorerPath(); 083 } 084 else 085 { 086 getLogger().error("Trying to access to something that is not a Resource or a ResourceCollection : " + resource.getId()); 087 return ""; 088 } 089 } 090 catch (UnknownAmetysObjectException e) 091 { 092 getLogger().warn("Link to unexisting resource " + uri); 093 return ""; 094 } 095 096 int i = path.lastIndexOf("."); 097 String extension = i != -1 ? path.substring(i) : null; 098 path = i != -1 ? path.substring(0, i) : path; 099 100 StringBuilder result = new StringBuilder(); 101 102 result.append(getUriPrefix(resource, download, absolute, internal)); 103 104 String realPrefix = null; 105 if (resource instanceof Resource) 106 { 107 realPrefix = getRealPrefix((Resource) resource, prefix); 108 } 109 110 if (StringUtils.isNotEmpty(realPrefix)) 111 { 112 result.append("/_").append(realPrefix); 113 } 114 115 if (StringUtils.isNotEmpty(version) && resource instanceof VersionableAmetysObject) 116 { 117 result.append("/__version").append(version); 118 } 119 120 result.append(path); 121 122 if (suffix != null) 123 { 124 result.append(suffix); 125 } 126 127 if (extension != null) 128 { 129 result.append(extension); 130 } 131 132 String fullUri = result.toString(); 133 134 // the ';' char is handled by Tomcat as an URI path parameter 135 // Replace it by its encoded value, which may look weird in Office, but at least it won't fail 136 fullUri = fullUri.replaceAll(";", "%3B"); 137 138 return URIUtils.encodeURI(fullUri, download ? Collections.singletonMap("download", "true") : null); 139 } 140 141 @Override 142 protected String getUriPrefix(AmetysObject object, boolean download, boolean absolute, boolean internal) 143 { 144 Project project = null; 145 String projectName = null; 146 String siteName = null; 147 148 String token = _authenticationTokenManager.generateToken(60, "Workspaces-Webdav", null); 149 150 project = _getProject(object); 151 projectName = project.getName(); 152 153 siteName = project.getSites().iterator().next().getName(); 154 155 if (internal) 156 { 157 return "cocoon://_workspaces/dav/" + projectName + "/" + token; 158 } 159 else if (absolute) 160 { 161 return _webPrefixHandler.getAbsoluteUriPrefix(siteName) + "/_workspaces/dav/" + projectName + "/" + token; 162 } 163 else 164 { 165 return _webPrefixHandler.getUriPrefix(siteName) + "/_workspaces/dav/" + projectName + "/" + token; 166 } 167 } 168 169 @Override 170 protected String getRealPrefix(Resource resource, String prefix) 171 { 172 return null; 173 } 174}