Search in sources :

Example 1 with InternalResource

use of com.github.bordertech.wcomponents.InternalResource in project wcomponents by BorderTech.

the class SourcePanel method preparePaintComponent.

@Override
protected void preparePaintComponent(Request request) {
    if (!isInitialised()) {
        String fileName = "/com/github/bordertech/wcomponents/examples/sunlight.dark.css";
        InternalResource resource = new InternalResource(fileName, fileName);
        cssUrl.setText(resource.getTargetUrl());
        fileName = "/com/github/bordertech/wcomponents/examples/sunlight-min.js";
        resource = new InternalResource(fileName, fileName);
        script1Url.setText(resource.getTargetUrl());
        fileName = "/com/github/bordertech/wcomponents/examples/sunlight.java-min.js";
        resource = new InternalResource(fileName, fileName);
        script2Url.setText(resource.getTargetUrl());
        setInitialised(true);
    }
    super.preparePaintComponent(request);
}
Also used : InternalResource(com.github.bordertech.wcomponents.InternalResource)

Example 2 with InternalResource

use of com.github.bordertech.wcomponents.InternalResource in project wcomponents by BorderTech.

the class WWindowInterceptor_Test method testServiceRequest.

@Test
public void testServiceRequest() {
    final int initialServletStep = 123;
    final int initialWindowStep = 111;
    // Create app
    WApplication app = new WApplication();
    WWindow window = new WWindow();
    window.setContent(new MockLabel("dummy"));
    app.add(window);
    WContent content = new WContent();
    content.setContentAccess(new InternalResource("/wcomponents-test.properties", "test"));
    app.add(content);
    app.setLocked(true);
    // Set up servlet env
    WServlet.WServletEnvironment servletEnvironment = new WServlet.WServletEnvironment("/app", "", "");
    servletEnvironment.setStep(initialServletStep);
    // Set user session
    UIContext uic = createUIContext();
    uic.setUI(app);
    uic.setEnvironment(servletEnvironment);
    setActiveContext(uic);
    window.setStep(initialWindowStep);
    window.display();
    // Target the content first - should pass through and update the environment's step
    WWindowInterceptor interceptor = new WWindowInterceptor(true);
    TestInterceptor testInterceptor = new TestInterceptor();
    interceptor.setBackingComponent(testInterceptor);
    interceptor.attachUI(app);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, content.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Servlet step should have changed after serviceRequest", initialServletStep + 1, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Servlet step should have changed after preparePaint", initialServletStep + 2, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Servlet step should have changed after paint", initialServletStep + 3, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    servletEnvironment.setStep(initialServletStep);
    request = new MockRequest();
    request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Window step should have changed after serviceRequest", initialWindowStep + 1, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Window step should have changed after preparePaintnot have changed", initialWindowStep + 2, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Window step should have changed after paint", initialWindowStep + 3, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    String actualTargetId = testInterceptor.hiddenParams.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    Assert.assertEquals("Hidden params target id should be window id", window.getId(), actualTargetId);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) InternalResource(com.github.bordertech.wcomponents.InternalResource) UIContext(com.github.bordertech.wcomponents.UIContext) WWindow(com.github.bordertech.wcomponents.WWindow) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) WContent(com.github.bordertech.wcomponents.WContent) WApplication(com.github.bordertech.wcomponents.WApplication) MockLabel(com.github.bordertech.wcomponents.MockLabel) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with InternalResource

use of com.github.bordertech.wcomponents.InternalResource in project wcomponents by BorderTech.

the class ServletUtil method handleStaticResourceRequest.

/**
 * Handles a request for static resources.
 *
 * @param request the http request.
 * @param response the http response.
 */
public static void handleStaticResourceRequest(final HttpServletRequest request, final HttpServletResponse response) {
    String staticRequest = request.getParameter(WServlet.STATIC_RESOURCE_PARAM_NAME);
    try {
        InternalResource staticResource = InternalResourceMap.getResource(staticRequest);
        boolean headersOnly = "HEAD".equals(request.getMethod());
        if (staticResource == null) {
            LOG.warn("Static resource [" + staticRequest + "] not found.");
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        InputStream resourceStream = staticResource.getStream();
        if (resourceStream == null) {
            LOG.warn("Static resource [" + staticRequest + "] not found. Stream for content is null.");
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        int size = resourceStream.available();
        String fileName = WebUtilities.encodeForContentDispositionHeader(staticRequest.substring(staticRequest.lastIndexOf('/') + 1));
        if (size > 0) {
            response.setContentLength(size);
        }
        response.setContentType(WebUtilities.getContentType(staticRequest));
        response.setHeader("Cache-Control", CacheType.CONTENT_CACHE.getSettings());
        String param = request.getParameter(WContent.URL_CONTENT_MODE_PARAMETER_KEY);
        if ("inline".equals(param)) {
            response.setHeader("Content-Disposition", "inline; filename=" + fileName);
        } else if ("attach".equals(param)) {
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        } else {
            // added "filename=" to comply with https://tools.ietf.org/html/rfc6266
            response.setHeader("Content-Disposition", "filename=" + fileName);
        }
        if (!headersOnly) {
            StreamUtil.copy(resourceStream, response.getOutputStream());
        }
    } catch (IOException e) {
        LOG.warn("Could not process static resource [" + staticRequest + "]. ", e);
        response.reset();
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}
Also used : InternalResource(com.github.bordertech.wcomponents.InternalResource) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

InternalResource (com.github.bordertech.wcomponents.InternalResource)3 MockLabel (com.github.bordertech.wcomponents.MockLabel)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 WContent (com.github.bordertech.wcomponents.WContent)1 WWindow (com.github.bordertech.wcomponents.WWindow)1 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)1 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)1 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)1 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Test (org.junit.Test)1