1 package org.apache.archiva.repository.content.maven2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.model.ArtifactReference;
23 import org.apache.archiva.repository.ManagedRepositoryContent;
24 import org.apache.archiva.repository.content.PathParser;
25 import org.apache.archiva.repository.layout.LayoutException;
26 import org.apache.archiva.repository.metadata.MetadataTools;
27 import org.apache.commons.lang.StringUtils;
28
29
30
31
32
33 public class RepositoryRequest
34 {
35 private PathParser defaultPathParser = new DefaultPathParser();
36
37 public RepositoryRequest()
38 {
39
40 }
41
42
43
44
45
46
47
48
49
50 public ArtifactReference toArtifactReference( String requestedPath )
51 throws LayoutException
52 {
53 if ( StringUtils.isBlank( requestedPath ) )
54 {
55 throw new LayoutException( "Blank request path is not a valid." );
56 }
57
58 String path = requestedPath;
59 while ( path.startsWith( "/" ) )
60 {
61 path = path.substring( 1 );
62
63
64 if ( "/".equals( path ) )
65 {
66 throw new LayoutException( "Invalid request path: Slash only." );
67 }
68 }
69
70 if ( isDefault( path ) )
71 {
72 return defaultPathParser.toArtifactReference( path );
73 }
74 else if ( isLegacy( path ) )
75 {
76 throw new LayoutException( "Legacy Maven1 repository not supported anymore." );
77 }
78 else
79 {
80 throw new LayoutException( "Not a valid request path layout, too short." );
81 }
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public boolean isMetadata( String requestedPath )
98 {
99 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_METADATA );
100 }
101
102
103
104
105
106 public boolean isArchetypeCatalog( String requestedPath )
107 {
108 return requestedPath.endsWith( "/" + MetadataTools.MAVEN_ARCHETYPE_CATALOG );
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public boolean isSupportFile( String requestedPath )
128 {
129 int idx = requestedPath.lastIndexOf( '.' );
130 if ( idx <= 0 )
131 {
132 return false;
133 }
134
135 String ext = requestedPath.substring( idx );
136 return ( ".sha1".equals( ext ) || ".md5".equals( ext ) || ".asc".equals( ext ) || ".pgp".equals( ext ) );
137 }
138
139 public boolean isMetadataSupportFile( String requestedPath )
140 {
141 if ( isSupportFile( requestedPath ) )
142 {
143 String basefilePath = StringUtils.substring( requestedPath, 0, requestedPath.lastIndexOf( '.' ) );
144 if ( isMetadata( basefilePath ) )
145 {
146 return true;
147 }
148 }
149
150 return false;
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public boolean isDefault( String requestedPath )
168 {
169 if ( StringUtils.isBlank( requestedPath ) )
170 {
171 return false;
172 }
173
174 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
175 if ( pathParts.length > 3 )
176 {
177 return true;
178 }
179 else if ( pathParts.length == 3 )
180 {
181
182 if ( isMetadata( requestedPath ) )
183 {
184 return true;
185 }
186 else
187 {
188
189 int idx = requestedPath.lastIndexOf( '.' );
190 if ( idx > 0 )
191 {
192 String base = requestedPath.substring( 0, idx );
193 if ( isMetadata( base ) && isSupportFile( requestedPath ) )
194 {
195 return true;
196 }
197 }
198
199 return false;
200 }
201 }
202 else
203 {
204 return false;
205 }
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public boolean isLegacy( String requestedPath )
223 {
224 if ( StringUtils.isBlank( requestedPath ) )
225 {
226 return false;
227 }
228
229 String pathParts[] = StringUtils.splitPreserveAllTokens( requestedPath, '/' );
230 return pathParts.length == 3;
231 }
232
233
234
235
236
237
238
239
240
241 public String toNativePath( String requestedPath, ManagedRepositoryContent repository )
242 throws LayoutException
243 {
244 if ( StringUtils.isBlank( requestedPath ) )
245 {
246 throw new LayoutException( "Request Path is blank." );
247 }
248
249 String referencedResource = requestedPath;
250
251 String supportfile = "";
252
253
254 if ( isSupportFile( requestedPath ) )
255 {
256 int idx = requestedPath.lastIndexOf( '.' );
257 referencedResource = requestedPath.substring( 0, idx );
258 supportfile = requestedPath.substring( idx );
259 }
260
261 if ( isMetadata( referencedResource ) )
262 {
263
264
265
266
267 return requestedPath;
268 }
269
270
271 ArtifactReference ref = toArtifactReference( referencedResource );
272 String adjustedPath = repository.toPath( ref );
273 return adjustedPath + supportfile;
274 }
275 }