Author: max_shaposhnik
Date: 2010-02-05 10:12:29 -0500 (Fri, 05 Feb 2010)
New Revision: 1703
Modified:
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 POST & PUT Added
Modified:
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 2010-02-05
15:00:41 UTC (rev 1702)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java 2010-02-05
15:12:29 UTC (rev 1703)
@@ -55,7 +55,7 @@
/** The headers array. */
NVPair[] headers;
-
+
/** Logger. */
private static final Log LOG = ExoLogger.getLogger(BaseConnector.class);
@@ -63,24 +63,47 @@
* {@inheritDoc}
*/
@Override
- public HTTPResponse fetchGet(HttpServletRequest httpRequest, String url) throws
MalformedURLException, ProtocolNotSuppException,
- IOException, ModuleException,ParseException
+ 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);
+ 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)
+ {
+ LOG.error("Received Error: " + resp.getReasonLine());
+ LOG.error(resp.getText());
+ }
+ return resp;
+ }
- if (resp.getStatusCode() >= 300)
- {
- LOG.error("Received Error: " + resp.getReasonLine());
- LOG.error(resp.getText());
- }
+ /**
+ * {@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)
+ {
+ LOG.error("Received Error: " + resp.getReasonLine());
+ LOG.error(resp.getText());
+ }
return resp;
}
@@ -88,28 +111,50 @@
* {@inheritDoc}
*/
@Override
- public HTTPResponse fetchPost(HttpServletRequest httpRequest, String url) throws
MalformedURLException, ProtocolNotSuppException,
- IOException, ModuleException,ParseException
+ public HTTPResponse doPut(HttpServletRequest httpRequest, String url) throws
MalformedURLException,
+ ProtocolNotSuppException, IOException, ModuleException, ParseException
{
URL url_obj = null;
- url_obj = new URL(url);
+ url_obj = new URL(url);
- conn = new HTTPConnection(url_obj);
- conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
- prepareRequestHeaders(httpRequest);
+ 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)
- {
- LOG.error("Received Error: " + resp.getReasonLine());
- LOG.error(resp.getText());
- }
+ 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());
+ }
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);
+ 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());
+ }
+ return resp;
+ }
+
+ /**
* Prepares request headers.
*
* @param httpRequest the http request
Modified:
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 2010-02-05
15:00:41 UTC (rev 1702)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java 2010-02-05
15:12:29 UTC (rev 1703)
@@ -58,5 +58,27 @@
*/
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;
}
Modified:
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 2010-02-05
15:00:41 UTC (rev 1702)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java 2010-02-05
15:12:29 UTC (rev 1703)
@@ -23,8 +23,10 @@
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;
@@ -88,6 +90,7 @@
}
}
+
/**
* Handles POST proxy request.
*
@@ -132,6 +135,94 @@
}
/**
+ * 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