org.rx.net.http.ServerRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
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);
}
}