]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d438059cf45f35d900ab80c8bace22f747a77ca0
[tmp/jakarta-migration.git] /
1 /*
2  * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the GNU Lesser General Public License
6  * (LGPL) version 2.1 which accompanies this distribution, and is available at
7  * http://www.gnu.org/licenses/lgpl.html
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * Contributors:
15  *     Nuxeo - initial API and implementation
16  *
17  * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
18  */
19 package org.collectionspace.hello.services.nuxeo;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.List;
25 import java.util.Map;
26
27
28 import org.restlet.Client;
29 import org.restlet.data.ChallengeResponse;
30 import org.restlet.data.ChallengeScheme;
31 import org.restlet.data.Cookie;
32 import org.restlet.data.Form;
33 import org.restlet.data.MediaType;
34 import org.restlet.data.Method;
35 import org.restlet.data.Parameter;
36 import org.restlet.data.Protocol;
37 import org.restlet.data.Request;
38 import org.restlet.resource.OutputRepresentation;
39 import org.restlet.resource.Representation;
40 import org.restlet.util.Series;
41
42 public class NuxeoRESTClient {
43
44     public static final int AUTH_TYPE_NONE = 0;
45     public static final int AUTH_TYPE_BASIC = 1;
46     public static final int AUTH_TYPE_SECRET = 2;
47     protected String baseURL = "http://127.0.0.1:8080/nuxeo";
48     protected String restPrefix = "restAPI";
49     protected String davPrefix = "dav";
50     protected List<Cookie> cookies;
51     protected int authType = AUTH_TYPE_NONE;
52     protected String userName;
53     protected String password;
54     protected String secretToken;
55     protected Client restClient;
56
57     public NuxeoRESTClient(String baseURL) {
58         this.baseURL = baseURL;
59     }
60
61     public NuxeoRESTClient(String protocol, String serverIP, String serverPort) {
62         this(protocol, serverIP, serverPort, "nuxeo");
63     }
64
65     public NuxeoRESTClient(String protocol, String serverIP, String serverPort,
66             String servletPath) {
67         StringBuffer sb = new StringBuffer();
68         sb.append(protocol);
69         sb.append("://");
70         sb.append(serverIP);
71         if (serverPort != null && !serverIP.equals("80")) {
72             sb.append(':');
73             sb.append(serverPort);
74         }
75         sb.append(servletPath);
76         sb.append('/');
77         baseURL = sb.toString();
78     }
79
80     public void setBasicAuthentication(String userName, String password) {
81         authType = AUTH_TYPE_BASIC;
82         this.userName = userName;
83         this.password = password;
84     }
85
86     public void setSharedSecretAuthentication(String userName,
87             String secretToken) {
88         authType = AUTH_TYPE_SECRET;
89         this.userName = userName;
90         this.secretToken = secretToken;
91     }
92
93     public void setCookies(List<Cookie> cookies) {
94         this.cookies = cookies;
95     }
96
97     public Representation post(List<String> pathParams,
98             Map<String, String> queryParams, InputStream istream) {
99         String path = "";
100         StringBuffer pathBuffer = new StringBuffer();
101
102         if (pathParams != null) {
103             for (String p : pathParams) {
104                 pathBuffer.append(p);
105                 pathBuffer.append('/');
106             }
107             path = pathBuffer.toString();
108         }
109
110         return post(path, queryParams, istream);
111     }
112
113     public Representation post(String subPath,
114             Map<String, String> queryParams, InputStream istream) {
115         StringBuffer urlBuffer = new StringBuffer();
116
117         if (subPath.startsWith("/")) {
118             subPath = subPath.substring(1);
119         }
120         if (subPath.endsWith("/")) {
121             subPath = subPath.substring(0, subPath.length() - 1);
122         }
123
124         urlBuffer.append(baseURL);
125         urlBuffer.append('/');
126         urlBuffer.append(restPrefix);
127         urlBuffer.append('/');
128         urlBuffer.append(subPath);
129
130         if (queryParams != null) {
131             urlBuffer.append('?');
132             
133             String qpValue = null;
134             for (String qpName : queryParams.keySet()) {
135                 urlBuffer.append(qpName);
136                 urlBuffer.append('=');
137                 qpValue = queryParams.get(qpName);
138                 if (qpValue != null) {
139                         urlBuffer.append(qpValue.replaceAll(" ", "%20"));
140                 }
141                 urlBuffer.append('&');
142             }
143         }
144
145         String completeURL = urlBuffer.toString();
146         // debug statement should be made conditional
147         System.err.println("CollectionSpace: The complete post URL is: " + completeURL);
148         Request request = new Request(Method.POST, completeURL);
149
150         setupAuth(request);
151         setupCookies(request);
152         final InputStream in = istream;
153         request.setEntity(new OutputRepresentation(
154                 MediaType.MULTIPART_FORM_DATA) {
155
156             @Override
157             public void write(OutputStream outputStream) throws IOException {
158                 byte[] buffer = new byte[1024 * 64];
159                 int read;
160                 while ((read = in.read(buffer)) != -1) {
161                     outputStream.write(buffer, 0, read);
162                 }
163
164             }
165         });
166
167         return getRestClient().handle(request).getEntity();
168     }
169
170     public Representation get(List<String> pathParams,
171             Map<String, String> queryParams) {
172         String path = "";
173         StringBuffer pathBuffer = new StringBuffer();
174
175         if (pathParams != null) {
176             for (String p : pathParams) {
177                 pathBuffer.append(p);
178                 pathBuffer.append('/');
179             }
180             path = pathBuffer.toString();
181         }
182
183         return get(path, queryParams);
184     }
185
186     public Representation get(String subPath,
187             Map<String, String> queryParams) {
188         StringBuffer urlBuffer = new StringBuffer();
189
190         if (subPath.startsWith("/")) {
191             subPath = subPath.substring(1);
192         }
193         if (subPath.endsWith("/")) {
194             subPath = subPath.substring(0, subPath.length() - 1);
195         }
196
197         urlBuffer.append(baseURL);
198         urlBuffer.append('/');
199         urlBuffer.append(restPrefix);
200         urlBuffer.append('/');
201         urlBuffer.append(subPath);
202
203         if (queryParams != null) {
204             urlBuffer.append('?');
205             for (String qpName : queryParams.keySet()) {
206                 urlBuffer.append(qpName);
207                 urlBuffer.append('=');
208                 urlBuffer.append(queryParams.get(qpName).replaceAll(" ", "%20"));
209                 urlBuffer.append('&');
210             }
211         }
212
213         String completeURL = urlBuffer.toString();
214         System.out.println("\nNuxeoRESTClient: calling " + completeURL);
215         Request request = new Request(Method.GET, completeURL);
216         setupAuth(request);
217         setupCookies(request);
218
219         return getRestClient().handle(request).getEntity();
220     }
221
222     protected void setupAuth(Request request) {
223
224         if (authType == AUTH_TYPE_BASIC) {
225             ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
226             ChallengeResponse authentication = new ChallengeResponse(scheme,
227                     userName, password);
228             request.setChallengeResponse(authentication);
229
230         } else if (authType == AUTH_TYPE_SECRET) {
231             Series<Parameter> additionnalHeaders = new Form();
232
233             Map<String, String> securityHeaders = PortalSSOAuthenticationProvider.getHeaders(
234                     secretToken, userName);
235
236             for (String hn : securityHeaders.keySet()) {
237                 additionnalHeaders.add(hn, securityHeaders.get(hn));
238             }
239
240             request.getAttributes().put("org.restlet.http.headers",
241                     additionnalHeaders);
242         }
243     }
244
245     protected void setupCookies(Request request) {
246         if (cookies != null) {
247             request.getCookies().clear();
248             for (Cookie cookie : cookies) {
249                 request.getCookies().add(cookie);
250             }
251         }
252
253     }
254
255     protected Client getRestClient() {
256         if (restClient == null) {
257             if (baseURL.startsWith("https")) {
258                 restClient = new Client(Protocol.HTTPS);
259             } else {
260                 restClient = new Client(Protocol.HTTP);
261             }
262         }
263
264         return restClient;
265     }
266
267     public int getAuthType() {
268         return authType;
269     }
270
271     public void setAuthType(int authType) {
272         this.authType = authType;
273     }
274
275     public String getDavPrefix() {
276         return davPrefix;
277     }
278
279     public void setDavPrefix(String davPrefix) {
280         this.davPrefix = davPrefix;
281     }
282
283     public String getPassword() {
284         return password;
285     }
286
287     public void setPassword(String password) {
288         this.password = password;
289     }
290
291     public String getRestPrefix() {
292         return restPrefix;
293     }
294
295     public void setRestPrefix(String restPrefix) {
296         this.restPrefix = restPrefix;
297     }
298
299     public String getSecretToken() {
300         return secretToken;
301     }
302
303     public void setSecretToken(String secretToken) {
304         this.secretToken = secretToken;
305     }
306
307     public String getUserName() {
308         return userName;
309     }
310
311     public void setUserName(String userName) {
312         this.userName = userName;
313     }
314 }