Repository: wicket Updated Branches: refs/heads/master 1a7998544 -> 97448b58f
WICKET-5452 Make Wicket-Atmosphere testable - AtmosphereTester Quick and dirty implementation of WicketAtmosphereTester (cherry picked from commit 2ab1d798e3348ba7037ab461e1be6df55cd68457) Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/54337592 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/54337592 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/54337592 Branch: refs/heads/master Commit: 54337592c72be89d38c0f8984ecb2d083e91895b Parents: 1a79985 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Tue Aug 12 17:08:30 2014 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Fri Aug 15 10:10:49 2014 +0200 ---------------------------------------------------------------------- .../wicket/atmosphere/AtmosphereBehavior.java | 18 ++--- .../atmosphere/tester/TesterAsyncSupport.java | 70 ++++++++++++++++ .../atmosphere/tester/TesterEventBus.java | 66 +++++++++++++++ .../tester/WicketAtmosphereTester.java | 76 ++++++++++++++++++ .../atmosphere/AtmosphereApplication.java | 79 ++++++++++++++++++ .../wicket/atmosphere/AtmosphereTest.java | 65 +++++++++++++++ .../apache/wicket/atmosphere/ChatMessage.java | 42 ++++++++++ .../org/apache/wicket/atmosphere/HomePage.html | 24 ++++++ .../org/apache/wicket/atmosphere/HomePage.java | 84 ++++++++++++++++++++ .../wicket/atmosphere/ReceiverFilter.java | 40 ++++++++++ 10 files changed, 551 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java index 61d281d..be4a0e0 100644 --- a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java +++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereBehavior.java @@ -24,7 +24,7 @@ import org.apache.wicket.Page; import org.apache.wicket.WicketRuntimeException; import org.apache.wicket.ajax.json.JSONException; import org.apache.wicket.ajax.json.JSONObject; -import org.apache.wicket.behavior.Behavior; +import org.apache.wicket.behavior.AbstractAjaxBehavior; import org.apache.wicket.markup.head.IHeaderResponse; import org.apache.wicket.markup.head.JavaScriptHeaderItem; import org.apache.wicket.markup.head.OnDomReadyHeaderItem; @@ -49,9 +49,8 @@ import org.slf4j.LoggerFactory; * * @author papegaaij */ -public class AtmosphereBehavior extends Behavior +public class AtmosphereBehavior extends AbstractAjaxBehavior implements - IResourceListener, AtmosphereResourceEventListener { private static final Logger log = LoggerFactory.getLogger(AtmosphereBehavior.class); @@ -68,8 +67,6 @@ public class AtmosphereBehavior extends Behavior private String applicationKey; - private Component component; - /** * Construct. @@ -85,12 +82,6 @@ public class AtmosphereBehavior extends Behavior } @Override - public void bind(Component component) - { - this.component = component; - } - - @Override public final boolean getStatelessHint(Component component) { return false; @@ -106,8 +97,9 @@ public class AtmosphereBehavior extends Behavior { } + @Override - public void onResourceRequested() + public void onRequest() { RequestCycle requestCycle = RequestCycle.get(); ServletWebRequest request = (ServletWebRequest)requestCycle.getRequest(); @@ -119,7 +111,7 @@ public class AtmosphereBehavior extends Behavior meteor.suspend(-1); String uuid = meteor.getAtmosphereResource().uuid(); - Page page = component.getPage(); + Page page = getComponent().getPage(); page.setMetaData(ATMOSPHERE_UUID, uuid); findEventBus().registerPage(uuid, page); } http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAsyncSupport.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAsyncSupport.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAsyncSupport.java new file mode 100644 index 0000000..60a3cc0 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterAsyncSupport.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere.tester; + +import java.io.IOException; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + +import org.atmosphere.cpr.Action; +import org.atmosphere.cpr.AsyncSupport; +import org.atmosphere.cpr.AtmosphereRequest; +import org.atmosphere.cpr.AtmosphereResource; +import org.atmosphere.cpr.AtmosphereResponse; + +/** + * + */ +public class TesterAsyncSupport<E extends AtmosphereResource> implements AsyncSupport<E> +{ + @Override + public String getContainerName() + { + return "wicket-atmosphere-tester"; + } + + @Override + public void init(ServletConfig sc) throws ServletException + { + + } + + @Override + public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException + { + return Action.CONTINUE; + } + + @Override + public void action(E actionEvent) + { + System.err.println("TesterEventSupport#action(): " + actionEvent); + } + + @Override + public boolean supportWebSocket() + { + return true; + } + + @Override + public AsyncSupport complete(E r) + { + return this; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java new file mode 100644 index 0000000..8f30a37 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/TesterEventBus.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere.tester; + +import org.apache.wicket.application.IComponentOnBeforeRenderListener; +import org.apache.wicket.atmosphere.EventBus; +import org.apache.wicket.protocol.http.WebApplication; +import org.atmosphere.cpr.AtmosphereConfig; +import org.atmosphere.cpr.AtmosphereFramework; +import org.atmosphere.cpr.BroadcasterLifeCyclePolicy; +import org.atmosphere.cpr.DefaultBroadcasterFactory; +import org.atmosphere.util.SimpleBroadcaster; + +/** + * + */ +public class TesterEventBus extends EventBus +{ + AtmosphereFramework framework = new AtmosphereFramework(); + AtmosphereConfig config = new AtmosphereConfig(framework); + IComponentOnBeforeRenderListener eventSubscriptionCollector; + + public TesterEventBus(WebApplication application) + { + super(application, new SimpleBroadcaster()); + + framework.setBroadcasterFactory(new TesterBroadcasterFactory(config)); + + getBroadcaster().initialize("wicket-atmopshere-tester", config); + } + + @Override + protected IComponentOnBeforeRenderListener createEventSubscriptionCollector() + { + eventSubscriptionCollector = super.createEventSubscriptionCollector(); + return eventSubscriptionCollector; + } + + @Override + public SimpleBroadcaster getBroadcaster() + { + return (SimpleBroadcaster) super.getBroadcaster(); + } + + private static class TesterBroadcasterFactory extends DefaultBroadcasterFactory + { + protected TesterBroadcasterFactory(AtmosphereConfig c) + { + super(SimpleBroadcaster.class, BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.NEVER.name(), c); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/WicketAtmosphereTester.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/WicketAtmosphereTester.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/WicketAtmosphereTester.java new file mode 100644 index 0000000..9ae8ad6 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/tester/WicketAtmosphereTester.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere.tester; + +import org.apache.wicket.Page; +import org.apache.wicket.atmosphere.AtmosphereBehavior; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.util.tester.WicketTester; +import org.atmosphere.cpr.AtmosphereRequest; +import org.atmosphere.cpr.AtmosphereResource; +import org.atmosphere.cpr.AtmosphereResourceImpl; +import org.atmosphere.cpr.AtmosphereResponse; +import org.atmosphere.handler.AtmosphereHandlerAdapter; +import org.atmosphere.util.SimpleBroadcaster; + +/** + * + */ +public class WicketAtmosphereTester +{ + private final TesterEventBus eventBus; + + public WicketAtmosphereTester(final WicketTester wicketTester, Page page) + { + WebApplication application = wicketTester.getApplication(); + this.eventBus = new TesterEventBus(application); + + AtmosphereBehavior atmosphereBehavior = new AtmosphereBehavior() + { + @Override + public void onRequest() + { + SimpleBroadcaster broadcaster = eventBus.getBroadcaster(); + + AtmosphereResource atmosphereResource = new AtmosphereResourceImpl(); + AtmosphereRequest atmosphereRequest = AtmosphereRequest.wrap(wicketTester.getRequest()); + AtmosphereResponse atmosphereResponse = AtmosphereResponse.wrap(wicketTester.getResponse()); + TesterAsyncSupport asyncSupport = new TesterAsyncSupport(); + atmosphereResource.initialize(eventBus.config, broadcaster, atmosphereRequest, atmosphereResponse, + asyncSupport, new AtmosphereHandlerAdapter()); + + atmosphereResource.setBroadcaster(broadcaster); + broadcaster.addAtmosphereResource(atmosphereResource); + + String uuid = atmosphereResource.uuid(); + Page page = getComponent().getPage(); + eventBus.eventSubscriptionCollector.onBeforeRender(page); + page.setMetaData(ATMOSPHERE_UUID, uuid); + eventBus.registerPage(uuid, page); + } + }; + page.add(atmosphereBehavior); + + wicketTester.executeBehavior(atmosphereBehavior); + } + + public WicketAtmosphereTester post(Object payload) + { + eventBus.post(payload); + return this; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereApplication.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereApplication.java b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereApplication.java new file mode 100644 index 0000000..aca6e62 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereApplication.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere; + +import java.util.Date; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.wicket.Application; +import org.apache.wicket.atmosphere.config.AtmosphereLogLevel; +import org.apache.wicket.atmosphere.config.AtmosphereTransport; +import org.apache.wicket.protocol.http.WebApplication; + +/** + * Application object for your web application. If you want to run this application without + * deploying, run the Start class. + */ +public class AtmosphereApplication extends WebApplication +{ + private EventBus eventBus; + + @Override + public Class<HomePage> getHomePage() + { + return HomePage.class; + } + + public EventBus getEventBus() + { + return eventBus; + } + + public static AtmosphereApplication get() + { + return (AtmosphereApplication)Application.get(); + } + + @Override + public void init() + { + super.init(); + eventBus = new EventBus(this); + eventBus.getParameters().setTransport(AtmosphereTransport.STREAMING); + eventBus.getParameters().setLogLevel(AtmosphereLogLevel.DEBUG); + + ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + final Runnable beeper = new Runnable() + { + @Override + public void run() + { + try + { + eventBus.post(new Date()); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + }; + scheduler.scheduleWithFixedDelay(beeper, 2, 2, TimeUnit.SECONDS); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java new file mode 100644 index 0000000..5accac5 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/AtmosphereTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere; + +import java.util.Date; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.atmosphere.tester.WicketAtmosphereTester; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class AtmosphereTest extends Assert +{ + @Test + public void aa() + { + WicketTester tester = new WicketTester(); + HomePage page = new HomePage(new PageParameters()) + { + @Subscribe + public void updateTime(AjaxRequestTarget target, Date event) + { + super.updateTime(target, event); + + System.err.println("updateTime"); + } + + @Subscribe(contextAwareFilter = ReceiverFilter.class) + public void receiveMessage(AjaxRequestTarget target, ChatMessage message) + { + super.receiveMessage(target, message); + + System.err.println("receiveMessage"); + } + }; + + tester.startPage(page); + + WicketAtmosphereTester waTester = new WicketAtmosphereTester(tester, page); + + Date payload = new Date(); + waTester.post(payload); + + tester.destroy(); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ChatMessage.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ChatMessage.java b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ChatMessage.java new file mode 100644 index 0000000..c59039f --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ChatMessage.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere; + +import java.io.Serializable; + +public class ChatMessage implements Serializable +{ + + private String receiver; + private String message; + + public ChatMessage(String receiver, String message) + { + this.receiver = receiver; + this.message = message; + } + + public String getReceiver() + { + return receiver; + } + + public String getMessage() + { + return message; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.html ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.html b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.html new file mode 100644 index 0000000..a3e4dac --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.html @@ -0,0 +1,24 @@ +<!DOCTYPE html> +<html xmlns:wicket="http://wicket.apache.org"> +<head> +<title>Wicket Examples - atmosphere</title> +<link rel="stylesheet" type="text/css" href="style.css" /> +</head> +<body> + <p> + The current time is (updated every 2 seconds from the server using push): <span wicket:id="time"></span> + </p> + <p>This page also shows a very simple 'chat' client. Type a message in the + second input field and send it to everyone. To send a private message, paste + the session id (you can find this in the session cookie) of the receiver in + the first input field and send a message.</p> + <p> + Message received: <span wicket:id="message"></span> + </p> + <form wicket:id="form"> + <div>Session id of receiver: <input wicket:id="receiver" /></div> + Message: <input wicket:id="input" /> <a href="#" wicket:id="send">Send + message</a> + </form> +</body> +</html> http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.java b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.java new file mode 100644 index 0000000..34063c7 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/HomePage.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere; + +import java.util.Date; + +import org.apache.wicket.Component; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.form.Form; +import org.apache.wicket.markup.html.form.TextField; +import org.apache.wicket.model.Model; +import org.apache.wicket.request.mapper.parameter.PageParameters; + +public class HomePage extends WebPage +{ + private static final long serialVersionUID = 1L; + + private Component timeLabel; + private Component messageLabel; + private TextField<String> receiver; + private TextField<String> input; + + public HomePage(final PageParameters parameters) + { + super(parameters); + + add(timeLabel = new Label("time", Model.of("start")).setOutputMarkupId(true)); + add(messageLabel = new Label("message", Model.of("-")).setOutputMarkupId(true)); + + Form<Void> form = new Form<Void>("form"); + add(form); + form.add(receiver = new TextField<String>("receiver", Model.of(""))); + form.add(input = new TextField<String>("input", Model.of(""))); + form.add(new AjaxSubmitLink("send", form) + { + private static final long serialVersionUID = 1L; + + @Override + protected void onSubmit(AjaxRequestTarget target, Form<?> form) + { + EventBus.get().post( + new ChatMessage(receiver.getModelObject(), input.getModelObject())); + } + + @Override + protected void onError(AjaxRequestTarget target, Form<?> form) + { + } + }); + + setVersioned(false); + } + + @Subscribe + public void updateTime(AjaxRequestTarget target, Date event) + { + timeLabel.setDefaultModelObject(event.toString()); + target.add(timeLabel); + } + + @Subscribe(contextAwareFilter = ReceiverFilter.class) + public void receiveMessage(AjaxRequestTarget target, ChatMessage message) + { + messageLabel.setDefaultModelObject(message.getMessage()); + target.add(messageLabel); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/54337592/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ReceiverFilter.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ReceiverFilter.java b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ReceiverFilter.java new file mode 100644 index 0000000..f035433 --- /dev/null +++ b/wicket-experimental/wicket-atmosphere/src/test/java/org/apache/wicket/atmosphere/ReceiverFilter.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.atmosphere; + +import org.apache.wicket.Session; + +import com.google.common.base.Predicate; + +public class ReceiverFilter implements Predicate<AtmosphereEvent> +{ + public ReceiverFilter() + { + } + + @Override + public boolean apply(AtmosphereEvent input) + { + if (input.getPayload() instanceof ChatMessage) + { + ChatMessage msg = (ChatMessage)input.getPayload(); + return msg.getReceiver() == null || msg.getReceiver().isEmpty() || + msg.getReceiver().equals(Session.get().getId()); + } + return false; + } +}
