From do-not-reply at jboss.org Tue Jun 29 11:12:55 2010 Content-Type: multipart/mixed; boundary="===============7144480928689592736==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: exo-jcr-commits at lists.jboss.org Subject: [exo-jcr-commits] exo-jcr SVN: r2717 - ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/proxy. Date: Tue, 29 Jun 2010 11:12:55 -0400 Message-ID: <201006291512.o5TFCtai028637@svn01.web.mwc.hst.phx2.redhat.com> --===============7144480928689592736== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/re= st/ext/proxy/BaseConnector.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ex= t/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/ex= t/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 Max Shaposh= nik - * @version $Id$ - */ -public class BaseConnector extends Connector -{ - - /** The connection. */ - private HTTPConnection conn; - - /** The HTTPResponse. */ - HTTPResponse resp =3D null; - - /** The form_data array. */ - NVPair[] form_data; - - /** The headers array. */ - NVPair[] headers; - - /** Logger. */ - private static final Log LOG =3D ExoLogger.getLogger("exo.ws.rest.ext.B= aseConnector"); - - /** - * {@inheritDoc} - */ - @Override - public HTTPResponse fetchGet(HttpServletRequest httpRequest, String url= ) throws MalformedURLException, - ProtocolNotSuppException, IOException, ModuleException, ParseExcepti= on - { - URL url_obj =3D null; - url_obj =3D new URL(url); - - conn =3D new HTTPConnection(url_obj); - conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - prepareRequestHeaders(httpRequest); - prepareFormParams(url_obj); - conn.setAllowUserInteraction(false); - resp =3D conn.Get(url_obj.getProtocol() + "://" + url_obj.getAuthori= ty() + url_obj.getPath(), form_data, headers); - if (resp.getStatusCode() >=3D 300) - { - LOG.error("Received Error: " + resp.getReasonLine()); - LOG.error(new String(resp.getData())); - } - return resp; - } - - /** - * {@inheritDoc} - */ - @Override - public HTTPResponse fetchPost(HttpServletRequest httpRequest, String ur= l) throws MalformedURLException, - ProtocolNotSuppException, IOException, ModuleException, ParseExcepti= on - { - URL url_obj =3D null; - url_obj =3D new URL(url); - - conn =3D new HTTPConnection(url_obj); - conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - conn.setAllowUserInteraction(false); - prepareRequestHeaders(httpRequest); - - byte[] body =3D new byte[httpRequest.getContentLength()]; - new DataInputStream(httpRequest.getInputStream()).readFully(body); - resp =3D conn.Post(url_obj.getProtocol() + "://" + url_obj.getAuthor= ity() + url_obj.getPath(), body, headers); - if (resp.getStatusCode() >=3D 300) - { - LOG.error("Received Error: " + resp.getReasonLine()); - LOG.error(new String(resp.getData())); - } - return resp; - } - - /** - * {@inheritDoc} - */ - @Override - public HTTPResponse doPut(HttpServletRequest httpRequest, String url) t= hrows MalformedURLException, - ProtocolNotSuppException, IOException, ModuleException, ParseExcepti= on - { - URL url_obj =3D null; - url_obj =3D new URL(url); - - conn =3D new HTTPConnection(url_obj); - conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - conn.setAllowUserInteraction(false); - prepareRequestHeaders(httpRequest); - - byte[] body =3D new byte[httpRequest.getContentLength()]; - new DataInputStream(httpRequest.getInputStream()).readFully(body); - resp =3D conn.Put(url_obj.getProtocol() + "://" + url_obj.getAuthori= ty() + url_obj.getPath(), body, headers); - if (resp.getStatusCode() >=3D 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, ParseExcepti= on - { - URL url_obj =3D null; - url_obj =3D new URL(url); - - conn =3D new HTTPConnection(url_obj); - conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - conn.setAllowUserInteraction(false); - prepareRequestHeaders(httpRequest); - resp =3D conn.Delete(url_obj.getProtocol() + "://" + url_obj.getAuth= ority() + url_obj.getPath(), headers); - if (resp.getStatusCode() >=3D 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 hds =3D new ArrayList(); - for (Enumeration en =3D httpRequest.getHeaderNames(); en.has= MoreElements();) - { - NVPair pair =3D null; - String headerName =3D (String)en.nextElement(); - if (!headerName.equalsIgnoreCase(HttpHeaders.HOST)) { //Do not n= eed to send host - for (Enumeration en2 =3D httpRequest.getHeaders(headerNam= e); en2.hasMoreElements();) - { - pair =3D new NVPair(headerName, en2.nextElement()); - } - hds.add(pair); - this.headers =3D new NVPair[hds.size()]; - this.headers =3D hds.toArray(headers); - } - } - } - - /** - * Prepares form params. - * = - * @param url the url - */ - private void prepareFormParams(URL url) - { - String query =3D url.getQuery(); - if (query !=3D null) - { - try - { - this.form_data =3D Codecs.query2nv(query); - } - catch (ParseException e) - { - LOG.error(e.getMessage()); - } - } - } -} Deleted: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/re= st/ext/proxy/Connector.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ex= t/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/ex= t/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 Max Shaposh= nik - * @version $Id$ - */ -public abstract class Connector -{ - - /** The connect timeout. */ - protected static final int DEFAULT_CONNECT_TIMEOUT_MS =3D 5000; = - = - = - /** - * Do GET proxy request. - * = - * @param httpRequest the HttpServletRequest - * @param url the url to request - * @return response HTTPResponse - */ - abstract HTTPResponse fetchGet(HttpServletRequest httpRequest, String u= rl) 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 u= rl) throws MalformedURLException, ProtocolNotSuppException, - IOException, ModuleException,ParseException; - -} Modified: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/r= est/ext/proxy/ProxyService.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ex= t/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/ex= t/proxy/ProxyService.java 2010-06-29 15:12:54 UTC (rev 2717) @@ -78,8 +78,8 @@ URL url =3D new URL(urlParam); HTTPConnection conn =3D new HTTPConnection(url); conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - NVPair[] headerPairs =3D - toNVPair(headers.getRequestHeaders(), Collections.singleton(Ht= tpHeaders.HOST.toLowerCase())); + NVPair[] headerPairs =3D toNVPair(headers.getRequestHeaders(), // + Collections.singleton(new CaseInsensitiveStringWrapper(HttpHea= ders.HOST))); conn.setAllowUserInteraction(false); NVPair credentials =3D getCredentials(url); if (credentials !=3D null) @@ -89,11 +89,16 @@ HTTPResponse resp =3D conn.Delete(url.getFile(), headerPairs); if (resp.getStatusCode() >=3D 300) { - LOG.warn("DELETE Received : " + resp.getReasonLine()); - byte[] data =3D resp.getData(); - if (data !=3D 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 =3D resp.getData(); + if (data !=3D null) + { + LOG.debug("DELETE. Text : " + new String(data)); + } } } return createResponse(resp); @@ -129,8 +134,8 @@ URL url =3D new URL(urlParam); HTTPConnection conn =3D new HTTPConnection(url); conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - NVPair[] headerPairs =3D - toNVPair(headers.getRequestHeaders(), Collections.singleton(Ht= tpHeaders.HOST.toLowerCase())); + NVPair[] headerPairs =3D toNVPair(headers.getRequestHeaders(), // + Collections.singleton(new CaseInsensitiveStringWrapper(HttpHea= ders.HOST))); conn.setAllowUserInteraction(false); NVPair credentials =3D getCredentials(url); if (credentials !=3D null) @@ -140,11 +145,16 @@ HTTPResponse resp =3D conn.Get(url.getFile(), (NVPair[])null, hea= derPairs); if (resp.getStatusCode() >=3D 300) { - LOG.warn("GET Received: " + resp.getReasonLine()); - byte[] data =3D resp.getData(); - if (data !=3D 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 =3D resp.getData(); + if (data !=3D null) + { + LOG.debug("GET. Text : " + new String(data)); + } } } return createResponse(resp); @@ -182,8 +192,8 @@ URL url =3D new URL(urlParam); HTTPConnection conn =3D new HTTPConnection(url); conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - NVPair[] headerPairs =3D - toNVPair(headers.getRequestHeaders(), Collections.singleton(Ht= tpHeaders.HOST.toLowerCase())); + NVPair[] headerPairs =3D toNVPair(headers.getRequestHeaders(), // + Collections.singleton(new CaseInsensitiveStringWrapper(HttpHea= ders.HOST))); conn.setAllowUserInteraction(false); NVPair credentials =3D getCredentials(url); if (credentials !=3D null) @@ -210,11 +220,16 @@ = if (resp.getStatusCode() >=3D 300) { - LOG.warn("POST Received: " + resp.getReasonLine()); - byte[] data =3D resp.getData(); - if (data !=3D 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 =3D resp.getData(); + if (data !=3D null) + { + LOG.debug("POST. Text : " + new String(data)); + } } } return createResponse(resp); @@ -252,8 +267,8 @@ URL url =3D new URL(urlParam); HTTPConnection conn =3D new HTTPConnection(url); conn.setTimeout(DEFAULT_CONNECT_TIMEOUT_MS); - NVPair[] headerPairs =3D - toNVPair(headers.getRequestHeaders(), Collections.singleton(Ht= tpHeaders.HOST.toLowerCase())); + NVPair[] headerPairs =3D toNVPair(headers.getRequestHeaders(), // + Collections.singleton(new CaseInsensitiveStringWrapper(HttpHea= ders.HOST))); conn.setAllowUserInteraction(false); NVPair credentials =3D getCredentials(url); if (credentials !=3D null) @@ -280,11 +295,16 @@ = if (resp.getStatusCode() >=3D 300) { - LOG.warn("PUT Received : " + resp.getReasonLine()); - byte[] data =3D resp.getData(); - if (data !=3D 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 =3D resp.getData(); + if (data !=3D null) + { + LOG.debug("PUT Received : " + new String(data)); + } } } return createResponse(resp); @@ -371,12 +391,12 @@ return credentials; } = - private NVPair[] toNVPair(MultivaluedMap map, Set skip) + private NVPair[] toNVPair(MultivaluedMap map, Set skip) { List hds =3D new ArrayList(); for (Entry> 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 =3D string; + if (string !=3D null) + { + this.caselessString =3D string.toLowerCase(); + } + else + { + this.caselessString =3D null; + } + } + + public String toString() + { + return string =3D=3D null ? "null" : string; + } + + @Override + public boolean equals(Object obj) + { + if (obj =3D=3D null) + return false; + if (obj.getClass() !=3D getClass()) + return false; + CaseInsensitiveStringWrapper other =3D (CaseInsensitiveStringWrap= per)obj; + return (caselessString =3D=3D null && other.caselessString =3D=3D= null) + || (caselessString !=3D null && caselessString.equals(other.ca= selessString)); + } + + @Override + public int hashCode() + { + return caselessString =3D=3D null ? 0 : caselessString.hashCode(); + } + + } + } --===============7144480928689592736==--