Author: max_shaposhnik
Date: 2010-02-05 06:38:25 -0500 (Fri, 05 Feb 2010)
New Revision: 1694
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
Log:
EXOJCR-468 first service version
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java
===================================================================
---
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java
(rev 0)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java 2010-02-05
11:38:25 UTC (rev 1694)
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.proxy;
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.exoplatform.common.http.client.Codecs;
+import org.exoplatform.common.http.client.HTTPConnection;
+import org.exoplatform.common.http.client.HTTPResponse;
+import org.exoplatform.common.http.client.ModuleException;
+import org.exoplatform.common.http.client.NVPair;
+import org.exoplatform.common.http.client.ParseException;
+import org.exoplatform.common.http.client.ProtocolNotSuppException;
+
+/**
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id$
+ */
+public class BaseConnector extends Connector
+{
+
+ private HTTPConnection conn;
+
+ HTTPResponse resp = null;
+
+ NVPair[] form_data;
+
+ NVPair[] headers;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public HTTPResponse fetchGet(HttpServletRequest httpRequest, String url) throws
MalformedURLException, ProtocolNotSuppException,
+ IOException, ModuleException,ParseException
+ {
+ URL url_obj = null;
+ url_obj = new URL(url);
+
+ conn = new HTTPConnection(url_obj);
+ conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
+ prepareRequestHeaders(httpRequest);
+ prepareFormParams(url_obj);
+
+ resp = conn.Get(url_obj.getProtocol() + "://" + url_obj.getAuthority()
+ url_obj.getPath(), form_data, headers);
+
+ if (resp.getStatusCode() >= 300)
+ {
+ System.err.println("Received Error: " + resp.getReasonLine());
+ System.err.println(resp.getText());
+ }
+ return resp;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public HTTPResponse fetchPost(HttpServletRequest httpRequest, String url) throws
MalformedURLException, ProtocolNotSuppException,
+ IOException, ModuleException,ParseException
+ {
+ URL url_obj = null;
+ url_obj = new URL(url);
+
+ conn = new HTTPConnection(url_obj);
+ conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
+ prepareRequestHeaders(httpRequest);
+
+ byte[] body = new byte[httpRequest.getContentLength()];;
+ new DataInputStream(httpRequest.getInputStream()).readFully(body);
+ resp = conn.Post(url_obj.getProtocol() + "://" +
url_obj.getAuthority() + url_obj.getPath(), body, headers);
+ if (resp.getStatusCode() >= 300)
+ {
+ System.err.println("Received Error: " + resp.getReasonLine());
+ System.err.println(resp.getText());
+ }
+ return resp;
+ }
+
+ /**
+ * Prepares request headers.
+ *
+ * @param httpRequest the http request
+ */
+ private void prepareRequestHeaders(HttpServletRequest httpRequest)
+ {
+ ArrayList<NVPair> hds = new ArrayList<NVPair>();
+ for (Enumeration<String> en = httpRequest.getHeaderNames();
en.hasMoreElements();)
+ {
+ NVPair pair = null;
+ String headerName = (String)en.nextElement();
+ for (Enumeration<String> en2 = httpRequest.getHeaders(headerName);
en2.hasMoreElements();)
+ {
+ pair = new NVPair(headerName, en2.nextElement());
+ }
+ hds.add(pair);
+ this.headers = new NVPair[hds.size()];
+ this.headers = hds.toArray(headers);
+ }
+ }
+
+ /**
+ * Prepares form params.
+ *
+ * @param url the url
+ */
+ private void prepareFormParams(URL url)
+ {
+ String query = url.getQuery();
+ if (query != null)
+ {
+ try
+ {
+ this.form_data = Codecs.query2nv(query);
+ }
+ catch (ParseException e)
+ {
+ System.err.println(e.getMessage());
+ }
+ }
+ }
+}
Property changes on:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java
===================================================================
---
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java
(rev 0)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java 2010-02-05
11:38:25 UTC (rev 1694)
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.proxy;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.exoplatform.common.http.client.HTTPResponse;
+import org.exoplatform.common.http.client.ModuleException;
+import org.exoplatform.common.http.client.ParseException;
+import org.exoplatform.common.http.client.ProtocolNotSuppException;
+
+/**
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id$
+ */
+public abstract class Connector
+{
+
+ protected static final int DEFAULT_CONNECT_TIMEOUT_MS = 5000;
+
+
+ /**
+ * Do GET proxy request.
+ *
+ * @param httpRequest the HttpServletRequest
+ * @param url the url to request
+ * @return response HTTPResponse
+ */
+ abstract HTTPResponse fetchGet(HttpServletRequest httpRequest, String url) throws
MalformedURLException, ProtocolNotSuppException,
+ IOException, ModuleException,ParseException;
+
+ /**
+ * Do POST proxy request.
+ *
+ * @param httpRequest the HttpServletRequest
+ * @param url the url to request
+ * @return response HTTPResponse
+ */
+ abstract HTTPResponse fetchPost(HttpServletRequest httpRequest, String url) throws
MalformedURLException, ProtocolNotSuppException,
+ IOException, ModuleException,ParseException;
+
+}
Property changes on:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
===================================================================
---
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
(rev 0)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java 2010-02-05
11:38:25 UTC (rev 1694)
@@ -0,0 +1,174 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.proxy;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Enumeration;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+
+import org.exoplatform.common.http.client.HTTPResponse;
+import org.exoplatform.common.http.client.ModuleException;
+import org.exoplatform.common.http.client.ParseException;
+import org.exoplatform.common.http.client.ProtocolNotSuppException;
+import org.exoplatform.services.rest.resource.ResourceContainer;
+
+/**
+ * @author <a href="mailto:max.shaposhnik@exoplatform.com">Max
Shaposhnik</a>
+ * @version $Id$
+ */
+@Path("proxy")
+public class ProxyService implements ResourceContainer
+{
+ /**
+ * Handles GET proxy request.
+ *
+ * @param httpRequestHttpServletRequest
+ * @param url the url to request
+ * @return response Response
+ */
+ @GET
+ public Response doProxyGet(@Context HttpServletRequest httpRequest,
@QueryParam("url") String url)
+ {
+ BaseConnector conn = new BaseConnector();
+ if (url == null)
+ {
+ Throwable e = new Throwable("Necessary URL parameter not found in proxy
request");
+ throw new WebApplicationException(e, createErrorResponse(e, 404));
+ }
+ try
+ {
+ HTTPResponse resp = conn.fetchGet(httpRequest, url);
+ return createResponse(resp);
+ }
+ catch (MalformedURLException mue)
+ {
+ throw new WebApplicationException(mue, createErrorResponse(mue, 400));
+ }
+ catch (ProtocolNotSuppException pnse)
+ {
+ throw new WebApplicationException(pnse, createErrorResponse(pnse, 400));
+ }
+ catch (IOException ioe)
+ {
+ throw new WebApplicationException(ioe, createErrorResponse(ioe, 500));
+ }
+ catch (ModuleException me)
+ {
+ throw new WebApplicationException(me, createErrorResponse(me, 500));
+ }
+ catch (ParseException pe)
+ {
+ throw new WebApplicationException(pe, createErrorResponse(pe, 400));
+ }
+
+ }
+ /**
+ * Handles POST proxy request.
+ *
+ * @param httpRequestHttpServletRequest
+ * @param url the url to request
+ * @return response Response
+ */
+ @POST
+ public Response doProxyPost(@Context HttpServletRequest httpRequest,
@QueryParam("url") String url)
+ {
+ BaseConnector conn = new BaseConnector();
+ if (url == null)
+ {
+ Throwable e = new Throwable("Necessary URL parameter not found in proxy
request");
+ throw new WebApplicationException(e, createErrorResponse(e, 404));
+ }
+ try
+ {
+ HTTPResponse resp = conn.fetchPost(httpRequest, url);
+ return createResponse(resp);
+ }
+ catch (MalformedURLException mue)
+ {
+ throw new WebApplicationException(mue, createErrorResponse(mue, 400));
+ }
+ catch (ProtocolNotSuppException pnse)
+ {
+ throw new WebApplicationException(pnse, createErrorResponse(pnse, 400));
+ }
+ catch (IOException ioe)
+ {
+ throw new WebApplicationException(ioe, createErrorResponse(ioe, 500));
+ }
+ catch (ModuleException me)
+ {
+ throw new WebApplicationException(me, createErrorResponse(me, 500));
+ }
+ catch (ParseException pe)
+ {
+ throw new WebApplicationException(pe, createErrorResponse(pe, 400));
+ }
+ }
+
+ /**
+ * Creates the response from HTTP response.
+ *
+ * @param httpResponse the http response
+ * @return response Response
+ */
+ private Response createResponse(HTTPResponse httpResponse)
+ {
+ ResponseBuilder responseBuilder;
+ try
+ {
+ responseBuilder = Response.status(httpResponse.getStatusCode());
+ for (Enumeration<String> en = httpResponse.listHeaders();
en.hasMoreElements();)
+ {
+ String headerName = (String)en.nextElement();
+ responseBuilder.header(headerName, httpResponse.getHeader(headerName));
+ }
+ return responseBuilder.entity(httpResponse.getInputStream()).build();
+ }
+ catch (IOException e)
+ {
+ throw new WebApplicationException(e, createErrorResponse(e, 500));
+ }
+ catch (ModuleException me)
+ {
+ throw new WebApplicationException(me, createErrorResponse(me, 400));
+ }
+ }
+
+ /**
+ * Creates the error response.
+ *
+ * @param t Throwable
+ * @param status integer response status
+ * @return response Response
+ */
+ private Response createErrorResponse(Throwable t, int status)
+ {
+ return
Response.status(status).entity(t.getMessage()).type("text/plain").build();
+ }
+}
Property changes on:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native