Search in sources :

Example 1 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class JavaSerializer method deserialize.

@Override
public Object deserialize(final byte[] data) {
    ThreadContext old = ThreadContext.get(false);
    final ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream ois = null;
    try {
        Application oldApplication = ThreadContext.getApplication();
        try {
            ois = newObjectInputStream(in);
            String applicationName = (String) ois.readObject();
            if (applicationName != null) {
                Application app = Application.get(applicationName);
                if (app != null) {
                    ThreadContext.setApplication(app);
                }
            }
            return ois.readObject();
        } finally {
            try {
                ThreadContext.setApplication(oldApplication);
                IOUtils.close(ois);
            } finally {
                in.close();
            }
        }
    } catch (ClassNotFoundException | IOException cnfx) {
        throw new RuntimeException("Could not deserialize object from byte[]", cnfx);
    } finally {
        ThreadContext.restore(old);
    }
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) ThreadContext(org.apache.wicket.ThreadContext) IOException(java.io.IOException) Application(org.apache.wicket.Application) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class WicketFilterTest method options.

@Test
public void options() throws IOException, ServletException, ParseException {
    try {
        application = new MockApplication();
        WicketFilter filter = new WicketFilter();
        filter.init(new FilterTestingConfig());
        ThreadContext.setApplication(application);
        final String failure = "Should never get here when an OPTIONS request is issued";
        IResource resource = new AbstractResource() {

            @Override
            protected ResourceResponse newResourceResponse(Attributes attributes) {
                fail(failure);
                return null;
            }
        };
        application.getSharedResources().add("foo.txt", resource);
        // check OPTIONS request is processed correctly
        MockHttpServletRequest request = new MockHttpServletRequest(application, null, null);
        request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.txt");
        // test that we do not care about case
        request.setMethod("OPtioNS");
        MockHttpServletResponse response = new MockHttpServletResponse(request);
        filter.doFilter(request, response, new FilterChain() {

            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            }
        });
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        assertEquals("0", response.getHeader("Content-Length"));
        assertFalse(Strings.isEmpty(response.getHeader("Allow")));
        assertTrue(response.getHeader("Allow").toUpperCase().contains("GET"));
        assertTrue(response.getHeader("Allow").toUpperCase().contains("POST"));
        // try with a GET request to make sure we fail correctly
        request = new MockHttpServletRequest(application, null, null);
        request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.txt");
        response = new MockHttpServletResponse(request);
        try {
            filter.doFilter(request, response, new FilterChain() {

                @Override
                public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
                }
            });
        } catch (AssertionError e) {
            assertTrue(failure.equals(e.getMessage()));
        }
    } finally {
        ThreadContext.detach();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockApplication(org.apache.wicket.mock.MockApplication) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) IResource(org.apache.wicket.request.resource.IResource) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class MetaDataRoleAuthorizationStrategy method authorizeAll.

/**
 * Grants permission to all roles to create instances of the given component class.
 *
 * @param <T>
 *
 * @param componentClass
 *            The component class
 */
public static <T extends Component> void authorizeAll(final Class<T> componentClass) {
    Application application = Application.get();
    InstantiationPermissions authorizedRoles = application.getMetaData(INSTANTIATION_PERMISSIONS);
    if (authorizedRoles != null) {
        authorizedRoles.authorizeAll(componentClass);
    }
}
Also used : Application(org.apache.wicket.Application)

Example 4 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class MetaDataRoleAuthorizationStrategy method authorize.

/**
 * Authorizes the given role to create component instances of type componentClass. This
 * authorization is added to any previously authorized roles.
 *
 * @param <T>
 *
 * @param componentClass
 *            The component type that is subject for the authorization
 * @param roles
 *            The comma separated roles that are authorized to create component instances of
 *            type componentClass
 */
public static <T extends Component> void authorize(final Class<T> componentClass, final String roles) {
    final Application application = Application.get();
    InstantiationPermissions permissions = application.getMetaData(INSTANTIATION_PERMISSIONS);
    if (permissions == null) {
        permissions = new InstantiationPermissions();
        application.setMetaData(INSTANTIATION_PERMISSIONS, permissions);
    }
    permissions.authorize(componentClass, new Roles(roles));
}
Also used : Roles(org.apache.wicket.authroles.authorization.strategies.role.Roles) Application(org.apache.wicket.Application)

Example 5 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class AbstractWebSocketProcessor method broadcastMessage.

/**
 * Exports the Wicket thread locals and broadcasts the received message from the client to all
 * interested components and behaviors in the page with id {@code #pageId}
 * <p>
 *     Note: ConnectedMessage and ClosedMessage messages are notification-only. I.e. whatever the
 *     components/behaviors write in the WebSocketRequestHandler will be ignored because the protocol
 *     doesn't expect response from the user.
 * </p>
 *
 * @param message
 *      the message to broadcast
 */
public final void broadcastMessage(final IWebSocketMessage message) {
    IKey key = getRegistryKey();
    IWebSocketConnection connection = connectionRegistry.getConnection(application, sessionId, key);
    if (connection != null && (connection.isOpen() || message instanceof ClosedMessage)) {
        Application oldApplication = ThreadContext.getApplication();
        Session oldSession = ThreadContext.getSession();
        RequestCycle oldRequestCycle = ThreadContext.getRequestCycle();
        WebResponse webResponse = webSocketSettings.newWebSocketResponse(connection);
        try {
            WebSocketRequestMapper requestMapper = new WebSocketRequestMapper(application.getRootRequestMapper());
            RequestCycle requestCycle = createRequestCycle(requestMapper, webResponse);
            ThreadContext.setRequestCycle(requestCycle);
            ThreadContext.setApplication(application);
            Session session;
            if (oldSession == null || message instanceof IWebSocketPushMessage) {
                ISessionStore sessionStore = application.getSessionStore();
                session = sessionStore.lookup(webRequest);
                ThreadContext.setSession(session);
            } else {
                session = oldSession;
            }
            IPageManager pageManager = session.getPageManager();
            Page page = getPage(pageManager);
            if (page != null) {
                WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);
                WebSocketPayload payload = createEventPayload(message, requestHandler);
                if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage || message instanceof AbortedMessage)) {
                    requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
                }
                IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
                requestMapper.setHandler(broadcastingHandler);
                requestCycle.processRequestAndDetach();
            } else {
                LOG.debug("Page with id '{}' has been expired. No message will be broadcast!", pageId);
            }
        } catch (Exception x) {
            LOG.error("An error occurred during processing of a WebSocket message", x);
        } finally {
            try {
                webResponse.close();
            } finally {
                ThreadContext.setApplication(oldApplication);
                ThreadContext.setRequestCycle(oldRequestCycle);
                ThreadContext.setSession(oldSession);
            }
        }
    } else {
        LOG.debug("Either there is no connection({}) or it is closed.", connection);
    }
}
Also used : IPageManager(org.apache.wicket.page.IPageManager) WebResponse(org.apache.wicket.request.http.WebResponse) ISessionStore(org.apache.wicket.session.ISessionStore) IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ClosedMessage(org.apache.wicket.protocol.ws.api.message.ClosedMessage) Page(org.apache.wicket.Page) WebPage(org.apache.wicket.markup.html.WebPage) WebSocketPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPayload) IWebSocketPushMessage(org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) IKey(org.apache.wicket.protocol.ws.api.registry.IKey) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.wicket.Session)

Aggregations

Application (org.apache.wicket.Application)21 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)5 WebApplication (org.apache.wicket.protocol.http.WebApplication)5 IOException (java.io.IOException)4 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)4 MockApplication (org.apache.wicket.mock.MockApplication)3 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)3 FilterChain (javax.servlet.FilterChain)2 ServletException (javax.servlet.ServletException)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 IApplication (org.apache.openmeetings.IApplication)2 Page (org.apache.wicket.Page)2 WebSocketSettings (org.apache.wicket.protocol.ws.WebSocketSettings)2 IKey (org.apache.wicket.protocol.ws.api.registry.IKey)2 IRequestHandler (org.apache.wicket.request.IRequestHandler)2 WebResponse (org.apache.wicket.request.http.WebResponse)2