Search in sources :

Example 1 with RequestImpl

use of com.developmentontheedge.be5.api.impl.RequestImpl in project be5 by DevelopmentOnTheEdge.

the class MainServlet method respond.

/**
 * The general routing method. Tries to determine and find a component using a given request URI.
 * Generation of response is delegated to a found component.
 */
private boolean respond(HttpServletRequest request, HttpServletResponse response, String method, String requestUri, Map<String, String[]> parameters) {
    String origin = request.getHeader("Origin");
    // TODO test origin
    response.addHeader("Access-Control-Allow-Credentials", "true");
    response.addHeader("Access-Control-Allow-Origin", origin);
    response.addHeader("Access-Control-Allow-Methods", "POST, GET");
    response.addHeader("Access-Control-Max-Age", "1728000");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    Response res = new ResponseImpl(response);
    Matcher matcher = uriPattern.matcher(requestUri);
    if (!matcher.matches()) {
        // This prevents triggering engine executions for resource URLs
        // todo remove
        log.log(Level.INFO, requestUri + " ContextPath=" + request.getContextPath());
        String contextPath = request.getContextPath();
        if (requestUri.startsWith(contextPath + (contextPath.endsWith("/") ? "" : "/") + "static/") || // || requestUri.startsWith("/static/")
        requestUri.contains("favicon.ico")) {
            return false;
        }
        String templateComponentID = "templateProcessor";
        if (!injector.hasComponent(templateComponentID)) {
            templateComponentID = "defaultTemplateProcessor";
        }
        RequestImpl req = new RequestImpl(request, requestUri, simplify(parameters));
        UserInfoHolder.setRequest(req);
        runTemplateProcessor(templateComponentID, req, res);
        return true;
    }
    String[] uriParts = requestUri.split("/");
    int ind = 1;
    while (!"api".equals(uriParts[ind]) && ind + 1 < uriParts.length) {
        ind++;
    }
    String subRequestUri = Joiner.on('/').join(Iterables.skip(Arrays.asList(uriParts), ind + 2));
    String componentId = uriParts[ind + 1];
    Request req = new RequestImpl(request, subRequestUri, simplify(parameters));
    UserInfoHolder.setRequest(req);
    runComponent(componentId, req, res);
    return true;
}
Also used : Response(com.developmentontheedge.be5.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Matcher(java.util.regex.Matcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(com.developmentontheedge.be5.api.Request) ServletRequest(javax.servlet.ServletRequest) ResponseImpl(com.developmentontheedge.be5.api.impl.ResponseImpl) RequestImpl(com.developmentontheedge.be5.api.impl.RequestImpl)

Example 2 with RequestImpl

use of com.developmentontheedge.be5.api.impl.RequestImpl in project be5 by DevelopmentOnTheEdge.

the class TestUtils method getSpyMockRequest.

protected Request getSpyMockRequest(String requestUri, Map<String, String> parameters, Map<String, Object> sessionValues) {
    HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
    when(httpServletRequest.getSession()).thenReturn(mock(HttpSession.class));
    Request request = Mockito.spy(new RequestImpl(httpServletRequest, null, parameters));
    when(request.getRequestUri()).thenReturn(requestUri);
    for (Map.Entry<String, Object> entry : sessionValues.entrySet()) {
        when(request.getAttribute(entry.getKey())).thenReturn(entry.getValue());
    }
    return request;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(com.developmentontheedge.be5.api.Request) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RequestImpl(com.developmentontheedge.be5.api.impl.RequestImpl)

Aggregations

Request (com.developmentontheedge.be5.api.Request)2 RequestImpl (com.developmentontheedge.be5.api.impl.RequestImpl)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Response (com.developmentontheedge.be5.api.Response)1 ResponseImpl (com.developmentontheedge.be5.api.impl.ResponseImpl)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1