Search in sources :

Example 21 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class KaptchaImage method onImage.

Object onImage() throws IOException {
    if (captchaText == null) {
        return new HttpError(HttpServletResponse.SC_NOT_FOUND, "Session expired.");
    }
    return new StreamResponse() {

        @Override
        public String getContentType() {
            return "image/jpeg";
        }

        @Override
        public InputStream getStream() throws IOException {
            BufferedImage image = producer.createImage(captchaText);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baos);
            return new ByteArrayInputStream(baos.toByteArray());
        }

        @Override
        public void prepareResponse(Response response) {
            response.setDateHeader("Expires", 0);
            // Set standard HTTP/1.1 no-cache headers.
            response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            // Set standard HTTP/1.0 no-cache header.
            response.setHeader("Pragma", "no-cache");
        }
    };
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.apache.tapestry5.http.services.Response) StreamResponse(org.apache.tapestry5.StreamResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamResponse(org.apache.tapestry5.StreamResponse) HttpError(org.apache.tapestry5.services.HttpError) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage)

Example 22 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class PageTesterModule method setupTestableOverrides.

@Contribute(ServiceOverride.class)
public static void setupTestableOverrides(MappedConfiguration<Class, Object> configuration, @Local TestableRequest request, @Local TestableResponse response, final ObjectLocator locator) {
    configuration.add(Request.class, request);
    configuration.add(Response.class, response);
    TestableCookieSinkSource cookies = new TestableCookieSinkSource();
    configuration.add(CookieSink.class, cookies);
    configuration.add(CookieSource.class, cookies);
    // With the significant changes to the handling of assets in 5.4, we introduced a problem:
    // We were checking at page render time whether to generate URLs for normal or compressed
    // assets and that peeked at the HttpServletRequest global, which isn't set up by PageTester.
    // What we're doing here is using a hacked version of that code to force GZip support
    // on.
    configuration.add(ResponseCompressionAnalyzer.class, new ResponseCompressionAnalyzer() {

        public boolean isGZipEnabled(ContentType contentType) {
            return locator.getObject(CompressionAnalyzer.class, null).isCompressable(contentType.getMimeType());
        }

        public boolean isGZipSupported() {
            return true;
        }
    });
}
Also used : ContentType(org.apache.tapestry5.http.ContentType) ResponseCompressionAnalyzer(org.apache.tapestry5.http.services.ResponseCompressionAnalyzer) Contribute(org.apache.tapestry5.ioc.annotations.Contribute)

Example 23 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class ClasspathAssetRequestHandler method handleAssetRequest.

public boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException {
    ChecksumPath path = new ChecksumPath(streamer, baseFolder, extraPath);
    final boolean handled;
    if (classpathAssetProtectionRule.block(path.resourcePath) && !path.resourcePath.equals(ChecksumPath.NON_EXISTING_RESOURCE)) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Blocked request for classpath asset '" + path.resourcePath + "'. Contribute a new ClasspathAssetProtectionRule if you need this asset to be publicly accessible.");
        }
        handled = false;
    } else {
        Resource resource = assetSource.resourceForPath(path.resourcePath);
        handled = path.stream(resource);
    }
    return handled;
}
Also used : Resource(org.apache.tapestry5.commons.Resource)

Example 24 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class TestableResponseImplTest method http_headers.

@Test
public void http_headers() {
    Document document = tester.renderPage(TestPageForHttpHeaders.class.getSimpleName());
    assertTrue(document.toString().contains("Test page for HTTP headers"));
    TestableResponse response = tester.getService(TestableResponse.class);
    assertEquals(response.getHeader(TestPageForHttpHeaders.DATE_HEADER_NAME), 12345L);
    assertEquals(response.getHeader(TestPageForHttpHeaders.INT_HEADER_NAME), 6789);
    assertEquals(response.getHeader(TestPageForHttpHeaders.STRING_HEADER_NAME), "foo-bar-baz-barney");
}
Also used : TestPageForHttpHeaders(org.apache.tapestry5.integration.app2.pages.TestPageForHttpHeaders) Document(org.apache.tapestry5.dom.Document) Test(org.testng.annotations.Test)

Example 25 with Response

use of org.apache.tapestry5.http.services.Response in project tapestry-5 by apache.

the class ComponentEventLinkEncoderImplTest method root_index_page_gone.

@Test
public void root_index_page_gone() {
    RequestSecurityManager manager = mockRequestSecurityManager();
    Response response = mockResponse();
    ContextPathEncoder contextPathEncoder = getService(ContextPathEncoder.class);
    expect(manager.checkPageSecurity("Index")).andReturn(LinkSecurity.INSECURE);
    train_encodeURL(response, "/", "MAGIC");
    replay();
    ComponentEventLinkEncoder encoder = new ComponentEventLinkEncoderImpl(null, contextPathEncoder, null, response, manager, null, null, false, "", "", null, null);
    PageRenderRequestParameters parameters = new PageRenderRequestParameters("Index", new EmptyEventContext());
    Link link = encoder.createPageRenderLink(parameters);
    assertEquals(link.toURI(), "MAGIC");
    verify();
}
Also used : Response(org.apache.tapestry5.http.services.Response) PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ContextPathEncoder(org.apache.tapestry5.services.ContextPathEncoder) ComponentEventLinkEncoder(org.apache.tapestry5.services.ComponentEventLinkEncoder) EmptyEventContext(org.apache.tapestry5.internal.EmptyEventContext) Link(org.apache.tapestry5.http.Link) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)53 Response (org.apache.tapestry5.http.services.Response)47 Request (org.apache.tapestry5.http.services.Request)25 Link (org.apache.tapestry5.http.Link)23 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 LocalizationSetter (org.apache.tapestry5.services.LocalizationSetter)12 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)12 ComponentClassResolver (org.apache.tapestry5.services.ComponentClassResolver)11 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)11 EmptyEventContext (org.apache.tapestry5.internal.EmptyEventContext)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 RequestFilter (org.apache.tapestry5.http.services.RequestFilter)8 RequestHandler (org.apache.tapestry5.http.services.RequestHandler)8 JSONObject (org.apache.tapestry5.json.JSONObject)8 Context (org.apache.tapestry5.http.services.Context)7 Dispatcher (org.apache.tapestry5.http.services.Dispatcher)7 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)7 ComponentEventRequestParameters (org.apache.tapestry5.services.ComponentEventRequestParameters)6 ComponentRequestHandler (org.apache.tapestry5.services.ComponentRequestHandler)6 IOException (java.io.IOException)5