1 package org.apache.continuum.webdav.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33 requestPathInfo = removePrefix( requestPathInfo );
34
35
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
73 requestPathInfo = removePrefix( requestPathInfo );
74
75
76 if ( requestPathInfo.startsWith( "/" ) )
77 {
78 requestPathInfo = requestPathInfo.substring( 1 );
79 }
80
81 int projectId = 0;
82
83 try
84 {
85
86 int slash = requestPathInfo.indexOf( '/' );
87 if ( slash > 0 )
88 {
89
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 }