List of usage examples for org.apache.http.client.methods HttpRequestBase getParams
@Deprecated
public HttpParams getParams()
From source file:it.staiger.jmeter.protocol.http.sampler.HTTPHC4DynamicFilePost.java
/** * Parses the result and fills the SampleResult with its info * @param httpRequest the executed request * @param localContext the Http context which was used * @param res the SampleResult which is to be filled * @param httpResponse the response which is to be parsed * @throws IllegalStateException/*from ww w . j a v a2 s . c om*/ * @throws IOException */ private void parseResponse(HttpRequestBase httpRequest, HttpContext localContext, HTTPSampleResult res, HttpResponse httpResponse) throws IllegalStateException, IOException { // Needs to be done after execute to pick up all the headers final HttpRequest request = (HttpRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); // We've finished with the request, so we can add the LocalAddress to it for display final InetAddress localAddr = (InetAddress) httpRequest.getParams() .getParameter(ConnRoutePNames.LOCAL_ADDRESS); if (localAddr != null) { request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString()); } res.setRequestHeaders(getConnectionHeaders(request)); Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE); if (contentType != null) { String ct = contentType.getValue(); res.setContentType(ct); res.setEncodingAndType(ct); } HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); res.setResponseData(readResponse(res, instream, (int) entity.getContentLength())); } res.sampleEnd(); // Done with the sampling proper. currentRequest = null; // Now collect the results into the HTTPSampleResult: StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); res.setResponseCode(Integer.toString(statusCode)); res.setResponseMessage(statusLine.getReasonPhrase()); res.setSuccessful(isSuccessCode(statusCode)); res.setResponseHeaders(getResponseHeaders(httpResponse)); if (res.isRedirect()) { final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION); if (headerLocation == null) { // HTTP protocol violation, but avoids NPE throw new IllegalArgumentException( "Missing location header in redirect for " + httpRequest.getRequestLine()); } String redirectLocation = headerLocation.getValue(); res.setRedirectLocation(redirectLocation); } // record some sizes to allow HTTPSampleResult.getBytes() with different options HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_METRICS); long headerBytes = res.getResponseHeaders().length() // condensed length (without \r) + httpResponse.getAllHeaders().length // Add \r for each header + 1 // Add \r for initial header + 2; // final \r\n before data long totalBytes = metrics.getReceivedBytesCount(); res.setHeadersSize((int) headerBytes); res.setBodySize((int) (totalBytes - headerBytes)); if (log.isDebugEnabled()) { log.debug("ResponseHeadersSize=" + res.getHeadersSize() + " Content-Length=" + res.getBodySize() + " Total=" + (res.getHeadersSize() + res.getBodySize())); } // If we redirected automatically, the URL may have changed if (getAutoRedirects()) { HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); URI redirectURI = req.getURI(); if (redirectURI.isAbsolute()) { res.setURL(redirectURI.toURL()); } else { res.setURL(new URL(new URL(target.toURI()), redirectURI.toString())); } } }
From source file:org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.java
/** * This is used to get the dynamically set time out values from the message * context. If the values are not available or invalid then the default * values or the values set by the configuration will be used * * @param msgContext the active MessageContext * @param httpMethod method//from ww w . jav a2s.c o m */ protected void setTimeouts(MessageContext msgContext, HttpRequestBase httpMethod) { // If the SO_TIMEOUT of CONNECTION_TIMEOUT is set by dynamically the // override the static config Integer tempSoTimeoutProperty = (Integer) msgContext.getProperty(HTTPConstants.SO_TIMEOUT); Integer tempConnTimeoutProperty = (Integer) msgContext.getProperty(HTTPConstants.CONNECTION_TIMEOUT); long timeout = msgContext.getOptions().getTimeOutInMilliSeconds(); if (tempConnTimeoutProperty != null) { // timeout for initial connection httpMethod.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, tempConnTimeoutProperty); } if (tempSoTimeoutProperty != null) { // SO_TIMEOUT -- timeout for blocking reads httpMethod.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, tempSoTimeoutProperty); } else { // set timeout in client if (timeout > 0) { httpMethod.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, (int) timeout); } } }
From source file:org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.java
/** * Setup following elements on httpRequest: * <ul>// w ww. ja v a 2s . c o m * <li>ConnRoutePNames.LOCAL_ADDRESS enabling IP-SPOOFING</li> * <li>Socket and connection timeout</li> * <li>Redirect handling</li> * <li>Keep Alive header or Connection Close</li> * <li>Calls setConnectionHeaders to setup headers</li> * <li>Calls setConnectionCookie to setup Cookie</li> * </ul> * * @param url * {@link URL} of the request * @param httpRequest * http request for the request * @param res * sample result to set cookies on * @throws IOException * if hostname/ip to use could not be figured out */ protected void setupRequest(URL url, HttpRequestBase httpRequest, HTTPSampleResult res) throws IOException { HttpParams requestParams = httpRequest.getParams(); // Set up the local address if one exists final InetAddress inetAddr = getIpSourceAddress(); if (inetAddr != null) {// Use special field ip source address (for pseudo 'ip spoofing') requestParams.setParameter(ConnRoutePNames.LOCAL_ADDRESS, inetAddr); } else if (localAddress != null) { requestParams.setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress); } else { // reset in case was set previously requestParams.removeParameter(ConnRoutePNames.LOCAL_ADDRESS); } int rto = getResponseTimeout(); if (rto > 0) { requestParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, rto); } int cto = getConnectTimeout(); if (cto > 0) { requestParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, cto); } requestParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, getAutoRedirects()); // a well-behaved browser is supposed to send 'Connection: close' // with the last request to an HTTP server. Instead, most browsers // leave it to the server to close the connection after their // timeout period. Leave it to the JMeter user to decide. if (getUseKeepAlive()) { httpRequest.setHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE); } else { httpRequest.setHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE); } setConnectionHeaders(httpRequest, url, getHeaderManager(), getCacheManager()); String cookies = setConnectionCookie(httpRequest, url, getCookieManager()); if (res != null) { res.setCookies(cookies); } }
From source file:org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient.java
private void prepareRequest(HttpRequestBase request, IOperationMonitor monitor) { UserCredentials httpCredentials = location.getCredentials(httpAuthenticationType); if (httpCredentials != null) { HttpUtil.configureAuthentication(getHttpClient(), location, httpCredentials); if (isPreemptiveAuthenticationEnabled()) { // create or pre-populate auth cache HttpHost host = HttpUtil.createHost(request); Object authCache = getContext().getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { authCache = new BasicAuthCache(); getContext().setAttribute(ClientContext.AUTH_CACHE, authCache); }/*from w ww . j a v a 2 s . co m*/ if (authCache instanceof BasicAuthCache) { if (((BasicAuthCache) authCache).get(host) == null) { ((BasicAuthCache) authCache).put(host, new BasicScheme()); } } } } HttpUtil.configureProxy(getHttpClient(), location); CertificateCredentials socketCredentials = location.getCredentials(AuthenticationType.CERTIFICATE); if (socketCredentials != null) { SslSupport support = new SslSupport(new TrustManager[] { new TrustAllTrustManager() }, socketCredentials.getKeyStoreFileName(), socketCredentials.getPassword(), socketCredentials.getKeyStoreType()); request.getParams().setParameter(SslSupport.class.getName(), support); } else { // remove the token that associates certificate credentials with the connection getContext().removeAttribute(ClientContext.USER_TOKEN); } getContext().setAttribute(HttpUtil.CONTEXT_KEY_MONITOR_THREAD, getMonitorThread()); }
From source file:org.apache.manifoldcf.crawler.connectors.meridio.CommonsHTTPSender.java
/** * invoke creates a socket connection, sends the request SOAP message and then * reads the response SOAP message back from the SOAP server * * @param msgContext the messsage context * * @throws AxisFault/* w ww .j ava2 s . co m*/ */ public void invoke(MessageContext msgContext) throws AxisFault { if (log.isDebugEnabled()) { log.debug(Messages.getMessage("enter00", "CommonsHTTPSender::invoke")); } // Catch all exceptions and turn them into AxisFaults try { // Get the URL URL targetURL = new URL(msgContext.getStrProp(MessageContext.TRANS_URL)); // Get the HttpClient HttpClient httpClient = (HttpClient) msgContext.getProperty( org.apache.manifoldcf.crawler.connectors.meridio.meridiowrapper.MeridioWrapper.HTTPCLIENT_PROPERTY); boolean posting = true; // If we're SOAP 1.2, allow the web method to be set from the // MessageContext. if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { String webMethod = msgContext.getStrProp(SOAP12Constants.PROP_WEBMETHOD); if (webMethod != null) { posting = webMethod.equals(HTTPConstants.HEADER_POST); } } boolean http10 = false; String httpVersion = msgContext.getStrProp(MessageContext.HTTP_TRANSPORT_VERSION); if (httpVersion != null) { if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_V10)) { http10 = true; } // assume 1.1 } HttpRequestBase method; if (posting) { HttpPost postMethod = new HttpPost(targetURL.toString()); // set false as default, addContetInfo can overwrite HttpProtocolParams.setUseExpectContinue(postMethod.getParams(), false); Message reqMessage = msgContext.getRequestMessage(); boolean httpChunkStream = addContextInfo(postMethod, msgContext); HttpEntity requestEntity = null; requestEntity = new MessageRequestEntity(reqMessage, httpChunkStream, http10 || !httpChunkStream); postMethod.setEntity(requestEntity); method = postMethod; } else { method = new HttpGet(targetURL.toString()); } if (http10) HttpProtocolParams.setVersion(method.getParams(), new ProtocolVersion("HTTP", 1, 0)); BackgroundHTTPThread methodThread = new BackgroundHTTPThread(httpClient, method); methodThread.start(); try { int returnCode = methodThread.getResponseCode(); String contentType = getHeader(methodThread, HTTPConstants.HEADER_CONTENT_TYPE); String contentLocation = getHeader(methodThread, HTTPConstants.HEADER_CONTENT_LOCATION); String contentLength = getHeader(methodThread, HTTPConstants.HEADER_CONTENT_LENGTH); if ((returnCode > 199) && (returnCode < 300)) { // SOAP return is OK - so fall through } else if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { // For now, if we're SOAP 1.2, fall through, since the range of // valid result codes is much greater } else if ((contentType != null) && !contentType.equals("text/html") && ((returnCode > 499) && (returnCode < 600))) { // SOAP Fault should be in here - so fall through } else { String statusMessage = methodThread.getResponseStatus(); AxisFault fault = new AxisFault("HTTP", "(" + returnCode + ")" + statusMessage, null, null); fault.setFaultDetailString(Messages.getMessage("return01", "" + returnCode, getResponseBodyAsString(methodThread))); fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_HTTPERRORCODE, Integer.toString(returnCode)); throw fault; } String contentEncoding = methodThread.getFirstHeader(HTTPConstants.HEADER_CONTENT_ENCODING); if (contentEncoding != null) { AxisFault fault = new AxisFault("HTTP", "unsupported content-encoding of '" + contentEncoding + "' found", null, null); throw fault; } Map<String, List<String>> responseHeaders = methodThread.getResponseHeaders(); InputStream dataStream = methodThread.getSafeInputStream(); Message outMsg = new Message(new BackgroundInputStream(methodThread, dataStream), false, contentType, contentLocation); // Transfer HTTP headers of HTTP message to MIME headers of SOAP message MimeHeaders responseMimeHeaders = outMsg.getMimeHeaders(); for (String name : responseHeaders.keySet()) { List<String> values = responseHeaders.get(name); for (String value : values) { responseMimeHeaders.addHeader(name, value); } } outMsg.setMessageType(Message.RESPONSE); // Put the message in the message context. msgContext.setResponseMessage(outMsg); // Pass off the method thread to the stream for closure methodThread = null; } finally { if (methodThread != null) { methodThread.abort(); methodThread.finishUp(); } } } catch (AxisFault af) { log.debug(af); throw af; } catch (Exception e) { log.debug(e); throw AxisFault.makeFault(e); } if (log.isDebugEnabled()) { log.debug(Messages.getMessage("exit00", "CommonsHTTPSender::invoke")); } }
From source file:org.apache.manifoldcf.sharepoint.CommonsHTTPSender.java
/** * invoke creates a socket connection, sends the request SOAP message and then * reads the response SOAP message back from the SOAP server * * @param msgContext the messsage context * * @throws AxisFault/*from w ww . j a va 2s. com*/ */ public void invoke(MessageContext msgContext) throws AxisFault { if (log.isDebugEnabled()) { log.debug(Messages.getMessage("enter00", "CommonsHTTPSender::invoke")); } // Catch all exceptions and turn them into AxisFaults try { // Get the URL URL targetURL = new URL(msgContext.getStrProp(MessageContext.TRANS_URL)); // Get the HttpClient HttpClient httpClient = (HttpClient) msgContext.getProperty(HTTPCLIENT_PROPERTY); boolean posting = true; // If we're SOAP 1.2, allow the web method to be set from the // MessageContext. if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { String webMethod = msgContext.getStrProp(SOAP12Constants.PROP_WEBMETHOD); if (webMethod != null) { posting = webMethod.equals(HTTPConstants.HEADER_POST); } } boolean http10 = false; String httpVersion = msgContext.getStrProp(MessageContext.HTTP_TRANSPORT_VERSION); if (httpVersion != null) { if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_V10)) { http10 = true; } // assume 1.1 } HttpRequestBase method; if (posting) { HttpPost postMethod = new HttpPost(targetURL.toString()); // set false as default, addContetInfo can overwrite HttpProtocolParams.setUseExpectContinue(postMethod.getParams(), false); Message reqMessage = msgContext.getRequestMessage(); boolean httpChunkStream = addContextInfo(postMethod, msgContext); HttpEntity requestEntity = null; requestEntity = new MessageRequestEntity(reqMessage, httpChunkStream, http10 || !httpChunkStream); postMethod.setEntity(requestEntity); method = postMethod; } else { method = new HttpGet(targetURL.toString()); } if (http10) HttpProtocolParams.setVersion(method.getParams(), new ProtocolVersion("HTTP", 1, 0)); BackgroundHTTPThread methodThread = new BackgroundHTTPThread(httpClient, method); methodThread.start(); try { int returnCode = methodThread.getResponseCode(); String contentType = getHeader(methodThread, HTTPConstants.HEADER_CONTENT_TYPE); String contentLocation = getHeader(methodThread, HTTPConstants.HEADER_CONTENT_LOCATION); String contentLength = getHeader(methodThread, HTTPConstants.HEADER_CONTENT_LENGTH); if ((returnCode > 199) && (returnCode < 300)) { // SOAP return is OK - so fall through } else if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { // For now, if we're SOAP 1.2, fall through, since the range of // valid result codes is much greater } else if ((contentType != null) && !contentType.equals("text/html") && ((returnCode > 499) && (returnCode < 600))) { // SOAP Fault should be in here - so fall through } else { String statusMessage = methodThread.getResponseStatus(); AxisFault fault = new AxisFault("HTTP", "(" + returnCode + ")" + statusMessage, null, null); fault.setFaultDetailString(Messages.getMessage("return01", "" + returnCode, getResponseBodyAsString(methodThread))); fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_HTTPERRORCODE, Integer.toString(returnCode)); throw fault; } String contentEncoding = methodThread.getFirstHeader(HTTPConstants.HEADER_CONTENT_ENCODING); if (contentEncoding != null) { AxisFault fault = new AxisFault("HTTP", "unsupported content-encoding of '" + contentEncoding + "' found", null, null); throw fault; } Map<String, List<String>> responseHeaders = methodThread.getResponseHeaders(); InputStream dataStream = methodThread.getSafeInputStream(); Message outMsg = new Message(new BackgroundInputStream(methodThread, dataStream), false, contentType, contentLocation); // Transfer HTTP headers of HTTP message to MIME headers of SOAP message MimeHeaders responseMimeHeaders = outMsg.getMimeHeaders(); for (String name : responseHeaders.keySet()) { List<String> values = responseHeaders.get(name); for (String value : values) { responseMimeHeaders.addHeader(name, value); } } outMsg.setMessageType(Message.RESPONSE); // Put the message in the message context. msgContext.setResponseMessage(outMsg); // Pass off the method thread to the stream for closure methodThread = null; } finally { if (methodThread != null) { methodThread.abort(); methodThread.finishUp(); } } } catch (AxisFault af) { log.debug(af); throw af; } catch (Exception e) { log.debug(e); throw AxisFault.makeFault(e); } if (log.isDebugEnabled()) { log.debug(Messages.getMessage("exit00", "CommonsHTTPSender::invoke")); } }
From source file:com.google.acre.script.AcreFetch.java
@SuppressWarnings("boxing") public void fetch(boolean system, String response_encoding, boolean log_to_user, boolean no_redirect) { if (request_url.length() > 2047) { throw new AcreURLFetchException("fetching URL failed - url is too long"); }/*from w w w.j av a 2 s .c om*/ DefaultHttpClient client = new DefaultHttpClient(_connectionManager, null); HttpParams params = client.getParams(); // pass the deadline down to the invoked service. // this will be ignored unless we are fetching from another // acre server. // note that we may send a deadline that is already passed: // it's not our job to throw here since we don't know how // the target service will interpret the quota header. // NOTE: this is done *after* the user sets the headers to overwrite // whatever settings they might have tried to change for this value // (which could be a security hazard) long sub_deadline = (HostEnv.LIMIT_EXECUTION_TIME) ? _deadline - HostEnv.SUBREQUEST_DEADLINE_ADVANCE : System.currentTimeMillis() + HostEnv.ACRE_URLFETCH_TIMEOUT; int reentries = _reentries + 1; request_headers.put(HostEnv.ACRE_QUOTAS_HEADER, "td=" + sub_deadline + ",r=" + reentries); // if this is not an internal call, we need to invoke the call thru a proxy if (!_internal) { // XXX No sense wasting the resources to gzip inside the network. // XXX seems that twitter gets upset when we do this /* if (!request_headers.containsKey("accept-encoding")) { request_headers.put("accept-encoding", "gzip"); } */ String proxy_host = Configuration.Values.HTTP_PROXY_HOST.getValue(); int proxy_port = -1; if (!(proxy_host.length() == 0)) { proxy_port = Configuration.Values.HTTP_PROXY_PORT.getInteger(); HttpHost proxy = new HttpHost(proxy_host, proxy_port, "http"); params.setParameter(AllClientPNames.DEFAULT_PROXY, proxy); } } params.setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // in msec long timeout = _deadline - System.currentTimeMillis(); if (timeout < 0) timeout = 0; params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, (int) timeout); params.setParameter(AllClientPNames.SO_TIMEOUT, (int) timeout); // we're not streaming the request so this should be a win. params.setParameter(AllClientPNames.TCP_NODELAY, true); // reuse an existing socket if it is in TIME_WAIT state. params.setParameter(AllClientPNames.SO_REUSEADDR, true); // set the encoding of our POST payloads to UTF-8 params.setParameter(AllClientPNames.HTTP_CONTENT_CHARSET, "UTF-8"); BasicCookieStore cstore = new BasicCookieStore(); for (AcreCookie cookie : request_cookies.values()) { cstore.addCookie(cookie.toClientCookie()); } client.setCookieStore(cstore); HttpRequestBase method; HashMap<String, String> logmsg = new HashMap<String, String>(); logmsg.put("Method", request_method); logmsg.put("URL", request_url); params.setParameter(AllClientPNames.HANDLE_REDIRECTS, !no_redirect); logmsg.put("Redirect", Boolean.toString(!no_redirect)); try { if (request_method.equals("GET")) { method = new HttpGet(request_url); } else if (request_method.equals("POST")) { method = new HttpPost(request_url); } else if (request_method.equals("HEAD")) { method = new HttpHead(request_url); } else if (request_method.equals("PUT")) { method = new HttpPut(request_url); } else if (request_method.equals("DELETE")) { method = new HttpDelete(request_url); } else if (request_method.equals("PROPFIND")) { method = new HttpPropFind(request_url); } else { throw new AcreURLFetchException("Failed: unsupported (so far) method " + request_method); } method.getParams().setBooleanParameter(AllClientPNames.USE_EXPECT_CONTINUE, false); } catch (java.lang.IllegalArgumentException e) { throw new AcreURLFetchException("Unable to fetch URL; this is most likely an issue with URL encoding."); } catch (java.lang.IllegalStateException e) { throw new AcreURLFetchException("Unable to fetch URL; possibly an illegal protocol?"); } StringBuilder request_header_log = new StringBuilder(); for (Map.Entry<String, String> header : request_headers.entrySet()) { String key = header.getKey(); String value = header.getValue(); // XXX should suppress cookie headers? // content-type and length? if ("content-type".equalsIgnoreCase(key)) { Matcher m = contentTypeCharsetPattern.matcher(value); if (m.find()) { content_type = m.group(1); content_type_charset = m.group(2); } else { content_type_charset = "utf-8"; } method.addHeader(key, value); } else if ("content-length".equalsIgnoreCase(key)) { // ignore user-supplied content-length, which is // probably wrong due to chars vs bytes and is // redundant anyway ArrayList<String> msg = new ArrayList<String>(); msg.add("User-supplied content-length header is ignored"); _acre_response.log("warn", msg); } else if ("user-agent".equalsIgnoreCase(key)) { params.setParameter(AllClientPNames.USER_AGENT, value); } else { method.addHeader(key, value); } if (!("x-acre-auth".equalsIgnoreCase(key))) { request_header_log.append(key + ": " + value + "\r\n"); } } logmsg.put("Headers", request_header_log.toString()); // XXX need more detailed error checking if (method instanceof HttpEntityEnclosingRequestBase && request_body != null) { HttpEntityEnclosingRequestBase em = (HttpEntityEnclosingRequestBase) method; try { if (request_body instanceof String) { StringEntity ent = new StringEntity((String) request_body, content_type_charset); em.setEntity(ent); } else if (request_body instanceof JSBinary) { ByteArrayEntity ent = new ByteArrayEntity(((JSBinary) request_body).get_data()); em.setEntity(ent); } } catch (UnsupportedEncodingException e) { throw new AcreURLFetchException( "Failed to fetch URL. " + " - Unsupported charset: " + content_type_charset); } } if (!system && log_to_user) { ArrayList<Object> msg = new ArrayList<Object>(); msg.add("urlfetch request"); msg.add(logmsg); _acre_response.log("debug", msg); } _logger.info("urlfetch.request", logmsg); long startTime = System.currentTimeMillis(); try { // this sends the http request and waits HttpResponse hres = client.execute(method); status = hres.getStatusLine().getStatusCode(); HashMap<String, String> res_logmsg = new HashMap<String, String>(); res_logmsg.put("URL", request_url); res_logmsg.put("Status", ((Integer) status).toString()); Header content_type_header = null; // translate response headers StringBuilder response_header_log = new StringBuilder(); Header[] rawheaders = hres.getAllHeaders(); for (Header rawheader : rawheaders) { String headername = rawheader.getName().toLowerCase(); if (headername.equalsIgnoreCase("content-type")) { content_type_header = rawheader; // XXX should strip everything after ; content_type = rawheader.getValue(); // XXX don't set content_type_parameters, deprecated? } else if (headername.equalsIgnoreCase("x-metaweb-cost")) { _costCollector.merge(rawheader.getValue()); } else if (headername.equalsIgnoreCase("x-metaweb-tid")) { res_logmsg.put("ITID", rawheader.getValue()); } headers.put(headername, rawheader.getValue()); response_header_log.append(headername + ": " + rawheader.getValue() + "\r\n"); } res_logmsg.put("Headers", response_header_log.toString()); if (!system && log_to_user) { ArrayList<Object> msg = new ArrayList<Object>(); msg.add("urlfetch response"); msg.add(res_logmsg); _acre_response.log("debug", msg); } _logger.info("urlfetch.response", res_logmsg); // read cookies for (Cookie c : cstore.getCookies()) { cookies.put(c.getName(), new AcreCookie(c)); } // get body encoding String charset = null; if (content_type_header != null) { HeaderElement values[] = content_type_header.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } if (charset == null) charset = response_encoding; // read body HttpEntity ent = hres.getEntity(); if (ent != null) { InputStream res_stream = ent.getContent(); Header cenc = ent.getContentEncoding(); if (cenc != null && res_stream != null) { HeaderElement[] codecs = cenc.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { res_stream = new GZIPInputStream(res_stream); } } } long firstByteTime = 0; long endTime = 0; if (content_type != null && (content_type.startsWith("image/") || content_type.startsWith("application/octet-stream") || content_type.startsWith("multipart/form-data"))) { // HttpClient's InputStream doesn't support mark/reset, so // wrap it with one that does. BufferedInputStream bufis = new BufferedInputStream(res_stream); bufis.mark(2); bufis.read(); firstByteTime = System.currentTimeMillis(); bufis.reset(); byte[] data = IOUtils.toByteArray(bufis); endTime = System.currentTimeMillis(); body = new JSBinary(); ((JSBinary) body).set_data(data); try { if (res_stream != null) { res_stream.close(); } } catch (IOException e) { // ignore } } else if (res_stream == null || charset == null) { firstByteTime = endTime = System.currentTimeMillis(); body = ""; } else { StringWriter writer = new StringWriter(); Reader reader = new InputStreamReader(res_stream, charset); int i = reader.read(); firstByteTime = System.currentTimeMillis(); writer.write(i); IOUtils.copy(reader, writer); endTime = System.currentTimeMillis(); body = writer.toString(); try { reader.close(); writer.close(); } catch (IOException e) { // ignore } } long waitingTime = firstByteTime - startTime; long readingTime = endTime - firstByteTime; _logger.debug("urlfetch.timings", "waiting time: " + waitingTime + "ms"); _logger.debug("urlfetch.timings", "reading time: " + readingTime + "ms"); Statistics.instance().collectUrlfetchTime(startTime, firstByteTime, endTime); _costCollector.collect((system) ? "asuc" : "auuc").collect((system) ? "asuw" : "auuw", waitingTime) .collect((system) ? "asub" : "auub", waitingTime); } } catch (IllegalArgumentException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("failed to fetch URL. " + " - Request Error: " + cause.getMessage()); } catch (IOException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage()); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage()); } finally { method.abort(); } }
From source file:org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.java
protected HttpResponse executeMethod(AbstractHttpClient httpClient, MessageContext msgContext, URL url, HttpRequestBase method) throws IOException { HttpHost httpHost = this.getHostConfiguration(httpClient, msgContext, url); // set the custom headers, if available addCustomHeaders(method, msgContext); // add compression headers if needed if (msgContext.isPropertyTrue(HTTPConstants.MC_ACCEPT_GZIP)) { method.addHeader(HTTPConstants.HEADER_ACCEPT_ENCODING, HTTPConstants.COMPRESSION_GZIP); }/*from w w w . jav a 2 s .c o m*/ if (msgContext.isPropertyTrue(HTTPConstants.MC_GZIP_REQUEST)) { method.addHeader(HTTPConstants.HEADER_CONTENT_ENCODING, HTTPConstants.COMPRESSION_GZIP); } if (msgContext.getProperty(HTTPConstants.HTTP_METHOD_PARAMS) != null) { HttpParams params = (HttpParams) msgContext.getProperty(HTTPConstants.HTTP_METHOD_PARAMS); method.setParams(params); } String cookiePolicy = (String) msgContext.getProperty(HTTPConstants.COOKIE_POLICY); if (cookiePolicy != null) { method.getParams().setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy); } setTimeouts(msgContext, method); HttpContext localContext = new BasicHttpContext(); // Why do we have add context here return httpClient.execute(httpHost, method, localContext); }