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