Skip to content

Commit

Permalink
Modified code to support the Static Tweets in the SampleStream, pom,
Browse files Browse the repository at this point in the history
etc.
  • Loading branch information
oracle committed Aug 16, 2016
1 parent 54889c7 commit 2dcc059
Show file tree
Hide file tree
Showing 7 changed files with 13,408 additions and 8 deletions.
5 changes: 0 additions & 5 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
Expand Down Expand Up @@ -81,6 +86,7 @@
<directory>src/main/config</directory>
<includes>
<include>twitter-auth.json</include>
<include>SampleTweets.json</include>
</includes>
</resource>
</resources>
Expand Down
13,216 changes: 13,216 additions & 0 deletions src/main/config/SampleTweets.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/main/java/com/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public void service(Request request, Response response) throws Exception {
}
}, "/time");



// Add the StaticHttpHandler to serve static resources from the static folder
// server.getServerConfiguration().addHttpHandler(
// new StaticHttpHandler("src/main/resources/static/"), "static/");
Expand All @@ -91,6 +93,7 @@ public void service(Request request, Response response) throws Exception {
System.out.println(String.format("%sapplication.wadl to see the WADL resource", BASE_URI));
System.out.println(String.format("%stweets to see the JAX-RS resource", BASE_URI));
System.out.println(String.format("%stime to see the time", BASE_URI));
System.out.println(String.format("%sstatictweets<?xxx> to see a set of stored static tweets", BASE_URI));
System.out.println(String.format("%sjarstatic/index.html to see the jar static resource", BASE_URI));
System.out.println();
System.out.println("Press enter to stop the server...");
Expand All @@ -99,3 +102,4 @@ public void service(Request request, Response response) throws Exception {
server.shutdown();
}
}

109 changes: 106 additions & 3 deletions src/main/java/com/example/SampleStreamExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
import org.glassfish.jersey.server.ChunkedOutput;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class SampleStreamExample {

private static final String ENDPOINT = "https://stream.twitter.com/1.1/statuses/sample.json";
Expand Down Expand Up @@ -60,7 +65,100 @@ public SampleStreamExample() {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

public void runStaticTwitterStream(final ChunkedOutput<String> output, final String queryString) throws IOException {
StringBuilder sb = new StringBuilder(1024);
StringBuilder sbOut = new StringBuilder(1024);
URL fileurl = ClassLoader.getSystemResource("SampleTweets.json");
requireNonNull(fileurl, "Could not find SampleTweets.json");
//we ensure stream and underlying file closes using Java7 try w/ resources stmt
System.out.println("!!!!! Into the code runStaticTwitter !!!!!!!!!!!!!!!!!");

try (Stream<String> stream = Files.lines(Paths.get(fileurl.toURI()), StandardCharsets.UTF_8)) {
stream.forEach(sb::append);

if (queryString != null) {
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(sb.toString());

sbOut.append("{\"tweets\" : [");

if (element.isJsonObject()) {
JsonObject jsonObject = element.getAsJsonObject();
JsonArray jsonArray = jsonObject.getAsJsonArray("tweets");
System.out.println("ArraryLenght=" + jsonArray.size() + ", Looking for " +
queryString.toUpperCase());
int cnt = 0;
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject tweet = jsonArray.get(i).getAsJsonObject();
JsonElement text = tweet.get("text");
if (text != null) {
// System.out.println(text.getAsString());



if (tweet.toString().toUpperCase().contains(queryString.toUpperCase())) {
// System.out.println("Found in Tweet Text");

cnt++;
sbOut.append(tweet.toString());
continue;
}

JsonObject entities = tweet.getAsJsonObject("entities");
if (entities != null) {
JsonArray hashtags = entities.getAsJsonArray("hashtags");
for (int k = 0; hashtags != null && k < hashtags.size(); k++) {
JsonObject hashtag = hashtags.get(k).getAsJsonObject();
JsonElement hashtagText = hashtag.get("text");

if (hashtagText != null) {

if (hashtagText.toString().toUpperCase().contains(queryString.toUpperCase())) {
// System.out.println("Found in Hashtag");

cnt++;
sbOut.append(tweet.toString());

continue;
}
}

}
}
}


}
System.out.println("Found Objects = " + cnt +" in Static Tweets");
sbOut.append("]}");


}
}

//String script = "Java.asJSONCompatible(" + sb.toString() + ")"; //requires 8u60 or later
// Map<String, String> contents = (Map<String, String>) engine.eval(script);

} catch (URISyntaxException | IOException e) {
// } catch (URISyntaxException | IOException | ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
output.write("");;

}
System.out.println("!!!! Writing Objects !!!!");
if (queryString == null) {
output.write( sb.toString());
} else {
output.write( sbOut.toString());
}

}


public void runTwitterStream(final ChunkedOutput<String> output, final Integer i) throws IOException {
final ConsumerCredentials consumerCredentials = new ConsumerCredentials(consumerKey, consumerSecret);
Expand Down Expand Up @@ -89,13 +187,15 @@ public void runTwitterStream(final ChunkedOutput<String> output, final Integer i
+ response.getStatus() + ", reason: " + response.getStatusInfo().getReasonPhrase()
+ ", entity: " + errorEntity);
}
output.write("{\"tweets\":[");
output.write("{\"tweets\":[");
// System.out.println("{\"tweets\":[");
String chunk;
final int max = i.intValue()-1;
for (int msgRead = 0; msgRead < max; msgRead++) {
if ((chunk = chunkedInput.read()) != null) {
output.write(chunk + ",\n");
System.out.println("RAW MSG: " + chunk);
// System.out.println("RAW MSG: " + chunk);
System.out.println(chunk + ",");
// Object result = engine.eval("Java.asJSONCompatible(" + msg + ")");
// assert (result instanceof Map);
// Map<String, Object> contents = (Map<String, Object>) result;
Expand All @@ -108,9 +208,12 @@ public void runTwitterStream(final ChunkedOutput<String> output, final Integer i
}
if ((chunk = chunkedInput.read()) != null) {
output.write(chunk); //we write the last line w/o the trailing comma
System.out.println(chunk);

}
chunkedInput.close();
output.write("]}");
output.write("]}");
// System.out.println("]}");
System.out.printf("The client read %d messages!\n", i);
}

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/example/StaticTweets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.example;


import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.server.ChunkedOutput;

/**
* Root resource (exposed at "statictweets" path)
*/
@Path("statictweets")
public class StaticTweets {

private static SampleStreamExample example = new SampleStreamExample();

/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as an application/json response.
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getIt() {

final ChunkedOutput<String> output = new ChunkedOutput<String>(String.class);

runTask(output);
return Response.ok()
.entity(output)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
.build();
}

// private final ScriptEngine engine;
//private final ChunkedOutput<String> output;


private void runTask(ChunkedOutput<String> output) {
new Thread(() -> {
try {
System.out.println("!!!!! Calling runStaticTwitter !!!!!!!!!!!!!!!!!");
example.runStaticTwitterStream(output, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
// the output will be probably returned even before
// a first chunk is written by the new thread
}


}
9 changes: 9 additions & 0 deletions src/test/java/com/example/MyServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ public void tearDown() throws Exception {
/**
* Test to see that the message "Got it!" is sent in the response.
*/
/*
@Test
public void testGetTweets() {
String responseMsg = target.path("tweets").request().get(String.class);
assertNotNull(responseMsg);
}
*/
@Test
public void testGetStaticTweets() {
String responseMsg = target.path("statictweets").request().get(String.class);
assertNotNull(responseMsg);
}
/*
@Test
public void testGetTimeseriesIndex() {
String responseMsg = target.path("timeseriesIndex").request().get(String.class);
Expand All @@ -56,4 +64,5 @@ public void testGetTimeseries() {
String responseMsg = target.path("timeseries/1").request().get(String.class);
assertNotNull(responseMsg);
}
*/
}

0 comments on commit 2dcc059

Please sign in to comment.