View Javadoc

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