Example usage for org.apache.commons.httpclient.methods EntityEnclosingMethod getRequestCharSet

List of usage examples for org.apache.commons.httpclient.methods EntityEnclosingMethod getRequestCharSet

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods EntityEnclosingMethod getRequestCharSet.

Prototype

public String getRequestCharSet() 

Source Link

Usage

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;
                }// www . 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.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  . jav a2  s  . c  om
                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;
                    }/*from   w ww  .  ja v a  2s .c  om*/
                    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.jmeter.protocol.http.sampler.HTTPHC3Impl.java

/**
 * Set up the PUT/PATCH/DELETE data//w  w w  .  j a va 2s .  c o m
 */
private String sendEntityData(EntityEnclosingMethod put) throws IOException {
    // Buffer to hold the put body, except file content
    StringBuilder putBody = new StringBuilder(1000);
    boolean hasPutBody = false;

    // Check if the header manager had a content type header
    // This allows the user to specify his own content-type for a POST request
    Header contentTypeHeader = put.getRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE);
    boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.getValue() != null
            && contentTypeHeader.getValue().length() > 0;
    HTTPFileArg[] files = getHTTPFiles();

    // If there are no arguments, we can send a file as the body of the request

    if (!hasArguments() && getSendFileAsPostBody()) {
        hasPutBody = true;

        // If getSendFileAsPostBody returned true, it's sure that file is not null
        File reservedFile = FileServer.getFileServer().getResolvedFile(files[0].getPath());
        FileRequestEntity fileRequestEntity = new FileRequestEntity(reservedFile, null);
        put.setRequestEntity(fileRequestEntity);
    }
    // If none of the arguments have a name specified, we
    // just send all the values as the put body
    else if (getSendParameterValuesAsPostBody()) {
        hasPutBody = true;

        // If a content encoding is specified, we set it as http parameter, so that
        // the post body will be encoded in the specified content encoding
        String contentEncoding = getContentEncoding();
        boolean haveContentEncoding = false;
        if (isNullOrEmptyTrimmed(contentEncoding)) {
            contentEncoding = null;
        } else {
            put.getParams().setContentCharset(contentEncoding);
            haveContentEncoding = true;
        }

        // Just append all the parameter values, and use that as the post body
        StringBuilder putBodyContent = new StringBuilder();
        for (JMeterProperty jMeterProperty : getArguments()) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            String value = null;
            if (haveContentEncoding) {
                value = arg.getEncodedValue(contentEncoding);
            } else {
                value = arg.getEncodedValue();
            }
            putBodyContent.append(value);
        }
        String contentTypeValue = null;
        if (hasContentTypeHeader) {
            contentTypeValue = put.getRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE).getValue();
        }
        StringRequestEntity requestEntity = new StringRequestEntity(putBodyContent.toString(), contentTypeValue,
                put.getRequestCharSet());
        put.setRequestEntity(requestEntity);
    }
    // Check if we have any content to send for body
    if (hasPutBody) {
        // If the request entity is repeatable, we can send it first to
        // our own stream, so we can return it
        if (put.getRequestEntity().isRepeatable()) {
            putBody.append("<actual file content, not shown here>");
        } else {
            putBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
        }
        if (!hasContentTypeHeader) {
            // Allow the mimetype of the file to control the content type
            // This is not obvious in GUI if you are not uploading any files,
            // but just sending the content of nameless parameters
            // TODO: needs a multiple file upload scenerio
            HTTPFileArg file = files.length > 0 ? files[0] : null;
            if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
                put.setRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
            }
        }
        // Set the content length
        put.setRequestHeader(HTTPConstants.HEADER_CONTENT_LENGTH,
                Long.toString(put.getRequestEntity().getContentLength()));
    }
    return putBody.toString();
}