Search in sources :

Example 1 with Response

use of spark.Response 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 Response

use of spark.Response in project searchcode-server by boyter.

the class CodeRouteServiceTest method testGetCodeNoParams.

public void testGetCodeNoParams() {
    CodeRouteService codeRouteService = new CodeRouteService();
    Request request = Mockito.mock(Request.class);
    Response response = Mockito.mock(Response.class);
    try {
        codeRouteService.getCode(request, response);
    } catch (HaltException ex) {
    }
    verify(response, times(1)).redirect("/404/");
}
Also used : Response(spark.Response) HaltException(spark.HaltException) CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request)

Example 3 with Response

use of spark.Response in project searchcode-server by boyter.

the class CodeRouteServiceTest method testGetCodeWithParamsNoMatch.

public void testGetCodeWithParamsNoMatch() {
    CodeRouteService codeRouteService = new CodeRouteService();
    Request request = Mockito.mock(Request.class);
    Response response = Mockito.mock(Response.class);
    when(request.params(":codeid")).thenReturn("NOTHING-SHOULD-MATCH-THIS-EVER");
    try {
        codeRouteService.getCode(request, response);
    } catch (HaltException ex) {
    }
    verify(response, times(1)).redirect("/404/");
}
Also used : Response(spark.Response) HaltException(spark.HaltException) CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request)

Example 4 with Response

use of spark.Response in project searchcode-server by boyter.

the class CodeRouteServiceTest method testGetProject.

public void testGetProject() {
    CodeRouteService codeRouteService = new CodeRouteService();
    Request request = Mockito.mock(Request.class);
    Response response = Mockito.mock(Response.class);
    try {
        codeRouteService.getProject(request, response);
    } catch (HaltException ex) {
    }
    verify(response, times(1)).redirect("/404/");
}
Also used : Response(spark.Response) HaltException(spark.HaltException) CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request)

Example 5 with Response

use of spark.Response in project newts by OpenNMS.

the class Web method main.

public static void main(String... args) {
    staticFileLocation("/static");
    get(new VelocityRoute("/stations") {

        @Override
        public Object handle(Request request, Response response) {
            Map<String, Object> model = Maps.newHashMap();
            model.put("stationsMap", STATION_NAMES);
            return modelAndView(model, "index.wm");
        }
    });
    get(new VelocityRoute("/summer88") {

        @Override
        public Object handle(Request arg0, Response arg1) {
            Map<String, Object> model = Maps.newHashMap();
            model.put("stationIds", STATION_IDS);
            return modelAndView(model, "summer.wm");
        }
    });
    get(new VelocityRoute("/stations/:stationName") {

        @Override
        public Object handle(Request request, Response response) {
            String stationName = request.params(":stationName");
            String id = STATION_IDS.get(stationName);
            if (id == null) {
                halt(404, "No such station");
            }
            Map<String, String> model = Maps.newHashMap();
            model.put("location", STATION_NAMES.get(stationName));
            model.put("id", id);
            model.put("start", request.queryParams("start"));
            model.put("end", request.queryParams("end"));
            model.put("resolution", request.queryParams("resolution"));
            return modelAndView(model, "station.wm");
        }
    });
}
Also used : Response(spark.Response) Request(spark.Request) VelocityRoute(spark.template.velocity.VelocityRoute) Map(java.util.Map)

Aggregations

Response (spark.Response)9 Request (spark.Request)8 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)4 HaltException (spark.HaltException)4 Values (com.searchcode.app.config.Values)2 CodeResult (com.searchcode.app.dto.CodeResult)2 CodeMatcher (com.searchcode.app.service.CodeMatcher)2 CodeSearcher (com.searchcode.app.service.CodeSearcher)2 Singleton (com.searchcode.app.service.Singleton)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)2 Gson (com.google.gson.Gson)1 App (com.searchcode.app.App)1 Data (com.searchcode.app.dao.Data)1 Repo (com.searchcode.app.dao.Repo)1 com.searchcode.app.dto (com.searchcode.app.dto)1 CodeMatchResult (com.searchcode.app.dto.CodeMatchResult)1 SearchResult (com.searchcode.app.dto.SearchResult)1 RepoResult (com.searchcode.app.model.RepoResult)1