Example usage for io.netty.handler.codec.http HttpHeaders iteratorAsString

List of usage examples for io.netty.handler.codec.http HttpHeaders iteratorAsString

Introduction

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

Prototype

public final Iterator<Entry<String, String>> iteratorAsString() 

Source Link

Document

Iterator that converts each Entry 's key and value to a String .

Usage

From source file:org.ballerinalang.mime.util.MultipartDataSource.java

License:Open Source License

/**
 * Write body part headers to output stream.
 *
 * @param writer   Represent the outputstream writer
 * @param bodyPart Represent ballerina body part
 * @throws IOException When an error occurs while writing body part headers
 *///  w  ww .  j  a  v  a2  s  .c o  m
private void writeBodyPartHeaders(Writer writer, BMap<String, BValue> bodyPart) throws IOException {
    HttpHeaders httpHeaders;
    if (bodyPart.getNativeData(ENTITY_HEADERS) != null) {
        httpHeaders = (HttpHeaders) bodyPart.getNativeData(ENTITY_HEADERS);
    } else {
        httpHeaders = new DefaultHttpHeaders();
        bodyPart.addNativeData(ENTITY_HEADERS, httpHeaders);
    }
    String contentType = MimeUtil.getContentTypeWithParameters(bodyPart);
    httpHeaders.set(HttpHeaderNames.CONTENT_TYPE.toString(), contentType);
    String contentDisposition = MimeUtil.getContentDisposition(bodyPart);
    if (!contentDisposition.isEmpty()) {
        httpHeaders.set(HttpHeaderNames.CONTENT_DISPOSITION.toString(), contentDisposition);
    }

    BValue contentId = bodyPart.get(CONTENT_ID_FIELD);
    if (contentId != null && !contentId.stringValue().isEmpty()) {
        httpHeaders.set(CONTENT_ID, contentId.stringValue());
    }
    Iterator<Map.Entry<String, String>> iterator = httpHeaders.iteratorAsString();
    while (iterator.hasNext()) {
        Map.Entry<String, String> entry = iterator.next();
        writer.write(entry.getKey());
        writer.write(COLON);
        writer.write(SPACE);
        writer.write(entry.getValue());
        writer.write(CRLF);
    }
    // Mark the end of the headers for this body part
    writer.write(CRLF);
    writer.flush();
}

From source file:uapi.web.http.netty.internal.NettyHttpRequest.java

License:Open Source License

NettyHttpRequest(final ILogger logger, final HttpRequest httpRequest) {
    this._logger = logger;
    this._request = httpRequest;

    HttpHeaders headers = this._request.headers();
    Looper.from(headers.iteratorAsString())
            .foreach(entry -> this._headers.put(entry.getKey().toLowerCase(), entry.getValue()));

    this._uri = this._request.uri();
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(this._uri);
    Map<String, List<String>> params = queryStringDecoder.parameters();
    Looper.from(params.entrySet()).foreach(entry -> this._params.put(entry.getKey(), entry.getValue()));

    HttpVersion version = this._request.protocolVersion();
    if (HttpVersion.HTTP_1_0.equals(version)) {
        this._version = uapi.web.http.HttpVersion.V_1_0;
    } else if (HttpVersion.HTTP_1_1.equals(version)) {
        this._version = uapi.web.http.HttpVersion.V_1_1;
    } else {//  ww w.  java  2  s.  c om
        throw new KernelException("Unsupported Http version - {}", version);
    }

    HttpMethod method = this._request.method();
    if (HttpMethod.GET.equals(method)) {
        this._method = uapi.web.http.HttpMethod.GET;
    } else if (HttpMethod.PUT.equals(method)) {
        this._method = uapi.web.http.HttpMethod.PUT;
    } else if (HttpMethod.POST.equals(method)) {
        this._method = uapi.web.http.HttpMethod.POST;
    } else if (HttpMethod.PATCH.equals(method)) {
        this._method = uapi.web.http.HttpMethod.PATCH;
    } else if (HttpMethod.DELETE.equals(method)) {
        this._method = uapi.web.http.HttpMethod.DELETE;
    } else {
        throw new KernelException("Unsupported http method {}", method.toString());
    }

    // Decode content type
    String contentTypeString = this._headers.get(HttpHeaderNames.CONTENT_TYPE.toString());
    if (contentTypeString == null) {
        this._conentType = ContentType.TEXT;
        this._charset = Charset.forName("UTF-8");
    } else {
        String[] contentTypeInfo = contentTypeString.split(";");
        if (contentTypeInfo.length < 0) {
            this._conentType = ContentType.TEXT;
            this._charset = CharsetUtil.UTF_8;
        } else if (contentTypeInfo.length == 1) {
            this._conentType = ContentType.parse(contentTypeInfo[0].trim());
            this._charset = CharsetUtil.UTF_8;
        } else {
            this._conentType = ContentType.parse(contentTypeInfo[0].trim());
            this._charset = Looper.from(contentTypeInfo).map(info -> info.split("="))
                    .filter(kv -> kv.length == 2).filter(kv -> kv[0].trim().equalsIgnoreCase("charset"))
                    .map(kv -> kv[1].trim()).map(Charset::forName).first(CharsetUtil.UTF_8);
        }
    }
}