Example usage for io.netty.handler.codec.http HttpHeaderValues MULTIPART_FORM_DATA

List of usage examples for io.netty.handler.codec.http HttpHeaderValues MULTIPART_FORM_DATA

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderValues MULTIPART_FORM_DATA.

Prototype

AsciiString MULTIPART_FORM_DATA

To view the source code for io.netty.handler.codec.http HttpHeaderValues MULTIPART_FORM_DATA.

Click Source Link

Document

"multipart/form-data"

Usage

From source file:io.gatling.http.client.body.multipart.MultipartFormDataRequestBodyBuilder.java

License:Apache License

@Override
public RequestBody<List<Part<?>>> build(String contentType, Charset charset) {

    byte[] boundary;
    String contentTypeBoundaryAttribute = extractContentTypeBoundaryAttribute(contentType);
    if (contentTypeBoundaryAttribute != null) {
        boundary = contentTypeBoundaryAttribute.getBytes(US_ASCII);
    } else {/*from   w  ww. j  a v  a2  s . c  o  m*/
        boundary = computeMultipartBoundary();
        contentType = patchContentTypeWithBoundaryAttribute(
                withDefault(contentType, HttpHeaderValues.MULTIPART_FORM_DATA), boundary);
    }

    return new MultipartFormDataRequestBody(content, contentType, charset, boundary);
}

From source file:io.jsync.http.impl.DefaultHttpServerRequest.java

License:Open Source License

@Override
public HttpServerRequest expectMultiPart(boolean expect) {
    if (expect) {
        String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
        if (contentType != null) {
            HttpMethod method = request.method();
            AsciiString lowerCaseContentType = new AsciiString(contentType.toLowerCase());
            boolean isURLEncoded = lowerCaseContentType
                    .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA) || isURLEncoded)
                    && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                            || method.equals(HttpMethod.PATCH))) {
                decoder = new HttpPostRequestDecoder(new DataFactory(), request);
            }/*from   w w  w. j a v  a 2  s .  c  om*/
        }
    } else {
        decoder = null;
    }
    return this;
}

From source file:io.vertx.core.http.impl.Http2ServerRequestImpl.java

License:Open Source License

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();/*ww  w  .  jav a2 s .c  om*/
        if (expect) {
            if (postRequestDecoder == null) {
                CharSequence contentType = headers.get(HttpHeaderNames.CONTENT_TYPE);
                if (contentType != null) {
                    io.netty.handler.codec.http.HttpMethod method = io.netty.handler.codec.http.HttpMethod
                            .valueOf(headers.method().toString());
                    String lowerCaseContentType = contentType.toString().toLowerCase();
                    boolean isURLEncoded = lowerCaseContentType
                            .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString());
                    if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString())
                            || isURLEncoded)
                            && (method == io.netty.handler.codec.http.HttpMethod.POST
                                    || method == io.netty.handler.codec.http.HttpMethod.PUT
                                    || method == io.netty.handler.codec.http.HttpMethod.PATCH
                                    || method == io.netty.handler.codec.http.HttpMethod.DELETE)) {
                        HttpRequest req = new DefaultHttpRequest(
                                io.netty.handler.codec.http.HttpVersion.HTTP_1_1, method,
                                headers.path().toString());
                        req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentType);
                        postRequestDecoder = new HttpPostRequestDecoder(
                                new NettyFileUploadDataFactory(vertx, this, () -> uploadHandler), req);
                    }
                }
            }
        } else {
            postRequestDecoder = null;
        }
    }
    return this;
}

From source file:org.ballerinalang.test.util.HttpClientRequest.java

License:Open Source License

/**
 * Sends multipart/form-data requests.//from   w  w w.  j  av  a2s .  c  o m
 *
 * @param requestUrl - The URL of the service
 * @param headers    - http request header map
 * @param formData   - map of form data
 * @return - HttpResponse from the end point
 * @throws IOException If an error occurs while sending the GET request
 */
public static HttpResponse doMultipartFormData(String requestUrl, Map<String, String> headers,
        Map<String, String> formData) throws IOException {
    String boundary = "---" + System.currentTimeMillis() + "---";
    String lineFeed = "\r\n";
    HttpURLConnection urlConnection = null;

    try {
        urlConnection = getURLConnection(requestUrl);
        setHeadersAndMethod(urlConnection, headers, TestConstant.HTTP_METHOD_POST);
        urlConnection.setUseCaches(false);
        urlConnection.setDoInput(true);
        urlConnection.setRequestProperty(HttpHeaderNames.CONTENT_TYPE.toString(),
                HttpHeaderValues.MULTIPART_FORM_DATA + "; boundary=" + boundary);

        try (OutputStream out = urlConnection.getOutputStream()) {
            Writer writer = new OutputStreamWriter(out, TestConstant.CHARSET_NAME);
            for (Map.Entry<String, String> data : formData.entrySet()) {
                writer.append("--" + boundary).append(lineFeed);
                writer.append("Content-Disposition: form-data; name=\"" + data.getKey() + "\"")
                        .append(lineFeed);
                writer.append(HttpHeaderNames.CONTENT_TYPE.toString() + ":" + HttpHeaderValues.TEXT_PLAIN
                        + "; charset=" + TestConstant.CHARSET_NAME).append(lineFeed);
                writer.append(lineFeed);
                writer.append(data.getValue()).append(lineFeed);
                writer.flush();
            }
            writer.append(lineFeed).flush();
            writer.append("--" + boundary + "--").append(lineFeed);
            writer.close();
        }
        return buildResponse(urlConnection);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:reactor.ipc.netty.http.client.HttpClientFormEncoder.java

License:Open Source License

/**
 * Finalize the request by preparing the Header in the request and returns the request
 * ready to be sent.<br> Once finalized, no data must be added.<br> If the request
 * does not need chunk (isChunked() == false), this request is the only object to send
 * to the remote server.//  w  ww .j  a  va 2s .  c o m
 *
 * @return the request object (chunked or not according to size of body)
 *
 * @throws ErrorDataEncoderException if the encoding is in error or if the finalize
 *                                   were already done
 */
HttpRequest finalizeRequest() throws ErrorDataEncoderException {
    // Finalize the multipartHttpDatas
    if (!headerFinalized) {
        if (isMultipart) {
            InternalAttribute internal = new InternalAttribute(charset);
            if (duringMixedMode) {
                internal.addValue("\r\n--" + multipartMixedBoundary + "--");
            }
            internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
            multipartHttpDatas.add(internal);
            multipartMixedBoundary = null;
            currentFileUpload = null;
            duringMixedMode = false;
            globalBodySize += internal.size();
        }
        headerFinalized = true;
    } else {
        throw new ErrorDataEncoderException("Header already encoded");
    }

    HttpHeaders headers = request.headers();
    List<String> contentTypes = headers.getAll(HttpHeaderNames.CONTENT_TYPE);
    List<String> transferEncoding = headers.getAll(HttpHeaderNames.TRANSFER_ENCODING);
    if (contentTypes != null) {
        headers.remove(HttpHeaderNames.CONTENT_TYPE);
        for (String contentType : contentTypes) {
            // "multipart/form-data; boundary=--89421926422648"
            String lowercased = contentType.toLowerCase();
            if (lowercased.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString())
                    || lowercased.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
                // ignore
            } else {
                headers.add(HttpHeaderNames.CONTENT_TYPE, contentType);
            }
        }
    }
    if (isMultipart) {
        String value = HttpHeaderValues.MULTIPART_FORM_DATA + "; " + HttpHeaderValues.BOUNDARY + '='
                + multipartDataBoundary;
        headers.add(HttpHeaderNames.CONTENT_TYPE, value);
    } else {
        // Not multipart
        headers.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
    }
    // Now consider size for chunk or not
    long realSize = globalBodySize;
    if (isMultipart) {
        iterator = multipartHttpDatas.listIterator();
    } else {
        realSize -= 1; // last '&' removed
        iterator = multipartHttpDatas.listIterator();
    }
    headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize));
    if (realSize > chunkSize || isMultipart) {
        isChunked = true;
        if (transferEncoding != null) {
            headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
            for (CharSequence v : transferEncoding) {
                if (HttpHeaderValues.CHUNKED.contentEqualsIgnoreCase(v)) {
                    // ignore
                } else {
                    headers.add(HttpHeaderNames.TRANSFER_ENCODING, v);
                }
            }
        }
        HttpUtil.setTransferEncodingChunked(request, true);

        // wrap to hide the possible content
        return new WrappedHttpRequest(request);
    } else {
        // get the only one body and set it to the request
        HttpContent chunk = nextChunk();
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullRequest = (FullHttpRequest) request;
            ByteBuf chunkContent = chunk.content();
            if (fullRequest.content() != chunkContent) {
                fullRequest.content().clear().writeBytes(chunkContent);
                chunkContent.release();
            }
            return fullRequest;
        } else {
            return new WrappedFullHttpRequest(request, chunk);
        }
    }
}