View Javadoc

1   /**
2    * LICENCIA LGPL:
3    * 
4    * Esta librería es Software Libre; Usted puede redistribuirla y/o modificarla
5    * bajo los términos de la GNU Lesser General Public License (LGPL) tal y como 
6    * ha sido publicada por la Free Software Foundation; o bien la versión 2.1 de 
7    * la Licencia, o (a su elección) cualquier versión posterior.
8    * 
9    * Esta librería se distribuye con la esperanza de que sea útil, pero SIN 
10   * NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o 
11   * ADECUACIÓN A UN PROPÓSITO PARTICULAR. Consulte la GNU Lesser General Public 
12   * License (LGPL) para más detalles
13   * 
14   * Usted debe recibir una copia de la GNU Lesser General Public License (LGPL) 
15   * junto con esta librería; si no es así, escriba a la Free Software Foundation 
16   * Inc. 51 Franklin Street, 5º Piso, Boston, MA 02110-1301, USA o consulte
17   * <http://www.gnu.org/licenses/>.
18   *
19   * Copyright 2011 Agencia de Tecnología y Certificación Electrónica
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   * Clase que representa a documentos ubicados en URLs. Si hay que configurar
35   * algún tipo de proxy o establecer keystores para comunicación SSL deberá
36   * hacerse antes de llamar a esta clase. 
37   * 
38   * @author <a href="mailto:jgutierrez@accv.es">José M Gutiérrez</a>
39   */
40  public class URLDocument extends Document {
41  
42  	/**
43  	 * Logger de la clase
44  	 */
45  	Logger logger = Logger.getLogger(URLDocument.class);
46  	
47  	/**
48  	 * URL con el contenido del documento
49  	 */
50  	private URL url;
51  	
52  	/**
53  	 * Inicialización del objeto mediante una URL
54  	 * 
55  	 * @param url URL con el contenido del documento
56  	 * @throws InitDocumentException El fichero es nulo o no existe
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  	 * (non-Javadoc)
77  	 * @see es.accv.arangi.base.document.IDocument#getHash()
78  	 */
79  	public byte[] getHash() throws HashingException {
80  		return getHash (HashingAlgorithm.getDefault());
81  	}
82  
83  	/*
84  	 * (non-Javadoc)
85  	 * @see es.accv.arangi.base.document.IDocument#getHash(java.lang.String)
86  	 */
87  	public byte[] getHash(String hashingAlgorithm) throws HashingException {
88  		
89  		logger.debug("[URLDocument.getHash]::Entrada::" + hashingAlgorithm);
90  		
91  		//-- Hacer un FileInputStream y utilizar un InputStreamDocument para obtener el hash
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 	 * (non-Javadoc)
119 	 * @see es.accv.arangi.base.document.IDocument#getInputStream()
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 }