View Javadoc

1   package org.apache.continuum.webdav;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.jackrabbit.util.Text;
23  import org.apache.jackrabbit.webdav.DavLocatorFactory;
24  import org.apache.jackrabbit.webdav.DavResourceLocator;
25  
26  public class ContinuumBuildAgentDavResourceLocator
27      implements DavResourceLocator
28  {
29      private final String prefix;
30  
31      private final String resourcePath;
32  
33      private final String href;
34  
35      private final DavLocatorFactory davLocatorFactory;
36  
37      private final int projectId;
38  
39      public ContinuumBuildAgentDavResourceLocator( String prefix, String resourcePath,
40                                                    DavLocatorFactory davLocatorFactory, int projectId )
41      {
42          this.prefix = prefix;
43          this.davLocatorFactory = davLocatorFactory;
44          this.projectId = projectId;
45  
46          String escapedPath = Text.escapePath( resourcePath );
47          String hrefPrefix = prefix;
48  
49          // Ensure no extra slashes when href is joined
50          if ( hrefPrefix.endsWith( "/" ) && escapedPath.startsWith( "/" ) )
51          {
52              hrefPrefix = hrefPrefix.substring( 0, hrefPrefix.length() - 1 );
53          }
54  
55          href = hrefPrefix + escapedPath;
56  
57          String path = resourcePath;
58  
59          if ( !resourcePath.startsWith( "/" ) )
60          {
61              path = "/" + resourcePath;
62          }
63  
64          //Remove trailing slashes otherwise Text.getRelativeParent fails
65          if ( resourcePath.endsWith( "/" ) && resourcePath.length() > 1 )
66          {
67              path = resourcePath.substring( 0, resourcePath.length() - 1 );
68          }
69  
70          this.resourcePath = path;
71      }
72  
73      public DavLocatorFactory getFactory()
74      {
75          return davLocatorFactory;
76      }
77  
78      public String getHref( boolean isCollection )
79      {
80          // avoid doubled trailing '/' for the root item
81          String suffix = ( isCollection && !isRootLocation() && !href.endsWith( "/" ) ) ? "/" : "";
82          return href + suffix;
83      }
84  
85      public String getPrefix()
86      {
87          return prefix;
88      }
89  
90      public int getProjectId()
91      {
92          return projectId;
93      }
94  
95      public String getRepositoryPath()
96      {
97          return getResourcePath();
98      }
99  
100     public String getResourcePath()
101     {
102         return resourcePath;
103     }
104 
105     public String getWorkspaceName()
106     {
107         return "";
108     }
109 
110     public String getWorkspacePath()
111     {
112         return "";
113     }
114 
115     /**
116      * Computes the hash code from the href, which is built using the final fields prefix and resourcePath.
117      *
118      * @return the hash code
119      */
120     public int hashCode()
121     {
122         return href.hashCode();
123     }
124 
125     public boolean isRootLocation()
126     {
127         return "/".equals( resourcePath );
128     }
129 
130     public boolean isSameWorkspace( DavResourceLocator locator )
131     {
132         return isSameWorkspace( locator.getWorkspaceName() );
133     }
134 
135     public boolean isSameWorkspace( String workspaceName )
136     {
137         return getWorkspaceName().equals( workspaceName );
138     }
139 
140     /**
141      * Equality of path is achieved if the specified object is a <code>DavResourceLocator</code> object with the same
142      * hash code.
143      *
144      * @param obj the object to compare to
145      * @return <code>true</code> if the 2 objects are equal; <code>false</code> otherwise
146      */
147     public boolean equals( Object obj )
148     {
149         if ( obj instanceof DavResourceLocator )
150         {
151             DavResourceLocator other = (DavResourceLocator) obj;
152             return hashCode() == other.hashCode();
153         }
154         return false;
155     }
156 }