package com.blc.donate2mail.contactimport.action;
import static net.oauth.OAuth.HMAC_SHA1;
import static net.oauth.OAuth.OAUTH_SIGNATURE_METHOD;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.ParameterStyle;
import net.oauth.example.consumer.webapp.CookieConsumer;
/**
* @author Siddhartha Mukherjee , */
public class GoogleContactsConsumer extends HttpServlet {
private static final String forwardTo = "/gmailContacts.jsp";
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
OAuthConsumer consumer = null;
try {
consumer = CookieConsumer.getConsumer("googleContacts", getServletContext());
OAuthAccessor accessor = CookieConsumer.getAccessor(request, response, consumer);
// You can switch to a different signature method:
accessor.consumer.setProperty(OAUTH_SIGNATURE_METHOD, HMAC_SHA1);
// HMAC uses the access token secret as a factor,
// and it's a little less compute-intensive than RSA.
OAuthMessage message = accessor.newRequestMessage(OAuthMessage.GET,
"http://www.google.com/m8/feeds/contacts/default/full?updated-min=2010-03-16T00:00:00&max-results=1000", null);
OAuthMessage result = CookieConsumer.CLIENT.invoke(message, ParameterStyle.AUTHORIZATION_HEADER);
// Simply pass the data through to the browser:
response.setContentType("text/html");
InputStream in = result.getBodyAsStream();
Set gmailIds =new HashSet();
try{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("entry");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if(eElement!=null){
Element eEmailId= (Element) eElement.getElementsByTagName("gd:email").item(0);
if(eEmailId!=null){
gmailIds.add(eEmailId.getAttribute("address"));
}
}
}
}
request.setAttribute("emailIds",gmailIds);
}catch (Exception e) {
e.printStackTrace();
}
//CookieConsumer.copyResponse(result, response);
request.getRequestDispatcher(forwardTo).forward(request, response);
} 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;
}