private void executeRequest ( HttpMethod httpMethodProxyRequest , HttpServletRequest httpServletRequest , PluginResponse httpServletResponse , History history ) throws Exception { int intProxyResponseCode = 999 ; // Create a default HttpClient HttpClient httpClient = new HttpClient ( ) ; HttpState state = new HttpState ( ) ; try { httpMethodProxyRequest . setFollowRedirects ( false ) ; ArrayList < String > headersToRemove = getRemoveHeaders ( ) ; httpClient . getParams ( ) . setSoTimeout ( 60000 ) ; httpServletRequest . setAttribute ( "com.groupon.odo.removeHeaders" , headersToRemove ) ; // exception handling for httpclient HttpMethodRetryHandler noretryhandler = new HttpMethodRetryHandler ( ) { public boolean retryMethod ( final HttpMethod method , final IOException exception , int executionCount ) { return false ; } } ; httpMethodProxyRequest . getParams ( ) . setParameter ( HttpMethodParams . RETRY_HANDLER , noretryhandler ) ; intProxyResponseCode = httpClient . executeMethod ( httpMethodProxyRequest . getHostConfiguration ( ) , httpMethodProxyRequest , state ) ; } catch ( Exception e ) { // Return a gateway timeout httpServletResponse . setStatus ( 504 ) ; httpServletResponse . setHeader ( Constants . HEADER_STATUS , "504" ) ; httpServletResponse . flushBuffer ( ) ; return ; } logger . info ( "Response code: {}, {}" , intProxyResponseCode , HttpUtilities . getURL ( httpMethodProxyRequest . getURI ( ) . toString ( ) ) ) ; // Pass the response code back to the client httpServletResponse . setStatus ( intProxyResponseCode ) ; // Pass response headers back to the client Header [ ] headerArrayResponse = httpMethodProxyRequest . getResponseHeaders ( ) ; for ( Header header : headerArrayResponse ) { // remove transfer-encoding header. The http libraries will handle this encoding if ( header . getName ( ) . toLowerCase ( ) . equals ( "transfer-encoding" ) ) { continue ; } httpServletResponse . setHeader ( header . getName ( ) , header . getValue ( ) ) ; } // there is no data for a HTTP 304 or 204 if ( intProxyResponseCode != HttpServletResponse . SC_NOT_MODIFIED && intProxyResponseCode != HttpServletResponse . SC_NO_CONTENT ) { // Send the content to the client httpServletResponse . resetBuffer ( ) ; httpServletResponse . getOutputStream ( ) . write ( httpMethodProxyRequest . getResponseBody ( ) ) ; } // copy cookies to servlet response for ( Cookie cookie : state . getCookies ( ) ) { javax . servlet . http . Cookie servletCookie = new javax . servlet . http . Cookie ( cookie . getName ( ) , cookie . getValue ( ) ) ; if ( cookie . getPath ( ) != null ) { servletCookie . setPath ( cookie . getPath ( ) ) ; } if ( cookie . getDomain ( ) != null ) { servletCookie . setDomain ( cookie . getDomain ( ) ) ; } // convert expiry date to max age if ( cookie . getExpiryDate ( ) != null ) { servletCookie . setMaxAge ( ( int ) ( ( cookie . getExpiryDate ( ) . getTime ( ) - System . currentTimeMillis ( ) ) / 1000 ) ) ; } servletCookie . setSecure ( cookie . getSecure ( ) ) ; servletCookie . setVersion ( cookie . getVersion ( ) ) ; if ( cookie . getComment ( ) != null ) { servletCookie . setComment ( cookie . getComment ( ) ) ; } httpServletResponse . addCookie ( servletCookie ) ; } }
Execute a request