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);
}
}
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();
}
}
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);
}
}
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));
}
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);
}
}
Aggregations