List of usage examples for org.apache.commons.httpclient.methods RequestEntity getContentLength
public abstract long getContentLength();
From source file:com.navercorp.pinpoint.plugin.httpclient3.HttpClient3EntityExtractor.java
private static String entityUtilsToString(final RequestEntity entity, final String charSet) throws Exception { final FixedByteArrayOutputStream outStream = new FixedByteArrayOutputStream(MAX_READ_SIZE); entity.writeRequest(outStream);/*from ww w. j ava 2 s . c om*/ final String entityValue = outStream.toString(charSet); if (entity.getContentLength() > MAX_READ_SIZE) { StringBuilder sb = new StringBuilder(); sb.append(entityValue); sb.append(" (HTTP entity is large. length: "); sb.append(entity.getContentLength()); sb.append(" )"); return sb.toString(); } return entityValue; }
From source file:com.navercorp.pinpoint.plugin.httpclient3.HttpClient3EntityExtractor.java
@Override public String getEntity(HttpMethod httpMethod) { if (httpMethod instanceof EntityEnclosingMethod) { final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod; final RequestEntity entity = entityEnclosingMethod.getRequestEntity(); if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) { try { String entityValue; String charSet = entityEnclosingMethod.getRequestCharSet(); if (StringUtils.isEmpty(charSet)) { charSet = HttpConstants.DEFAULT_CONTENT_CHARSET; }/* w w w .j a v a 2 s . c o m*/ if (entity instanceof ByteArrayRequestEntity || entity instanceof StringRequestEntity) { entityValue = entityUtilsToString(entity, charSet); } else { entityValue = entity.getClass() + " (ContentType:" + entity.getContentType() + ")"; } return entityValue; } catch (Exception e) { if (isDebug) { logger.debug("Failed to get entity. httpMethod={}", httpMethod, e); } } } } return null; }
From source file:com.eviware.soapui.impl.wsdl.submit.filters.PostPackagingRequestFilter.java
@Override public void filterAbstractHttpRequest(SubmitContext context, AbstractHttpRequest<?> request) { ExtendedHttpMethod httpMethod = (ExtendedHttpMethod) context .getProperty(BaseHttpRequestTransport.HTTP_METHOD); Settings settings = request.getSettings(); // chunking?//w w w. jav a 2 s . c o m if (httpMethod.getParams().getVersion().equals(HttpVersion.HTTP_1_1) && httpMethod instanceof EntityEnclosingMethod) { EntityEnclosingMethod entityEnclosingMethod = ((EntityEnclosingMethod) httpMethod); long limit = settings.getLong(HttpSettings.CHUNKING_THRESHOLD, -1); RequestEntity requestEntity = entityEnclosingMethod.getRequestEntity(); entityEnclosingMethod.setContentChunked( limit >= 0 && requestEntity != null ? requestEntity.getContentLength() > limit : false); } }
From source file:com.navercorp.pinpoint.plugin.httpclient3.HttpClient3RequestTrace.java
@Override public String getEntityValue() { if (httpMethod instanceof EntityEnclosingMethod) { final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod; final RequestEntity entity = entityEnclosingMethod.getRequestEntity(); if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) { try { String entityValue; String charSet = entityEnclosingMethod.getRequestCharSet(); if (StringUtils.isEmpty(charSet)) { charSet = HttpConstants.DEFAULT_CONTENT_CHARSET; }/* w w w . j ava 2 s .co m*/ if (entity instanceof ByteArrayRequestEntity || entity instanceof StringRequestEntity) { entityValue = entityUtilsToString(entity, charSet); } else { entityValue = entity.getClass() + " (ContentType:" + entity.getContentType() + ")"; } return entityValue; } catch (Exception e) { if (isDebug) { logger.debug("Failed to get entity. httpMethod={}", this.httpMethod, e); } } } } return null; }
From source file:com.baidu.oped.apm.profiler.modifier.connector.httpclient3.interceptor.ExecuteInterceptor.java
private void recordEntity(HttpMethod httpMethod, Trace trace) { if (httpMethod instanceof EntityEnclosingMethod) { final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod; final RequestEntity entity = entityEnclosingMethod.getRequestEntity(); if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) { if (entitySampler.isSampling()) { try { String entityValue; String charSet = entityEnclosingMethod.getRequestCharSet(); if (charSet == null || charSet.isEmpty()) { charSet = HttpConstants.DEFAULT_CONTENT_CHARSET; }/* w w w. j a v a2 s . c o m*/ if (entity instanceof ByteArrayRequestEntity) { entityValue = readByteArray((ByteArrayRequestEntity) entity, charSet); } else if (entity instanceof StringRequestEntity) { entityValue = readString((StringRequestEntity) entity); } else { entityValue = entity.getClass() + " (ContentType:" + entity.getContentType() + ")"; } trace.recordAttribute(AnnotationKey.HTTP_PARAM_ENTITY, entityValue); } catch (Exception e) { logger.debug("HttpEntityEnclosingRequest entity record fail. Caused:{}", e.getMessage(), e); } } } } }
From source file:org.apache.hadoop.chukwa.datacollection.sender.ChukwaHttpSender.java
/** * Handles the HTTP post. Throws HttpException on failure *//*from ww w . j a v a 2 s . c o m*/ @SuppressWarnings("deprecation") private void doPost(PostMethod method, RequestEntity data, String dest) throws IOException, HttpException { HttpMethodParams pars = method.getParams(); pars.setParameter(HttpMethodParams.RETRY_HANDLER, (Object) new HttpMethodRetryHandler() { public boolean retryMethod(HttpMethod m, IOException e, int exec) { return !(e instanceof java.net.ConnectException) && (exec < MAX_RETRIES_PER_COLLECTOR); } }); method.setParams(pars); method.setPath(dest); //send it across the network method.setRequestEntity(data); log.info("HTTP post to " + dest + " length = " + data.getContentLength()); // Send POST request client.setTimeout(8000); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("HTTP post response statusCode: " + statusCode + ", statusLine: " + method.getStatusLine()); //do something aggressive here throw new HttpException("got back a failure from server"); } //implicitly "else" log.info( "got success back from the remote collector; response length " + method.getResponseContentLength()); //FIXME: should parse acks here InputStream rstream = null; // Get the response body byte[] resp_buf = method.getResponseBody(); rstream = new ByteArrayInputStream(resp_buf); BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); String line; while ((line = br.readLine()) != null) { System.out.println("response: " + line); } }
From source file:org.apache.servicemix.http.processors.ProviderProcessor.java
public void process(MessageExchange exchange) throws Exception { if (exchange.getStatus() == ExchangeStatus.DONE || exchange.getStatus() == ExchangeStatus.ERROR) { PostMethod method = methods.remove(exchange.getExchangeId()); if (method != null) { method.releaseConnection();/*from w ww . j a va2s.c o m*/ } return; } boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC)); txSync |= endpoint.isSynchronous(); NormalizedMessage nm = exchange.getMessage("in"); if (nm == null) { throw new IllegalStateException("Exchange has no input message"); } String locationURI = endpoint.getLocationURI(); // Incorporated because of JIRA SM-695 Object newDestinationURI = nm.getProperty(JbiConstants.HTTP_DESTINATION_URI); if (newDestinationURI != null) { locationURI = (String) newDestinationURI; log.debug("Location URI overridden: " + locationURI); } PostMethod method = new PostMethod(getRelUri(locationURI)); SoapMessage soapMessage = new SoapMessage(); soapHelper.getJBIMarshaler().fromNMS(soapMessage, nm); Context context = soapHelper.createContext(soapMessage); soapHelper.onSend(context); SoapWriter writer = soapHelper.getSoapMarshaler().createWriter(soapMessage); copyHeaderInformation(nm, method); RequestEntity entity = writeMessage(writer); // remove content-type header that may have been part of the in message if (!endpoint.isWantContentTypeHeaderFromExchangeIntoHttpRequest()) { method.removeRequestHeader(HEADER_CONTENT_TYPE); method.addRequestHeader(HEADER_CONTENT_TYPE, entity.getContentType()); } if (entity.getContentLength() < 0) { method.removeRequestHeader(HEADER_CONTENT_LENGTH); } else { method.setRequestHeader(HEADER_CONTENT_LENGTH, Long.toString(entity.getContentLength())); } if (endpoint.isSoap() && method.getRequestHeader(HEADER_SOAP_ACTION) == null) { if (endpoint.getSoapAction() != null) { method.setRequestHeader(HEADER_SOAP_ACTION, endpoint.getSoapAction()); } else { // method.setRequestHeader(HEADER_SOAP_ACTION, "\"\""); } } method.setRequestHeader(HEADER_X_CORRELATION_ID, (String) exchange.getProperty(JbiConstants.CORRELATION_ID)); String smxInstanceName = System.getProperty(SMX_INSTANCE_NAME_PROPERTY); if (smxInstanceName != null) { method.setRequestHeader(HEADER_X_POWERED_BY, smxInstanceName); } else { log.warn(SMX_INSTANCE_NAME_PROPERTY + " property was not set in servicemix.xml file"); } method.setRequestEntity(entity); boolean close = true; try { // Set the retry handler int retries = getConfiguration().isStreamingEnabled() ? 0 : getConfiguration().getRetryCount(); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(retries, true)); // Set authentication if (endpoint.getBasicAuthentication() != null) { endpoint.getBasicAuthentication().applyCredentials(getClient(), exchange, nm); } // Execute the HTTP method //method.getParams().setLongParameter(HttpConnectionParams.CONNECTION_TIMEOUT, getClient().getParams().getSoTimeout()); int response = -1; try { response = getClient().executeMethod(getHostConfiguration(locationURI, exchange, nm), method); } catch (Exception ex) { try { if (listener != null) { if (ex instanceof SocketException) { log.error("Connection to address: " + locationURI + " was refused"); listener.onConnectionRefused(locationURI); } else if (ex instanceof SocketTimeoutException) { log.error("Connection to address: " + locationURI + " timed out"); listener.onTimeout(locationURI); } } } catch (Exception ex2) { log.warn("Error in HttpConnectionListener: " + ex2.getMessage()); } finally { throw ex; } } try { if (listener != null) { listener.onPostExecuted(locationURI, response); } } catch (Exception ex) { log.warn("Error in HttpConnectionListener: " + ex.getMessage()); } if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) { if (!(exchange instanceof InOnly)) { SoapReader reader = soapHelper.getSoapMarshaler().createReader(); Header contentType = method.getResponseHeader(HEADER_CONTENT_TYPE); String content = convertResponseBodyToString(method); logInOut(locationURI, response, method, soapMessage, content); soapMessage = reader.read(new ByteArrayInputStream(content.getBytes()), contentType != null ? contentType.getValue() : null); context.setFaultMessage(soapMessage); soapHelper.onAnswer(context); Fault fault = exchange.createFault(); fault.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(method)); soapHelper.getJBIMarshaler().toNMS(fault, soapMessage); exchange.setFault(fault); if (txSync) { channel.sendSync(exchange); } else { methods.put(exchange.getExchangeId(), method); channel.send(exchange); close = false; } return; } else { String content = convertResponseBodyToString(method); // even if it is InOnly, some out could come to us logInOut(locationURI, response, method, soapMessage, content); throw new Exception("Invalid status response: " + response); } } if (exchange instanceof InOut) { close = processInOut(exchange, method, context, txSync, close); } else if (exchange instanceof InOptionalOut) { close = processInOptionalOut(method, exchange, context, txSync, close); } else { exchange.setStatus(ExchangeStatus.DONE); channel.send(exchange); } } finally { if (close) { method.releaseConnection(); } } }