1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package es.accv.arangi.base.document;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URL;
26
27 import org.apache.log4j.Logger;
28
29 import es.accv.arangi.base.algorithm.HashingAlgorithm;
30 import es.accv.arangi.base.exception.document.HashingException;
31 import es.accv.arangi.base.exception.document.InitDocumentException;
32
33
34
35
36
37
38
39
40 public class URLDocument extends Document {
41
42
43
44
45 Logger logger = Logger.getLogger(URLDocument.class);
46
47
48
49
50 private URL url;
51
52
53
54
55
56
57
58 public URLDocument (URL url) throws InitDocumentException {
59 logger.debug("[URLDocument]::Entrada::" + url);
60 if (url == null) {
61 logger.info("[URLDocument]::La URL es nula");
62 throw new InitDocumentException ("La URL es nula");
63 }
64
65 try {
66 url.openConnection();
67 } catch (IOException e) {
68 logger.info("[URLDocument]::No es posible conectarse a la URL indicada: " + url, e);
69 throw new InitDocumentException ("No es posible conectarse a la URL indicada: " + url, e);
70 }
71
72 this.url = url;
73 }
74
75
76
77
78
79 public byte[] getHash() throws HashingException {
80 return getHash (HashingAlgorithm.getDefault());
81 }
82
83
84
85
86
87 public byte[] getHash(String hashingAlgorithm) throws HashingException {
88
89 logger.debug("[URLDocument.getHash]::Entrada::" + hashingAlgorithm);
90
91
92 InputStream is = null;
93 byte[] hash = null;
94 try {
95 is = url.openStream();
96 InputStreamDocument isd = new InputStreamDocument (is);
97 hash = isd.getHash(hashingAlgorithm);
98 logger.debug ("[URLDocument.getHash]::FIN::Devolviendo: " + hash);
99
100 return hash;
101
102 } catch (IOException e) {
103 logger.info("[URLDocument.getHash]::No ha sido posible leer la URL: " + url, e);
104 throw new HashingException ("No ha sido posible leer la URL:" + url, e);
105 } finally {
106 if (is != null) {
107 try {
108 is.close();
109 } catch (IOException e) {
110 logger.info("[URLDocument.getHash]::El fichero no existe", e);
111 return hash;
112 }
113 }
114 }
115 }
116
117
118
119
120
121 public InputStream getInputStream() {
122 try {
123 return url.openStream();
124 } catch (IOException e) {
125 logger.info("[URLDocument.getInputStream]::No ha sido posible leer la URL: " + url, e);
126 return null;
127 }
128 }
129
130 }