Search in sources :

Example 6 with FileEntity

use of org.apache.http.entity.FileEntity in project jena by apache.

the class TestAdmin method addTestDataset.

private static void addTestDataset(String filename) {
    File f = new File(filename);
    org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType.parse(WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
    HttpEntity e = new FileEntity(f, ct);
    execHttpPost(ServerCtl.urlRoot() + "$/" + opDatasets, e);
}
Also used : FileEntity(org.apache.http.entity.FileEntity) HttpEntity(org.apache.http.HttpEntity) File(java.io.File)

Example 7 with FileEntity

use of org.apache.http.entity.FileEntity in project jmeter by apache.

the class HTTPHC4Impl method sendEntityData.

// TODO merge put and post methods as far as possible.
// e.g. post checks for multipart form/files, and if not, invokes sendData(HttpEntityEnclosingRequestBase)
/**
     * Creates the entity data to be sent.
     * <p>
     * If there is a file entry with a non-empty MIME type we use that to
     * set the request Content-Type header, otherwise we default to whatever
     * header is present from a Header Manager.
     * <p>
     * If the content charset {@link #getContentEncoding()} is null or empty 
     * we use the HC4 default provided by {@link HTTP#DEF_CONTENT_CHARSET} which is
     * ISO-8859-1.
     * 
     * @param entity to be processed, e.g. PUT or PATCH
     * @return the entity content, may be empty
     * @throws  UnsupportedEncodingException for invalid charset name
     * @throws IOException cannot really occur for ByteArrayOutputStream methods
     */
protected String sendEntityData(HttpEntityEnclosingRequestBase entity) throws IOException {
    boolean hasEntityBody = false;
    final HTTPFileArg[] files = getHTTPFiles();
    // 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
    final HTTPFileArg file = files.length > 0 ? files[0] : null;
    String contentTypeValue = null;
    if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
        contentTypeValue = file.getMimeType();
        // we provide the MIME type here
        entity.setHeader(HEADER_CONTENT_TYPE, contentTypeValue);
    }
    // Check for local contentEncoding (charset) override; fall back to default for content body
    // we do this here rather so we can use the same charset to retrieve the data
    final String charset = getContentEncoding(HTTP.DEF_CONTENT_CHARSET.name());
    if (!hasArguments() && getSendFileAsPostBody()) {
        hasEntityBody = true;
        // If getSendFileAsPostBody returned true, it's sure that file is not null
        File reservedFile = FileServer.getFileServer().getResolvedFile(files[0].getPath());
        // no need for content-type here
        FileEntity fileRequestEntity = new FileEntity(reservedFile);
        entity.setEntity(fileRequestEntity);
    } else // just send all the values as the entity body
    if (getSendParameterValuesAsPostBody()) {
        hasEntityBody = true;
        // Just append all the parameter values, and use that as the entity body
        Arguments arguments = getArguments();
        StringBuilder entityBodyContent = new StringBuilder(arguments.getArgumentCount() * 15);
        for (JMeterProperty jMeterProperty : arguments) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            // Note: if "Encoded?" is not selected, arg.getEncodedValue is equivalent to arg.getValue
            if (charset != null) {
                entityBodyContent.append(arg.getEncodedValue(charset));
            } else {
                entityBodyContent.append(arg.getEncodedValue());
            }
        }
        StringEntity requestEntity = new StringEntity(entityBodyContent.toString(), charset);
        entity.setEntity(requestEntity);
    }
    // Check if we have any content to send for body
    if (hasEntityBody) {
        // If the request entity is repeatable, we can send it first to
        // our own stream, so we can return it
        final HttpEntity entityEntry = entity.getEntity();
        // Buffer to hold the entity body
        StringBuilder entityBody = null;
        if (entityEntry.isRepeatable()) {
            entityBody = new StringBuilder(1000);
            // FIXME Charset
            try (InputStream in = entityEntry.getContent();
                InputStream bounded = new BoundedInputStream(in, MAX_BODY_RETAIN_SIZE)) {
                entityBody.append(IOUtils.toString(bounded));
            }
            if (entityEntry.getContentLength() > MAX_BODY_RETAIN_SIZE) {
                entityBody.append("<actual file content shortened>");
            }
        } else {
            entityBody = new StringBuilder(65);
            // this probably cannot happen
            entityBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
        }
        return entityBody.toString();
    }
    // may be the empty string
    return "";
}
Also used : FileEntity(org.apache.http.entity.FileEntity) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HttpEntity(org.apache.http.HttpEntity) GZIPInputStream(java.util.zip.GZIPInputStream) DeflateInputStream(org.apache.http.client.entity.DeflateInputStream) BrotliInputStream(org.brotli.dec.BrotliInputStream) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) Arguments(org.apache.jmeter.config.Arguments) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) StringEntity(org.apache.http.entity.StringEntity) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) File(java.io.File)

Example 8 with FileEntity

use of org.apache.http.entity.FileEntity in project jmeter by apache.

the class HTTPHC4Impl method sendPostData.

// TODO needs cleaning up
/**
     * 
     * @param post {@link HttpPost}
     * @return String posted body if computable
     * @throws IOException if sending the data fails due to I/O
     */
protected String sendPostData(HttpPost post) throws IOException {
    // Buffer to hold the post body, except file content
    StringBuilder postedBody = new StringBuilder(1000);
    HTTPFileArg[] files = getHTTPFiles();
    final String contentEncoding = getContentEncodingOrNull();
    final boolean haveContentEncoding = contentEncoding != null;
    // application/x-www-form-urlencoded post request
    if (getUseMultipartForPost()) {
        // If a content encoding is specified, we use that as the
        // encoding of any parameter values
        Charset charset = null;
        if (haveContentEncoding) {
            charset = Charset.forName(contentEncoding);
        } else {
            charset = MIME.DEFAULT_CHARSET;
        }
        if (log.isDebugEnabled()) {
            log.debug("Building multipart with:getDoBrowserCompatibleMultipart(): {}, with charset:{}, haveContentEncoding:{}", getDoBrowserCompatibleMultipart(), charset, haveContentEncoding);
        }
        // Write the request to our own stream
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(charset);
        if (getDoBrowserCompatibleMultipart()) {
            multipartEntityBuilder.setLaxMode();
        } else {
            multipartEntityBuilder.setStrictMode();
        }
        // Add any parameters
        for (JMeterProperty jMeterProperty : getArguments()) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            String parameterName = arg.getName();
            if (arg.isSkippable(parameterName)) {
                continue;
            }
            StringBody stringBody = new StringBody(arg.getValue(), ContentType.create("text/plain", charset));
            FormBodyPart formPart = FormBodyPartBuilder.create(parameterName, stringBody).build();
            multipartEntityBuilder.addPart(formPart);
        }
        // Add any files
        // Cannot retrieve parts once added to the MultiPartEntity, so have to save them here.
        ViewableFileBody[] fileBodies = new ViewableFileBody[files.length];
        for (int i = 0; i < files.length; i++) {
            HTTPFileArg file = files[i];
            File reservedFile = FileServer.getFileServer().getResolvedFile(file.getPath());
            fileBodies[i] = new ViewableFileBody(reservedFile, file.getMimeType());
            multipartEntityBuilder.addPart(file.getParamName(), fileBodies[i]);
        }
        HttpEntity entity = multipartEntityBuilder.build();
        post.setEntity(entity);
        if (entity.isRepeatable()) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            for (ViewableFileBody fileBody : fileBodies) {
                fileBody.hideFileData = true;
            }
            entity.writeTo(bos);
            for (ViewableFileBody fileBody : fileBodies) {
                fileBody.hideFileData = false;
            }
            bos.flush();
            // We get the posted bytes using the encoding used to create it
            postedBody.append(bos.toString(// $NON-NLS-1$ this is the default used by HttpClient
            contentEncoding == null ? // $NON-NLS-1$ this is the default used by HttpClient
            "US-ASCII" : contentEncoding));
            bos.close();
        } else {
            // $NON-NLS-1$
            postedBody.append("<Multipart was not repeatable, cannot view what was sent>");
        }
    //            // Set the content type TODO - needed?
    //            String multiPartContentType = multiPart.getContentType().getValue();
    //            post.setHeader(HEADER_CONTENT_TYPE, multiPartContentType);
    } else {
        // not multipart
        // 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 = post.getFirstHeader(HTTPConstants.HEADER_CONTENT_TYPE);
        boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.getValue() != null && contentTypeHeader.getValue().length() > 0;
        // TODO: needs a multiple file upload scenerio
        if (!hasArguments() && getSendFileAsPostBody()) {
            // If getSendFileAsPostBody returned true, it's sure that file is not null
            HTTPFileArg file = files[0];
            if (!hasContentTypeHeader) {
                // Allow the mimetype of the file to control the content type
                if (file.getMimeType() != null && file.getMimeType().length() > 0) {
                    post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
                } else {
                    post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                }
            }
            // TODO is null correct?
            FileEntity fileRequestEntity = new FileEntity(new File(file.getPath()), (ContentType) null);
            post.setEntity(fileRequestEntity);
            // We just add placeholder text for file content
            postedBody.append("<actual file content, not shown here>");
        } else {
            // the post body will be encoded in the specified content encoding
            if (haveContentEncoding) {
                post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, contentEncoding);
            }
            // just send all the values as the post body
            if (getSendParameterValuesAsPostBody()) {
                // TODO: needs a multiple file upload scenerio
                if (!hasContentTypeHeader) {
                    HTTPFileArg file = files.length > 0 ? files[0] : null;
                    if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
                        post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
                    } else {
                        // TODO - is this the correct default?
                        post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                    }
                }
                // Just append all the parameter values, and use that as the post body
                StringBuilder postBody = new StringBuilder();
                for (JMeterProperty jMeterProperty : getArguments()) {
                    HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
                    // Note: if "Encoded?" is not selected, arg.getEncodedValue is equivalent to arg.getValue
                    if (haveContentEncoding) {
                        postBody.append(arg.getEncodedValue(contentEncoding));
                    } else {
                        postBody.append(arg.getEncodedValue());
                    }
                }
                // Let StringEntity perform the encoding
                StringEntity requestEntity = new StringEntity(postBody.toString(), contentEncoding);
                post.setEntity(requestEntity);
                postedBody.append(postBody.toString());
            } else {
                // Set the content type
                if (!hasContentTypeHeader) {
                    post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                }
                // Add the parameters
                PropertyIterator args = getArguments().iterator();
                List<NameValuePair> nvps = new ArrayList<>();
                String urlContentEncoding = contentEncoding;
                if (urlContentEncoding == null || urlContentEncoding.length() == 0) {
                    // Use the default encoding for urls
                    urlContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
                }
                while (args.hasNext()) {
                    HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
                    // The HTTPClient always urlencodes both name and value,
                    // so if the argument is already encoded, we have to decode
                    // it before adding it to the post request
                    String parameterName = arg.getName();
                    if (arg.isSkippable(parameterName)) {
                        continue;
                    }
                    String parameterValue = arg.getValue();
                    if (!arg.isAlwaysEncoded()) {
                        // The value is already encoded by the user
                        // Must decode the value now, so that when the
                        // httpclient encodes it, we end up with the same value
                        // as the user had entered.
                        parameterName = URLDecoder.decode(parameterName, urlContentEncoding);
                        parameterValue = URLDecoder.decode(parameterValue, urlContentEncoding);
                    }
                    // Add the parameter, httpclient will urlencode it
                    nvps.add(new BasicNameValuePair(parameterName, parameterValue));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, urlContentEncoding);
                post.setEntity(entity);
                if (entity.isRepeatable()) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    post.getEntity().writeTo(bos);
                    bos.flush();
                    // We get the posted bytes using the encoding used to create it
                    postedBody.append(bos.toString(contentEncoding != null ? contentEncoding : SampleResult.DEFAULT_HTTP_ENCODING));
                    bos.close();
                } else {
                    postedBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
                }
            }
        }
    }
    return postedBody.toString();
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) FormBodyPart(org.apache.http.entity.mime.FormBodyPart) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) FileEntity(org.apache.http.entity.FileEntity) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File)

Example 9 with FileEntity

use of org.apache.http.entity.FileEntity in project camel by apache.

the class HttpProducer method createRequestEntity.

/**
     * Creates a holder object for the data to send to the remote server.
     *
     * @param exchange the exchange with the IN message with data to send
     * @return the data holder
     * @throws CamelExchangeException is thrown if error creating RequestEntity
     */
protected HttpEntity createRequestEntity(Exchange exchange) throws CamelExchangeException {
    Message in = exchange.getIn();
    if (in.getBody() == null) {
        return null;
    }
    HttpEntity answer = in.getBody(HttpEntity.class);
    if (answer == null) {
        try {
            Object data = in.getBody();
            if (data != null) {
                String contentTypeString = ExchangeHelper.getContentType(exchange);
                ContentType contentType = null;
                //it removes "boundary" from Content-Type; I have to use contentType.create method.
                if (contentTypeString != null) {
                    // using ContentType.parser for charset
                    if (contentTypeString.indexOf("charset") > 0) {
                        contentType = ContentType.parse(contentTypeString);
                    } else {
                        contentType = ContentType.create(contentTypeString);
                    }
                }
                if (contentTypeString != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentTypeString)) {
                    if (!getEndpoint().getComponent().isAllowJavaSerializedObject()) {
                        throw new CamelExchangeException("Content-type " + org.apache.camel.http.common.HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
                    }
                    // serialized java object
                    Serializable obj = in.getMandatoryBody(Serializable.class);
                    // write object to output stream
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    HttpHelper.writeObjectToStream(bos, obj);
                    ByteArrayEntity entity = new ByteArrayEntity(bos.toByteArray());
                    entity.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                    IOHelper.close(bos);
                    answer = entity;
                } else if (data instanceof File || data instanceof GenericFile) {
                    // file based (could potentially also be a FTP file etc)
                    File file = in.getBody(File.class);
                    if (file != null) {
                        if (contentType != null) {
                            answer = new FileEntity(file, contentType);
                        } else {
                            answer = new FileEntity(file);
                        }
                    }
                } else if (data instanceof String) {
                    // be a bit careful with String as any type can most likely be converted to String
                    // so we only do an instanceof check and accept String if the body is really a String
                    // do not fallback to use the default charset as it can influence the request
                    // (for example application/x-www-form-urlencoded forms being sent)
                    String charset = IOHelper.getCharsetName(exchange, false);
                    if (charset == null && contentType != null) {
                        // okay try to get the charset from the content-type
                        Charset cs = contentType.getCharset();
                        if (cs != null) {
                            charset = cs.name();
                        }
                    }
                    StringEntity entity = new StringEntity((String) data, charset);
                    if (contentType != null) {
                        entity.setContentType(contentType.toString());
                    }
                    answer = entity;
                }
                // fallback as input stream
                if (answer == null) {
                    // force the body as an input stream since this is the fallback
                    InputStream is = in.getMandatoryBody(InputStream.class);
                    String length = in.getHeader(Exchange.CONTENT_LENGTH, String.class);
                    InputStreamEntity entity = null;
                    if (ObjectHelper.isEmpty(length)) {
                        entity = new InputStreamEntity(is, -1);
                    } else {
                        entity = new InputStreamEntity(is, Long.parseLong(length));
                    }
                    if (contentType != null) {
                        entity.setContentType(contentType.toString());
                    }
                    answer = entity;
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new CamelExchangeException("Error creating RequestEntity from message body", exchange, e);
        } catch (IOException e) {
            throw new CamelExchangeException("Error serializing message body", exchange, e);
        }
    }
    return answer;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) Serializable(java.io.Serializable) FileEntity(org.apache.http.entity.FileEntity) Message(org.apache.camel.Message) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile)

Example 10 with FileEntity

use of org.apache.http.entity.FileEntity in project jena by apache.

the class TestAdmin method add_delete_dataset_2.

// Try to add twice
@Test
public void add_delete_dataset_2() {
    checkNotThere(dsTest);
    File f = new File(fileBase + "config-ds-1.ttl");
    {
        org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType.parse(WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
        HttpEntity e = new FileEntity(f, ct);
        execHttpPost(ServerCtl.urlRoot() + "$/" + opDatasets, e);
    }
    // Check exists.
    checkExists(dsTest);
    try {
        org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType.parse(WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
        HttpEntity e = new FileEntity(f, ct);
        execHttpPost(ServerCtl.urlRoot() + "$/" + opDatasets, e);
    } catch (HttpException ex) {
        assertEquals(HttpSC.CONFLICT_409, ex.getResponseCode());
    }
    // Check exists.
    checkExists(dsTest);
    deleteDataset(dsTest);
}
Also used : FileEntity(org.apache.http.entity.FileEntity) HttpEntity(org.apache.http.HttpEntity) HttpException(org.apache.jena.atlas.web.HttpException) File(java.io.File) Test(org.junit.Test)

Aggregations

FileEntity (org.apache.http.entity.FileEntity)12 File (java.io.File)11 HttpEntity (org.apache.http.HttpEntity)7 StringEntity (org.apache.http.entity.StringEntity)4 InputStream (java.io.InputStream)3 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 HttpResponse (org.apache.http.HttpResponse)2 HttpPut (org.apache.http.client.methods.HttpPut)2 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)2 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)2 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1