View Javadoc

1   package org.apache.continuum.webdav.util;
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.commons.lang.ArrayUtils;
23  import org.apache.commons.lang.StringUtils;
24  
25  public class WorkingCopyPathUtil
26  {
27      public static String getLogicalResource( final String href )
28      {
29          String logicalResource = null;
30          String requestPathInfo = StringUtils.defaultString( href );
31  
32          //remove prefix ie /workingcopy/blah becomes /blah
33          requestPathInfo = removePrefix( requestPathInfo );
34  
35          // Remove prefixing slash as the project id doesn't contain it;
36          if ( requestPathInfo.startsWith( "/" ) )
37          {
38              requestPathInfo = requestPathInfo.substring( 1 );
39          }
40  
41          int slash = requestPathInfo.indexOf( '/' );
42          if ( slash > 0 )
43          {
44              logicalResource = requestPathInfo.substring( slash );
45  
46              if ( logicalResource.endsWith( "/.." ) )
47              {
48                  logicalResource += "/";
49              }
50  
51              if ( logicalResource != null && logicalResource.startsWith( "//" ) )
52              {
53                  logicalResource = logicalResource.substring( 1 );
54              }
55  
56              if ( logicalResource == null )
57              {
58                  logicalResource = "/";
59              }
60          }
61          else
62          {
63              logicalResource = "/";
64          }
65          return logicalResource;
66      }
67  
68      public static int getProjectId( final String href )
69      {
70          String requestPathInfo = StringUtils.defaultString( href );
71  
72          // Remove prefix ie /workingcopy/blah becomes /blah
73          requestPathInfo = removePrefix( requestPathInfo );
74  
75          // Remove prefixing slash as the project id doesn't contain it;
76          if ( requestPathInfo.startsWith( "/" ) )
77          {
78              requestPathInfo = requestPathInfo.substring( 1 );
79          }
80  
81          int projectId = 0;
82  
83          try
84          {
85              // Find first element, if slash exists.
86              int slash = requestPathInfo.indexOf( '/' );
87              if ( slash > 0 )
88              {
89                  // Filtered: "1/src/main/java/" -> "1"
90                  projectId = Integer.parseInt( requestPathInfo.substring( 0, slash ) );
91              }
92              else
93              {
94                  projectId = Integer.parseInt( requestPathInfo );
95              }
96          }
97          catch ( NumberFormatException e )
98          {
99          }
100 
101         return projectId;
102     }
103 
104     private static String removePrefix( final String href )
105     {
106         String[] parts = StringUtils.split( href, '/' );
107         parts = (String[]) ArrayUtils.subarray( parts, 1, parts.length );
108         if ( parts == null || parts.length == 0 )
109         {
110             return "/";
111         }
112 
113         String joinedString = StringUtils.join( parts, '/' );
114         if ( href.endsWith( "/" ) )
115         {
116             joinedString = joinedString + "/";
117         }
118 
119         return joinedString;
120     }
121 }