2 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
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
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.
15 * Nuxeo - initial API and implementation
17 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
19 package org.collectionspace.hello.services.nuxeo;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.List;
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;
42 public class NuxeoRESTClient {
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;
57 public NuxeoRESTClient(String baseURL) {
58 this.baseURL = baseURL;
61 public NuxeoRESTClient(String protocol, String serverIP, String serverPort) {
62 this(protocol, serverIP, serverPort, "nuxeo");
65 public NuxeoRESTClient(String protocol, String serverIP, String serverPort,
67 StringBuffer sb = new StringBuffer();
71 if (serverPort != null && !serverIP.equals("80")) {
73 sb.append(serverPort);
75 sb.append(servletPath);
77 baseURL = sb.toString();
80 public void setBasicAuthentication(String userName, String password) {
81 authType = AUTH_TYPE_BASIC;
82 this.userName = userName;
83 this.password = password;
86 public void setSharedSecretAuthentication(String userName,
88 authType = AUTH_TYPE_SECRET;
89 this.userName = userName;
90 this.secretToken = secretToken;
93 public void setCookies(List<Cookie> cookies) {
94 this.cookies = cookies;
97 public Representation post(List<String> pathParams,
98 Map<String, String> queryParams, InputStream istream) {
100 StringBuffer pathBuffer = new StringBuffer();
102 if (pathParams != null) {
103 for (String p : pathParams) {
104 pathBuffer.append(p);
105 pathBuffer.append('/');
107 path = pathBuffer.toString();
110 return post(path, queryParams, istream);
113 public Representation post(String subPath,
114 Map<String, String> queryParams, InputStream istream) {
115 StringBuffer urlBuffer = new StringBuffer();
117 if (subPath.startsWith("/")) {
118 subPath = subPath.substring(1);
120 if (subPath.endsWith("/")) {
121 subPath = subPath.substring(0, subPath.length() - 1);
124 urlBuffer.append(baseURL);
125 urlBuffer.append('/');
126 urlBuffer.append(restPrefix);
127 urlBuffer.append('/');
128 urlBuffer.append(subPath);
130 if (queryParams != null) {
131 urlBuffer.append('?');
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"));
141 urlBuffer.append('&');
145 String completeURL = urlBuffer.toString();
146 System.out.println("\nNuxeoRESTClient: calling " + completeURL);
147 Request request = new Request(Method.POST, completeURL);
150 setupCookies(request);
151 final InputStream in = istream;
152 request.setEntity(new OutputRepresentation(
153 MediaType.MULTIPART_FORM_DATA) {
156 public void write(OutputStream outputStream) throws IOException {
157 byte[] buffer = new byte[1024 * 64];
159 while ((read = in.read(buffer)) != -1) {
160 outputStream.write(buffer, 0, read);
166 return getRestClient().handle(request).getEntity();
169 public Representation get(List<String> pathParams,
170 Map<String, String> queryParams) {
172 StringBuffer pathBuffer = new StringBuffer();
174 if (pathParams != null) {
175 for (String p : pathParams) {
176 pathBuffer.append(p);
177 pathBuffer.append('/');
179 path = pathBuffer.toString();
182 return get(path, queryParams);
185 public Representation get(String subPath,
186 Map<String, String> queryParams) {
187 StringBuffer urlBuffer = new StringBuffer();
189 if (subPath.startsWith("/")) {
190 subPath = subPath.substring(1);
192 if (subPath.endsWith("/")) {
193 subPath = subPath.substring(0, subPath.length() - 1);
196 urlBuffer.append(baseURL);
197 urlBuffer.append('/');
198 urlBuffer.append(restPrefix);
199 urlBuffer.append('/');
200 urlBuffer.append(subPath);
202 if (queryParams != null) {
203 urlBuffer.append('?');
204 for (String qpName : queryParams.keySet()) {
205 urlBuffer.append(qpName);
206 urlBuffer.append('=');
207 urlBuffer.append(queryParams.get(qpName).replaceAll(" ", "%20"));
208 urlBuffer.append('&');
212 String completeURL = urlBuffer.toString();
213 System.out.println("\nNuxeoRESTClient: calling " + completeURL);
214 Request request = new Request(Method.GET, completeURL);
216 setupCookies(request);
218 return getRestClient().handle(request).getEntity();
221 protected void setupAuth(Request request) {
223 if (authType == AUTH_TYPE_BASIC) {
224 ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
225 ChallengeResponse authentication = new ChallengeResponse(scheme,
227 request.setChallengeResponse(authentication);
229 } else if (authType == AUTH_TYPE_SECRET) {
230 Series<Parameter> additionnalHeaders = new Form();
232 Map<String, String> securityHeaders = PortalSSOAuthenticationProvider.getHeaders(
233 secretToken, userName);
235 for (String hn : securityHeaders.keySet()) {
236 additionnalHeaders.add(hn, securityHeaders.get(hn));
239 request.getAttributes().put("org.restlet.http.headers",
244 protected void setupCookies(Request request) {
245 if (cookies != null) {
246 request.getCookies().clear();
247 for (Cookie cookie : cookies) {
248 request.getCookies().add(cookie);
254 protected Client getRestClient() {
255 if (restClient == null) {
256 if (baseURL.startsWith("https")) {
257 restClient = new Client(Protocol.HTTPS);
259 restClient = new Client(Protocol.HTTP);
266 public int getAuthType() {
270 public void setAuthType(int authType) {
271 this.authType = authType;
274 public String getDavPrefix() {
278 public void setDavPrefix(String davPrefix) {
279 this.davPrefix = davPrefix;
282 public String getPassword() {
286 public void setPassword(String password) {
287 this.password = password;
290 public String getRestPrefix() {
294 public void setRestPrefix(String restPrefix) {
295 this.restPrefix = restPrefix;
298 public String getSecretToken() {
302 public void setSecretToken(String secretToken) {
303 this.secretToken = secretToken;
306 public String getUserName() {
310 public void setUserName(String userName) {
311 this.userName = userName;