Search in sources :

Example 6 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class HeaderResponseTest method before.

/**
 * Prepare
 */
@Before
public void before() {
    final Response realResponse = new StringResponse();
    headerResponse = new HeaderResponse() {

        @Override
        protected Response getRealResponse() {
            return realResponse;
        }
    };
    reference = new ResourceReference("testReference") {

        private static final long serialVersionUID = 1L;

        @Override
        public IResource getResource() {
            return null;
        }
    };
    RequestCycle requestCycle = mock(RequestCycle.class);
    when(requestCycle.urlFor(any(IRequestHandler.class))).thenReturn(RESOURCE_NAME);
    when(requestCycle.find(any())).thenReturn(Optional.empty());
    Request request = mock(Request.class);
    when(request.getCharset()).thenReturn(Charset.defaultCharset());
    when(requestCycle.getRequest()).thenReturn(request);
    UrlRenderer urlRenderer = mock(UrlRenderer.class);
    when(urlRenderer.renderContextRelativeUrl((any(String.class)))).thenReturn(RESOURCE_NAME);
    when(requestCycle.getUrlRenderer()).thenReturn(urlRenderer);
    ThreadContext.setRequestCycle(requestCycle);
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Request(org.apache.wicket.request.Request) StringResponse(org.apache.wicket.response.StringResponse) ResourceReference(org.apache.wicket.request.resource.ResourceReference) UrlRenderer(org.apache.wicket.request.UrlRenderer) IResource(org.apache.wicket.request.resource.IResource) Before(org.junit.Before)

Example 7 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class RequestCycleUrlForTest method before.

@Before
public void before() {
    Request request = mock(Request.class);
    Url baseUrl = Url.parse("");
    when(request.getClientUrl()).thenReturn(baseUrl);
    Response response = new StringResponse() {

        @Override
        public String encodeURL(CharSequence url) {
            return url + JSESSIONID;
        }
    };
    IRequestMapper mapper = mock(IRequestMapper.class);
    Url bookmarkablePageUrl = Url.parse(BOOKMARKABLE_PAGE_URL);
    when(mapper.mapHandler(argThat(new ExactClassMatcher<BookmarkablePageRequestHandler>(BookmarkablePageRequestHandler.class)))).thenReturn(bookmarkablePageUrl);
    Url resourceUrl = Url.parse(RESOURCE_URL);
    when(mapper.mapHandler(argThat(new ExactClassMatcher<ResourceRequestHandler>(ResourceRequestHandler.class)))).thenReturn(resourceUrl);
    Url resourceReferenceUrl = Url.parse(RES_REF_URL);
    when(mapper.mapHandler(argThat(new ExactClassMatcher<ResourceReferenceRequestHandler>(ResourceReferenceRequestHandler.class)))).thenReturn(resourceReferenceUrl);
    IExceptionMapper exceptionMapper = mock(IExceptionMapper.class);
    RequestCycleContext context = new RequestCycleContext(request, response, mapper, exceptionMapper);
    requestCycle = new RequestCycle(context);
    requestCycle.getUrlRenderer().setBaseUrl(baseUrl);
}
Also used : StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) Request(org.apache.wicket.request.Request) StringResponse(org.apache.wicket.response.StringResponse) IExceptionMapper(org.apache.wicket.request.IExceptionMapper) IRequestMapper(org.apache.wicket.request.IRequestMapper) Url(org.apache.wicket.request.Url) Before(org.junit.Before)

Example 8 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class FilteringHeaderResponseTest method createBucketOnTheFlyForFilteredHeaderItem.

/**
 * https://issues.apache.org/jira/browse/WICKET-5057
 * @throws Exception
 */
@Test
public void createBucketOnTheFlyForFilteredHeaderItem() throws Exception {
    try (FilteringHeaderResponse headerResponse = new FilteringHeaderResponse(new HeaderResponse() {

        @Override
        protected Response getRealResponse() {
            return new StringResponse();
        }
    }, "headerBucketName", Collections.emptyList())) {
        String filterName = "filterName";
        String headerContent = "content";
        FilteredHeaderItem item = new FilteredHeaderItem(StringHeaderItem.forString(headerContent), filterName);
        headerResponse.render(item);
        CharSequence realContent = headerResponse.getContent(filterName);
        assertEquals(headerContent, realContent.toString());
    }
}
Also used : StringResponse(org.apache.wicket.response.StringResponse) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) Response(org.apache.wicket.request.Response) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Test(org.junit.Test)

Example 9 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class WebPage method validateHeaders.

/**
 * Validate that each component which wanted to contribute to the header section actually was
 * able to do so.
 */
private void validateHeaders() {
    // search for HtmlHeaderContainer in the first level of children or deeper
    // if there are transparent resolvers used
    HtmlHeaderContainer header = visitChildren(new IVisitor<Component, HtmlHeaderContainer>() {

        @Override
        public void component(final Component component, final IVisit<HtmlHeaderContainer> visit) {
            if (component instanceof HtmlHeaderContainer) {
                visit.stop((HtmlHeaderContainer) component);
            } else if (component instanceof TransparentWebMarkupContainer == false) {
                visit.dontGoDeeper();
            }
        }
    });
    if (header == null) {
        // the markup must at least contain a <body> tag for wicket to automatically
        // create a HtmlHeaderContainer. Log an error if no header container
        // was created but any of the components or behaviors want to contribute
        // something to the header.
        header = new HtmlHeaderContainer(HtmlHeaderSectionHandler.HEADER_ID);
        add(header);
        RequestCycle requestCycle = getRequestCycle();
        Response orgResponse = requestCycle.getResponse();
        try {
            StringResponse tempResponse = new StringResponse();
            requestCycle.setResponse(tempResponse);
            // Render all header sections of all components on the page
            AbstractHeaderRenderStrategy.get().renderHeader(header, null, getPage());
            IHeaderResponse headerResponse = header.getHeaderResponse();
            headerResponse.close();
            CharSequence collectedHeaderOutput = tempResponse.getBuffer();
            if (collectedHeaderOutput.length() > 0) {
                reportMissingHead(collectedHeaderOutput);
            }
        } catch (Exception e) {
            // just swallow this exception, there isn't much we can do about.
            log.error("header/body check throws exception", e);
        } finally {
            this.remove(header);
            requestCycle.setResponse(orgResponse);
        }
    }
}
Also used : IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) HtmlHeaderContainer(org.apache.wicket.markup.html.internal.HtmlHeaderContainer) Component(org.apache.wicket.Component)

Example 10 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class FilteringHeaderResponse method getContent.

/**
 * Gets the content that was rendered to this header response and matched the filter with the
 * given name.
 *
 * @param filterName
 *            the name of the filter to get the bucket for
 * @return the content that was accepted by the filter with this name
 */
@SuppressWarnings("resource")
public final CharSequence getContent(String filterName) {
    if (filterName == null || !responseFilterMap.containsKey(filterName)) {
        return "";
    }
    List<HeaderItem> resp = responseFilterMap.get(filterName);
    final StringResponse strResponse = new StringResponse();
    IHeaderResponse headerRenderer = new HeaderResponse() {

        @Override
        protected Response getRealResponse() {
            return strResponse;
        }

        @Override
        public boolean wasRendered(Object object) {
            return FilteringHeaderResponse.this.getRealResponse().wasRendered(object);
        }

        @Override
        public void markRendered(Object object) {
            FilteringHeaderResponse.this.getRealResponse().markRendered(object);
        }
    };
    headerRenderer = decorate(headerRenderer);
    for (HeaderItem curItem : resp) {
        headerRenderer.render(curItem);
    }
    headerRenderer.close();
    return strResponse.getBuffer();
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) DecoratingHeaderResponse(org.apache.wicket.markup.html.DecoratingHeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) HeaderItem(org.apache.wicket.markup.head.HeaderItem) StringResponse(org.apache.wicket.response.StringResponse)

Aggregations

StringResponse (org.apache.wicket.response.StringResponse)14 Response (org.apache.wicket.request.Response)8 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)7 HeaderResponse (org.apache.wicket.markup.head.internal.HeaderResponse)5 Test (org.junit.Test)5 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)3 IRequestHandler (org.apache.wicket.request.IRequestHandler)2 Request (org.apache.wicket.request.Request)2 WebResponse (org.apache.wicket.request.http.WebResponse)2 Before (org.junit.Before)2 Application (org.apache.wicket.Application)1 Component (org.apache.wicket.Component)1 Behavior (org.apache.wicket.behavior.Behavior)1 PageProvider (org.apache.wicket.core.request.handler.PageProvider)1 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)1 HeaderItem (org.apache.wicket.markup.head.HeaderItem)1 PageHeaderItem (org.apache.wicket.markup.head.PageHeaderItem)1 DecoratingHeaderResponse (org.apache.wicket.markup.html.DecoratingHeaderResponse)1 HtmlHeaderContainer (org.apache.wicket.markup.html.internal.HtmlHeaderContainer)1 IExceptionMapper (org.apache.wicket.request.IExceptionMapper)1