Search in sources :

Example 21 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class StaticPageComponent method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    String language = UserInfoHolder.getLanguage();
    String page = req.getRequestUri();
    String staticPageContent = injector.getProject().getStaticPageContent(language, page);
    if (staticPageContent == null) {
        // todo localize
        res.sendErrorAsJson(new ErrorModel("500", ErrorTitles.formatTitle(Be5ErrorCode.NOT_FOUND, page), Collections.singletonMap(SELF_LINK, "static/" + page)), req.getDefaultMeta());
    } else {
        res.sendAsJson(new ResourceData(STATIC_ACTION, new StaticPagePresentation("", staticPageContent), Collections.singletonMap(SELF_LINK, "static/" + page)), req.getDefaultMeta());
    }
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) StaticPagePresentation(com.developmentontheedge.be5.model.StaticPagePresentation) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel)

Example 22 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class MainServlet method runTemplateProcessor.

private void runTemplateProcessor(String componentId, Request req, Response res) {
    if (UserInfoHolder.getUserInfo() == null) {
        injector.get(UserHelper.class).initGuest(req);
    }
    try {
        runRequestPreprocessors(componentId, req, res);
        new TemplateProcessor(servletContext).generate(req, res, injector);
    } catch (Be5Exception ex) {
        if (ex.getCode().isInternal() || ex.getCode().isAccessDenied()) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
        res.sendError(ex);
    } catch (Throwable e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        res.sendError(Be5Exception.internal(e));
    }
}
Also used : Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) UserHelper(com.developmentontheedge.be5.api.helpers.UserHelper) TemplateProcessor(com.developmentontheedge.be5.components.TemplateProcessor)

Example 23 with Response

use of com.developmentontheedge.be5.api.Response 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 24 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class MainServlet method runComponent.

void runComponent(String componentId, Request req, Response res) {
    if (UserInfoHolder.getUserInfo() == null) {
        injector.get(UserHelper.class).initGuest(req);
    }
    try {
        runRequestPreprocessors(componentId, req, res);
        Component component = getInjector().getComponent(componentId);
        component.generate(req, res, getInjector());
    } catch (Be5Exception e) {
        if (e.getCode().isInternal() || e.getCode().isAccessDenied()) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }
        res.sendError(e);
    } catch (Throwable e) {
        log.log(Level.SEVERE, e.getMessage(), e);
        res.sendError(Be5Exception.internal(e));
    }
}
Also used : Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) UserHelper(com.developmentontheedge.be5.api.helpers.UserHelper) Component(com.developmentontheedge.be5.api.Component)

Example 25 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class MainServletTest method runComponent.

@Test
public void runComponent() throws Exception {
    Request req = mock(Request.class);
    when(req.getRequestUri()).thenReturn("info.be");
    Response res = mock(Response.class);
    spyMainServlet.runComponent("static", req, res);
    verify(res).sendAsJson(any(ResourceData.class), any(Map.class));
}
Also used : Response(com.developmentontheedge.be5.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(com.developmentontheedge.be5.api.Request) HashMap(java.util.HashMap) Map(java.util.Map) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest) Test(org.junit.Test)

Aggregations

Response (com.developmentontheedge.be5.api.Response)23 Test (org.junit.Test)22 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)20 Request (com.developmentontheedge.be5.api.Request)6 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)5 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)5 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Action (com.developmentontheedge.be5.model.Action)4 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)2 UserHelper (com.developmentontheedge.be5.api.helpers.UserHelper)2 ResponseImpl (com.developmentontheedge.be5.api.impl.ResponseImpl)2 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)2 SqlMockOperationTest (com.developmentontheedge.be5.test.SqlMockOperationTest)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Date (java.util.Date)2