All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.net.http.ServerRequest Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net.http;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;
import io.netty.handler.codec.http.multipart.FileUpload;
import io.netty.util.CharsetUtil;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.rx.bean.MultiValueMap;
import org.rx.core.Strings;

import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Set;

import static org.rx.core.Extends.ifNull;
import static org.rx.core.Extends.require;

@Getter
public final class ServerRequest {
    private final InetSocketAddress remoteEndpoint;
    private final String uri;
    private final HttpMethod method;
    private final HttpHeaders headers = new DefaultHttpHeaders();
    @Getter(lazy = true)
    private final Set cookies = cookiesLazy();
    private final MultiValueMap queryString = new MultiValueMap<>();

    @Setter(AccessLevel.PROTECTED)
    private MultiValueMap form;
    @Setter(AccessLevel.PROTECTED)
    private MultiValueMap files;
    @Setter(AccessLevel.PROTECTED)
    private ByteBuf content;

    private Set cookiesLazy() {
        String cookie = headers.get(HttpHeaderNames.COOKIE);
        if (Strings.isEmpty(cookie)) {
            return Collections.emptySet();
        }
        return ServerCookieDecoder.STRICT.decode(cookie);
    }

    public String getContentType() {
        return headers.get(HttpHeaderNames.CONTENT_TYPE);
    }

    public ServerRequest(@NonNull InetSocketAddress remoteEndpoint, @NonNull String uri, HttpMethod method) {
        this.remoteEndpoint = remoteEndpoint;
        this.uri = uri;
        this.method = ifNull(method, HttpMethod.GET);
    }

    public String jsonBody() {
        require(content);
        return content.toString(CharsetUtil.UTF_8);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy