Author: aparfonov
Date: 2010-06-29 11:12:54 -0400 (Tue, 29 Jun 2010)
New Revision: 2717
Removed:
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
Modified:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java
Log:
EXOJCR-800
Deleted:
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-06-29
14:58:20 UTC (rev 2716)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/BaseConnector.java 2010-06-29
15:12:54 UTC (rev 2717)
@@ -1,206 +0,0 @@
-/*
- * 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 javax.ws.rs.core.HttpHeaders;
-
-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@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("exo.ws.rest.ext.BaseConnector");
-
- /**
- * {@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(new String(resp.getData()));
- }
- 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(new String(resp.getData()));
- }
- 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(new String(resp.getData()));
- }
- 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(new String(resp.getData()));
- }
- 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();
- if (!headerName.equalsIgnoreCase(HttpHeaders.HOST)) { //Do not need to send
host
- 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());
- }
- }
- }
-}
Deleted:
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-06-29
14:58:20 UTC (rev 2716)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/Connector.java 2010-06-29
15:12:54 UTC (rev 2717)
@@ -1,84 +0,0 @@
-/*
- * 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
-{
-
- /** 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;
-
-}
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-06-29
14:58:20 UTC (rev 2716)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy/ProxyService.java 2010-06-29
15:12:54 UTC (rev 2717)
@@ -78,8 +78,8 @@
URL url = new URL(urlParam);
HTTPConnection conn = new HTTPConnection(url);
conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
- NVPair[] headerPairs =
- toNVPair(headers.getRequestHeaders(),
Collections.singleton(HttpHeaders.HOST.toLowerCase()));
+ NVPair[] headerPairs = toNVPair(headers.getRequestHeaders(), //
+ Collections.singleton(new CaseInsensitiveStringWrapper(HttpHeaders.HOST)));
conn.setAllowUserInteraction(false);
NVPair credentials = getCredentials(url);
if (credentials != null)
@@ -89,11 +89,16 @@
HTTPResponse resp = conn.Delete(url.getFile(), headerPairs);
if (resp.getStatusCode() >= 300)
{
- LOG.warn("DELETE Received : " + resp.getReasonLine());
- byte[] data = resp.getData();
- if (data != null)
+ if (LOG.isDebugEnabled())
{
- LOG.warn("DELETE Received : " + new String(data));
+ // Do not read data if debug is off.
+ // Client may get empty response, may not read stream twice.
+ LOG.debug("DELETE. received status " + resp.getStatusCode() +
", " + resp.getReasonLine());
+ byte[] data = resp.getData();
+ if (data != null)
+ {
+ LOG.debug("DELETE. Text : " + new String(data));
+ }
}
}
return createResponse(resp);
@@ -129,8 +134,8 @@
URL url = new URL(urlParam);
HTTPConnection conn = new HTTPConnection(url);
conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
- NVPair[] headerPairs =
- toNVPair(headers.getRequestHeaders(),
Collections.singleton(HttpHeaders.HOST.toLowerCase()));
+ NVPair[] headerPairs = toNVPair(headers.getRequestHeaders(), //
+ Collections.singleton(new CaseInsensitiveStringWrapper(HttpHeaders.HOST)));
conn.setAllowUserInteraction(false);
NVPair credentials = getCredentials(url);
if (credentials != null)
@@ -140,11 +145,16 @@
HTTPResponse resp = conn.Get(url.getFile(), (NVPair[])null, headerPairs);
if (resp.getStatusCode() >= 300)
{
- LOG.warn("GET Received: " + resp.getReasonLine());
- byte[] data = resp.getData();
- if (data != null)
+ if (LOG.isDebugEnabled())
{
- LOG.warn("GET Received: " + new String(data));
+ // Do not read data if debug is off.
+ // Client may get empty response, may not read stream twice.
+ LOG.debug("GET. received status " + resp.getStatusCode() +
", " + resp.getReasonLine());
+ byte[] data = resp.getData();
+ if (data != null)
+ {
+ LOG.debug("GET. Text : " + new String(data));
+ }
}
}
return createResponse(resp);
@@ -182,8 +192,8 @@
URL url = new URL(urlParam);
HTTPConnection conn = new HTTPConnection(url);
conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
- NVPair[] headerPairs =
- toNVPair(headers.getRequestHeaders(),
Collections.singleton(HttpHeaders.HOST.toLowerCase()));
+ NVPair[] headerPairs = toNVPair(headers.getRequestHeaders(), //
+ Collections.singleton(new CaseInsensitiveStringWrapper(HttpHeaders.HOST)));
conn.setAllowUserInteraction(false);
NVPair credentials = getCredentials(url);
if (credentials != null)
@@ -210,11 +220,16 @@
if (resp.getStatusCode() >= 300)
{
- LOG.warn("POST Received: " + resp.getReasonLine());
- byte[] data = resp.getData();
- if (data != null)
+ if (LOG.isDebugEnabled())
{
- LOG.warn("POST Received: " + new String(data));
+ // Do not read data if debug is off.
+ // Client may get empty response, may not read stream twice.
+ LOG.debug("POST. received status " + resp.getStatusCode() +
", " + resp.getReasonLine());
+ byte[] data = resp.getData();
+ if (data != null)
+ {
+ LOG.debug("POST. Text : " + new String(data));
+ }
}
}
return createResponse(resp);
@@ -252,8 +267,8 @@
URL url = new URL(urlParam);
HTTPConnection conn = new HTTPConnection(url);
conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
- NVPair[] headerPairs =
- toNVPair(headers.getRequestHeaders(),
Collections.singleton(HttpHeaders.HOST.toLowerCase()));
+ NVPair[] headerPairs = toNVPair(headers.getRequestHeaders(), //
+ Collections.singleton(new CaseInsensitiveStringWrapper(HttpHeaders.HOST)));
conn.setAllowUserInteraction(false);
NVPair credentials = getCredentials(url);
if (credentials != null)
@@ -280,11 +295,16 @@
if (resp.getStatusCode() >= 300)
{
- LOG.warn("PUT Received : " + resp.getReasonLine());
- byte[] data = resp.getData();
- if (data != null)
+ if (LOG.isDebugEnabled())
{
- LOG.warn("PUT Received : " + new String(data));
+ // Do not read data if debug is off.
+ // Client may get empty response, may not read stream twice.
+ LOG.debug("PUT. received status " + resp.getStatusCode() +
", " + resp.getReasonLine());
+ byte[] data = resp.getData();
+ if (data != null)
+ {
+ LOG.debug("PUT Received : " + new String(data));
+ }
}
}
return createResponse(resp);
@@ -371,12 +391,12 @@
return credentials;
}
- private NVPair[] toNVPair(MultivaluedMap<String, String> map, Set<String>
skip)
+ private NVPair[] toNVPair(MultivaluedMap<String, String> map,
Set<CaseInsensitiveStringWrapper> skip)
{
List<NVPair> hds = new ArrayList<NVPair>();
for (Entry<String, List<String>> e : map.entrySet())
{
- if (!skip.contains(e.getKey()))
+ if (!skip.contains(new CaseInsensitiveStringWrapper(e.getKey())))
{
for (String v : e.getValue())
{
@@ -386,4 +406,50 @@
}
return hds.toArray(new NVPair[hds.size()]);
}
+
+ private class CaseInsensitiveStringWrapper
+ {
+
+ private final String string;
+
+ private final String caselessString;
+
+ CaseInsensitiveStringWrapper(String string)
+ {
+ this.string = string;
+ if (string != null)
+ {
+ this.caselessString = string.toLowerCase();
+ }
+ else
+ {
+ this.caselessString = null;
+ }
+ }
+
+ public String toString()
+ {
+ return string == null ? "null" : string;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj == null)
+ return false;
+ if (obj.getClass() != getClass())
+ return false;
+ CaseInsensitiveStringWrapper other = (CaseInsensitiveStringWrapper)obj;
+ return (caselessString == null && other.caselessString == null)
+ || (caselessString != null &&
caselessString.equals(other.caselessString));
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return caselessString == null ? 0 : caselessString.hashCode();
+ }
+
+ }
+
}