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

org.zodiac.netty.http.headers.Method Maven / Gradle / Ivy

package org.zodiac.netty.http.headers;

import java.util.Map;
import java.util.Objects;

import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.AsciiString;

/**
 * Enum of standard HTTP methods.
 *
 */
public enum Method implements org.zodiac.netty.http.base.HttpMethod {

    GET, PUT, POST, OPTIONS, HEAD, DELETE, TRACE, CONNECT,
    // WEBDAV
    PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK,
    UNKNOWN, PATCH;

    private final AsciiString stringValue;

    Method() {
        stringValue = AsciiString.of(name());
    }

    public HttpMethod toHttpMethod() {
        switch(this) {
            case GET :
                return HttpMethod.GET;
            case PUT :
                return HttpMethod.PUT;
            case POST :
                return HttpMethod.POST;
            case OPTIONS :
                return HttpMethod.OPTIONS;
            case HEAD :
                return HttpMethod.HEAD;
            case PATCH :
                return HttpMethod.PATCH;
            case TRACE :
                return HttpMethod.TRACE;
            case CONNECT :
                return HttpMethod.CONNECT;
            default :
                return HttpMethod.valueOf(name());
        }
    }

    @Override
    public String toString() {
        return name();
    }

    public CharSequence toCharSequence() {
        return stringValue;
    }

    public Method find(Object o) {
        for (Method m : values()) {
            if (m.is(o)) {
                return m;
            }
        }
        return null;
    }

    public static Method get(HttpRequest req) {
        HttpMethod m = req.method();
//        if (m == HttpMethod.GET) {
//            return GET;
//        } else if (m == HttpMethod.PUT) {
//            return PUT;
//        } else if (m == HttpMethod.POST) {
//            return POST;
//        } else if (m == HttpMethod.PUT) {
//            return PUT;
//        } else if (m == HttpMethod.OPTIONS) {
//            return OPTIONS;
//        } else if (m == HttpMethod.HEAD) {
//            return HEAD;
//        } else if (m == HttpMethod.PATCH) {
//            return PATCH;
//        } else if (m == HttpMethod.TRACE) {
//            return TRACE;
//        } else if (m == HttpMethod.CONNECT) {
//            return CONNECT;
//        }

        Method method = MethodHolder.getByNettyHeepMethod(m);
        if (null != method) {
            return method;
        }

        return Method.valueOf(m.name().toUpperCase());
    }

    public static Method valueOf(CharSequence seq) {
        Objects.requireNonNull(seq, "seq");
        for (Method m : values()) {
            if (StrUtil.equalsCharSeq(seq, m.stringValue)) {
                return m;
            }
        }
        throw new IllegalArgumentException(seq.toString());
    }

    private static class MethodHolder {
        private static final Map NETTY_HTTPMETHOD_METHOD_CACHE = CollUtil.concurrentMap();

        static {
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.GET, GET);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.PUT, PUT);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.POST, POST);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.OPTIONS, OPTIONS);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.HEAD, HEAD);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.PATCH, PATCH);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.TRACE, TRACE);
            NETTY_HTTPMETHOD_METHOD_CACHE.putIfAbsent(HttpMethod.CONNECT, CONNECT);
        }

        private static Method getByNettyHeepMethod(HttpMethod httpMethod) {
            if (null == httpMethod)
                return null;
            return NETTY_HTTPMETHOD_METHOD_CACHE.get(httpMethod);
        }

    }

    public static void main(String[] args) {
        System.out.println(MethodHolder.getByNettyHeepMethod(null));
        System.out.println(MethodHolder.getByNettyHeepMethod(HttpMethod.PUT));
        System.out.println(MethodHolder.getByNettyHeepMethod(HttpMethod.CONNECT));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy