Search in sources :

Example 21 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project wicket by apache.

the class AbstractTransformerBehavior method afterRender.

@Override
public void afterRender(final Component component) {
    final RequestCycle requestCycle = RequestCycle.get();
    try {
        BufferedWebResponse tempResponse = (BufferedWebResponse) requestCycle.getResponse();
        if (component instanceof Page && originalResponse instanceof WebResponse) {
            tempResponse.writeMetaData((WebResponse) originalResponse);
        }
        // Transform the data
        CharSequence output = transform(component, tempResponse.getText());
        originalResponse.write(output);
    } catch (Exception ex) {
        throw new WicketRuntimeException("Error while transforming the output of component: " + component, ex);
    } finally {
        // Restore the original response object
        requestCycle.setResponse(originalResponse);
    }
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Page(org.apache.wicket.Page) WicketRuntimeException(org.apache.wicket.WicketRuntimeException)

Example 22 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project wicket by apache.

the class AbstractTransformerBehavior method beforeRender.

@Override
public void beforeRender(Component component) {
    super.beforeRender(component);
    final RequestCycle requestCycle = RequestCycle.get();
    // Temporarily replace the web response with a String response
    originalResponse = requestCycle.getResponse();
    WebResponse origResponse = (WebResponse) ((originalResponse instanceof WebResponse) ? originalResponse : null);
    BufferedWebResponse tempResponse = newResponse(origResponse);
    // temporarily set StringResponse to collect the transformed output
    requestCycle.setResponse(tempResponse);
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 23 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project wicket by apache.

the class AjaxDownloadBehavior method initiate.

/**
 * Call this method to initiate the download.
 *
 * @param target
 *            the initiating Ajax target
 */
public void initiate(AjaxRequestTarget target) {
    if (getComponent() == null) {
        throw new WicketRuntimeException("not bound to a component");
    }
    ((WebResponse) RequestCycle.get().getResponse()).clearCookie(cookie(getName()));
    CharSequence url;
    if (resourceBehavior == null) {
        if (resourceReference.canBeRegistered()) {
            getComponent().getApplication().getResourceReferenceRegistry().registerResourceReference(resourceReference);
        }
        PageParameters parameters = new PageParameters();
        if (resourceParameters != null) {
            parameters.mergeWith(resourceParameters);
        }
        parameters.set(RESOURCE_PARAMETER_NAME, getName());
        url = getComponent().getRequestCycle().urlFor(new ResourceReferenceRequestHandler(resourceReference, parameters));
    } else {
        url = resourceBehavior.getUrl();
    }
    JSONObject settings = new JSONObject();
    settings.put("attributes", new JSONFunction(renderAjaxAttributes(getComponent())));
    settings.put("name", getName());
    settings.put("downloadUrl", url);
    settings.put("method", getLocation().name().toLowerCase(Locale.ENGLISH));
    target.appendJavaScript(String.format("Wicket.AjaxDownload.initiate(%s);", settings));
    onBeforeDownload(target);
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) WebResponse(org.apache.wicket.request.http.WebResponse) JSONObject(com.github.openjson.JSONObject) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) JSONFunction(org.apache.wicket.ajax.json.JSONFunction)

Example 24 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project wicket by apache.

the class AutoCompleteBehavior method onRequest.

@Override
protected final void onRequest(final String val, final RequestCycle requestCycle) {
    IRequestHandler target = new IRequestHandler() {

        @Override
        public void respond(final IRequestCycle requestCycle) {
            WebResponse r = (WebResponse) requestCycle.getResponse();
            // Determine encoding
            final String encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();
            r.setContentType("text/xml; charset=" + encoding);
            r.disableCaching();
            Iterator<T> comps = getChoices(val);
            int count = 0;
            renderer.renderHeader(r);
            while (comps.hasNext()) {
                final T comp = comps.next();
                renderer.render(comp, r, val);
                count += 1;
            }
            renderer.renderFooter(r, count);
        }
    };
    requestCycle.scheduleRequestHandlerAfterCurrent(target);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 25 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project wicket by apache.

the class WebApplication method renderXmlDecl.

/**
 * The rules if and when to insert an xml decl in the response are a bit tricky. Hence, we allow
 * the user to replace the default implementation per page and per application.
 * <p>
 * Default implementation: the page mime type must be "application/xhtml+xml" and request
 * HTTP_ACCEPT header must include "application/xhtml+xml" to automatically include the xml
 * decl. Please see <a href=
 * "https://developer.mozilla.org/en/Writing_JavaScript_for_XHTML#Finally:_Content_Negotiation"
 * >Writing JavaScript for XHTML</a> for details.
 * <p>
 * Please note that xml decls in Wicket's markup are only used for reading the markup. The
 * markup's xml decl will always be removed and never be used to configure the response.
 *
 * @param page
 *            The page currently being rendered
 * @param insert
 *            If false, than the rules are applied. If true, it'll always be written. In order
 *            to never insert it, than subclass renderXmlDecl() with an empty implementation.
 */
public void renderXmlDecl(final WebPage page, boolean insert) {
    if (insert || MarkupType.XML_MIME.equalsIgnoreCase(page.getMarkupType().getMimeType())) {
        final RequestCycle cycle = RequestCycle.get();
        if (insert == false) {
            WebRequest request = (WebRequest) cycle.getRequest();
            String accept = request.getHeader("Accept");
            insert = ((accept == null) || (accept.indexOf(MarkupType.XML_MIME) != -1));
        }
        if (insert) {
            WebResponse response = (WebResponse) cycle.getResponse();
            response.write("<?xml version='1.0'");
            String encoding = getRequestCycleSettings().getResponseRequestEncoding();
            if (Strings.isEmpty(encoding) == false) {
                response.write(" encoding='");
                response.write(encoding);
                response.write("'");
            }
            response.write(" ?>");
        }
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) ServletWebResponse(org.apache.wicket.protocol.http.servlet.ServletWebResponse) WebRequest(org.apache.wicket.request.http.WebRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Aggregations

WebResponse (org.apache.wicket.request.http.WebResponse)34 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)9 Test (org.junit.Test)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 Response (org.apache.wicket.request.Response)5 WebRequest (org.apache.wicket.request.http.WebRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)4 IRequestHandler (org.apache.wicket.request.IRequestHandler)3 Url (org.apache.wicket.request.Url)3 IOException (java.io.IOException)2 Application (org.apache.wicket.Application)2 Page (org.apache.wicket.Page)2 Attributes (org.apache.wicket.request.resource.IResource.Attributes)2 JSONObject (com.github.openjson.JSONObject)1 Member (com.hazelcast.core.Member)1 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)1 MembershipEvent (com.hazelcast.core.MembershipEvent)1 MembershipListener (com.hazelcast.core.MembershipListener)1