Search in sources :

Example 1 with HttpMethod

use of spark.route.HttpMethod in project spark by perwendel.

the class MatcherFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
    // handle static resources
    boolean consumedByStaticFile = staticFiles.consume(httpRequest, httpResponse);
    if (consumedByStaticFile) {
        return;
    }
    String method = getHttpMethodFrom(httpRequest);
    String httpMethodStr = method.toLowerCase();
    String uri = httpRequest.getRequestURI();
    String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER);
    Body body = Body.create();
    RequestWrapper requestWrapper = RequestWrapper.create();
    ResponseWrapper responseWrapper = ResponseWrapper.create();
    Response response = RequestResponseFactory.create(httpResponse);
    HttpMethod httpMethod = HttpMethod.get(httpMethodStr);
    RouteContext context = RouteContext.create().withMatcher(routeMatcher).withHttpRequest(httpRequest).withUri(uri).withAcceptType(acceptType).withBody(body).withRequestWrapper(requestWrapper).withResponseWrapper(responseWrapper).withResponse(response).withHttpMethod(httpMethod);
    try {
        try {
            BeforeFilters.execute(context);
            Routes.execute(context);
            AfterFilters.execute(context);
        } catch (HaltException halt) {
            Halt.modify(httpResponse, body, halt);
        } catch (Exception generalException) {
            GeneralError.modify(httpRequest, httpResponse, body, requestWrapper, responseWrapper, generalException);
        }
        // If redirected and content is null set to empty string to not throw NotConsumedException
        if (body.notSet() && responseWrapper.isRedirected()) {
            body.set("");
        }
        if (body.notSet() && hasOtherHandlers) {
            if (servletRequest instanceof HttpRequestWrapper) {
                ((HttpRequestWrapper) servletRequest).notConsumed(true);
                return;
            }
        }
        if (body.notSet() && !externalContainer) {
            LOG.info("The requested route [{}] has not been mapped in Spark for {}: [{}]", uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType);
            httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
            if (CustomErrorPages.existsFor(404)) {
                requestWrapper.setDelegate(RequestResponseFactory.create(httpRequest));
                responseWrapper.setDelegate(RequestResponseFactory.create(httpResponse));
                body.set(CustomErrorPages.getFor(404, requestWrapper, responseWrapper));
            } else {
                body.set(String.format(CustomErrorPages.NOT_FOUND));
            }
        }
    } finally {
        try {
            AfterAfterFilters.execute(context);
        } catch (Exception generalException) {
            GeneralError.modify(httpRequest, httpResponse, body, requestWrapper, responseWrapper, generalException);
        }
    }
    if (body.isSet()) {
        body.serializeTo(httpResponse, serializerChain, httpRequest);
    } else if (chain != null) {
        chain.doFilter(httpRequest, httpResponse);
    }
}
Also used : HaltException(spark.HaltException) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HaltException(spark.HaltException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Response(spark.Response) HttpRequestWrapper(spark.embeddedserver.jetty.HttpRequestWrapper) HttpRequestWrapper(spark.embeddedserver.jetty.HttpRequestWrapper) HttpMethod(spark.route.HttpMethod)

Example 2 with HttpMethod

use of spark.route.HttpMethod in project camel by apache.

the class SparkEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    ObjectHelper.notEmpty(verb, "verb", this);
    ObjectHelper.notEmpty(path, "path", this);
    // verb must be supported by Spark and lets convert to the actual name
    HttpMethod method = getCamelContext().getTypeConverter().mandatoryConvertTo(HttpMethod.class, verb);
    verb = method.name();
}
Also used : HttpMethod(spark.route.HttpMethod)

Aggregations

HttpMethod (spark.route.HttpMethod)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HaltException (spark.HaltException)1 Response (spark.Response)1 HttpRequestWrapper (spark.embeddedserver.jetty.HttpRequestWrapper)1