Andy Moncsek avatar Andy Moncsek committed 142695a

update TwitterEndpoint, delete entities

Comments (0)

Files changed (7)

JacpFXTwitterWebsocketClient/src/main/java/org/jacp/twitter/callbacks/SSEEndpoint.java

-/************************************************************************
- *
- * Copyright (C) 2010 - 2012
- *
- * [SSEEndpoint.java]
- * AHCP Project (http://jacp.googlecode.com)
- * All rights reserved.
- *
- * Licensed 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.jacp.twitter.callbacks;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import javafx.event.Event;
-import org.jacp.api.action.IAction;
-import org.jacp.api.annotations.CallbackComponent;
-import org.jacp.api.annotations.OnStart;
-import org.jacp.api.annotations.OnTearDown;
-import org.jacp.javafx.rcp.component.AStatefulCallbackComponent;
-import org.jacp.jee.entity.TwitterResult;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.logging.Logger;
-
-
-/**
- * A Stateful JacpFX component.
- *
- * @author <a href="mailto:amo.ahcp@gmail.com"> Andy Moncsek</a>
- */
-@CallbackComponent(id = "id003", name = "statefulCallback", active = true)
-public class SSEEndpoint extends AStatefulCallbackComponent {
-//    private Client client;
-//    private EventSource eventSource;
-    private final Logger log = Logger.getLogger(SSEEndpoint.class
-            .getName());
-
-    @Override
-    public Object handleAction(final IAction<Event, Object> arg0) {
-        return null;
-    }
-
-
-
-    @OnStart
-    public void init() {
-//        this.log.info("SSEEndpoint start");
-//        SslConfigurator sslConfig = SslConfigurator.newInstance()
-//                .trustStoreFile("NONE")
-//                .keyStoreFile("NONE");
-//
-//        client = ClientBuilder.newBuilder().sslContext(sslConfig.createSSLContext()).build();
-//        final javax.ws.rs.client.WebTarget webTarget = client.target(getURI());
-//        eventSource = new EventSource(webTarget) {
-//            @Override
-//            public void onEvent(InboundEvent inboundEvent) {
-//                //get the JSON data and parse it
-//                Gson parser = new GsonBuilder()
-//                        .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
-//                try {
-//                    log.info("got message");
-//                    TwitterResult result=  parser.fromJson(inboundEvent.getData(String.class,
-//                                MediaType.APPLICATION_JSON_TYPE), TwitterResult.class);
-//                    log.info("send message");
-//                    getActionListener("id001",result).performAction(null);
-//                } catch (IOException e) {
-//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
-//                }
-//
-//            }
-//        };
-    }
-
-
-
-
-    @OnTearDown
-    public void cleanup() {
-//        eventSource.close();
-//        client.close();
-        this.log.info("SSEEndpoint stop");
-
-    }
-
-    private URI getURI() {
-        try {
-            //return new URI("http", null, "192.168.32.130", 80, "/WebClient-SSE/twittersse", null, null);
-           return new URI("http", null, "localhost", 8080, "/WebClient-SSE/twittersse", null, null);
-        } catch (URISyntaxException e) {
-            e.printStackTrace();
-            return null;
-        }
-    }
-
-}

JacpFXTwitterWebsocketClient/src/main/java/org/jacp/twitter/callbacks/TwitterEndpointComponent.java

+/************************************************************************
+ *
+ * Copyright (C) 2010 - 2012
+ *
+ * [TwitterEndpointComponent.java]
+ * AHCP Project (http://jacp.googlecode.com)
+ * All rights reserved.
+ *
+ * Licensed 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.jacp.twitter.callbacks;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import javafx.event.Event;
+import org.glassfish.tyrus.client.ClientManager;
+import org.jacp.api.action.IAction;
+import org.jacp.api.annotations.CallbackComponent;
+import org.jacp.api.annotations.OnStart;
+import org.jacp.api.annotations.OnTearDown;
+import org.jacp.javafx.rcp.component.AStatefulCallbackComponent;
+import org.jacp.jee.entity.Query;
+import org.jacp.jee.entity.TwitterResult;
+
+
+import javax.websocket.*;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
+
+
+/**
+ * A Stateful JacpFX component which acts as a WebSocket ClientEndpoint. It receives queries from UI and sends them to Server and it can receive TwitterResults from Server and pass them to UI
+ *
+ * @author <a href="mailto:amo.ahcp@gmail.com"> Andy Moncsek</a>
+ */
+@ClientEndpoint
+@CallbackComponent(id = "id003", name = "twitterEndpointComponent", active = false)
+public class TwitterEndpointComponent extends AStatefulCallbackComponent {
+    private Session session;
+    private CountDownLatch messageLatch = new CountDownLatch(1);
+    private final Logger log = Logger.getLogger(TwitterEndpointComponent.class
+            .getName());
+
+    @Override
+    public Object handleAction(final IAction<Event, Object> arg0) {
+        if (arg0.getLastMessage() instanceof Query) try {
+            sendQuery((Query) arg0.getLastMessage());
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (EncodeException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+
+    @OnStart
+    /**
+     * JacpFX lifecycle
+     */
+    public void init() {
+        this.log.info("WebSocketEndpoint start");
+        ClientManager client = ClientManager.createClient();
+        try {
+            session = client.connectToServer(this, ClientEndpointConfig.Builder.create().build(), getURI());
+            messageLatch.await(5, TimeUnit.SECONDS);
+        } catch (DeploymentException e) {
+            e.printStackTrace();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    @OnTearDown
+    /**
+     * JacpFX lifecycle
+     */
+    public void cleanup() {
+        this.log.info("WebSocketEndpoint stop");
+        try {
+            if(session!=null)session.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @OnOpen
+    /**
+     * WebSocket lifecycle
+     */
+    public void onOpen(Session session) {
+        this.session = session;
+        messageLatch.countDown();
+    }
+
+    @OnMessage
+    /**
+     * WebSocket lifecycle
+     */
+    public void onTwitterMessage(TwitterResult result) {
+        this.getActionListener("id001", result);
+    }
+
+    private void sendQuery(Query query) throws IOException, EncodeException {
+        session.getBasicRemote().sendObject(query);
+    }
+
+
+    private URI getURI() {
+        try {
+            return new URI("ws", null, "localhost", 8080, "/WebClient-TwitterWebsocket/twitter", null, null);
+        } catch (URISyntaxException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+
+}

JacpFXTwitterWebsocketClient/src/main/java/org/jacp/twitter/components/QueryView.java

 import org.jacp.javafx.rcp.component.AFXComponent;
 import org.jacp.javafx.rcp.componentLayout.FXComponentLayout;
 import org.jacp.javafx.rcp.util.FXUtil.MessageUtil;
+import org.jacp.jee.entity.Query;
 import org.jacp.twitter.entities.Clear;
-import org.jacp.twitter.entities.Query;
 
 import java.net.URI;
 import java.net.URISyntaxException;
  */
 @Component(defaultExecutionTarget = "Ptop", id = "id002", name = "componentRight", active = true, resourceBundleLocation = "bundles.languageBundle", localeID = "en_US")
 public class QueryView extends AFXComponent {
-    private final static String TARGET_URI = "http://localhost:8080/WebClient-SSE";
     private final Logger log = Logger.getLogger(QueryView.class
             .getName());
 
         button.setOnAction(new EventHandler<ActionEvent>() {
             @Override
             public void handle(ActionEvent actionEvent) {
-                getActionListener(new Query(textField.getText())).performAction(actionEvent);
-                getActionListener("id001",new Clear()).performAction(actionEvent);
-
+                getActionListener("id003",new Query(textField.getText())).performAction(actionEvent);
             }
         });
         hbox.getChildren().addAll(label,textField,button);

JacpFXTwitterWebsocketClient/src/main/java/org/jacp/twitter/entities/Clear.java

-/*
- * Copyright (c) 2013, Andy Moncsek, inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided
- * that the following conditions are met:
- *
- *    Redistributions of source code must retain the above copyright notice, this list of conditions and the
- *    following disclaimer.
- *
- *    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
- *    the following disclaimer in the documentation and/or other materials provided with the distribution.
- *
- *    Neither the name of Andy Moncsek, inc. nor the names of its contributors may be used to endorse or
- *    promote products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-package org.jacp.twitter.entities;
-
-/**
- * Created with IntelliJ IDEA.
- * User: ady
- * Date: 13.03.13
- * Time: 14:01
- * To change this template use File | Settings | File Templates.
- */
-public class Clear {
-}

JacpFXTwitterWebsocketClient/src/main/java/org/jacp/twitter/entities/Query.java

-/*
- * Copyright (c) 2013, Andy Moncsek, inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided
- * that the following conditions are met:
- *
- *    Redistributions of source code must retain the above copyright notice, this list of conditions and the
- *    following disclaimer.
- *
- *    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
- *    the following disclaimer in the documentation and/or other materials provided with the distribution.
- *
- *    Neither the name of Andy Moncsek, inc. nor the names of its contributors may be used to endorse or
- *    promote products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-package org.jacp.twitter.entities;
-
-/**
- * Created with IntelliJ IDEA.
- * User: ady
- * Date: 13.03.13
- * Time: 13:57
- * To change this template use File | Settings | File Templates.
- */
-public class Query {
-    private final String query;
-
-    public Query(final String query){
-            this.query = query;
-    }
-
-    public String getQuery() {
-        return query;
-    }
-}

JacpFXTwitterWebsocketClient/src/main/java/org/jacp/twitter/entities/User.java

-package org.jacp.twitter.entities;
-
-/**
- * Created with IntelliJ IDEA.
- * User: ady
- * Date: 06.03.13
- * Time: 11:48
- * To change this template use File | Settings | File Templates.
- */
-public class User {
-    private final String userName;
-
-    public User(final String userName) {
-        this.userName = userName;
-    }
-
-    public String getUserName() {
-        return this.userName;
-    }
-}

JacpFXTwitterWebsocketClient/src/main/resources/main.xml

             <list>
                 <ref bean="tweetView"/>
                 <ref bean="queryView"/>
-                <ref bean="sseEndpoint"/>
+                <ref bean="twitterEndpointComponent"/>
             </list>
         </property>
     </bean>
     <bean id="tweetView" class="org.jacp.twitter.components.TweetView"/>
     <bean id="queryView" class="org.jacp.twitter.components.QueryView"/>
 
-    <bean id="sseEndpoint" class="org.jacp.twitter.callbacks.SSEEndpoint"/>
+    <bean id="twitterEndpointComponent" class="org.jacp.twitter.callbacks.TwitterEndpointComponent"/>
 
 </beans>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.