Monday, June 30, 2014

Collect your linkedin socail contact on MongoDb

I haven;t written my blog for sometimes.These days I write a simple application to collect my Linked social contact into MongoDB from BlueMix.I will share my experience.I put my code at jazzhub,you can get complete code from here.

LinkedIn  is a business-oriented social networking service.People can know other one's working status and job info from this website.Sometimes poeple want to save his connection into his database for information collection.

When you use my application to deploy on the bluemix,the first things you need to do you need to apply for a key for API usage.You can apply the key from

You can fill the below form,after that you can get the oath key.

The key class is  SocialServlet .


import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cloudfoundry.runtime.env.CloudEnvironment;


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class SocialServlet extends HttpServlet implements Servlet {

private static final String CONSUMER_KEY_OPTION = "";
private static final String CONSUMER_SECRET_OPTION = "";
private static final String ACCESS_TOKEN_OPTION = "";
private static final String ACCESS_TOKEN_SECRET_OPTION = "";
private static final int QUERY_MAX = 20;

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String connURL = getServiceURI();
MongoClient mongoClient = new MongoClient(new MongoClientURI(

ClientFactory client = new ClientFactory(
final LinkedInApiClient accessApi = client.getFactory()
long timeStart = System.currentTimeMillis();
System.out.println("Process start..");
ProfileAPI profileAPI = new ProfileAPI(accessApi);
DB mongoDB = mongoClient.getDB("db");
DBCollection usersProfile = mongoDB.getCollection("usersProfile");
BasicDBObject userDocument = new BasicDBObject();
int countQuery = 0;
Person profile = profileAPI.getProfile();
ConnectionsAPI connection = new ConnectionsAPI(accessApi);
List<Person> peopleConnection = connection.getConnections()
String parentId = profile.getId();
while (SocialServlet.QUERY_MAX > countQuery++) {
userDocument.put("lastName", profile.getFirstName());
if (profile.getLocation() != null) {
System.out.println("lastName is"
+ profile.getLocation().getName());
System.out.println("lastName is" + profile.getFirstName());
userDocument.put("lastName", profile.getLastName());
System.out.println("lastName is" + profile.getLastName());
userDocument.put("headline", profile.getHeadline());
System.out.println("headline is" + profile.getHeadline());
userDocument.put("summary", profile.getSummary());
System.out.println("summary is" + profile.getSummary());
userDocument.put("profileUrl", profile.getPublicProfileUrl());
System.out.println("profile url"
+ profile.getPublicProfileUrl());
userDocument.put("profileId", profile.getId());
userDocument.put("birthdate", profile.getDateOfBirth());
userDocument.put("profile birthdate", profile.getDateOfBirth());
System.out.println(" birthdate" + profile.getDateOfBirth());
parentId.equals(profile.getId()) ? "" : parentId);;
if (peopleConnection.size() > 0
&& peopleConnection.size() > countQuery) {
profile = profileAPI.getProfile(peopleConnection.get(

} else {
countQuery = SocialServlet.QUERY_MAX;

long timeEnd = System.currentTimeMillis();
.println("Your Linked social contact has been saved in the mongodb");
.println("Time schedule " + (timeEnd - timeStart) + " ms");

} catch (Exception e) {
out.println("Failed: " + e.getMessage());

public String getServiceURI() throws Exception {
CloudEnvironment environment = new CloudEnvironment();
if (environment.getServiceDataByLabels("mongodb").size() == 0) {
throw new Exception("No MongoDB service is bund to this app!!");

Map credential = (Map) ((Map) environment.getServiceDataByLabels(
System.out.println(" I have get credentials url"+(String) credential.get("url"));
return (String) credential.get("url");


Notes:You need to add cloudfoundry.jar to get the MongoDB credential url.In order to use Linked API,you need to download the api jar from the

After export the war from the eclipse project ,you can use cf command to deploy this way to the BlueMix.
Use command like:
cf p Mysocailcontact -p social.war 

Then bind mongodb service to the Mysocailcontact app.

After execute the below url

You can find the log we wrote in the code on the bluemix.

Login into the Mongo DB console on the bluemix,you can find the record on that db,the record will save my connection from this website.

No comments:

Post a Comment