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

io.github.jiashunx.masker.rest.framework.MRestRequest Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package io.github.jiashunx.masker.rest.framework;

import io.github.jiashunx.masker.rest.framework.cons.Constants;
import io.github.jiashunx.masker.rest.framework.serialize.MRestSerializer;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author jiashunx
 */
public class MRestRequest {

    private HttpRequest httpRequest;

    private Map attributes = new HashMap<>();
    private HttpMethod method;
    private String contextPath;
    /**
     * 带context-path的url.
     */
    private String originUrl;
    /**
     * 不带context-path的url.
     */
    private String url;
    private String urlQuery;
    private Map parameters;
    private Map> originParameters;
    private HttpHeaders headers;
    private List headerKeys;
    private List cookies;
    private Map cookieMap;
    private byte[] bodyBytes;

    public MRestRequest() {}

    public MRestRequest(MRestRequest restRequest) {
        this.httpRequest = restRequest.httpRequest;
        this.attributes = restRequest.attributes;
        this.method = restRequest.method;
        this.contextPath = restRequest.contextPath;
        this.originUrl = restRequest.originUrl;
        this.url = restRequest.url;
        this.urlQuery = restRequest.urlQuery;
        this.parameters = restRequest.parameters;
        this.originParameters = restRequest.originParameters;
        this.headers = restRequest.headers;
        this.headerKeys = restRequest.headerKeys;
        this.cookies = restRequest.cookies;
        this.cookieMap = restRequest.cookieMap;
        this.bodyBytes = restRequest.bodyBytes;
    }

    @Override
    protected MRestRequest clone() throws CloneNotSupportedException {
        return (MRestRequest) super.clone();
    }

    public void release() {

    }

    public String bodyToString() {
        return new String(bodyBytes, StandardCharsets.UTF_8);
    }

    public  List parseBodyToObjList(Class klass) {
        return MRestSerializer.jsonToList(bodyBytes, klass);
    }

    public  T parseBodyToObj(Class klass) {
        return MRestSerializer.jsonToObj(bodyBytes, klass);
    }

    public HttpRequest getHttpRequest() {
        return httpRequest;
    }

    public void setHttpRequest(HttpRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    public Map getAttributes() {
        return attributes;
    }

    public void setAttributes(Map attributes) {
        this.attributes = attributes;
    }

    public void setAttribute(String key, String value) {
        attributes.put(key, value);
    }

    public String getAttribute(String key) {
        return attributes.get(key);
    }

    public HttpMethod getMethod() {
        return method;
    }

    public void setMethod(HttpMethod method) {
        this.method = method;
    }

    public String getContextPath() {
        return contextPath;
    }

    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    public String getOriginUrl() {
        return originUrl;
    }

    public void setOriginUrl(String originUrl) {
        this.originUrl = originUrl;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlQuery() {
        return urlQuery;
    }

    public void setUrlQuery(String urlQuery) {
        this.urlQuery = urlQuery;
    }

    public Map getParameters() {
        return parameters;
    }

    public String getParameter(String key) {
        return getParameters().get(key);
    }

    public void setParameters(Map parameters) {
        this.parameters = parameters;
    }

    public Map> getOriginParameters() {
        return originParameters;
    }

    public void setOriginParameters(Map> originParameters) {
        this.originParameters = originParameters;
    }

    public boolean isUploadFile() {
        String contentType = getHeader(Constants.HTTP_HEADER_CONTENT_TYPE);
        if (contentType != null) {
            int idx = contentType.indexOf(";");
            if (idx > 0) {
                contentType = contentType.substring(0, idx);
            }
            contentType = contentType.trim();
        }
        return Constants.CONTENT_TYPE_MULTIPART_FORMDATA.equals(contentType);
    }

    /**
     * get header of "Accept".
     * @return Accept
     */
    public String getAccept() {
        return getHeader(Constants.HTTP_HEADER_ACCEPT);
    }

    /**
     * get first "Accept" from header.
     * @return Accept
     */
    public String getAcceptFirst() {
        String acceptFirst = getAccept();
        if (acceptFirst != null) {
            int idx = acceptFirst.indexOf(",");
            if (idx > 0) {
                acceptFirst = acceptFirst.substring(0, idx);
            }
        }
        if ("*/*".equals(acceptFirst)) {
            acceptFirst = null;
        }
        return acceptFirst;
    }

    public HttpHeaders getHeaders() {
        return headers;
    }

    public String getHeader(String key) {
        return getHeaders().get(key);
    }

    public List getHeaderAll(String key) {
        return getHeaders().getAll(key);
    }

    public List> getHeaderEntries() {
        return getHeaders().entries();
    }

    public List getHeaderKeys() {
        return this.headerKeys;
    }

    private void setHeaderKeys(List headerKeys) {
        this.headerKeys = Objects.requireNonNull(headerKeys);
    }

    public List getCookies() {
        return cookies;
    }

    public void setCookies(List cookies) {
        this.cookies = Objects.requireNonNull(cookies);
    }

    public Cookie getCookie(String key) {
        return getCookieMap().get(key);
    }

    public Map getCookieMap() {
        return cookieMap;
    }

    private void setCookieMap(Map cookieMap) {
        this.cookieMap = Objects.requireNonNull(cookieMap);
    }

    public void setHeaders(HttpHeaders headers) {
        this.headers = headers;

        // ----------- set headerKeys
        Set keySet = new HashSet<>();
        getHeaderEntries().forEach(entry -> {
            keySet.add(entry.getKey());
        });
        setHeaderKeys(new ArrayList<>(keySet));

        // ----------- set cookies
        List cookieNames = new ArrayList<>();
        getHeaderKeys().forEach(key -> {
            if (key.toLowerCase().equals(Constants.HTTP_HEADER_COOKIE.toLowerCase())
                    || key.toLowerCase().equals(Constants.HTTP_HEADER_SET_COOKIE.toLowerCase())) {
                cookieNames.add(key);
            }
        });
        List cookieStrList = new ArrayList<>(cookieNames.size());
        cookieNames.forEach(name -> {
            cookieStrList.addAll(getHeaderAll(name));
        });
        List _cookies = new ArrayList<>();
        cookieStrList.forEach(string -> {
            _cookies.addAll(ServerCookieDecoder.STRICT.decodeAll(string));
        });
        setCookies(_cookies);

        // ----------- set cookieMap
        Map _cookieMap = new HashMap<>();
        getCookies().forEach(cookie -> {
            _cookieMap.put(cookie.name(), cookie);
        });
        setCookieMap(_cookieMap);
    }

    public byte[] getBodyBytes() {
        return bodyBytes;
    }

    public void setBodyBytes(byte[] bodyBytes) {
        this.bodyBytes = bodyBytes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy