View Javadoc

1   package es.caib.signatura.impl;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileNotFoundException;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.net.URL;
10  import java.util.Date;
11  import java.util.Map;
12  import java.util.Properties;
13  import java.util.Vector;
14  
15  import es.caib.signatura.api.SignatureSignException;
16  /**
17   * 
18   * @author u07286
19   *
20   * TODO To change the template for this generated type comment go to
21   * Window - Preferences - Java - Code Style - Code Templates
22   */
23  public class SignaturaProperties {
24  
25  	private String configurationFile =null; //"signatura_api.properties";
26  	private String propertiesCache=null; //"/.signaturacaib/signatura_api.properties";
27  
28  
29  	private Properties properties;
30  	private String updateSite;
31  
32  	/**
33  	 * Do not use this method, it's for legacy applications. Use others constructors instead.
34  	 * @deprecated
35  	 * @throws FileNotFoundException
36  	 * @throws IOException
37  	 */
38  	public SignaturaProperties() throws FileNotFoundException, IOException {
39  		this(
40  				"http://www.caib.es/signaturacaib/"
41  				,new URL("http://www.caib.es/signaturacaib/signatura_api.properties")
42  				,"signatura_api.properties"
43  			);
44  	}
45  	
46  	/**
47  	 * Initializes properties that come packaged with the application
48  	 * @param signerConfiguration
49  	 * @throws FileNotFoundException
50  	 * @throws IOException
51  	 */
52  	public SignaturaProperties(Map signerConfiguration) throws FileNotFoundException, IOException {
53  		
54  			if (signerConfiguration != null) {
55  				getUserProperties(signerConfiguration);
56  			}else{
57  				throw new IOException("Signature properties cannot be null");
58  			}
59  		
60  	}
61  	
62  	/**
63  	 * Initializes properties from a configurationFileURL and auto updates them.
64  	 * When newer config found in the configurationFileURL they are stored at ${user.home}/.signaturacaib/${propertiesCachePath}
65  	 * @param updateSite
66  	 * @param configurationFile
67  	 * @param propertiesCachePath
68  	 */
69  	public  SignaturaProperties(String updateSite,URL configurationFile,String propertiesCachePath){
70  		this.configurationFile=configurationFile.toString();
71  		this.updateSite=updateSite;
72  		this.propertiesCache=propertiesCachePath;
73  		
74  		getCacheProperties();
75  		getWWWProperties();
76  	
77  	}
78  	
79  
80  	public void getCacheProperties() {
81  		try {
82  			File f = getCacheFile();
83  			Properties tempProperties = new Properties();
84  			tempProperties.load(new FileInputStream(f));
85  			updateProperties(tempProperties);
86  		} catch (FileNotFoundException e) {
87  		} catch (IOException e) {
88  		}
89  	}
90  
91  	private File getCacheFile() {
92  		File f = new File(System.getProperty("user.home")+"/.signaturacaib/"+propertiesCache);
93  		return f;
94  	}
95  
96  
97  	private boolean updateProperties(Properties tempProperties) {
98  		if (properties == null) {
99  			properties = tempProperties;
100 			return true;
101 		} else {
102 			try {
103 				Integer currentVersion = Integer.decode(properties
104 						.getProperty("build.number"));
105 				Integer newVersion = Integer.decode(tempProperties
106 						.getProperty("build.number"));
107 				if (newVersion.longValue() > currentVersion.longValue()) {
108 					properties = tempProperties;
109 					return true;
110 				}
111 			} catch (NumberFormatException e) {
112 			}
113 		}
114 		return false;
115 	}
116 
117 	public void getWWWProperties() {
118 		try {
119 			if ( updateSite != null) {
120 				URL url = new URL(configurationFile);
121 			Properties tempProperties = new Properties();
122 			tempProperties.load(url.openStream());
123 			if (updateProperties(tempProperties)) {
124 				try {
125 						File f = getCacheFile();
126 					if (! f.getParentFile().isDirectory())
127 						f.getParentFile().mkdirs();
128 					tempProperties.store(new FileOutputStream(f),
129 							"Descargado en fecha " + new Date());
130 				} catch( Throwable t ) {
131 					t.printStackTrace();
132 				}
133 			}
134 			}
135 		} catch (FileNotFoundException e) {
136 		} catch (IOException e) {
137 		}
138 	}
139 	
140 	public void getUserProperties(Map userProperties) throws IOException {
141 		properties = (Properties) userProperties;
142 	}
143 
144 	protected String getProperty(String name) {
145 		String value;
146 		value = properties.getProperty(name);
147 		if (value == null) {
148 			int i = name.indexOf(';');
149 			if (i > 0)
150 				value = properties.getProperty(name.substring(0, i));
151 		}
152 		if (value == null)
153 			value = properties.getProperty("altres");
154 		return value;
155 	}
156 
157 	public boolean needsRecognizedCertificate(String contentType) {
158 		boolean DEFAULT_VALUE=true;
159 		
160 		String propertieValue = getProperty(contentType);
161 		if (propertieValue == null)
162 			return DEFAULT_VALUE; //PJR 05-02-09 posem a true 
163 
164 		String[] signType = propertieValue.split(",");
165 
166 		if (signType == null || signType.length < 2)
167 			return DEFAULT_VALUE;
168 
169 		return signType[1].equalsIgnoreCase("CR");
170 	}
171 
172 	public boolean needsTimeStamp(String contentType) {
173 		//FIXME això només és per a fer una prova.
174 		boolean DEFAULT_VALUE=false;
175 		
176 		String propertieValue = getProperty(contentType);
177 		if (propertieValue == null)
178 			return DEFAULT_VALUE;
179 		String[] signType = propertieValue.split(",");
180 
181 		if (signType == null || signType.length < 2)
182 			return DEFAULT_VALUE;
183 
184 		return signType[0].equalsIgnoreCase("SAST");
185 	}
186 
187 	/**
188 	 *	Comprova si un contentType fa servir signatura del document original.
189 	 *	
190 	 *	Es llegeix un tercer parámetre en la llista de propietats del content 
191 	 *	type. Si aquest tercer parámetre es RAW, retorna cert. Si aquest tercer 
192 	 *	parámetre no hi és o te un valor diferent, retorna false.
193 	 */
194 	public boolean needsRawSignature( String contentType )
195 	{
196 		String propertieValue = getProperty(contentType);
197 		if (propertieValue == null)
198 			return false;
199 		String[] signType = propertieValue.split(",");
200 
201 		if (signType == null || signType.length < 3)
202 			return false;
203 
204 		return signType[2].equalsIgnoreCase("RAW");
205 	}
206 	
207 	public boolean needsAdvancedSignature(String contentType)
208 			throws SignatureSignException {
209 		return true;
210 	}
211 
212 	public boolean enDesenvolupament(){
213 		try{
214 			return new ValidadorProxy() . isEnDesenvolupament();
215 		}catch(Exception e){
216 			
217 		}
218 		return true;
219 	}
220 	
221 	public String getTimestampService(String principalName) {
222 		return properties.getProperty("ts." + principalName);
223 	}
224 
225 	public String getTimestampServiceApplicationId(String principalName) {
226 		return properties.getProperty("ts.applicationId." + principalName);
227 	}
228 	
229 	public String getTimestampServicePolicyOID(String name) {
230 		return properties.getProperty("ts.policyOID." + name);		
231 	}
232 	
233 	private String[] getMultiProperty (String name)
234 	{
235 		int i = 1;
236 		Vector v = new Vector ();
237 		while ( properties.getProperty(name+"."+i) != null)
238 		{
239 			v.add (properties.getProperty(name+"."+i));
240 			i++;
241 		}
242 		return (String [] ) v.toArray( new String [v.size()]);
243 	}
244 	
245 	public String[] getRecognizedPolicies ()
246 	{
247 		return getMultiProperty ("policy.recognized");
248 	}
249 
250 	public String[] getRecognizedSecureDevicePolicies ()
251 	{
252 		return getMultiProperty ("policy.recognizedsecuredevice");
253 	}
254 
255 	public String[] getAdvancedPolicies ()
256 	{
257 		return getMultiProperty ("policy.advanced");
258 	}
259 
260 	public String[] getRecognizedExtensions ()
261 	{
262 		return getMultiProperty ("extension.recognized");
263 	}
264 
265 	public String[] getAmbigousPoliciess ()
266 	{
267 		return getMultiProperty ("policy.ambigous");
268 	}
269 
270 	public String[] getRootCAs ()
271 	{
272 		return getMultiProperty ("ca");
273 	}
274 
275 	public String[] getPKCS11Drivers ()
276 	{
277 		return getMultiProperty ("pkcs11");
278 	}
279 
280 	public String getCertificateParser(String id) {
281 		
282 		return properties.getProperty(id);
283 	}
284 
285 	public String getPKCS11DriversDescription(String library) {
286 		String [] providersDescs=getMultiProperty ("pkcs11");
287 		int i=0;
288 		for(i=0;i<providersDescs.length;i++){
289 			if(providersDescs[i].contains(library)){
290 				return  getMultiProperty ("pkcs11.description")[i];
291 			}
292 		}
293 		return null;
294 	}
295 	
296 	public void getUpdateSite() throws FileNotFoundException, IOException {
297 		InputStream inputStream = getClass().getResourceAsStream("version.properties");
298 		if (inputStream == null) {
299 			throw new FileNotFoundException();
300 		}
301 		Properties tempProperties = new Properties();
302 		tempProperties.load(inputStream);
303 		inputStream.close();
304 		updateSite = tempProperties.getProperty("updateSite");
305 		configurationFile=tempProperties.getProperty("configurationFile");
306 		propertiesCache=tempProperties.getProperty("propertiesCache");
307 	}
308 	
309 	public String [] getPublicEmployeeOID() {
310 		//preparem per a un futur, en què puguin haber diversos certificats admesos per a compulsa
311 		String [] props=getMultiProperty("policy.publicemployeeOID");
312 		if(props.length==0 && properties.getProperty("policy.publicemployeeOID")!=null) return new String[]{properties.getProperty("policy.publicemployeeOID")};
313 		return props; 
314 		
315 	}
316 	
317 	public String getPublicEmployeeParser() {
318 		return properties.getProperty("certificateparser.funcionari", "es.caib.signatura.provider.impl.common.FuncionariParsedCertificateImpl");	
319 	}
320 }