svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / UrlReadFunction.java @ 45213
History | View | Annotate | Download (2.7 KB)
1 | 45011 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|---|---|
2 | |||
3 | import java.net.URL; |
||
4 | import java.security.KeyManagementException; |
||
5 | import java.security.NoSuchAlgorithmException; |
||
6 | import java.security.cert.CertificateException; |
||
7 | import java.security.cert.X509Certificate; |
||
8 | import javax.net.ssl.HttpsURLConnection; |
||
9 | import javax.net.ssl.SSLContext; |
||
10 | import javax.net.ssl.SSLSocketFactory; |
||
11 | import javax.net.ssl.TrustManager; |
||
12 | import javax.net.ssl.X509TrustManager; |
||
13 | import org.apache.commons.io.IOUtils; |
||
14 | import org.apache.commons.lang3.Range; |
||
15 | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_URLREAD; |
||
16 | import org.gvsig.expressionevaluator.Function; |
||
17 | import org.gvsig.expressionevaluator.Interpreter; |
||
18 | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
||
19 | |||
20 | /**
|
||
21 | *
|
||
22 | * @author jjdelcerro
|
||
23 | */
|
||
24 | public class UrlReadFunction extends AbstractFunction { |
||
25 | |||
26 | public static class X509TrustAllManager implements X509TrustManager { |
||
27 | private static SSLSocketFactory socketFactory = null; |
||
28 | |||
29 | public static void setDefaultSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { |
||
30 | if( socketFactory == null ) { |
||
31 | X509TrustAllManager trustAll = new X509TrustAllManager();
|
||
32 | SSLContext sslContext = SSLContext.getInstance("TLS"); |
||
33 | sslContext.init(null, new TrustManager[] { trustAll }, new java.security.SecureRandom()); |
||
34 | socketFactory = sslContext.getSocketFactory(); |
||
35 | } |
||
36 | if( HttpsURLConnection.getDefaultSSLSocketFactory()!=socketFactory ) { |
||
37 | HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
|
||
38 | } |
||
39 | } |
||
40 | |||
41 | @Override
|
||
42 | public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { |
||
43 | } |
||
44 | |||
45 | @Override
|
||
46 | public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { |
||
47 | } |
||
48 | |||
49 | @Override
|
||
50 | public X509Certificate[] getAcceptedIssuers() { |
||
51 | return null; |
||
52 | } |
||
53 | |||
54 | } |
||
55 | |||
56 | |||
57 | public UrlReadFunction() {
|
||
58 | super(Function.GROUP_PROGRAMMING,
|
||
59 | FUNCTION_URLREAD, |
||
60 | Range.is(1),
|
||
61 | null,
|
||
62 | FUNCTION_URLREAD+"({{expression}})",
|
||
63 | null,
|
||
64 | "String",
|
||
65 | false
|
||
66 | ); |
||
67 | this.addAlias("URLREAD"); |
||
68 | |||
69 | } |
||
70 | |||
71 | @Override
|
||
72 | public boolean allowConstantFolding() { |
||
73 | return false; |
||
74 | } |
||
75 | |||
76 | @Override
|
||
77 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
78 | URL url = getURL(args,0); |
||
79 | X509TrustAllManager.setDefaultSSLSocketFactory(); |
||
80 | String s = IOUtils.toString(url);
|
||
81 | return s;
|
||
82 | } |
||
83 | |||
84 | } |