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.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  import org.apache.log4j.Logger;
30  
31  import es.accv.arangi.base.algorithm.HashingAlgorithm;
32  import es.accv.arangi.base.exception.document.HashingException;
33  import es.accv.arangi.base.exception.document.InitDocumentException;
34  
35  /**
36   * Clase que representa a documentos en el sistema de archivos y en los que su 
37   * hash será un resumen de todo su contenido. 
38   * 
39   * @author <a href="mailto:jgutierrez@accv.es">José M Gutiérrez</a>
40   */
41  public class FileDocument extends Document {
42  
43  	/**
44  	 * Logger de la clase
45  	 */
46  	Logger logger = Logger.getLogger(FileDocument.class);
47  	
48  	/**
49  	 * Fichero con el contenido del documento
50  	 */
51  	private File file;
52  	
53  	/**
54  	 * Inicialización del objeto mediante un fichero
55  	 * 
56  	 * @param file Fichero con el contenido del documento
57  	 * @throws InitDocumentException El fichero es nulo o no existe
58  	 */
59  	public FileDocument (File file) throws InitDocumentException {
60  		logger.debug("[FileDocument]::Entrada::" + file);
61  		if (file == null) {
62  			logger.info("[FileDocument]::El fichero es nulo");
63  			throw new InitDocumentException ("El fichero es nulo");
64  		}
65  		if (!file.exists()) {
66  			logger.info("[FileDocument]::El fichero no existe en el sistema de archivos");
67  			throw new InitDocumentException ("El fichero no existe en el sistema de archivos");
68  		}
69  		
70  		this.file = file;
71  	}
72  
73  	/*
74  	 * (non-Javadoc)
75  	 * @see es.accv.arangi.base.document.IDocument#getHash()
76  	 */
77  	public byte[] getHash() throws HashingException {
78  		return getHash (HashingAlgorithm.getDefault());
79  	}
80  
81  	/*
82  	 * (non-Javadoc)
83  	 * @see es.accv.arangi.base.document.IDocument#getHash(java.lang.String)
84  	 */
85  	public byte[] getHash(String hashingAlgorithm) throws HashingException {
86  		
87  		logger.debug("[FileDocument.getHash]::Entrada::" + hashingAlgorithm);
88  		
89  		//-- Hacer un FileInputStream y utilizar un InputStreamDocument para obtener el hash
90  		InputStream is = null;
91  		try {
92  			is = new FileInputStream (this.file);
93  			InputStreamDocument isd = new InputStreamDocument (is);
94  			byte[] hash = isd.getHash(hashingAlgorithm);
95  			logger.debug ("[FileDocument.getHash]::FIN::Devolviendo: " + hash);
96  			
97  			return hash;
98  			
99  		} catch (FileNotFoundException e) {
100 			logger.info("[FileDocument.getHash]::El fichero no existe", e);
101 			throw new HashingException ("El fichero no existe", e);
102 		} finally {
103 			if (is != null) {
104 				try {
105 					is.close();
106 				} catch (IOException e) {
107 					logger.info("[FileDocument.getHash]::El fichero no existe", e);
108 					throw new HashingException ("No es posible cerrar el stream de lectura", e);
109 				}
110 			}
111 		}
112 	}
113 
114 	/*
115 	 * (non-Javadoc)
116 	 * @see es.accv.arangi.base.document.IDocument#getInputStream()
117 	 */
118 	public InputStream getInputStream() {
119 		try {
120 			return new FileInputStream (file);
121 		} catch (FileNotFoundException e) {
122 			logger.info("[FileDocument.getInputStream]::No es posible obtener un stream de lectura al fichero", e);
123 			return null;
124 		}
125 	}
126 
127 }