]> git.argeo.org Git - lgpl/argeo-commons.git/blob - sandbox/jackrabbit/WebDavTest.java
Prepare next development cycle
[lgpl/argeo-commons.git] / sandbox / jackrabbit / WebDavTest.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.sandbox.jackrabbit;
18
19 import java.io.FileInputStream;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.httpclient.Credentials;
24 import org.apache.commons.httpclient.HostConfiguration;
25 import org.apache.commons.httpclient.HttpClient;
26 import org.apache.commons.httpclient.HttpConnectionManager;
27 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
28 import org.apache.commons.httpclient.UsernamePasswordCredentials;
29 import org.apache.commons.httpclient.auth.AuthScope;
30 import org.apache.commons.httpclient.methods.GetMethod;
31 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
32 import org.apache.commons.httpclient.methods.RequestEntity;
33 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.jackrabbit.webdav.client.methods.CheckoutMethod;
37 import org.apache.jackrabbit.webdav.client.methods.CopyMethod;
38 import org.apache.jackrabbit.webdav.client.methods.DavMethod;
39 import org.apache.jackrabbit.webdav.client.methods.PropPatchMethod;
40 import org.apache.jackrabbit.webdav.client.methods.PutMethod;
41 import org.apache.jackrabbit.webdav.property.DavProperty;
42 import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
43 import org.apache.jackrabbit.webdav.version.DeltaVConstants;
44
45 public class WebDavTest {
46 private final static Log log = LogFactory.getLog(WebDavTest.class);
47
48 /**
49 * @param args
50 */
51 public static void main(String[] args) {
52 try {
53 HostConfiguration hostConfig = new HostConfiguration();
54 hostConfig.setHost("localhost", 7070);
55 // hostConfig.
56 HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
57 HttpConnectionManagerParams params = new HttpConnectionManagerParams();
58 int maxHostConnections = 20;
59 params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
60 connectionManager.setParams(params);
61 HttpClient client = new HttpClient(connectionManager);
62 Credentials creds = new UsernamePasswordCredentials("demo", "demo");
63 client.getState().setCredentials(AuthScope.ANY, creds);
64 client.setHostConfiguration(hostConfig);
65 // return client;
66
67 String baseUrl = "http://localhost:7070/webdav/default/";
68 // String fileName = "test.xml";
69 String file00 = "dummy00.xls";
70 String file01 = "dummy01.xls";
71 String url00 = baseUrl + file00;
72 String url01 = baseUrl + file01;
73 String urlCopied = baseUrl + "test-copied.xls";
74
75 // PUT
76 log.debug("Create " + url00);
77 PutMethod pm = new PutMethod(url00);
78 RequestEntity requestEntity = new InputStreamRequestEntity(
79 new FileInputStream(file00));
80 pm.setRequestEntity(requestEntity);
81 client.executeMethod(pm);
82 log.debug("POST status: " + pm.getStatusCode() + " "
83 + pm.getStatusText());
84
85 // PROP PATCH
86 List<DavProperty> props = new ArrayList<DavProperty>();
87 props.add(new DefaultDavProperty("auto-version",
88 DeltaVConstants.XML_CHECKOUT_CHECKIN,
89 DeltaVConstants.NAMESPACE));
90 PropPatchMethod pp = new PropPatchMethod(url00, props);
91 client.executeMethod(pp);
92 log.debug("PROP PATCH status: " + pp.getStatusCode() + " "
93 + pp.getStatusText());
94
95 // PUT (update)
96 log.debug("Update " + url00);
97 pm = new PutMethod(url00);
98 requestEntity = new InputStreamRequestEntity(new FileInputStream(
99 file01));
100 pm.setRequestEntity(requestEntity);
101 client.executeMethod(pm);
102 log.debug("POST status: " + pm.getStatusCode() + " "
103 + pm.getStatusText());
104
105 // COPY
106 log.debug("Copy to " + urlCopied);
107 DavMethod copy = new CopyMethod(url00, urlCopied, true);
108 client.executeMethod(copy);
109
110 log.debug("COPY status: " + copy.getStatusCode() + " "
111 + copy.getStatusText());
112
113 // GET
114 // CheckoutMethod gm = new CheckoutMethod(baseUrl + fileName);
115 log.debug("Retrieve " + urlCopied);
116 GetMethod gm = new GetMethod(urlCopied);
117 client.executeMethod(gm);
118 String responseGet = gm.getResponseBodyAsString();
119 log.debug("GET status: " + gm.getStatusCode() + " "
120 + gm.getStatusText());
121 // log.debug("GET: " + responseGet);
122 } catch (Exception e) {
123 e.printStackTrace();
124 }
125 }
126 }