1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package org.astrogrid.filestore.common.ivorn ;
56
57 import org.apache.commons.logging.Log ;
58 import org.apache.commons.logging.LogFactory ;
59
60 import java.net.URI ;
61 import java.net.URISyntaxException ;
62
63 import java.util.regex.Matcher ;
64 import java.util.regex.Pattern ;
65
66 import org.astrogrid.store.Ivorn ;
67
68 import org.astrogrid.config.Config ;
69 import org.astrogrid.config.SimpleConfig ;
70 import org.astrogrid.config.PropertyNotFoundException ;
71
72 import org.astrogrid.filestore.common.exception.FileStoreServiceException ;
73 import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
74
75 /***
76 * A parser for handling Filestore identifiers.
77 *
78 */
79 public class FileStoreIvornParser
80 {
81 /***
82 * Our debug logger.
83 *
84 */
85 private static Log log = LogFactory.getLog(FileStoreIvornParser.class);
86
87 /***
88 * The mock service identifier.
89 * Used to create mock ivorns in JUnit tests.
90 *
91 */
92 public static String MOCK_SERVICE_IDENT = "ivo://org.astrogrid.mock" ;
93
94 /***
95 * Our AstroGrid configuration.
96 *
97 */
98 protected static Config config = SimpleConfig.getSingleton() ;
99
100 /***
101 * Public constructor for a specific Ivorn.
102 * @param ivorn A vaild Ivorn identifier.
103 * @throws FileStoreIdentifierException If the identifier is not valid.
104 *
105 */
106 public FileStoreIvornParser(Ivorn ivorn)
107 throws FileStoreIdentifierException
108 {
109 this.setIvorn(ivorn) ;
110 }
111
112 /***
113 * Public constructor for a specific identifier.
114 * @param ident A vaild identifier.
115 * @throws FileStoreIdentifierException If the identifier is not valid.
116 *
117 */
118 public FileStoreIvornParser(String ident)
119 throws FileStoreIdentifierException
120 {
121 this.setIvorn(
122 parse(ident)
123 ) ;
124 }
125
126 /***
127 * Our target Ivorn.
128 *
129 */
130 private Ivorn ivorn ;
131
132 /***
133 * Our corresponding URI.
134 *
135 */
136 private URI uri ;
137
138 /***
139 * The service ident.
140 *
141 */
142 private String service ;
143
144 /***
145 * The resource ident.
146 *
147 */
148 private String resource ;
149
150 /***
151 * Get our target Ivorn.
152 *
153 */
154 public Ivorn getIvorn()
155 {
156 return this.ivorn ;
157 }
158
159 /***
160 * Convert a string identifier into an Ivorn.
161 * @param ident The identifier.
162 * @return a new Ivorn containing the identifier.
163 * @throws FileStoreIdentifierException If the identifier is not a valid ivorn.
164 *
165 */
166 protected static Ivorn parse(String ident)
167 throws FileStoreIdentifierException
168 {
169 if (null == ident)
170 {
171 throw new FileStoreIdentifierException(
172 "Null identifier"
173 ) ;
174 }
175 try {
176 return new Ivorn(ident) ;
177 }
178 catch (URISyntaxException ouch)
179 {
180 throw new FileStoreIdentifierException(
181 ouch
182 ) ;
183 }
184 }
185
186 /***
187 * Set our target Ivorn.
188 * @param A vaild Ivorn identifier.
189 * @throws FileStoreIdentifierException If the identifier is not valid.
190 *
191 */
192 protected void setIvorn(Ivorn ivorn)
193 throws FileStoreIdentifierException
194 {
195 log.debug("") ;
196 log.debug("----\"----") ;
197 log.debug("FilestoreIvornParser.setIvorn()") ;
198 log.debug(" Ivorn : " + ivorn) ;
199
200
201 if (null == ivorn)
202 {
203 throw new FileStoreIdentifierException(
204 "Null identifier"
205 ) ;
206 }
207
208
209 this.ivorn = ivorn ;
210
211
212 this.uri = null ;
213 this.service = null ;
214 this.resource = null ;
215
216
217 try {
218 this.uri = new URI(ivorn.toString()) ;
219
220
221 this.parseServiceIdent() ;
222
223
224 this.parseResourceIdent() ;
225 }
226
227
228 catch (URISyntaxException ouch)
229 {
230 throw new FileStoreIdentifierException(
231 ouch
232 ) ;
233 }
234 }
235
236 /***
237 * Parse the filestore ident.
238 * @throws FileStoreIdentifierException If the identifier is not a valid ivorn.
239 *
240 */
241 protected void parseServiceIdent()
242 throws FileStoreIdentifierException
243 {
244 log.debug("") ;
245 log.debug("----\"----") ;
246 log.debug("FilestoreIvornParser.parseServiceIdent()") ;
247 log.debug(" Ivorn : " + ivorn) ;
248 if (null != uri)
249 {
250 String auth = uri.getAuthority() ;
251 String path = uri.getPath() ;
252 log.debug(" Auth : " + auth) ;
253 log.debug(" Path : " + path) ;
254 StringBuffer buffer = new StringBuffer() ;
255 buffer.append(Ivorn.SCHEME) ;
256 buffer.append("://") ;
257
258
259 if (null != auth)
260 {
261
262
263 if (auth.length() > 0)
264 {
265
266
267 buffer.append(auth) ;
268
269
270 if (null != path)
271 {
272 if (path.length() > 1)
273 {
274 buffer.append(path) ;
275 }
276 }
277 }
278
279
280 else {
281 throw new FileStoreIdentifierException(
282 "Invalid identifier",
283 uri.toString()
284 ) ;
285 }
286 }
287
288
289 else {
290 throw new FileStoreIdentifierException(
291 "Invalid identifier",
292 uri.toString()
293 ) ;
294 }
295 this.service = buffer.toString() ;
296 }
297 log.debug(" Service : " + this.service) ;
298 }
299
300 /***
301 * Parse the resource ident.
302 *
303 */
304 protected void parseResourceIdent()
305 {
306 log.debug("") ;
307 log.debug("----\"----") ;
308 log.debug("FilestoreIvornParser.parseResourceIdent()") ;
309 log.debug(" Ivorn : " + ivorn) ;
310 if (null != uri)
311 {
312 this.resource = uri.getFragment() ;
313 }
314 log.debug(" Resource : " + this.resource) ;
315 }
316
317 /***
318 * Get the filestore ident as a string.
319 * @return The filestore ident, or null if no match was found.
320 *
321 */
322 public String getServiceIdent()
323 {
324 return this.service ;
325 }
326
327 /***
328 * Get the filestore ident as an ivorn.
329 * @return The filestore ivorn, or null if no match was found.
330 * @throws FileStoreIdentifierException If the service identifier is not valid.
331 *
332 */
333 public Ivorn getServiceIvorn()
334 throws FileStoreIdentifierException
335 {
336 try {
337 return new Ivorn(
338 this.service
339 ) ;
340 }
341 catch (URISyntaxException ouch)
342 {
343 throw new FileStoreIdentifierException(
344 ouch
345 ) ;
346 }
347 }
348
349 /***
350 * Get the resource ident as a string.
351 * @return The resource ident, or null if no match was found.
352 *
353 */
354 public String getResourceIdent()
355 {
356 return this.resource ;
357 }
358
359 /***
360 * Convert the parser to a String.
361 *
362 */
363 public String toString()
364 {
365 return "FilestoreIvornParser : " + ((null != ivorn) ? ivorn.toString() : null) ;
366 }
367
368 /***
369 * Check if this is a mock ivorn.
370 * @return true if this is a mock ivorn.
371 *
372 */
373 public boolean isMock()
374 {
375 if (null != this.service)
376 {
377 return this.service.startsWith(
378 MOCK_SERVICE_IDENT
379 ) ;
380 }
381 else {
382 return false ;
383 }
384 }
385 }