[exo-jcr-commits] exo-jcr SVN: r1886 - in ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext: proxy and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Feb 18 05:34:09 EST 2010


Author: max_shaposhnik
Date: 2010-02-18 05:34:09 -0500 (Thu, 18 Feb 2010)
New Revision: 1886

Added:
   ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/
   ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java
   ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java
   ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
Log:
Added proxy service

Added: ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java	                        (rev 0)
+++ ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java	2010-02-18 10:34:09 UTC (rev 1886)
@@ -0,0 +1,206 @@
+/*
+ * 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;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+/**
+ * @author <a href="mailto:max.shaposhnik at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id$
+ */
+public class BaseConnector extends Connector
+{
+
+   /** The connection. */
+   private HTTPConnection conn;
+
+   /** The HTTPResponse. */
+   HTTPResponse resp = null;
+
+   /** The form_data array. */
+   NVPair[] form_data;
+
+   /** The headers array. */
+   NVPair[] headers;
+
+   /** Logger. */
+   private static final Log LOG = ExoLogger.getLogger(BaseConnector.class);
+
+   /**
+    * {@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);
+      conn.setAllowUserInteraction(false);
+      resp = conn.Get(url_obj.getProtocol() + "://" + url_obj.getAuthority() + url_obj.getPath(), form_data, headers);
+      if (resp.getStatusCode() >= 300)
+      {
+         LOG.error("Received Error: " + resp.getReasonLine());
+         LOG.error(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);
+      conn.setAllowUserInteraction(false);
+      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)
+      {
+         LOG.error("Received Error: " + resp.getReasonLine());
+         LOG.error(resp.getText());
+      }
+      conn.stop();
+      return resp;
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   public HTTPResponse doPut(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);
+      conn.setAllowUserInteraction(false);
+      prepareRequestHeaders(httpRequest);
+
+      byte[] body = new byte[httpRequest.getContentLength()];
+      new DataInputStream(httpRequest.getInputStream()).readFully(body);
+      resp = conn.Put(url_obj.getProtocol() + "://" + url_obj.getAuthority() + url_obj.getPath(), body, headers);
+      if (resp.getStatusCode() >= 300)
+      {
+         LOG.error("Received Error: " + resp.getReasonLine());
+         LOG.error(resp.getText());
+      }
+      conn.stop();
+      return resp;
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   public HTTPResponse doDelete(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);
+      conn.setAllowUserInteraction(false);
+      prepareRequestHeaders(httpRequest);
+      resp = conn.Delete(url_obj.getProtocol() + "://" + url_obj.getAuthority() + url_obj.getPath(), headers);
+      if (resp.getStatusCode() >= 300)
+      {
+         LOG.error("Received Error: " + resp.getReasonLine());
+         LOG.error(resp.getText());
+      }
+      conn.stop();
+      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)
+         {
+            LOG.error(e.getMessage());
+         }
+      }
+   }
+}


Property changes on: ws/branches/2.2.x/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/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java	                        (rev 0)
+++ ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java	2010-02-18 10:34:09 UTC (rev 1886)
@@ -0,0 +1,84 @@
+/*
+ * 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 at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id$
+ */
+public abstract class Connector
+{
+
+   /** The  connect timeout. */
+   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;
+   
+   
+   /**
+    * Do PUT proxy request.
+    * 
+    * @param httpRequest the HttpServletRequest
+    * @param url the url to request
+    * @return  response HTTPResponse
+    */
+   abstract HTTPResponse doPut(HttpServletRequest httpRequest, String url) throws MalformedURLException, ProtocolNotSuppException,
+   IOException, ModuleException,ParseException;
+   
+   
+   /**
+    * Do DELETE proxy request.
+    * 
+    * @param httpRequest the HttpServletRequest
+    * @param url the url to request
+    * @return  response HTTPResponse
+    */
+   abstract HTTPResponse doDelete(HttpServletRequest httpRequest, String url) throws MalformedURLException, ProtocolNotSuppException,
+   IOException, ModuleException,ParseException;
+
+}


Property changes on: ws/branches/2.2.x/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/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
===================================================================
--- ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java	                        (rev 0)
+++ ws/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java	2010-02-18 10:34:09 UTC (rev 1886)
@@ -0,0 +1,265 @@
+/*
+ * 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.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+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 at exoplatform.com">Max Shaposhnik</a>
+ * @version $Id$
+ */
+ at 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));
+      }
+   }
+
+   /**
+    * Handles PUT proxy request.
+    * 
+    * @param httpRequestHttpServletRequest
+    * @param url the url to request
+    * @return  response Response
+    */
+   @PUT
+   public Response doProxyPut(@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.doPut(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 DELETE proxy request.
+    * 
+    * @param httpRequestHttpServletRequest
+    * @param url the url to request
+    * @return  response Response
+    */
+   @DELETE
+   public Response doProxyDelete(@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.doDelete(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/branches/2.2.x/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the exo-jcr-commits mailing list