Search in sources :

Example 21 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class LocaleAwarePageParametersTest method newApplication.

@Override
protected WebApplication newApplication() {
    return new MockApplication() {

        @Override
        protected void init() {
            super.init();
            mountPage("unaware", LocaleUnawarePageParametersPage.class);
            mount(new MountedMapper("aware", LocaleAwarePageParametersPage.class) {

                @Override
                protected Locale resolveLocale() {
                    return resolveUserLocale();
                }
            });
        }

        @Override
        public Session newSession(Request request, Response response) {
            final Session session = super.newSession(request, response);
            session.setLocale(Locale.GERMANY);
            return session;
        }
    };
}
Also used : Locale(java.util.Locale) Response(org.apache.wicket.request.Response) MockApplication(org.apache.wicket.mock.MockApplication) Request(org.apache.wicket.request.Request) Session(org.apache.wicket.Session)

Example 22 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class AjaxIndicatorAppender method afterRender.

@Override
public void afterRender(final Component component) {
    super.afterRender(component);
    final Response r = component.getResponse();
    r.write("<span style=\"display:none;\" class=\"");
    r.write(getSpanClass());
    r.write("\" ");
    r.write("id=\"");
    r.write(getMarkupId());
    r.write("\">");
    r.write("<img src=\"");
    r.write(getIndicatorUrl());
    r.write("\" alt=\"\"/></span>");
}
Also used : Response(org.apache.wicket.request.Response) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse)

Example 23 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class ComponentRenderer method renderComponent.

/**
 * Collects the Html generated by rendering a component.
 * <p>
 * Important notes:
 * <ul>
 * <li>this method is meant to render fresh component instances that are disposed after the html
 * has been generate. To avoid unwanted side effects do not use it with components that are from
 * an existing hierarchy.</li>
 * <li>does <strong>not</strong> support rendering
 * {@link org.apache.wicket.markup.html.panel.Fragment} instances</li>
 * <li>must be called on a thread bound to an application's {@link ThreadContext}!</li>
 * </ul>
 *
 * @param component
 *            the component to render.
 * @return the html rendered by the component
 *
 * @see ThreadContext
 */
public static CharSequence renderComponent(final Component component) {
    RequestCycle requestCycle = RequestCycle.get();
    final Response originalResponse = requestCycle.getResponse();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);
    MarkupContainer oldParent = component.getParent();
    if (oldParent != null && LOGGER.isWarnEnabled()) {
        LOGGER.warn("Component '{}' with a parent '{}' is passed for standalone rendering. " + "It is recommended to render only orphan components because they are not cleaned up/detached" + " after the rendering.", component, oldParent);
    }
    try {
        requestCycle.setResponse(tempResponse);
        // add the component to a dummy page just for the rendering
        RenderPage page = new RenderPage(component);
        page.internalInitialize();
        component.beforeRender();
        component.renderPart();
    } finally {
        if (oldParent != null) {
            // re-add the child to its old parent
            oldParent.add(component);
        }
        requestCycle.setResponse(originalResponse);
    }
    return tempResponse.getText();
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) Response(org.apache.wicket.request.Response) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) MarkupContainer(org.apache.wicket.MarkupContainer) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 24 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class AbstractResource method configureCache.

/**
 * Configure the web response header for client cache control.
 *
 * @param data
 *            resource data
 * @param attributes
 *            request attributes
 */
protected void configureCache(final ResourceResponse data, final Attributes attributes) {
    Response response = attributes.getResponse();
    if (response instanceof WebResponse) {
        Duration duration = data.getCacheDuration();
        WebResponse webResponse = (WebResponse) response;
        if (duration.compareTo(Duration.NONE) > 0) {
            webResponse.enableCaching(duration, data.getCacheScope());
        } else {
            webResponse.disableCaching();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebResponse(org.apache.wicket.request.http.WebResponse) Duration(org.apache.wicket.util.time.Duration)

Example 25 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class AbstractResource method setResponseHeaders.

/**
 * Sets the response header of resource response to the response received from the attributes
 *
 * @param resourceResponse
 *            the resource response to get the header fields from
 * @param attributes
 *            the attributes to get the response from to which the header information are going
 *            to be applied
 */
protected void setResponseHeaders(final ResourceResponse resourceResponse, final Attributes attributes) {
    Response response = attributes.getResponse();
    if (response instanceof WebResponse) {
        WebResponse webResponse = (WebResponse) response;
        // 1. Last Modified
        Time lastModified = resourceResponse.getLastModified();
        if (lastModified != null) {
            webResponse.setLastModifiedTime(lastModified);
        }
        // 2. Caching
        configureCache(resourceResponse, attributes);
        if (resourceResponse.getErrorCode() != null) {
            webResponse.sendError(resourceResponse.getErrorCode(), resourceResponse.getErrorMessage());
            return;
        }
        if (resourceResponse.getStatusCode() != null) {
            webResponse.setStatus(resourceResponse.getStatusCode());
        }
        if (!resourceResponse.dataNeedsToBeWritten(attributes)) {
            webResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
        // 3. Content Disposition
        String fileName = resourceResponse.getFileName();
        ContentDisposition disposition = resourceResponse.getContentDisposition();
        if (ContentDisposition.ATTACHMENT == disposition) {
            webResponse.setAttachmentHeader(fileName);
        } else if (ContentDisposition.INLINE == disposition) {
            webResponse.setInlineHeader(fileName);
        }
        // 4. Mime Type (+ encoding)
        String mimeType = resourceResponse.getContentType();
        if (mimeType != null) {
            final String encoding = resourceResponse.getTextEncoding();
            if (encoding == null) {
                webResponse.setContentType(mimeType);
            } else {
                webResponse.setContentType(mimeType + "; charset=" + encoding);
            }
        }
        // 5. Accept Range
        ContentRangeType acceptRange = resourceResponse.getAcceptRange();
        if (acceptRange != null) {
            webResponse.setAcceptRange(acceptRange.getTypeName());
        }
        long contentLength = resourceResponse.getContentLength();
        boolean contentRangeApplied = false;
        // 6. Content Range
        // for more information take a look here:
        // http://stackoverflow.com/questions/8293687/sample-http-range-request-session
        // if the content range header has been set directly
        // to the resource response use it otherwise calculate it
        String contentRange = resourceResponse.getContentRange();
        if (contentRange != null) {
            webResponse.setContentRange(contentRange);
        } else {
            // moment
            if (contentLength != -1 && ContentRangeType.BYTES.equals(acceptRange)) {
                contentRangeApplied = setResponseContentRangeHeaderFields(webResponse, attributes, contentLength);
            }
        }
        // 7. Content Length
        if (contentLength != -1 && !contentRangeApplied) {
            webResponse.setContentLength(contentLength);
        }
        // add custom headers and values
        final HttpHeaderCollection headers = resourceResponse.getHeaders();
        for (String name : headers.getHeaderNames()) {
            checkHeaderAccess(name);
            for (String value : headers.getHeaderValues(name)) {
                webResponse.addHeader(name, value);
            }
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebResponse(org.apache.wicket.request.http.WebResponse) Time(org.apache.wicket.util.time.Time) HttpHeaderCollection(org.apache.wicket.request.HttpHeaderCollection)

Aggregations

Response (org.apache.wicket.request.Response)30 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)9 WebResponse (org.apache.wicket.request.http.WebResponse)9 StringResponse (org.apache.wicket.response.StringResponse)9 Request (org.apache.wicket.request.Request)8 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)8 HeaderResponse (org.apache.wicket.markup.head.internal.HeaderResponse)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)4 MockApplication (org.apache.wicket.mock.MockApplication)4 Before (org.junit.Before)4 Session (org.apache.wicket.Session)3 IRequestCycle (org.apache.wicket.request.IRequestCycle)3 WicketTester (org.apache.wicket.util.tester.WicketTester)3 Test (org.junit.Test)3 Component (org.apache.wicket.Component)2 WebSession (org.apache.wicket.protocol.http.WebSession)2 Member (com.hazelcast.core.Member)1 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)1 MembershipEvent (com.hazelcast.core.MembershipEvent)1