Search in sources :

Example 36 with WebResponse

use of com.enonic.xp.web.WebResponse in project xp by enonic.

the class BasePortalHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) {
    final PortalRequest portalRequest;
    if (webRequest instanceof PortalRequest) {
        portalRequest = (PortalRequest) webRequest;
    } else {
        portalRequest = createPortalRequest(webRequest, webResponse);
    }
    try {
        PortalRequestAccessor.set(portalRequest.getRawRequest(), portalRequest);
        ContextAccessor.current().getLocalScope().setAttribute(portalRequest.getRepositoryId());
        ContextAccessor.current().getLocalScope().setAttribute(portalRequest.getBranch());
        final WebResponse returnedWebResponse = webHandlerChain.handle(portalRequest, webResponse);
        exceptionMapper.throwIfNeeded(returnedWebResponse);
        return returnedWebResponse;
    } catch (Exception e) {
        return handleError(portalRequest, e);
    }
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) WebException(com.enonic.xp.web.WebException) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 37 with WebResponse

use of com.enonic.xp.web.WebResponse in project xp by enonic.

the class WebAppHandlerTest method handle_executeController_error.

@Test
public void handle_executeController_error() throws Exception {
    mockResource("myapp:/assets/a.txt", null);
    this.request.setApplicationKey(ApplicationKey.from("myapp"));
    this.request.setRawPath("/webapp/myapp/a.txt");
    final WebResponse response = this.handler.doHandle(this.request, null, this.chain);
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatus());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) Test(org.junit.jupiter.api.Test)

Example 38 with WebResponse

use of com.enonic.xp.web.WebResponse in project xp by enonic.

the class WebAppHandlerTest method handle_serveAsset.

@Test
public void handle_serveAsset() throws Exception {
    final Resource resource = mockResource("myapp:/assets/a/b.txt", "hello".getBytes());
    this.request.setRawPath("/webapp/myapp/a/b.txt");
    this.request.setMethod(HttpMethod.GET);
    final WebResponse response = this.handler.doHandle(this.request, null, this.chain);
    assertEquals(HttpStatus.OK, response.getStatus());
    assertSame(resource, response.getBody());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) MockResource(com.enonic.xp.resource.MockResource) Resource(com.enonic.xp.resource.Resource) Test(org.junit.jupiter.api.Test)

Example 39 with WebResponse

use of com.enonic.xp.web.WebResponse in project xp by enonic.

the class WebDispatcherServlet method service.

@Override
protected void service(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    final WebRequest webRequest = newWebRequest(req);
    final WebSocketContext webSocketContext = this.webSocketContextFactory.newContext(req, res);
    webRequest.setWebSocketContext(webSocketContext);
    final WebResponse webResponse = doHandle(webRequest);
    final WebSocketConfig config = webResponse.getWebSocket();
    if ((webSocketContext != null) && (config != null)) {
        return;
    }
    responseSerializationService.serialize(webRequest, webResponse, res);
}
Also used : WebSocketConfig(com.enonic.xp.web.websocket.WebSocketConfig) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) WebSocketContext(com.enonic.xp.web.websocket.WebSocketContext)

Example 40 with WebResponse

use of com.enonic.xp.web.WebResponse in project xp by enonic.

the class TraceWebFilter method doHandle.

@Override
protected WebResponse doHandle(final WebRequest req, final WebResponse res, final WebHandlerChain chain) throws Exception {
    final Trace trace = Tracer.newTrace("portalRequest");
    if (trace == null) {
        return chain.handle(req, res);
    }
    trace.put("path", req.getPath());
    trace.put("rawpath", req.getRawPath());
    trace.put("url", ServletRequestUrlHelper.getFullUrl(req.getRawRequest()));
    trace.put("method", req.getMethod().toString());
    trace.put("host", req.getHost());
    trace.put("httpRequest", req);
    trace.put("httpResponse", res);
    trace.put("context", ContextAccessor.current());
    return Tracer.traceEx(trace, () -> {
        final WebResponse webResponse = chain.handle(req, res);
        addTraceInfo(trace, webResponse);
        trace.put("httpResponse", webResponse);
        return webResponse;
    });
}
Also used : Trace(com.enonic.xp.trace.Trace) WebResponse(com.enonic.xp.web.WebResponse)

Aggregations

WebResponse (com.enonic.xp.web.WebResponse)63 Test (org.junit.jupiter.api.Test)53 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)32 PortalResponse (com.enonic.xp.portal.PortalResponse)11 WebRequest (com.enonic.xp.web.WebRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 ByteSource (com.google.common.io.ByteSource)6 Cookie (javax.servlet.http.Cookie)6 ResourceKey (com.enonic.xp.resource.ResourceKey)5 WebException (com.enonic.xp.web.WebException)5 ServletOutputStream (javax.servlet.ServletOutputStream)5 PortalRequest (com.enonic.xp.portal.PortalRequest)4 Content (com.enonic.xp.content.Content)2 PropertyTree (com.enonic.xp.data.PropertyTree)2 PageUrlParams (com.enonic.xp.portal.url.PageUrlParams)2 MockResource (com.enonic.xp.resource.MockResource)2 Resource (com.enonic.xp.resource.Resource)2 ControllerMappingDescriptor (com.enonic.xp.site.mapping.ControllerMappingDescriptor)2 Trace (com.enonic.xp.trace.Trace)2 VirtualHost (com.enonic.xp.web.vhost.VirtualHost)2