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

colesico.framework.weblet.teleapi.origin.WebletAutoOrigin Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package colesico.framework.weblet.teleapi.origin;

import colesico.framework.http.HttpContext;
import colesico.framework.http.HttpMethod;
import colesico.framework.http.HttpRequest;
import colesico.framework.router.RouterContext;
import colesico.framework.weblet.teleapi.WebletOrigin;

import javax.inject.Provider;
import javax.inject.Singleton;

@Singleton
public class WebletAutoOrigin implements WebletOrigin {

    private final Provider httpContextProv;
    private final Provider routerContextProv;

    public WebletAutoOrigin(Provider httpContextProv, Provider routerContextProv) {
        this.httpContextProv = httpContextProv;
        this.routerContextProv = routerContextProv;
    }

    @Override
    public String getString(String name) {
        String value = null;
        HttpRequest httpRequest = httpContextProv.get().getRequest();
        switch (httpRequest.getRequestMethod().getName()) {
            case HttpMethod.GET:
            case HttpMethod.HEAD:
                if (httpRequest.getQueryParameters().hasKey(name)) {
                    return httpRequest.getQueryParameters().get(name);
                }
                return routerContextProv.get().getParameters().get(name);
            case HttpMethod.POST:
            case HttpMethod.PATCH:
            case HttpMethod.DELETE:
            case HttpMethod.PUT:
                if (httpRequest.getPostParameters().hasKey(name)) {
                    return httpRequest.getPostParameters().get(name);
                }
                if (httpRequest.getQueryParameters().hasKey(name)) {
                    return httpRequest.getQueryParameters().get(name);
                }
                RouterContext routerContext = routerContextProv.get();
                return routerContext.getParameters().get(name);
            default:
                return value;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy