Search in sources :

Example 6 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class LocalizedImageResource method onResourceRequested.

/**
 * @param parameters
 *            page parameters
 */
public final void onResourceRequested(PageParameters parameters) {
    bind();
    RequestCycle requestCycle = RequestCycle.get();
    Attributes attributes = new Attributes(requestCycle.getRequest(), requestCycle.getResponse(), parameters);
    resource.respond(attributes);
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Attributes(org.apache.wicket.request.resource.IResource.Attributes)

Example 7 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class FilteringHeaderResponse method get.

/**
 * @return the FilteringHeaderResponse being used in this RequestCycle
 */
public static FilteringHeaderResponse get() {
    RequestCycle requestCycle = RequestCycle.get();
    if (requestCycle == null) {
        throw new IllegalStateException("You can only get the FilteringHeaderResponse when there is a RequestCycle present");
    }
    FilteringHeaderResponse response = requestCycle.getMetaData(RESPONSE_KEY);
    if (response == null) {
        throw new IllegalStateException("No FilteringHeaderResponse is present in the request cycle.  This may mean that you have not decorated the header response with a FilteringHeaderResponse.  Simply calling the FilteringHeaderResponse constructor sets itself on the request cycle");
    }
    return response;
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 8 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class ServletWebResponseTest method encodeRedirectAbsoluteUrl.

/**
 * WICKET-5582 absolute URLs stay absolute after encoding
 */
@Test
public void encodeRedirectAbsoluteUrl() {
    final String url = "http://localhost:8080/path";
    ServletWebRequest webRequest = mock(ServletWebRequest.class);
    when(webRequest.isAjax()).thenReturn(Boolean.FALSE);
    Url baseUrl = Url.parse("./baseUrl");
    baseUrl.setProtocol("http");
    baseUrl.setHost("someHost");
    baseUrl.setPort(80);
    when(webRequest.getClientUrl()).thenReturn(baseUrl);
    UrlRenderer renderer = new UrlRenderer(webRequest);
    RequestCycle requestCycle = mock(RequestCycle.class);
    ThreadContext.setRequestCycle(requestCycle);
    when(requestCycle.getUrlRenderer()).thenReturn(renderer);
    HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
    when(httpServletResponse.encodeRedirectURL(Matchers.eq(url))).thenReturn(url + ";foo");
    ServletWebResponse webResponse = new ServletWebResponse(webRequest, httpServletResponse);
    assertEquals(url + ";foo", webResponse.encodeRedirectURL(url));
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) UrlRenderer(org.apache.wicket.request.UrlRenderer) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 9 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class AbstractMarkupFilter method getRequestUniqueId.

/**
 * Returns an id using the request-relative counter associated with the
 * underlying {@link org.apache.wicket.markup.MarkupResourceStream}'s owner container
 * (see {@link org.apache.wicket.markup.MarkupResourceStream#getContainerInfo()}).
 * This can be useful for autocomponent tags that need to get a tag id.
 *
 * @return
 * 		the request-relative id
 */
protected int getRequestUniqueId() {
    RequestCycle requestCycle = RequestCycle.get();
    Map<String, AtomicInteger> markupUniqueCounters = requestCycle.getMetaData(REQUEST_COUNTER_KEY);
    ContainerInfo containerInfo = getMarkupResourceStream().getContainerInfo();
    String cacheKey = containerInfo != null ? containerInfo.getContainerClass().getCanonicalName() : null;
    if (markupUniqueCounters == null) {
        markupUniqueCounters = new HashMap<>();
        requestCycle.setMetaData(REQUEST_COUNTER_KEY, markupUniqueCounters);
    }
    AtomicInteger counter = markupUniqueCounters.get(cacheKey);
    if (counter == null) {
        counter = new AtomicInteger();
        markupUniqueCounters.put(cacheKey, counter);
    }
    int cacheHash = cacheKey == null ? 0 : cacheKey.hashCode();
    // using the same algorithm of String#hashCode()
    return cacheHash * 31 + counter.getAndIncrement();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ContainerInfo(org.apache.wicket.markup.ContainerInfo)

Example 10 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class ModalWindow method getWindowOpenJavaScript.

/**
 * Returns the javascript used to open the window. Subclass
 * {@link #postProcessSettings(AppendingStringBuffer)} to modify the JavaScript if needed.
 *
 * See WICKET-12
 *
 * @return javascript that opens the window
 */
protected final String getWindowOpenJavaScript() {
    JSONObject settings = new JSONObject();
    settings.put("minWidth", getMinimalWidth());
    settings.put("minHeight", getMinimalHeight());
    settings.put("className", getCssClassName());
    settings.put("width", getInitialWidth());
    if ((isUseInitialHeight() == true) || (isCustomComponent() == false)) {
        settings.put("height", getInitialHeight());
    } else {
        settings.put("height", (Object) null);
    }
    settings.put("resizable", isResizable());
    if (isResizable() == false) {
        settings.put("widthUnit", getWidthUnit());
        settings.put("heightUnit", getHeightUnit());
    }
    if (isCustomComponent() == false) {
        Page page = createPage();
        if (page == null) {
            throw new WicketRuntimeException("Error creating page for modal dialog.");
        }
        CharSequence pageUrl;
        RequestCycle requestCycle = RequestCycle.get();
        page.getSession().getPageManager().touchPage(page);
        if (page.isPageStateless()) {
            pageUrl = requestCycle.urlFor(page.getClass(), page.getPageParameters());
        } else {
            IRequestHandler handler = new RenderPageRequestHandler(new PageProvider(page));
            pageUrl = requestCycle.urlFor(handler);
        }
        settings.put("src", pageUrl);
    } else {
        settings.put("element", new JSONFunction("document.getElementById(\"" + getContentMarkupId() + "\")"));
    }
    if (getCookieName() != null) {
        settings.put("cookieId", getCookieName());
    }
    String title = getTitle() != null ? getTitle().getObject() : null;
    if (title != null) {
        settings.put("title", getDefaultModelObjectAsString(title));
    }
    if (getMaskType() == MaskType.TRANSPARENT) {
        settings.put("mask", "transparent");
    } else if (getMaskType() == MaskType.SEMI_TRANSPARENT) {
        settings.put("mask", "semi-transparent");
    }
    settings.put("autoSize", autoSize);
    settings.put("unloadConfirmation", showUnloadConfirmation());
    // set true if we set a windowclosedcallback
    boolean haveCloseCallback = false;
    // notification request
    if (windowClosedCallback != null) {
        WindowClosedBehavior behavior = getBehaviors(WindowClosedBehavior.class).get(0);
        settings.put("onClose", new JSONFunction("function() { " + behavior.getCallbackScript() + " }"));
        haveCloseCallback = true;
    }
    // close window property (thus cleaning the shown flag)
    if ((closeButtonCallback != null) || (haveCloseCallback == false)) {
        CloseButtonBehavior behavior = getBehaviors(CloseButtonBehavior.class).get(0);
        settings.put("onCloseButton", new JSONFunction("function() { " + behavior.getCallbackScript() + "; return false; }"));
    }
    postProcessSettings(settings);
    AppendingStringBuffer buffer = new AppendingStringBuffer(500);
    buffer.append("var settings = ");
    buffer.append(settings.toString());
    buffer.append(";");
    buffer.append(getShowJavaScript());
    return buffer.toString();
}
Also used : AppendingStringBuffer(org.apache.wicket.util.string.AppendingStringBuffer) IRequestHandler(org.apache.wicket.request.IRequestHandler) RenderPageRequestHandler(org.apache.wicket.core.request.handler.RenderPageRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Page(org.apache.wicket.Page) JSONFunction(org.apache.wicket.ajax.json.JSONFunction) JSONObject(com.github.openjson.JSONObject) PageProvider(org.apache.wicket.core.request.handler.PageProvider)

Aggregations

RequestCycle (org.apache.wicket.request.cycle.RequestCycle)69 WebResponse (org.apache.wicket.request.http.WebResponse)14 IRequestHandler (org.apache.wicket.request.IRequestHandler)9 Url (org.apache.wicket.request.Url)9 Response (org.apache.wicket.request.Response)8 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)7 Page (org.apache.wicket.Page)6 Request (org.apache.wicket.request.Request)6 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)5 Test (org.junit.Test)5 Application (org.apache.wicket.Application)4 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)4 WebClientInfo (org.apache.wicket.protocol.http.request.WebClientInfo)4 WebRequest (org.apache.wicket.request.http.WebRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 WebApplication (org.apache.wicket.protocol.http.WebApplication)3 UrlRenderer (org.apache.wicket.request.UrlRenderer)3