Skip to content

Instantly share code, notes, and snippets.

Created July 15, 2013 14:20
Consuming LinkedIn REST based Web Services using Scribe OAuth Java Library / API - http://codeoftheday.blogspot.com/2013/07/consuming-linkedin-rest-based-web.html
/*
* Consuming LinkedIn REST based Web Services using Scribe OAuth Java Library / API
* http://codeoftheday.blogspot.com/2013/07/consuming-linkedin-rest-based-web.html
*/
package smhumayun.codeoftheday.linkedin.group;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.LinkedInApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
/**
* Main Servlet exposed as a service which will be used by client(s) in order
* to determine number of group members registered in a particular LinkedIn Group
*
* @author smhumayun
*/
public class MemberCountServlet extends HttpServlet {
private String apiKey;
private String apiSecret;
private String token;
private String tokenSecret;
private String url;
private String numMembersPrefix;
private String numMembersPostfix;
private String namePrefix;
private String namePostfix;
private long durationInMillis;
private HashMap<Long, GroupInfo> groups = new HashMap<Long, GroupInfo>();
private SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
@Override
public void init(ServletConfig config) throws ServletException {
this.apiKey = config.getServletContext().getInitParameter("apiKey");
this.apiSecret = config.getServletContext().getInitParameter("apiSecret");
this.token = config.getServletContext().getInitParameter("token");
this.tokenSecret = config.getServletContext().getInitParameter("tokenSecret");
this.url = config.getServletContext().getInitParameter("url");
try
{
durationInMillis = Integer.parseInt(config.getServletContext().getInitParameter("durationInMillis"));
}
catch(Exception e)
{
durationInMillis = 60 * 1000;
}
this.numMembersPrefix = config.getServletContext().getInitParameter("numMembersPrefix");
this.numMembersPostfix = config.getServletContext().getInitParameter("numMembersPostfix");
this.namePrefix = config.getServletContext().getInitParameter("namePrefix");
this.namePostfix = config.getServletContext().getInitParameter("namePostfix");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String content;
try
{
long gid;
try
{
gid = Long.parseLong(req.getParameter("gid"));
}
catch(Exception e)
{
gid = 0;
}
if(gid <= 0)
{
content = "INV";
logMsg("Invalid gid : " + req.getParameter("gid"));
}
else
{
GroupInfo groupInfo = groups.get(gid);
if(groupInfo == null)
groupInfo = new GroupInfo(gid);
if(groupInfo.getLastChecked() <= 0 || System.currentTimeMillis() - groupInfo.getLastChecked() > durationInMillis )
{
OAuthService oAuthService = new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.build();
OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, url.replace("{{gid}}", "" + groupInfo.getId()));
oAuthService.signRequest(new Token(token, tokenSecret), oAuthRequest);
Response response = oAuthRequest.send();
groupInfo.setLastChecked(System.currentTimeMillis());
content = response.getBody();
groupInfo.setName(content.substring(content.indexOf(namePrefix) + namePrefix.length() + 1, content.indexOf(namePostfix)));
String mc = content.substring(content.indexOf(numMembersPrefix) + numMembersPrefix.length() + 1, content.indexOf(numMembersPostfix));
try
{
groupInfo.setMemberCount(Long.parseLong(mc));
logMsg(groupInfo.getId() + " - " + groupInfo.getName() + " - " + groupInfo.getMemberCount() + " members");
content = "" + groupInfo.getMemberCount();
groups.put(groupInfo.getId(), groupInfo);
}
catch(Exception e)
{
logMsg("ERROR >>> " + groupInfo.getId() + " - " + groupInfo.getName() + " | mc {" + mc + "} | " + e.toString());
e.printStackTrace(System.out);
}
}
else
{
content = "" + groupInfo.getMemberCount();
logMsg(groupInfo.getId() + " - " + groupInfo.getName() + " - " + groupInfo.getMemberCount() + " members (cached)");
}
}
}
catch(Exception e)
{
content = "ERR";
e.printStackTrace(System.out);
}
resp.setContentType("text/plain");
resp.setContentLength(content.length());
PrintWriter out = resp.getWriter();
out.print(content);
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
private void logMsg (String msg)
{
System.out.println(df.format(new Date(System.currentTimeMillis())) + " | " + msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment