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

com.vtence.molecule.middlewares.HttpMethodOverride Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package com.vtence.molecule.middlewares;

import com.vtence.molecule.http.HttpMethod;
import com.vtence.molecule.Request;
import com.vtence.molecule.Response;

import java.io.IOException;

import static com.vtence.molecule.http.HttpMethod.POST;

public class HttpMethodOverride extends AbstractMiddleware {

    public static final String METHOD_OVERRIDE_PARAMETER = "_method";

    public void handle(Request request, Response response) throws Exception {
        if (overrideDetected(request) && validOverride(request)) {
            request.method(methodOverride(request).toUpperCase());
        }
        forward(request, response);
    }

    private boolean validOverride(Request request) {
        return HttpMethod.valid(methodOverride(request));
    }

    private boolean overrideDetected(Request request) throws IOException {
        return methodOverride(request) != null && request.method() == POST;
    }

    private String methodOverride(Request request) {
        return request.parameter(METHOD_OVERRIDE_PARAMETER);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy