1. Andy Moncsek
  2. JEEMax

Commits

Andy Moncsek  committed 42463f5

update chat, add TwitterSSE Client

  • Participants
  • Parent commits 41b9f69
  • Branches master

Comments (0)

Files changed (15)

File Domain/src/main/java/org/jacp/jee/entity/ChatUser.java

View file
  • Ignore whitespace
+/*
+ * 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.jee.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import java.io.Serializable;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: Andy Moncsek
+ * Date: 11.03.13
+ * Time: 21:11
+ * To change this template use File | Settings | File Templates.
+ */
+@Entity
+public class ChatUser implements Serializable {
+    @Id
+    private String id;
+    private String name;
+
+    public ChatUser(String name,String id) {
+        this.name = name;
+        this.id = id;
+    }
+
+    public ChatUser() {
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        ChatUser chatUser = (ChatUser) o;
+
+        if (id != null ? !id.equals(chatUser.id) : chatUser.id != null) return false;
+        if (name != null ? !name.equals(chatUser.name) : chatUser.name != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = id != null ? id.hashCode() : 0;
+        result = 31 * result + (name != null ? name.hashCode() : 0);
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "ChatUser{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                '}';
+    }
+}

File Domain/src/main/java/org/jacp/jee/entity/Tweet.java

View file
  • Ignore whitespace
+package org.jacp.jee.entity;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 11.03.13
+ * Time: 09:14
+ * To change this template use File | Settings | File Templates.
+ */
+@XmlRootElement
+public class Tweet implements Serializable {
+    private Date created_at;
+    private String from_user;
+    private String profile_image_url;
+    private String text;
+
+    public Date getCreated_at() {
+        return created_at;
+    }
+
+    public void setCreated_at(Date created_at) {
+        this.created_at = created_at;
+    }
+
+    public String getFrom_user() {
+        return from_user;
+    }
+
+    public void setFrom_user(String from_user) {
+        this.from_user = from_user;
+    }
+
+    public String getProfile_image_url() {
+        return profile_image_url;
+    }
+
+    public void setProfile_image_url(String profile_image_url) {
+        this.profile_image_url = profile_image_url;
+    }
+
+    public String getText() {
+        return text;
+    }
+
+    public void setText(String text) {
+        this.text = text;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Tweet tweet = (Tweet) o;
+
+        if (created_at != null ? !created_at.equals(tweet.created_at) : tweet.created_at != null) return false;
+        if (from_user != null ? !from_user.equals(tweet.from_user) : tweet.from_user != null) return false;
+        if (profile_image_url != null ? !profile_image_url.equals(tweet.profile_image_url) : tweet.profile_image_url != null)
+            return false;
+        if (text != null ? !text.equals(tweet.text) : tweet.text != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = created_at != null ? created_at.hashCode() : 0;
+        result = 31 * result + (from_user != null ? from_user.hashCode() : 0);
+        result = 31 * result + (profile_image_url != null ? profile_image_url.hashCode() : 0);
+        result = 31 * result + (text != null ? text.hashCode() : 0);
+        return result;
+    }
+}

File Domain/src/main/java/org/jacp/jee/entity/TwitterResult.java

View file
  • Ignore whitespace
+package org.jacp.jee.entity;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: Andy Moncsek
+ * Date: 11.03.13
+ * Time: 09:13
+ * To change this template use File | Settings | File Templates.
+ */
+@XmlRootElement
+public class TwitterResult implements Serializable{
+
+    private String query;
+    private String max_id;
+    private List<Tweet> results;
+
+    public String getQuery() {
+        return query;
+    }
+
+    public void setQuery(String query) {
+        this.query = query;
+    }
+
+    public String getMax_id() {
+        return max_id;
+    }
+
+    public void setMax_id(String max_id) {
+        this.max_id = max_id;
+    }
+
+    public List<Tweet> getResults() {
+        return results;
+    }
+
+    public void setResults(List<Tweet> results) {
+        this.results = results;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        TwitterResult that = (TwitterResult) o;
+
+        if (max_id != null ? !max_id.equals(that.max_id) : that.max_id != null) return false;
+        if (query != null ? !query.equals(that.query) : that.query != null) return false;
+        if (results != null ? !results.equals(that.results) : that.results != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = query != null ? query.hashCode() : 0;
+        result = 31 * result + (max_id != null ? max_id.hashCode() : 0);
+        result = 31 * result + (results != null ? results.hashCode() : 0);
+        return result;
+    }
+}

File Domain/src/main/resources/META-INF/persistence.xml

View file
  • Ignore whitespace
         <exclude-unlisted-classes>false</exclude-unlisted-classes>
 
         <properties>
-            <!--  <property
+           <property
                  name="eclipselink.ddl-generation"
-                 value="drop-and-create-tables" /> -->
+                 value="drop-and-create-tables" />
         </properties>
     </persistence-unit>
 

File EJB/src/main/java/org/jacp/jee/App.java

  • Ignore whitespace
-package org.jacp.jee;
-
-/**
- * Hello world!
- */
-public class App {
-    public static void main(String[] args) {
-        System.out.println("Hello World!");
-    }
-}

File EJBClient/src/main/java/org/jacp/jee/api/ChatUserBeanInterface.java

View file
  • Ignore whitespace
+/*
+ * 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.jee.api;
+
+import org.jacp.jee.entity.ChatUser;
+
+import javax.ejb.Remote;
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 11.03.13
+ * Time: 21:14
+ * To change this template use File | Settings | File Templates.
+ */
+public interface ChatUserBeanInterface {
+
+    /**
+     * save a new user
+     * @param user
+     */
+    void saveNewUser(ChatUser user);
+
+    /**
+     * get all users
+     * @return all users
+     */
+    List<ChatUser> getAllUsers();
+
+}

File JacpFXWebsocketChatClient/src/main/resources/server.properties

View file
  • Ignore whitespace
+server = ws://localhost:8080/WebClient-Websocket/chat

File JavaClient/pom.xml

View file
  • Ignore whitespace
             <version>${project.version}</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.core</groupId>
+            <artifactId>jersey-client</artifactId>
+            <version>${jersey.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.containers</groupId>
+            <artifactId>jersey-container-servlet</artifactId>
+            <version>${jersey.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.media</groupId>
+            <artifactId>jersey-media-sse</artifactId>
+            <version>${jersey.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>net.sf.json-lib</groupId>
+            <artifactId>json-lib</artifactId>
+            <version>2.4</version>
+            <classifier>jdk15</classifier>
+        </dependency>
     </dependencies>
 
 </project>

File JavaClient/src/test/java/test/TwitterJsonTest.java

View file
  • Ignore whitespace
+package test;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import net.sf.json.JSONObject;
+import org.jacp.jee.entity.TwitterResult;
+import org.junit.Test;
+
+import javax.ws.rs.core.MediaType;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 11.03.13
+ * Time: 08:58
+ * To change this template use File | Settings | File Templates.
+ */
+public class TwitterJsonTest {
+
+    @Test
+    public void connectToTwitter() {
+        String data =getFeedData("glassfish");
+        System.out.println(data);
+        Gson gson =new GsonBuilder()
+                .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
+        JSONObject jsonObject = JSONObject.fromObject(data);
+        TwitterResult result = gson.fromJson(data,TwitterResult.class)  ;
+        //TwitterResult result = (TwitterResult) JSONObject.toBean(jsonObject, TwitterResult.class);
+        System.out.println(result);
+    }
+
+    private String getFeedData(String input) {
+        StringBuilder sb = new StringBuilder();
+        String searchURL = "http://search.twitter.com/search.json?q=" + input + "&rpp=5&include_entities=true" +
+                "&with_twitter_user_id=true&result_type=mixed";
+        try {
+            URL twitter = new URL(searchURL);
+            URLConnection yc = null;
+
+            yc = twitter.openConnection();
+
+
+            BufferedReader in = new BufferedReader(
+                    new InputStreamReader(
+                            yc.getInputStream()));
+            String inputLine;
+
+            while ((inputLine = in.readLine()) != null) {
+                sb.append(inputLine);
+
+            }
+            in.close();
+
+            return sb.toString();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return new String("Error in getting data from the feeds ");
+    }
+}

File WebClient-SSE/src/main/java/org/jacp/sse/SSEResource.java

View file
  • Ignore whitespace
     public void sendMessage(String message) throws IOException {
         eventOutput.write(new OutboundEvent.Builder().name("custom-message").data(String.class, message).build());
     }
+
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    public void sendJsonMessage(String message) throws IOException {
+        eventOutput.write(new OutboundEvent.Builder().name("custom-message").data(String.class, message).build());
+    }
 }

File WebClient-SSE/src/main/java/org/jacp/sse/WebClient.java

View file
  • Ignore whitespace
                     public void onEvent(InboundEvent inboundEvent) {
                         try {
                             //get the JSON data and parse it
+                            System.out.println("EVENT: "+inboundEvent.getName());
                             JSONObject jsonObject = JSONObject.fromObject(inboundEvent.getData(String.class,
                                     MediaType.APPLICATION_JSON_TYPE));
                             JSONArray jsonArray = (((JSONArray) (jsonObject.get("results"))));

File WebClient-SSE/src/main/java/org/jacp/sse/beans/TwitterTimerBean.java

View file
  • Ignore whitespace
             System.out.println("String" + randomElement);
             String message = getFeedData(randomElement);
             System.out.println("Posting message");
-            webTarget.path("twittersse").request().post(Entity.text(message));
+            webTarget.path("twittersse").request().post(Entity.json(message));
         } catch (URISyntaxException e) {
             e.printStackTrace();
         }
             String inputLine;
 
             while ((inputLine = in.readLine()) != null) {
-                System.out.println("Input line" + inputLine);
                 sb.append(inputLine);
 
             }

File WebClient-Websocket/src/main/java/org/jacp/beans/ejb/ChatUserBean.java

View file
  • Ignore whitespace
+/*
+ * 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.beans.ejb;
+
+import org.jacp.jee.api.ChatUserBeanInterface;
+import org.jacp.jee.entity.ChatUser;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Local;
+import javax.ejb.Remote;
+import javax.ejb.Stateless;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: Andy Moncsek
+ * Date: 11.03.13
+ * Time: 21:13
+ * simple bean to store and access users
+ */
+
+@Stateless(mappedName = "ChatUserBean")
+public class ChatUserBean implements Serializable {
+    @PersistenceContext(unitName = "messagePU")
+    private EntityManager em;
+
+    @PostConstruct
+    public void init() {
+                  System.out.println("ChatUserBean created");
+    }
+
+    public void saveNewUser(ChatUser user) {
+        em.persist(user);
+    }
+
+
+    public List<ChatUser> getAllUsers() {
+        Query query = em.createQuery("SELECT e FROM ChatUser e");
+        return query.getResultList();
+    }
+}

File WebClient-Websocket/src/main/java/org/jacp/beans/websocket/chat/ChatUserProxyBean.java

View file
  • Ignore whitespace
+/*
+ * 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.beans.websocket.chat;
+
+import org.jacp.beans.ejb.ChatUserBean;
+import org.jacp.jee.entity.ChatUser;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.util.List;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: Andy Moncsek
+ * Date: 12.03.13
+ * Time: 08:46
+ * This is simply a workaround to access ejb's in a websocket endpoint
+ */
+@Named
+public class ChatUserProxyBean {
+    @Inject
+    private ChatUserBean bean;
+
+    public void saveNewUser(ChatUser user){
+            bean.saveNewUser(user);
+
+    }
+
+    public List<ChatUser> getAllUsers() {
+        return bean.getAllUsers();
+    }
+
+}

File pom.xml

View file
  • Ignore whitespace
                 <module>EAR-SSE</module>
                 <module>JavaClient</module>
                 <module>JacpFXWebsocketChatClient</module>
+				<module>JacpFXSSETwitterClient</module>
             </modules>
         </profile>
         <profile>
                 <module>WebClient-SSE</module>
                 <module>EAR-SSE</module>
                 <module>JavaClient</module>
+				<module>JacpFXSSETwitterClient</module>
             </modules>
         </profile>
     </profiles>
         <module>EAR-SSE</module>
         <module>JavaClient</module>
         <module>JacpFXWebsocketChatClient</module>
+		<module>JacpFXSSETwitterClient</module>
     </modules>
     <properties>
         <javax.persistence.version>2.0.0</javax.persistence.version>
                 <version>${project.version}</version>
                 <type>jar</type>
             </dependency>
+			 <dependency>
+                <groupId>${project.groupId}</groupId>
+                <artifactId>JacpFXSSETwitterClient</artifactId>
+                <version>${project.version}</version>
+                <type>jar</type>
+            </dependency>
         </dependencies>
     </dependencyManagement>
     <dependencies>
             <artifactId>commons-lang</artifactId>
             <version>2.6</version>
         </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.2.2</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>