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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 package org.astrogrid.filestore.common.file ;
113
114 import java.net.URL ;
115
116 import java.io.IOException ;
117 import java.io.InputStream ;
118 import java.io.OutputStream ;
119
120 import java.util.Map ;
121 import java.util.Map.Entry ;
122 import java.util.Date ;
123 import java.util.Iterator ;
124 import java.util.Properties ;
125 import java.util.Enumeration ;
126
127 import java.text.ParseException ;
128 import org.astrogrid.store.Ivorn ;
129
130 import org.astrogrid.filestore.common.FileStoreDateFormat;
131 import org.astrogrid.filestore.common.ivorn.FileStoreIvornParser ;
132 import org.astrogrid.filestore.common.ivorn.FileStoreIvornFactory ;
133 import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
134
135 /***
136 * A wrapper for a set of file properties.
137 *
138 */
139 public class FileProperties
140 {
141 /***
142 * The property key for the resource ivorn.
143 *
144 */
145 public static final String STORE_RESOURCE_IVORN = "org.astrogrid.filestore.resource.ivorn" ;
146
147 /***
148 * The property key for the resource URL.
149 *
150 */
151 public static final String STORE_RESOURCE_URL = "org.astrogrid.filestore.resource.url" ;
152
153 /***
154 * The property key for the transfer source URL.
155 *
156 */
157 public static final String TRANSFER_SOURCE_URL = "org.astrogrid.filestore.transfer.source.url" ;
158
159 /***
160 * The property key for content size.
161 *
162 */
163 public static final String CONTENT_SIZE_PROPERTY = "org.astrogrid.filestore.content.size" ;
164
165 /***
166 * The property key for content type.
167 *
168 */
169 public static final String MIME_TYPE_PROPERTY = "org.astrogrid.filestore.content.type" ;
170
171 /***
172 * The property key for content encoding.
173 *
174 */
175 public static final String MIME_ENCODING_PROPERTY = "org.astrogrid.filestore.content.encoding" ;
176
177 /***
178 * The property key for the file create date.
179 *
180 */
181 public static final String FILE_CREATED_DATE_PROPERTY = "org.astrogrid.filestore.created.date" ;
182
183 /***
184 * The property key for the file modify date.
185 *
186 */
187 public static final String FILE_MODIFIED_DATE_PROPERTY = "org.astrogrid.filestore.modified.date" ;
188
189 /***
190 * Known MIME type values.
191 *
192 */
193 public static final String MIME_TYPE_TEXT = "text/raw" ;
194 public static final String MIME_TYPE_XML = "text/xml" ;
195 public static final String MIME_TYPE_HTML = "text/html" ;
196
197 public static final String MIME_TYPE_VOLIST = "text/xml +org.astrogrid.mime.volist" ;
198 public static final String MIME_TYPE_VOTABLE = "text/xml +org.astrogrid.mime.votable" ;
199
200 public static final String MIME_TYPE_JOB = "text/xml +org.astrogrid.mime.job" ;
201 public static final String MIME_TYPE_WORKFLOW = "text/xml +org.astrogrid.mime.workflow" ;
202
203 public static final String MIME_TYPE_ADQL = "text/xml +org.astrogrid.mime.adql" ;
204
205 /***
206 * Public constructor.
207 *
208 */
209 public FileProperties()
210 {
211 }
212
213 /***
214 * Public constructor from an array of values.
215 * @param array An array of property values.
216 *
217 */
218 public FileProperties(FileProperty[] array)
219 {
220 if (null != array)
221 {
222 for (int i = 0 ; i < array.length ; i++)
223 {
224 this.setProperty(
225 array[i].getName(),
226 array[i].getValue()
227 ) ;
228 }
229 }
230 }
231
232 /***
233 * Public constructor from another set of properties.
234 * @param that The other set of properties.
235 *
236 */
237 public FileProperties(FileProperties that)
238 {
239 this(
240 that.toArray()
241 ) ;
242 }
243
244 /***
245 * Our internal map of properties.
246 *
247 */
248 private Properties properties = new Properties() ;
249
250 /***
251 * Access to our list of properties.
252 *
253 */
254 protected Map getProperties()
255 {
256 return this.properties ;
257 }
258
259 /***
260 * Get a property.
261 *
262 */
263 public String getProperty(String key)
264 {
265 return properties.getProperty(key) ;
266 }
267
268 /***
269 * Set a property.
270 * @param key The property key (name).
271 * @param value The property value.
272 *
273 */
274 public void setProperty(String key, String value)
275 {
276 if (null != key)
277 {
278 if (null != value)
279 {
280 properties.setProperty(key, value) ;
281 }
282 }
283 }
284
285 /***
286 * Set a property.
287 * @param property The property to set.
288 *
289 */
290 public void setProperty(FileProperty property)
291 {
292 if (null != property)
293 {
294 this.setProperty(
295 property.getName(),
296 property.getValue()
297 ) ;
298 }
299 }
300
301 /***
302 * Merge the properties from another map.
303 * @param that The set of properties to merge.
304 *
305 */
306 public void merge(FileProperties that)
307 {
308 if (null != that)
309 {
310 Iterator iter = that.properties.entrySet().iterator() ;
311 while (iter.hasNext())
312 {
313 Entry entry = (Entry) iter.next() ;
314 String key = (String) entry.getKey();
315 String value = (String) entry.getValue();
316 if (null != value)
317 {
318 this.setProperty(
319 key,
320 value
321 ) ;
322 }
323 }
324 }
325 }
326
327 /***
328 * Merge the properties from another map, using a filter to exclude specific properties.
329 * @param that The set of properties to merge.
330 * @param filter The filter for excluded properties.
331 *
332 */
333 public void merge(FileProperties that, PropertyFilter filter)
334 {
335 if (null != that)
336 {
337 Iterator iter = that.properties.entrySet().iterator() ;
338 while (iter.hasNext())
339 {
340 Entry entry = (Entry) iter.next() ;
341 FileProperty property = filter.filter(
342 new FileProperty(
343 (String) entry.getKey(),
344 (String) entry.getValue()
345 )
346 ) ;
347 if (null != property)
348 {
349 this.setProperty(
350 property
351 ) ;
352 }
353 }
354 }
355 }
356
357 /***
358 * Merge the properties from an array.
359 * @param arrya The set of properties to merge.
360 * @param filter The filter for excluded properties.
361 *
362 */
363 public void merge(FileProperty[] array, PropertyFilter filter)
364 {
365 if (null != array)
366 {
367 for (int i = 0 ; i < array.length ; i++)
368 {
369 FileProperty property = filter.filter(
370 array[i]
371 ) ;
372 if (null != property)
373 {
374 this.setProperty(
375 property
376 ) ;
377 }
378 }
379 }
380 }
381
382 /***
383 * Convert our properties into an array.
384 *
385 */
386 public FileProperty[] toArray()
387 {
388
389
390 FileProperty[] array = new FileProperty[this.properties.size()] ;
391
392
393 Iterator iter = this.properties.entrySet().iterator() ;
394 for (int i = 0 ; i < array.length ; i++)
395 {
396 array[i] = new FileProperty(
397 (Entry) iter.next()
398 ) ;
399 }
400 return array ;
401 }
402
403 /***
404 * Load our properties from a file.
405 *
406 */
407 public void load(InputStream stream)
408 throws IOException
409 {
410 this.properties.load(
411 stream
412 ) ;
413 }
414
415 /***
416 * Save our properties to a file.
417 *
418 */
419 public void save(OutputStream stream)
420 throws IOException
421 {
422 this.properties.store(
423 stream,
424 this.getClass().getName()
425 ) ;
426 }
427
428 /***
429 * Get the resource ivorn.
430 * @throws FileStoreIdentifierException if the service or resource identifiers are invalid.
431 *
432 */
433 public Ivorn getStoreResourceIvorn()
434 throws FileStoreIdentifierException
435 {
436 String ivorn = this.getProperty(
437 STORE_RESOURCE_IVORN
438 ) ;
439 if (null != ivorn)
440 {
441 try {
442 return new Ivorn(
443 ivorn
444 ) ;
445 }
446 catch (Exception ouch)
447 {
448 throw new FileStoreIdentifierException(
449 ivorn
450 );
451 }
452 }
453 else {
454 return null ;
455 }
456 }
457
458 /***
459 * Set the resource ivorn.
460 * @throws FileStoreIdentifierException if the service or resource identifiers are invalid.
461 *
462 */
463 public void setStoreResourceIvorn(Ivorn ivorn)
464 {
465 if (null != ivorn)
466 {
467 this.setProperty(
468 STORE_RESOURCE_IVORN,
469 ivorn.toString()
470 ) ;
471 }
472 else {
473 this.setProperty(
474 STORE_RESOURCE_IVORN,
475 null
476 ) ;
477 }
478 }
479
480 /***
481 * Get the service ivorn.
482 * @throws FileStoreIdentifierException if the service identifier is invalid.
483 *
484 */
485 public Ivorn getStoreServiceIvorn()
486 throws FileStoreIdentifierException
487 {
488 String ivorn = this.getProperty(
489 STORE_RESOURCE_IVORN
490 ) ;
491 if (null != ivorn)
492 {
493 return new FileStoreIvornParser(
494 ivorn
495 ).getServiceIvorn() ;
496 }
497 else {
498 return null ;
499 }
500 }
501
502 /***
503 * Get the resource identifier.
504 *
505 */
506 public String getStoreResourceIdent()
507 throws FileStoreIdentifierException
508 {
509 String ivorn = this.getProperty(
510 STORE_RESOURCE_IVORN
511 ) ;
512 if (null != ivorn)
513 {
514 return new FileStoreIvornParser(
515 ivorn
516 ).getResourceIdent() ;
517 }
518 else {
519 return null ;
520 }
521 }
522
523 /***
524 * Get the content size.
525 *
526 */
527 public long getContentSize()
528 {
529 String string = getProperty(
530 FileProperties.CONTENT_SIZE_PROPERTY
531 ) ;
532 if (null != string)
533 {
534 try {
535 return Long.parseLong(string) ;
536 }
537 catch (NumberFormatException ouch)
538 {
539 return -1L ;
540 }
541 }
542 else {
543 return 0L ;
544 }
545 }
546
547 /***
548 * Get the content mime type.
549 *
550 */
551 public String getContentType()
552 {
553 return getProperty(
554 FileProperties.MIME_TYPE_PROPERTY
555 ) ;
556 }
557
558 /***
559 * Get the (external) resource URL.
560 * @throws FileStoreIdentifierException if the service or resource identifiers are invalid.
561 * @deprecated Use exportInit on the parent service to get a transfer URL.
562 *
563 */
564 public URL getStoreResourceUrl()
565 {
566 try {
567 return new URL(
568 this.getProperty(
569 STORE_RESOURCE_URL
570 )
571 ) ;
572 }
573 catch (Exception ouch)
574 {
575 return null ;
576 }
577 }
578
579 /***
580 * Get the file create date.
581 * @return The file create date, if the file has stored data, or null if the file does not contains any data.
582 *
583 */
584 public Date getFileCreateDate()
585 {
586
587
588 FileStoreDateFormat formatter = new FileStoreDateFormat() ;
589
590
591 try {
592 return formatter.parse(
593 this.getProperty(
594 FileProperties.FILE_CREATED_DATE_PROPERTY
595 )
596 );
597 }
598 catch (ParseException ouch)
599 {
600 return null ;
601 }
602 }
603
604 /***
605 * Get the file modified date.
606 * @return The file modified date, if the file has stored data, or null if the file does not contains any data.
607 *
608 */
609 public Date getFileModifyDate()
610 {
611
612
613 FileStoreDateFormat formatter = new FileStoreDateFormat() ;
614
615
616 try {
617 return formatter.parse(
618 this.getProperty(
619 FileProperties.FILE_MODIFIED_DATE_PROPERTY
620 )
621 );
622 }
623 catch (ParseException ouch)
624 {
625 return null ;
626 }
627 }
628 }
629