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