Search in sources :

Example 31 with WebResponse

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

the class RedirectRequestHandler method respond.

public void respond(final IRequestCycle requestCycle) {
    final String location;
    final String url = getRedirectUrl();
    if (url.charAt(0) == '/') {
        // context-absolute url
        location = requestCycle.getUrlRenderer().renderContextRelativeUrl(url);
    } else {
        // if relative url, servlet container will translate to absolute as
        // per the servlet spec
        // if absolute url still do the same
        location = url;
    }
    WebResponse response = (WebResponse) requestCycle.getResponse();
    if (status == HttpServletResponse.SC_MOVED_TEMPORARILY) {
        response.sendRedirect(location);
    } else {
        response.setStatus(status);
        response.setHeader("Location", location);
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse)

Example 32 with WebResponse

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

the class RedirectRequestHandlerTest method seeOtherShouldSetLocationHeader.

/**
 * https://issues.apache.org/jira/browse/WICKET-5131
 */
@Test
public void seeOtherShouldSetLocationHeader() {
    RedirectRequestHandler handler = new RedirectRequestHandler(REDIRECT_URL, HttpServletResponse.SC_SEE_OTHER);
    IRequestCycle requestCycle = Mockito.mock(IRequestCycle.class);
    WebResponse webResponse = Mockito.mock(WebResponse.class);
    Mockito.when(requestCycle.getResponse()).thenReturn(webResponse);
    handler.respond(requestCycle);
    Mockito.verify(webResponse).setStatus(HttpServletResponse.SC_SEE_OTHER);
    Mockito.verify(webResponse).setHeader("Location", REDIRECT_URL);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestCycle(org.apache.wicket.request.IRequestCycle) Test(org.junit.Test)

Example 33 with WebResponse

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

the class TextRequestHandler method respond.

/**
 * Responds by sending the string property.
 *
 * @see org.apache.wicket.request.IRequestHandler#respond(org.apache.wicket.request.IRequestCycle)
 */
@Override
public void respond(final IRequestCycle requestCycle) {
    String encoding = getEncoding(requestCycle);
    // Get servlet response to use when responding with resource
    final WebResponse response = (WebResponse) requestCycle.getResponse();
    response.setContentType(contentType + ";charset=" + encoding);
    // send string to client
    try {
        byte[] bytes = string.getBytes(encoding);
        response.setContentLength(bytes.length);
        response.write(bytes);
    } catch (IOException e) {
        throw new RuntimeException("Unable to render string: " + e.getMessage(), e);
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IOException(java.io.IOException)

Example 34 with WebResponse

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

the class Application method init.

@Override
protected void init() {
    setWicketApplicationName(super.getName());
    getSecuritySettings().setAuthenticationStrategy(new OmAuthenticationStrategy());
    getApplicationSettings().setAccessDeniedPage(AccessDeniedPage.class);
    getComponentInstantiationListeners().add(new SpringComponentInjector(this, ctx, true));
    hazelcast.getCluster().getLocalMember().setStringAttribute(NAME_ATTR_KEY, hazelcast.getName());
    hazelWsTopic = hazelcast.getTopic("default");
    hazelWsTopic.addMessageListener(msg -> {
        String serverId = msg.getPublishingMember().getStringAttribute(NAME_ATTR_KEY);
        if (serverId.equals(hazelcast.getName())) {
            return;
        }
        WbWebSocketHelper.send(msg.getMessageObject());
    });
    hazelcast.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberRemoved(MembershipEvent evt) {
            // server down, need to remove all online clients, process persistent addresses
            String serverId = evt.getMember().getStringAttribute(NAME_ATTR_KEY);
            cm.clean(serverId);
            scm.clean(serverId);
            updateJpaAddresses();
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent evt) {
        // no-op
        }

        @Override
        public void memberAdded(MembershipEvent evt) {
            // server added, need to process persistent addresses
            updateJpaAddresses();
            // check for duplicate instance-names
            Set<String> names = new HashSet<>();
            for (Member m : evt.getMembers()) {
                if (evt.getMember().getUuid().equals(m.getUuid())) {
                    continue;
                }
                String serverId = m.getStringAttribute(NAME_ATTR_KEY);
                names.add(serverId);
            }
            String serverId = evt.getMember().getStringAttribute(NAME_ATTR_KEY);
            if (names.contains(serverId)) {
                log.warn("Duplicate cluster instance with name {} found {}", serverId, evt.getMember());
            }
        }
    });
    setPageManagerProvider(new DefaultPageManagerProvider(this) {

        @Override
        protected IDataStore newDataStore() {
            return new HazelcastDataStore(hazelcast);
        }
    });
    // Add custom resource loader at the beginning, so it will be checked first in the
    // chain of Resource Loaders, if not found it will search in Wicket's internal
    // Resource Loader for a the property key
    getResourceSettings().getStringResourceLoaders().add(0, new LabelResourceLoader());
    getJavaScriptLibrarySettings().setJQueryReference(getV3());
    getRequestCycleListeners().add(new WebSocketAwareCsrfPreventionRequestCycleListener() {

        @Override
        public void onEndRequest(RequestCycle cycle) {
            Response resp = cycle.getResponse();
            if (resp instanceof WebResponse && !(resp instanceof WebSocketResponse)) {
                WebResponse wresp = (WebResponse) resp;
                wresp.setHeader("X-XSS-Protection", "1; mode=block");
                wresp.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload");
                wresp.setHeader("X-Content-Type-Options", "nosniff");
                wresp.setHeader("X-Frame-Options", xFrameOptions);
                Url reqUrl = cycle.getRequest().getUrl();
                wresp.setHeader("Content-Security-Policy", String.format("%s; connect-src 'self' %s;", contentSecurityPolicy, getWsUrl(reqUrl)));
            }
        }
    });
    super.init();
    // register some widgets
    dashboardContext = new DashboardContext();
    dashboardContext.setDashboardPersister(new UserDashboardPersister());
    WidgetRegistry widgetRegistry = dashboardContext.getWidgetRegistry();
    widgetRegistry.registerWidget(new MyRoomsWidgetDescriptor());
    widgetRegistry.registerWidget(new RecentRoomsWidgetDescriptor());
    widgetRegistry.registerWidget(new WelcomeWidgetDescriptor());
    widgetRegistry.registerWidget(new StartWidgetDescriptor());
    widgetRegistry.registerWidget(new RssWidgetDescriptor());
    widgetRegistry.registerWidget(new AdminWidgetDescriptor());
    // add dashboard context injector
    getComponentInstantiationListeners().add(new DashboardContextInjector(dashboardContext));
    DashboardSettings dashboardSettings = DashboardSettings.get();
    dashboardSettings.setIncludeJQueryUI(false);
    getRootRequestMapperAsCompound().add(new NoVersionMapper(getHomePage()));
    getRootRequestMapperAsCompound().add(new NoVersionMapper(NOTINIT_MAPPING, NotInitedPage.class));
    getRootRequestMapperAsCompound().add(new NoVersionMapper(HASH_MAPPING, HashPage.class));
    getRootRequestMapperAsCompound().add(new NoVersionMapper(SIGNIN_MAPPING, getSignInPageClass()));
    getRootRequestMapperAsCompound().add(new NoVersionMapper("oauth/${oauthid}", getSignInPageClass()));
    mountPage("install", InstallWizardPage.class);
    mountPage("activate", ActivatePage.class);
    mountPage("reset", ResetPage.class);
    mountResource("/recordings/mp4/${id}", new Mp4RecordingResourceReference());
    // should be in sync with VideoPlayer
    mountResource("/recordings/png/${id}", new PngRecordingResourceReference());
    mountResource("/room/file/${id}", new RoomResourceReference());
    mountResource("/room/preview/${id}", new RoomPreviewResourceReference());
    mountResource("/profile/${id}", new ProfileImageResourceReference());
    mountResource("/group/${id}", new GroupLogoResourceReference());
    log.debug("Application::init");
    try {
        if (OmFileHelper.getOmHome() == null) {
            OmFileHelper.setOmHome(new File(getServletContext().getRealPath("/")));
        }
        LabelDao.initLanguageMap();
        log.debug("webAppPath : {}", OmFileHelper.getOmHome());
        // Init all global config properties
        cfgDao.reinit();
        // Init properties
        setXFrameOptions(cfgDao.getString(CONFIG_HEADER_XFRAME, HEADER_XFRAME_SAMEORIGIN));
        setContentSecurityPolicy(cfgDao.getString(CONFIG_HEADER_CSP, HEADER_CSP_SELF));
        updateJpaAddresses();
        setExtProcessTtl(cfgDao.getInt(CONFIG_EXT_PROCESS_TTL, getExtProcessTtl()));
        Version.logOMStarted();
        // we are starting so all processing recordings are now errors
        recordingDao.resetProcessingStatus();
        setInitComplete(true);
    } catch (Exception err) {
        log.error("[appStart]", err);
    }
}
Also used : DashboardContext(org.wicketstuff.dashboard.web.DashboardContext) ProfileImageResourceReference(org.apache.openmeetings.web.util.ProfileImageResourceReference) Set(java.util.Set) HashSet(java.util.HashSet) MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) DashboardSettings(org.wicketstuff.dashboard.web.DashboardSettings) MembershipEvent(com.hazelcast.core.MembershipEvent) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) RecentRoomsWidgetDescriptor(org.apache.openmeetings.web.user.dashboard.RecentRoomsWidgetDescriptor) Url(org.apache.wicket.request.Url) OpenmeetingsVariables.getBaseUrl(org.apache.openmeetings.util.OpenmeetingsVariables.getBaseUrl) MyRoomsWidgetDescriptor(org.apache.openmeetings.web.user.dashboard.MyRoomsWidgetDescriptor) HazelcastDataStore(org.wicketstuff.datastores.hazelcast.HazelcastDataStore) DashboardContextInjector(org.wicketstuff.dashboard.web.DashboardContextInjector) Member(com.hazelcast.core.Member) RoomPreviewResourceReference(org.apache.openmeetings.web.room.RoomPreviewResourceReference) IDataStore(org.apache.wicket.pageStore.IDataStore) WebResponse(org.apache.wicket.request.http.WebResponse) WelcomeWidgetDescriptor(org.apache.openmeetings.web.user.dashboard.WelcomeWidgetDescriptor) AdminWidgetDescriptor(org.apache.openmeetings.web.user.dashboard.admin.AdminWidgetDescriptor) RssWidgetDescriptor(org.apache.openmeetings.web.user.dashboard.RssWidgetDescriptor) Mp4RecordingResourceReference(org.apache.openmeetings.web.user.record.Mp4RecordingResourceReference) NotInitedPage(org.apache.openmeetings.web.pages.NotInitedPage) RoomResourceReference(org.apache.openmeetings.web.room.RoomResourceReference) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) UnknownHostException(java.net.UnknownHostException) WebSocketAwareCsrfPreventionRequestCycleListener(org.apache.wicket.protocol.ws.WebSocketAwareCsrfPreventionRequestCycleListener) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebSocketResponse(org.apache.wicket.protocol.ws.api.WebSocketResponse) HashPage(org.apache.openmeetings.web.pages.HashPage) StartWidgetDescriptor(org.apache.openmeetings.web.user.dashboard.StartWidgetDescriptor) GroupLogoResourceReference(org.apache.openmeetings.web.util.GroupLogoResourceReference) WidgetRegistry(org.wicketstuff.dashboard.WidgetRegistry) SpringComponentInjector(org.apache.wicket.spring.injection.annot.SpringComponentInjector) MembershipListener(com.hazelcast.core.MembershipListener) WebSocketResponse(org.apache.wicket.protocol.ws.api.WebSocketResponse) PngRecordingResourceReference(org.apache.openmeetings.web.user.record.PngRecordingResourceReference) File(java.io.File) UserDashboardPersister(org.apache.openmeetings.web.util.UserDashboardPersister) DefaultPageManagerProvider(org.apache.wicket.DefaultPageManagerProvider)

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