Skip to content
This repository was archived by the owner on Jan 30, 2019. It is now read-only.

Commit 83bb6bd

Browse files
author
Arun Gupta
committed
Starting a sample to showcase Twitter Search
Former-commit-id: 80188e3fc9156c9e882363f54e014751980a26b3
1 parent ae9fe9a commit 83bb6bd

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>json</artifactId>
6+
<groupId>org.sample</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.glassfish.sample</groupId>
12+
<artifactId>twitter-search</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
16+
<name>twitter-search</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.glassfish.samples</groupId>
21+
<artifactId>twitter-api</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
26+
<artifactId>oauth-client</artifactId>
27+
<version>1.11</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
31+
<artifactId>oauth-signature</artifactId>
32+
<version>1.11</version>
33+
</dependency>
34+
</dependencies>
35+
36+
</project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.sample.twitter.search;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.inject.Inject;
45+
import javax.servlet.ServletException;
46+
import javax.servlet.annotation.WebServlet;
47+
import javax.servlet.http.HttpServlet;
48+
import javax.servlet.http.HttpServletRequest;
49+
import javax.servlet.http.HttpServletResponse;
50+
import org.glassfish.samples.twitter.api.SearchResults;
51+
import org.glassfish.samples.twitter.api.SearchResultsTweet;
52+
import org.glassfish.samples.twitter.api.Twitter;
53+
54+
/**
55+
* @author Arun Gupta
56+
*/
57+
@WebServlet(urlPatterns = {"/TestServlet"})
58+
public class TestServlet extends HttpServlet {
59+
60+
@Inject
61+
Twitter twitter;
62+
63+
/**
64+
* Processes requests for both HTTP
65+
* <code>GET</code> and
66+
* <code>POST</code> methods.
67+
*
68+
* @param request servlet request
69+
* @param response servlet response
70+
* @throws ServletException if a servlet-specific error occurs
71+
* @throws IOException if an I/O error occurs
72+
*/
73+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
74+
throws ServletException, IOException {
75+
response.setContentType("text/html;charset=UTF-8");
76+
try (PrintWriter out = response.getWriter()) {
77+
out.println("<!DOCTYPE html>");
78+
out.println("<html>");
79+
out.println("<head>");
80+
out.println("<title>Servlet TestServlet</title>");
81+
out.println("</head>");
82+
out.println("<body>");
83+
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
84+
SearchResults results = twitter.search("%23javaone%20%23javaee7%20%23q1", SearchResults.class);
85+
for (SearchResultsTweet tweet : results.getResults()) {
86+
tweet.getFrom_user();
87+
}
88+
out.println("</body>");
89+
out.println("</html>");
90+
}
91+
}
92+
93+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
94+
/**
95+
* Handles the HTTP
96+
* <code>GET</code> method.
97+
*
98+
* @param request servlet request
99+
* @param response servlet response
100+
* @throws ServletException if a servlet-specific error occurs
101+
* @throws IOException if an I/O error occurs
102+
*/
103+
@Override
104+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
105+
throws ServletException, IOException {
106+
processRequest(request, response);
107+
}
108+
109+
/**
110+
* Handles the HTTP
111+
* <code>POST</code> method.
112+
*
113+
* @param request servlet request
114+
* @param response servlet response
115+
* @throws ServletException if a servlet-specific error occurs
116+
* @throws IOException if an I/O error occurs
117+
*/
118+
@Override
119+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
120+
throws ServletException, IOException {
121+
processRequest(request, response);
122+
}
123+
124+
/**
125+
* Returns a short description of the servlet.
126+
*
127+
* @return a String containing servlet description
128+
*/
129+
@Override
130+
public String getServletInfo() {
131+
return "Short description";
132+
}// </editor-fold>
133+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.sample.twitter.search;
41+
42+
import javax.inject.Inject;
43+
import org.glassfish.samples.twitter.api.SearchResults;
44+
import org.glassfish.samples.twitter.api.Twitter;
45+
46+
/**
47+
* @author Arun Gupta
48+
*/
49+
public class TwitterSearch {
50+
51+
@Inject
52+
Twitter twitter;
53+
54+
public SearchResults getResults(String query) {
55+
return twitter.search(query, SearchResults.class);
56+
}
57+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
5+
</beans>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
2+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3+
"http://www.w3.org/TR/html4/loose.dtd">
4+
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8+
<title>JSP Page</title>
9+
</head>
10+
<body>
11+
<h1>Hello World!</h1>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)