001package org.apache.archiva.webdav; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.jackrabbit.util.Text; 023import org.apache.jackrabbit.webdav.DavLocatorFactory; 024import org.apache.jackrabbit.webdav.DavResourceLocator; 025 026/** 027 */ 028public class ArchivaDavResourceLocator 029 implements DavResourceLocator, RepositoryLocator 030{ 031 private final String prefix; 032 033 private final String resourcePath; 034 035 private final String href; 036 037 private final String repositoryId; 038 039 private final DavLocatorFactory davLocatorFactory; 040 041 // retains the trailing '/' at the end of the path, which is used to determine if it is a 042 // virtual repo browse request 043 private final String origResourcePath; 044 045 public ArchivaDavResourceLocator( String prefix, String resourcePath, String repositoryId, 046 DavLocatorFactory davLocatorFactory ) 047 { 048 this.prefix = prefix; 049 this.repositoryId = repositoryId; 050 this.davLocatorFactory = davLocatorFactory; 051 052 String path = resourcePath; 053 054 if ( !resourcePath.startsWith( "/" ) ) 055 { 056 path = "/" + resourcePath; 057 } 058 059 String escapedPath = Text.escapePath( resourcePath ); 060 String hrefPrefix = prefix; 061 062 // Ensure no extra slashes when href is joined 063 if ( hrefPrefix.endsWith( "/" ) && escapedPath.startsWith( "/" ) ) 064 { 065 hrefPrefix = hrefPrefix.substring( 0, hrefPrefix.length() - 1 ); 066 } 067 068 href = hrefPrefix + escapedPath; 069 070 this.origResourcePath = path; 071 072 //Remove trailing slashes otherwise Text.getRelativeParent fails 073 if ( resourcePath.endsWith( "/" ) && resourcePath.length() > 1 ) 074 { 075 path = resourcePath.substring( 0, resourcePath.length() - 1 ); 076 } 077 078 this.resourcePath = path; 079 } 080 081 @Override 082 public String getRepositoryId() 083 { 084 return repositoryId; 085 } 086 087 @Override 088 public String getPrefix() 089 { 090 return prefix; 091 } 092 093 @Override 094 public String getResourcePath() 095 { 096 return resourcePath; 097 } 098 099 @Override 100 public String getWorkspacePath() 101 { 102 return ""; 103 } 104 105 @Override 106 public String getWorkspaceName() 107 { 108 return ""; 109 } 110 111 @Override 112 public boolean isSameWorkspace( DavResourceLocator locator ) 113 { 114 return isSameWorkspace( locator.getWorkspaceName() ); 115 } 116 117 @Override 118 public boolean isSameWorkspace( String workspaceName ) 119 { 120 return getWorkspaceName().equals( workspaceName ); 121 } 122 123 @Override 124 public String getHref( boolean isCollection ) 125 { 126 // avoid doubled trailing '/' for the root item 127 String suffix = ( isCollection && !isRootLocation() && !href.endsWith( "/" ) ) ? "/" : ""; 128 return href + suffix; 129 } 130 131 @Override 132 public boolean isRootLocation() 133 { 134 return "/".equals( resourcePath ); 135 } 136 137 @Override 138 public DavLocatorFactory getFactory() 139 { 140 return davLocatorFactory; 141 } 142 143 @Override 144 public String getRepositoryPath() 145 { 146 return getResourcePath(); 147 } 148 149 /** 150 * Computes the hash code from the href, which is built using the final fields prefix and resourcePath. 151 * 152 * @return the hash code 153 */ 154 @Override 155 public int hashCode() 156 { 157 return href.hashCode(); 158 } 159 160 /** 161 * Equality of path is achieved if the specified object is a <code>DavResourceLocator</code> object with the same 162 * hash code. 163 * 164 * @param obj the object to compare to 165 * @return <code>true</code> if the 2 objects are equal; <code>false</code> otherwise 166 */ 167 @Override 168 public boolean equals( Object obj ) 169 { 170 if ( obj instanceof DavResourceLocator ) 171 { 172 DavResourceLocator other = (DavResourceLocator) obj; 173 return hashCode() == other.hashCode(); 174 } 175 return false; 176 } 177 178 public String getOrigResourcePath() 179 { 180 return origResourcePath; 181 } 182}