Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.installer / org.gvsig.desktop.installer.izpack / src / main / scripts / WebDAVClient.groovy @ 42300

History | View | Annotate | Download (3.26 KB)

1

    
2
import java.security.SecureRandom;
3
import javax.net.ssl.SSLContext;
4
import javax.net.ssl.TrustManager;
5
import javax.net.ssl.X509TrustManager
6
import java.security.cert.X509Certificate
7
import java.security.cert.CertificateException
8

    
9
import org.apache.http.conn.ssl.SSLSocketFactory;
10

    
11
import com.github.sardine.impl.SardineImpl;
12

    
13
public class X509TrustedManager implements X509TrustManager,TrustManager {
14
    public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
15
    }
16

    
17
    public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
18
    }
19

    
20
    public X509Certificate[] getAcceptedIssuers() {
21
      return null;
22
    }
23
}
24

    
25
public class SardineTrusted extends SardineImpl {
26

    
27
  SSLSocketFactory socketFactory = null;
28

    
29
  public SardineTrusted(String username, String password) {
30
    super(username,password);
31
  }
32

    
33
  protected SSLSocketFactory createDefaultSecureSocketFactory() {
34
    if( socketFactory == null ) {
35
      SSLContext sslContext = SSLContext.getInstance("SSL");
36
      def trustManagers = new X509TrustedManager[1];
37
      X509TrustedManager trustManager = new X509TrustedManager()
38
      trustManagers[0] = trustManager
39
      sslContext.init(null, trustManagers, new SecureRandom());
40
      socketFactory = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
41
    }
42
    return socketFactory;
43
  }
44

    
45
}
46

    
47
public class WebDAVClient {
48

    
49
    def log;
50

    
51
    private String user;
52
    private String password;
53
    private Object sardine;
54

    
55
    public WebDAVClient(logger) {
56
            this.log = logger;
57
    }
58

    
59
    public void login(String user, String password) {
60
        log.info("[WEBDAV] login as '"+user+"'.");
61
        this.user = user;
62
        this.password = password;
63

    
64
        this.sardine = new SardineTrusted(this.user,this.password)
65
        this.sardine.enablePreemptiveAuthentication("devel.gvsig.net");
66
        this.sardine.enablePreemptiveAuthentication("downloads.gvsig.net");
67
        this.sardine.enablePreemptiveAuthentication("devel.gvsig.org");
68
        this.sardine.enablePreemptiveAuthentication("downloads.gvsig.org");
69
    }
70

    
71
    public void login() {
72
        log.info("[WEBDAV] login as guest");
73
        this.sardine = new SardineTrusted();
74
    }
75

    
76
    public boolean exists(String url) throws Exception {
77
        return sardine.exists(url);
78
    }
79

    
80
    public void makedirs(String url) throws Exception {
81
        log.info("[WEBDAV] makedirs '"+url+"'.");
82
        URL u = new URL(url);
83
        String[] x = u.getPath().split("/");
84
        String path = "";
85
        for( int i=1; i<x.length; i++ ) {
86
          path = path + "/" + x[i];
87
          URL t = new URL(u,path);
88
          mkdir(t.toString());
89
        }
90
    }
91

    
92
    public void mkdir(String url) throws Exception {
93
        if( ! exists(url) ) {
94
            log.info("[WEBDAV] mkdir '"+url+"'.");
95
            sardine.createDirectory(url);
96
        }
97
    }
98

    
99
    public void put(String source, String target) throws Exception {
100
        log.info("[WEBDAV] put '" + source + "' to '" + target + "'...");
101
        InputStream fis = new FileInputStream(new File(source));
102
        sardine.put(target, fis);
103
        log.info("[WEBDAV] put ok.");
104
    }
105

    
106
    public List list(String url) throws Exception {
107
        List resources = sardine.list(url);
108
        return resources;
109
    }
110
}