Search in sources :

Example 1 with IRequestCycle

use of org.apache.wicket.request.IRequestCycle in project midpoint by Evolveum.

the class AjaxDownloadBehaviorFromStream method onRequest.

public void onRequest() {
    final InputStream byteStream = initStream();
    if (byteStream == null) {
        return;
    }
    IResourceStream resourceStream = new AbstractResourceStream() {

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public InputStream getInputStream() throws ResourceStreamNotFoundException {
            return byteStream;
        }

        @Override
        public void close() throws IOException {
            byteStream.close();
        }
    };
    ResourceStreamRequestHandler reqHandler = new ResourceStreamRequestHandler(resourceStream) {

        @Override
        public void respond(IRequestCycle requestCycle) {
            super.respond(requestCycle);
        }
    }.setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ONE_SECOND);
    if (StringUtils.isNotEmpty(getFileName())) {
        reqHandler.setFileName(getFileName());
    }
    getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(reqHandler);
}
Also used : AbstractResourceStream(org.apache.wicket.util.resource.AbstractResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) InputStream(java.io.InputStream) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 2 with IRequestCycle

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

the class DownloadLink method onClick.

@Override
public void onClick() {
    final File file = getModelObject();
    if (file == null) {
        throw new IllegalStateException(getClass().getName() + " failed to retrieve a File object from model");
    }
    String fileName = fileNameModel != null ? fileNameModel.getObject() : null;
    if (Strings.isEmpty(fileName)) {
        fileName = file.getName();
    }
    IResourceStream resourceStream = new FileResourceStream(new org.apache.wicket.util.file.File(file));
    getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {

        @Override
        public void respond(IRequestCycle requestCycle) {
            super.respond(requestCycle);
            if (deleteAfter) {
                Files.remove(file);
            }
        }
    }.setFileName(fileName).setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(cacheDuration));
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) IResourceStream(org.apache.wicket.util.resource.IResourceStream) File(java.io.File) ResourceStreamRequestHandler(org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 3 with IRequestCycle

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

the class RequestSettingRequestHandler method respond.

@Override
public void respond(IRequestCycle requestCycle) {
    RequestCycle cycle = (RequestCycle) requestCycle;
    Request originalRequest = cycle.getRequest();
    try {
        cycle.setRequest(request);
        delegate.respond(requestCycle);
    } finally {
        cycle.setRequest(originalRequest);
    }
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) Request(org.apache.wicket.request.Request)

Example 4 with IRequestCycle

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

the class AjaxRequestHandler method respond.

/**
 * @see org.apache.wicket.core.request.handler.IPageRequestHandler#respond(org.apache.wicket.request.IRequestCycle)
 */
@Override
public final void respond(final IRequestCycle requestCycle) {
    final RequestCycle rc = (RequestCycle) requestCycle;
    final WebResponse response = (WebResponse) requestCycle.getResponse();
    if (shouldRedirectToPage(requestCycle)) {
        // the page itself has been added to the request target, we simply issue a redirect
        // back to the page
        IRequestHandler handler = new RenderPageRequestHandler(new PageProvider(page));
        final String url = rc.urlFor(handler).toString();
        response.sendRedirect(url);
        return;
    }
    respondersFrozen = true;
    for (ITargetRespondListener listener : respondListeners) {
        listener.onTargetRespond(this);
    }
    final Application app = page.getApplication();
    page.send(app, Broadcast.BREADTH, this);
    // Determine encoding
    final String encoding = app.getRequestCycleSettings().getResponseRequestEncoding();
    // Set content type based on markup type for page
    update.setContentType(response, encoding);
    // Make sure it is not cached by a client
    response.disableCaching();
    final StringResponse bodyResponse = new StringResponse();
    update.writeTo(bodyResponse, encoding);
    CharSequence filteredResponse = invokeResponseFilters(bodyResponse);
    response.write(filteredResponse);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) RenderPageRequestHandler(org.apache.wicket.core.request.handler.RenderPageRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) PageProvider(org.apache.wicket.core.request.handler.PageProvider) StringResponse(org.apache.wicket.response.StringResponse) Application(org.apache.wicket.Application)

Example 5 with IRequestCycle

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

the class RedirectRequestHandlerTest method tempMovedShouldRedirect.

/**
 * tempMovedShouldRedirect()
 */
@Test
public void tempMovedShouldRedirect() {
    RedirectRequestHandler handler = new RedirectRequestHandler(REDIRECT_URL, HttpServletResponse.SC_MOVED_TEMPORARILY);
    IRequestCycle requestCycle = Mockito.mock(IRequestCycle.class);
    WebResponse webResponse = Mockito.mock(WebResponse.class);
    Mockito.when(requestCycle.getResponse()).thenReturn(webResponse);
    handler.respond(requestCycle);
    Mockito.verify(webResponse).sendRedirect(REDIRECT_URL);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestCycle(org.apache.wicket.request.IRequestCycle) Test(org.junit.Test)

Aggregations

IRequestCycle (org.apache.wicket.request.IRequestCycle)13 WebResponse (org.apache.wicket.request.http.WebResponse)5 IRequestHandler (org.apache.wicket.request.IRequestHandler)4 ResourceStreamRequestHandler (org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)4 IResourceStream (org.apache.wicket.util.resource.IResourceStream)4 Test (org.junit.Test)4 Request (org.apache.wicket.request.Request)2 ReplaceHandlerException (org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)2 File (java.io.File)1 InputStream (java.io.InputStream)1 Application (org.apache.wicket.Application)1 Session (org.apache.wicket.Session)1 PageProvider (org.apache.wicket.core.request.handler.PageProvider)1 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)1 MockWebRequest (org.apache.wicket.mock.MockWebRequest)1 IExceptionMapper (org.apache.wicket.request.IExceptionMapper)1 IRequestMapper (org.apache.wicket.request.IRequestMapper)1 Response (org.apache.wicket.request.Response)1