org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage Java Examples

The following examples show how to use org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebSocketTestPage.java    From inception with Apache License 2.0 6 votes vote down vote up
public WebSocketTestPage(TestEnv aTestVars)
{
    testVars = aTestVars;

    // add WebSocketBehaviour to listen to messages
    add(new WebSocketBehavior()
    {

        private static final long serialVersionUID = -4076782377506950851L;

        @Override
        protected void onPush(WebSocketRequestHandler aHandler, IWebSocketPushMessage aMessage)
        {
            assertThat(aMessage).isEqualTo(testVars.getTestMessage());
            super.onPush(aHandler, aMessage);
        }
    });
}
 
Example #2
Source File: AbstractWebSocketProcessor.java    From onedev with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private WebSocketPayload createEventPayload(IWebSocketMessage message, WebSocketRequestHandler handler)
{
	final WebSocketPayload payload;
	if (message instanceof TextMessage)
	{
		payload = new WebSocketTextPayload((TextMessage) message, handler);
	}
	else if (message instanceof BinaryMessage)
	{
		payload = new WebSocketBinaryPayload((BinaryMessage) message, handler);
	}
	else if (message instanceof ConnectedMessage)
	{
		payload = new WebSocketConnectedPayload((ConnectedMessage) message, handler);
	}
	else if (message instanceof ClosedMessage)
	{
		payload = new WebSocketClosedPayload((ClosedMessage) message, handler);
	}
	else if (message instanceof AbortedMessage)
	{
		payload = new WebSocketAbortedPayload((AbortedMessage) message, handler);
	}
	else if (message instanceof IWebSocketPushMessage)
	{
		payload = new WebSocketPushPayload((IWebSocketPushMessage) message, handler);
	}
	else
	{
		throw new IllegalArgumentException("Unsupported message type: " + message.getClass().getName());
	}
	return payload;
}
 
Example #3
Source File: CustomerListPage.java    From wicket-spring-boot with Apache License 2.0 5 votes vote down vote up
public CustomerListPage() {
	FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
	feedbackPanel.setOutputMarkupId(true);
	add(feedbackPanel);
	
	add(new WebSocketBehavior() {

		@Override
		protected void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) {
			if (message instanceof CustomerChangedEvent) {
				CustomerChangedEvent event = (CustomerChangedEvent)message;
				info("changed/created " + event.getCustomer().getFirstname() + " " + event.getCustomer().getLastname());
				handler.add(feedbackPanel);
			}
		}

	});
	
	customerFilterModel = new CompoundPropertyModel<>(new CustomerFilter());
	CustomerDataProvider customerDataProvider = new CustomerDataProvider(customerFilterModel);
	
	queue(new BookmarkablePageLink<Customer>("create", CustomerCreatePage.class));
	
	queue(new ValidationForm<>("form", customerFilterModel));
	queue(new LabeledFormBorder<>(getString("id"), new TextField<>("id")));
	queue(new LabeledFormBorder<>(getString("username"), new UsernameSearchTextField("usernameLike")));
	queue(new LabeledFormBorder<>(getString("firstname"), new TextField<String>("firstnameLike").add(StringValidator.minimumLength(3))));
	queue(new LabeledFormBorder<>(getString("lastname"), new TextField<String>("lastnameLike").add(StringValidator.minimumLength(3))));
	queue(new LabeledFormBorder<>(getString("active"), new CheckBox("active")));
	queue(cancelButton());
	
	customerDataTable(customerDataProvider);

}
 
Example #4
Source File: WebSocketMessageSenderDefault.java    From wicket-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void send(IWebSocketPushMessage event) {
	Application application = Application.get(WicketWebInitializer.WICKET_FILTERNAME);
	WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
	IWebSocketConnectionRegistry connectionRegistry = webSocketSettings.getConnectionRegistry();
	Collection<IWebSocketConnection> connections = connectionRegistry.getConnections(application);
	log.trace("sending event to {} connections", connections.size());
	for (IWebSocketConnection connection : connections) {
		connection.sendMessage(event);
	}
}
 
Example #5
Source File: AbstractWebSocketProcessor.java    From onedev with MIT License 4 votes vote down vote up
/**
 * 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())
	{
		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;
			}

			// Session is null if we copy the url with jsessionid to a new browser window 
			if (session != null) {
				IPageManager pageManager = session.getPageManager();
				Page page = getPage(pageManager);

				if (page != null) {
					WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);

					@SuppressWarnings("rawtypes")
					WebSocketPayload payload = createEventPayload(message, requestHandler);

					if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage))
					{
						requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
					}

					IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
					requestMapper.setHandler(broadcastingHandler);
					requestCycle.processRequestAndDetach();
				}
			}
		}
		catch (Exception x)
		{
			try {
				connection.sendMessage(WebSocketManager.ERROR_MESSAGE);
			} catch (IOException e1) {
			}
			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);
	}
}
 
Example #6
Source File: WebSocketMessageBroadcaster.java    From wicket-spring-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Broadcasts a push message to the Wicket page (and it's components)
 * associated with all connections. The components can then send messages or
 * component updates to client by adding them to the target.
 * 
 * @param event
 */
public void send(IWebSocketPushMessage event);