package contactimport.action;
import static net.oauth.OAuth.HMAC_SHA1;
import static net.oauth.OAuth.OAUTH_SIGNATURE_METHOD;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.OAuthProblemException;
import net.oauth.ParameterStyle;
import net.oauth.client.OAuthResponseMessage;
import net.oauth.example.consumer.webapp.CookieConsumer;
import net.oauth.http.HttpMessage;
import net.oauth.http.HttpResponseMessage;
/**
* A trivial consumer of the 'friends_timeline' service at Twitter.
*
* */
public class TwitterConsumer extends HttpServlet {
private static final String forwardTo = "/twitterContactsScreenName.jsp";
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
OAuthConsumer consumer = null;
try {
consumer = CookieConsumer.getConsumer("twitter", getServletContext());
OAuthAccessor accessor = CookieConsumer.getAccessor(request, response, consumer);
accessor.consumer.setProperty(OAUTH_SIGNATURE_METHOD, HMAC_SHA1);
OAuthResponseMessage result = CookieConsumer.CLIENT.access(accessor.newRequestMessage(OAuthMessage.GET,
"http://api.twitter.com/1/statuses/followers.xml", null), ParameterStyle.AUTHORIZATION_HEADER);
int status = result.getHttpResponse().getStatusCode();
System.out.println(status);
if (status != HttpResponseMessage.STATUS_OK) {
OAuthProblemException problem = result.toOAuthProblemException();
if (problem.getProblem() != null) {
throw problem;
}
Map<String, Object> dump = problem.getParameters();
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(dump.get(HttpMessage.REQUEST));
out.println("----------------------");
out.println(dump.get(HttpMessage.RESPONSE));
} else {
// Simply pass the data through to the browser:
response.setContentType(result.getHeader("Content-Type"));
InputStream in = result.getBodyAsStream();
//CookieConsumer.copyResponse(result, response);
try{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("user");
System.out.println("-----------------------");
List<String> contactScreenName=new ArrayList();
List<String> contactScreenStatus=new ArrayList();
HttpSession session =request.getSession(true);
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
Element msg= (Element) eElement.getElementsByTagName("screen_name").item(0);
contactScreenName.add(msg.getNodeValue());
NodeList statusData = eElement.getElementsByTagName("status");
for(int i=0;i<statusData.getLength();i++){
Node statusNode = statusData.item(i);
if (statusNode.getNodeType() == Node.ELEMENT_NODE) {
Element statusElement = (Element)statusNode;
Element text= (Element) statusElement.getElementsByTagName("text").item(0);
contactScreenStatus.add(text.getNodeValue());
}
}
}
System.out.println("-----------------------");
}
session.setAttribute("contactScreenName",contactScreenName);
request.getRequestDispatcher(forwardTo).forward(request, response);
}catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
CookieConsumer.handleException(e, request, response, consumer);
}
}
private static String getTagValue(String sTag, Element eElement){
NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
private static final long serialVersionUID = 1L;
}