Index: /user/src/services/callbacks/TwitterCallback.java
===================================================================
--- /user/src/services/callbacks/TwitterCallback.java (revision 758)
+++ /user/src/services/callbacks/TwitterCallback.java (revision 758)
@@ -0,0 +1,182 @@
+package services.callbacks;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+
+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 datastore.client.PersistentHashMapClient;
+
+import entites.SNDetails;
+import entites.User;
+
+
+import services.users.Register;
+import session.SessionManagementIM;
+import twitter4j.Twitter;
+import twitter4j.TwitterException;
+import twitter4j.TwitterFactory;
+
+import twitter4j.auth.AccessToken;
+import twitter4j.auth.RequestToken;
+import twitter4j.conf.ConfigurationBuilder;
+
+
+/**
+ * Servlet implementation class CallbackServlet
+ * 
+ * 
+ * @author Giagkos Mytilinis
+ */
+public class TwitterCallback extends RunningWebService {
+	
+	private static final long serialVersionUID = 1L;
+
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public TwitterCallback() {
+        super();
+        serviceName = "Twitter Callback: ";
+        DEBUG = true;
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+    
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+	
+		ConfigurationBuilder cb = new ConfigurationBuilder();
+		cb.setDebugEnabled(true)
+		  .setIncludeEntitiesEnabled(true)
+		  .setUseSSL(true);
+		
+		Twitter twitter = new TwitterFactory(cb.build()).getInstance();
+		
+		HttpSession session = request.getSession();
+				
+		twitter.setOAuthConsumer(Register.getTwitterConsumerToken(), Register.getTwitterConsumerSecret());
+		try {
+			AccessToken accessToken = twitter.getOAuthAccessToken(new RequestToken((String) session.getAttribute("token"), 
+					(String) session.getAttribute("tokenSecret")),request.getParameter("oauth_verifier"));
+            twitter.setOAuthAccessToken(accessToken);
+			
+			session.setAttribute("acc_token", accessToken.getToken());
+			session.setAttribute("acc_tokenSecret", accessToken.getTokenSecret());
+			
+			ConfigurationBuilder userConfBuilder = new ConfigurationBuilder();
+			userConfBuilder.setDebugEnabled(true)
+			  .setOAuthConsumerKey(Register.getTwitterConsumerToken())
+			  .setOAuthConsumerSecret(Register.getTwitterConsumerSecret())
+			  .setOAuthAccessToken(accessToken.getToken())
+			  .setOAuthAccessTokenSecret(accessToken.getTokenSecret())
+			  .setIncludeEntitiesEnabled(true);
+			
+			TwitterFactory tf = new TwitterFactory(userConfBuilder.build());
+			Twitter userTwitter = tf.getInstance ();
+			
+			/*
+			 * Search in the DB according to social network and userID. This search can return
+			 * either one or none results.
+			 */
+			
+			SNDetails socialNetworkInfo = new SNDetails();
+			socialNetworkInfo.setSnName("twitter");
+			socialNetworkInfo.setSnIdentifier(Long.toString(userTwitter.getId()));
+					
+			List<HashMap<String, String>> results = socialNetworkInfo.read();
+			
+			String token = null; 
+			
+			PersistentHashMapClient hashClient = new PersistentHashMapClient();
+			
+			if(results.size()==0){
+				/*
+				 *  If there are no results, we can either add a new Modissense account
+				 *  or connect a social network account to an existing one.
+				 */
+				socialNetworkInfo.setSnToken(accessToken.getToken());
+				socialNetworkInfo.setSnTokenSecret(accessToken.getTokenSecret());
+				
+				if(!((String)session.getAttribute("uidtoken")).equals("null")){
+						
+					if(DEBUG)System.out.println(serviceName+"Connect Twitter account to existing Modissense user.");
+					
+					token = (String) session.getAttribute("uidtoken");
+					if(DEBUG)System.out.println(serviceName+"user token = "+token);
+					
+					int usid = hashClient.getUserId(token);
+						
+					socialNetworkInfo.setUserId(usid);
+					if(socialNetworkInfo.create()){
+						System.out.println(serviceName+"sn_list entry created successfully.");
+					}
+					else
+						System.out.println(serviceName+"sn_list entry failed");
+				}else{
+					if(DEBUG)System.out.println(serviceName+"Create new Modissense user.");
+					
+					User newUser = new User();
+					newUser.setUsername(userTwitter.getScreenName());
+					if(newUser.create()){
+						if(DEBUG)System.out.println(serviceName+"user entry created successfully.");
+						User theSameUser = new User(userTwitter.getScreenName());
+						socialNetworkInfo.setUserId(theSameUser.getId());
+						
+						token = Utils.randomStringGenerator(32); 
+						if(DEBUG)System.out.println(serviceName+"New user acquired token = "+token);
+						hashClient.add(token, theSameUser.getId());
+						
+						if(socialNetworkInfo.create()){
+							System.out.println(serviceName+"sn_list entry created successfully.");
+						}
+						else
+							System.out.println(serviceName+"sn_list entry failed");
+					}
+					else
+						System.out.println(serviceName+"user entry failed.");
+				}
+			} else{
+				/*
+				 * In the DB there is a twitter account with the specific user id. Update twitter access token
+				 * as it may have expired and check if there is a valid session token for this user.
+				 */
+				HashMap<String, String> tokensToUpdate = new HashMap<String, String>();
+				tokensToUpdate.put("sn_token", "'"+accessToken.getToken()+"'");
+				tokensToUpdate.put("sn_token_secret", "'"+accessToken.getTokenSecret()+"'");
+				socialNetworkInfo.update(tokensToUpdate);
+				
+				if(hashClient.getSession(socialNetworkInfo.getUserId()).equals("null")){
+						
+					token = Utils.randomStringGenerator(32); 
+					hashClient.add(token, socialNetworkInfo.getUserId());
+					if(DEBUG)System.out.println(serviceName+"Existing user got new token. Token = "+token);
+				}else{
+					token=hashClient.getSession(socialNetworkInfo.getUserId());
+					if(DEBUG)System.out.println(serviceName+"Existing user logged in. Valid token= "+token);
+				}
+			}
+			
+			
+		    response.sendRedirect(response.encodeRedirectURL((String)session.getAttribute("callback")+"&uid="+token));
+			
+			
+		} catch (TwitterException e) {
+		
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+	}
+
+}
Index: /user/src/services/callbacks/FbCallback.java
===================================================================
--- /user/src/services/callbacks/FbCallback.java (revision 758)
+++ /user/src/services/callbacks/FbCallback.java (revision 758)
@@ -0,0 +1,210 @@
+package services.callbacks;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+
+import javax.net.ssl.HttpsURLConnection;
+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 org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+
+import datastore.client.PersistentHashMapClient;
+
+import services.users.Register;
+import session.SessionManagementIM;
+
+import entites.SNDetails;
+import entites.User;
+
+/**
+ * 
+ * Servlet implementation class CallbackServlet
+ * 
+ * @author Giagkos Mytilinis
+ */
+
+public class FbCallback extends RunningWebService {
+	
+	private static final long serialVersionUID = 1L;
+    
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public FbCallback() {
+        super();
+        serviceName = "Facebook Callback: ";
+        DEBUG = true;
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+		HttpSession session = request.getSession();
+		
+		String code = request.getParameter("code");
+		
+		if(DEBUG){
+			System.out.println(serviceName+"-----Parameters-----");
+			Enumeration<String> enm = request.getParameterNames();
+			while(enm.hasMoreElements()){
+				String pname = enm.nextElement();
+				System.out.print(serviceName+"param: "+pname);
+				if(request.getParameter(pname)!=null)
+					System.out.println(serviceName+"value: "+request.getParameter(pname));
+			}
+		}
+		
+		if(((String) session.getAttribute("state")).equals(request.getParameter("state"))){
+			 if(request.getParameter("error")!=null){
+				 System.out.println(serviceName+"The user denied to authorize your app");
+				 System.out.println(serviceName+request.getParameter("error_reason"));
+				 System.out.println(serviceName+request.getParameter("error"));
+				 System.out.println(serviceName+request.getParameter("error_description"));
+			 }else{
+				 URL url = new URL("https://graph.facebook.com/oauth/access_token?client_id="+Register.getFbAppID()+"&" +
+							"redirect_uri="+OAuth.percentEncode(Register.getFbRedirectURI())+"&client_secret="+Register.getFbAppSecret() +
+							"&code="+code);
+				 HttpsURLConnection conn =(HttpsURLConnection) url.openConnection();conn.setRequestMethod("GET");
+				 conn.setDoOutput(true);
+			     
+			     StringBuffer answer = new StringBuffer();
+			     BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+			     String line;
+			     while ((line = reader.readLine()) != null) {
+			            answer.append(line);
+			     }
+			     reader.close();
+			   
+			     String accessAnswer = answer.toString();
+			     String [] answerParts = accessAnswer.split("=");
+			     String accessToken = answerParts[1];
+			     
+			     if(DEBUG)System.out.println(serviceName+"fb access token = "+accessToken);
+			     url = new URL("https://graph.facebook.com/me?access_token="+accessToken);
+				 conn =(HttpsURLConnection) url.openConnection();conn.setRequestMethod("GET");
+				 conn.setDoOutput(true);
+			     
+			     answer = new StringBuffer();
+			     reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+			     
+			     while ((line = reader.readLine()) != null) {
+			            answer.append(line);
+			     }
+			     reader.close();
+			    
+			     String userAnswer = answer.toString();
+			     
+			     JSONObject userObj =(JSONObject) JSONValue.parse(userAnswer);
+			     String userId = (String) userObj.get("id");
+			     String username = (String) userObj.get("username");
+			     
+			     /*
+				  * Search in the DB according to social network and userID. This search can return
+				  * either one or none results.
+				  */
+			     
+			     SNDetails socialNetworkInfo = new SNDetails();
+				 socialNetworkInfo.setSnName("facebook");
+				 socialNetworkInfo.setSnIdentifier(userId);
+					
+				 List<HashMap<String, String>> results = socialNetworkInfo.read();
+				 String token = null;
+				 
+				 PersistentHashMapClient hashClient = new PersistentHashMapClient();
+				 	
+				 if(results.size()==0){
+					 	/*
+	    				 *  If there are no results, we can either add a new Modissense account
+	    				 *  or connect a social network account to an existing one.
+	    				 */
+						
+						socialNetworkInfo.setSnToken(accessToken);
+						
+						if(!((String)session.getAttribute("uidtoken")).equals("null")){
+					
+							if(DEBUG)System.out.println(serviceName+"Connect Facebook account to existing Modissense user.");
+							
+							token = (String) session.getAttribute("uidtoken");
+							if(DEBUG)System.out.println(serviceName+"user token = "+token);
+							
+							int usid = hashClient.getUserId(token);
+							
+							socialNetworkInfo.setUserId(usid);
+							if(socialNetworkInfo.create()){
+								System.out.println(serviceName+"sn_list entry created successfully.");
+							}
+							else
+								System.out.println(serviceName+"sn_list entry failed");
+						}else{
+							if(DEBUG)System.out.println(serviceName+"Create new Modissense user.");
+							
+							User newUser = new User();
+							newUser.setUsername(username);
+							if(newUser.create()){
+								if(DEBUG)System.out.println(serviceName+"user entry created successfully.");
+								User theSameUser = new User(username);
+								socialNetworkInfo.setUserId(theSameUser.getId());			
+								
+								token = Utils.randomStringGenerator(32); 
+								if(DEBUG)System.out.println(serviceName+"New user acquired token = "+token);
+								hashClient.add(token, theSameUser.getId());
+									
+								if(socialNetworkInfo.create()){
+									System.out.println(serviceName+"sn_list entry created successfully.");
+								}
+								else
+									System.out.println(serviceName+"sn_list entry failed");
+							}
+							else
+								System.out.println(serviceName+"user entry failed.");
+						}
+					} else{
+						/*
+	    				 * In the DB there is a twitter account with the specific user id. Update twitter access token
+	    				 * as it may have expired and check if there is a valid session token for this user.
+	    				 */
+						
+						HashMap<String, String> tokensToUpdate = new HashMap<String, String>();
+						tokensToUpdate.put("sn_token", "'"+accessToken+"'");
+						socialNetworkInfo.update(tokensToUpdate);
+						
+						if(hashClient.getSession(socialNetworkInfo.getUserId()).equals("null")){
+							
+							token = Utils.randomStringGenerator(32); 
+							hashClient.add(token, socialNetworkInfo.getUserId());
+							if(DEBUG)System.out.println(serviceName+"Existing user got new token. Token = "+token);
+						}else{
+							token=hashClient.getSession(socialNetworkInfo.getUserId());
+							if(DEBUG)System.out.println(serviceName+"Existing user logged in. Valid token= "+token);
+						}
+					}
+					
+				 
+				 response.sendRedirect(response.encodeRedirectURL((String)session.getAttribute("callback")+"&uid="+token));
+			     
+			 }
+		}
+	
+		
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+	}
+
+}
+
Index: /user/src/services/callbacks/FoursquareCallback.java
===================================================================
--- /user/src/services/callbacks/FoursquareCallback.java (revision 758)
+++ /user/src/services/callbacks/FoursquareCallback.java (revision 758)
@@ -0,0 +1,172 @@
+package services.callbacks;
+
+import java.io.IOException;
+
+import java.util.HashMap;
+import java.util.List;
+
+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 datastore.client.PersistentHashMapClient;
+
+import services.users.Register;
+import session.SessionManagementIM;
+
+
+import entites.SNDetails;
+import entites.User;
+import fi.foyt.foursquare.api.FoursquareApi;
+import fi.foyt.foursquare.api.FoursquareApiException;
+import fi.foyt.foursquare.api.Result;
+import fi.foyt.foursquare.api.entities.CompleteUser;
+
+/**
+ * Servlet implementation class CallbackServlet
+ * 
+ * @author Giagkos Mytilinis
+ */
+
+public class FoursquareCallback extends RunningWebService {
+	
+private static final long serialVersionUID = 1L;
+    
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public FoursquareCallback() {
+        super();
+        serviceName = "Foursquare Callback: ";
+        DEBUG = true;
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		
+        FoursquareApi foursquareApi = new FoursquareApi(Register.getFoursquareClientID()
+        		,Register.getFoursquareClientSecret(),Register.getFoursquareCallback());
+
+        String code=request.getParameter("code");
+        
+        HttpSession session = request.getSession();
+        
+        try {
+
+              foursquareApi.authenticateCode(code);
+              String accessToken = foursquareApi.getOAuthToken();
+               
+              Result<CompleteUser> result = foursquareApi.user("self");
+              if (result.getMeta().getCode() == 200) {
+                  CompleteUser fUser = result.getResult();
+                  if(DEBUG)System.out.println(serviceName+"foursquare user "+fUser.getId());
+                  
+                /*
+      			 * Search in the DB according to social network and userID. This search can return
+      			 * either one or none results.
+      			 */
+
+                  SNDetails socialNetworkInfo = new SNDetails();
+      			socialNetworkInfo.setSnName("foursquare");
+      			socialNetworkInfo.setSnIdentifier(fUser.getId());
+      			    			
+      			List<HashMap<String, String>> results = socialNetworkInfo.read();
+      			
+
+    			String token = null;
+    			PersistentHashMapClient hashClient = new PersistentHashMapClient();
+    			
+    			if(results.size()==0){
+    				
+    				/*
+    				 *  If there are no results, we can either add a new Modissense account
+    				 *  or connect a social network account to an existing one.
+    				 */
+    				
+    				socialNetworkInfo.setSnToken(accessToken);
+    				
+    				if(!((String)session.getAttribute("uidtoken")).equals("null")){
+    					
+    					if(DEBUG)System.out.println(serviceName+"Connect Foursquare account to existing Modissense user.");
+    					
+    					token = (String) session.getAttribute("uidtoken");
+    					if(DEBUG)System.out.println(serviceName+"user token = "+token);
+    					
+    					int usid = hashClient.getUserId(token);
+    					
+    					socialNetworkInfo.setUserId(usid);
+    					if(socialNetworkInfo.create()){
+    						System.out.println(serviceName+"sn_list entry created successfully.");
+    					}
+    					else
+    						System.out.println(serviceName+"sn_list entry failed");
+    				}else{
+    					if(DEBUG)System.out.println(serviceName+"Create new Modissense user.");
+    					
+    					User newUser = new User();
+    					newUser.setUsername(fUser.getFirstName()+fUser.getLastName());
+    					if(newUser.create()){
+    						if(DEBUG)System.out.println(serviceName+"user entry created successfully.");
+    					
+    						User theSameUser = new User(fUser.getFirstName()+fUser.getLastName());
+    						socialNetworkInfo.setUserId(theSameUser.getId());
+    						
+    						token = Utils.randomStringGenerator(32); 
+    						if(DEBUG)System.out.println(serviceName+"New user acquired token = "+token);
+    						hashClient.add(token, theSameUser.getId());
+    						
+    						if(socialNetworkInfo.create()){
+    							System.out.println(serviceName+"sn_list entry created successfully.");
+    						}
+    						else
+    							System.out.println(serviceName+"sn_list entry failed");
+    					}
+    					else
+    						System.out.println(serviceName+"user entry failed.");
+    				}
+    			} else{
+    				/*
+    				 * In the DB there is a twitter account with the specific user id. Update twitter access token
+    				 * as it may have expired and check if there is a valid session token for this user.
+    				 */
+    				
+    				HashMap<String, String> tokensToUpdate = new HashMap<String, String>();
+					tokensToUpdate.put("sn_token", "'"+accessToken+"'");
+					socialNetworkInfo.update(tokensToUpdate);
+    				
+    				if(hashClient.getSession(socialNetworkInfo.getUserId()).equals("null")){
+    					token = Utils.randomStringGenerator(32); 
+    					hashClient.add(token, socialNetworkInfo.getUserId());
+    					if(DEBUG)System.out.println(serviceName+"Existing user got new token. Token = "+token);
+					}else{
+						token=hashClient.getSession(socialNetworkInfo.getUserId());
+						if(DEBUG)System.out.println(serviceName+"Existing user logged in. Valid token= "+token);
+					}
+    			}
+    			
+    			response.sendRedirect(response.encodeRedirectURL((String)session.getAttribute("callback")+"&uid="+token));
+
+              }
+              else {
+                  System.out.println(serviceName+"Error occured: ");
+                  System.out.println(serviceName+"  code: " + result.getMeta().getCode());
+                  System.out.println(serviceName+"  type: " + result.getMeta().getErrorType());
+                  System.out.println(serviceName+"  detail: " + result.getMeta().getErrorDetail());
+              }
+              
+        } catch (FoursquareApiException e) {
+              e.printStackTrace();
+        }		
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+	}
+
+}
Index: /user/src/services/users/GetNetworks.java
===================================================================
--- /user/src/services/users/GetNetworks.java (revision 758)
+++ /user/src/services/users/GetNetworks.java (revision 758)
@@ -0,0 +1,98 @@
+package services.users;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.List;
+
+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 session.SessionManagementIM;
+
+import datastore.client.PersistentHashMapClient;
+import description.HTMLDescription;
+import description.ServiceDescription;
+
+import entites.SNDetails;
+
+
+
+public class GetNetworks extends RunningWebService {
+	
+private static final long serialVersionUID = 1L;
+private ServiceDescription description;
+    
+    /**
+     * @see HttpServlet#HttpServlet()
+     */
+    public GetNetworks() {
+        super();
+        
+        serviceName = "Get Networks";
+        
+        description = new HTMLDescription("Get Networks");
+        description.addParameter("String", "token");
+        description.addParameter("String", "format");
+        description.setReturnValue("json String");
+        description.setDescription("This service is used to get the networks, a user has connected to ModisSense platform.");   
+   
+    }
+
+	/**
+	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
+		
+		if(request.getParameter("info")!=null)
+			response.getOutputStream().print(this.description.serialize());
+		else{
+			
+			String token = (String) request.getParameter("token");
+			
+			System.out.println(serviceName+"get networks for user with token = "+token);
+			PersistentHashMapClient hashClient = new PersistentHashMapClient();
+			int usid = hashClient.getUserId(token);
+			
+			System.out.println(serviceName+"get networks for user = "+usid);
+			
+			SNDetails loggedInUserSN = new SNDetails();
+			loggedInUserSN.setUserId(usid);
+			
+			List<HashMap<String, String>> results = loggedInUserSN.read();
+			int rSize= results.size();
+			
+			String jsonToSend = "{\"networks\":[";
+		
+			for(int i=0;i<rSize;i++){
+				if(i!=rSize-1)
+					jsonToSend+="{\"Name\":\""+results.get(i).get("sn_name")+"\"},";
+				else
+					jsonToSend+="{\"Name\":\""+results.get(i).get("sn_name")+"\"}";
+			}
+			
+			jsonToSend+="]}";
+			
+			response.setHeader("Content-Type", "application/json");
+			response.setHeader("charset", "utf-8");
+			response.setCharacterEncoding("UTF-8");
+			PrintWriter out = response.getWriter();
+			
+			String format = request.getParameter("format");
+			if(format.equals("jsonp"))
+				out.write("callback("+jsonToSend+")");
+			else if(format.equals("json"))
+				out.write(jsonToSend);
+		}
+	}
+
+	/**
+	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
+	 */
+	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException{
+	}
+
+}
Index: /user/src/supportingClasses/RunningWebService.java
===================================================================
--- /user/src/supportingClasses/RunningWebService.java (revision 758)
+++ /user/src/supportingClasses/RunningWebService.java (revision 758)
@@ -0,0 +1,11 @@
+package supportingClasses;
+
+import javax.servlet.http.HttpServlet;
+
+public abstract class RunningWebService extends HttpServlet {
+	
+	protected static final long serialVersionUID = 1L;
+	protected String serviceName;
+	protected boolean DEBUG;
+	
+}
Index: /er/src/supportingClasses/TwitterCallback.java
===================================================================
--- /user/src/supportingClasses/TwitterCallback.java (revision 757)
+++  (revision )
@@ -1,182 +1,0 @@
-package supportingClasses;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-
-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 datastore.client.PersistentHashMapClient;
-
-import entites.SNDetails;
-import entites.User;
-
-
-import services.users.Register;
-import session.SessionManagementIM;
-import twitter4j.Twitter;
-import twitter4j.TwitterException;
-import twitter4j.TwitterFactory;
-
-import twitter4j.auth.AccessToken;
-import twitter4j.auth.RequestToken;
-import twitter4j.conf.ConfigurationBuilder;
-
-
-/**
- * Servlet implementation class CallbackServlet
- * 
- * 
- * @author Giagkos Mytilinis
- */
-public class TwitterCallback extends RunningWebService {
-	
-	private static final long serialVersionUID = 1L;
-
-    /**
-     * @see HttpServlet#HttpServlet()
-     */
-    public TwitterCallback() {
-        super();
-        serviceName = "Twitter Callback: ";
-        DEBUG = true;
-    }
-
-	/**
-	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-	 */
-    
-	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-		
-	
-		ConfigurationBuilder cb = new ConfigurationBuilder();
-		cb.setDebugEnabled(true)
-		  .setIncludeEntitiesEnabled(true)
-		  .setUseSSL(true);
-		
-		Twitter twitter = new TwitterFactory(cb.build()).getInstance();
-		
-		HttpSession session = request.getSession();
-				
-		twitter.setOAuthConsumer(Register.getTwitterConsumerToken(), Register.getTwitterConsumerSecret());
-		try {
-			AccessToken accessToken = twitter.getOAuthAccessToken(new RequestToken((String) session.getAttribute("token"), 
-					(String) session.getAttribute("tokenSecret")),request.getParameter("oauth_verifier"));
-            twitter.setOAuthAccessToken(accessToken);
-			
-			session.setAttribute("acc_token", accessToken.getToken());
-			session.setAttribute("acc_tokenSecret", accessToken.getTokenSecret());
-			
-			ConfigurationBuilder userConfBuilder = new ConfigurationBuilder();
-			userConfBuilder.setDebugEnabled(true)
-			  .setOAuthConsumerKey(Register.getTwitterConsumerToken())
-			  .setOAuthConsumerSecret(Register.getTwitterConsumerSecret())
-			  .setOAuthAccessToken(accessToken.getToken())
-			  .setOAuthAccessTokenSecret(accessToken.getTokenSecret())
-			  .setIncludeEntitiesEnabled(true);
-			
-			TwitterFactory tf = new TwitterFactory(userConfBuilder.build());
-			Twitter userTwitter = tf.getInstance ();
-			
-			/*
-			 * Search in the DB according to social network and userID. This search can return
-			 * either one or none results.
-			 */
-			
-			SNDetails socialNetworkInfo = new SNDetails();
-			socialNetworkInfo.setSnName("twitter");
-			socialNetworkInfo.setSnIdentifier(Long.toString(userTwitter.getId()));
-					
-			List<HashMap<String, String>> results = socialNetworkInfo.read();
-			
-			String token = null; 
-			
-			PersistentHashMapClient hashClient = new PersistentHashMapClient();
-			
-			if(results.size()==0){
-				/*
-				 *  If there are no results, we can either add a new Modissense account
-				 *  or connect a social network account to an existing one.
-				 */
-				socialNetworkInfo.setSnToken(accessToken.getToken());
-				socialNetworkInfo.setSnTokenSecret(accessToken.getTokenSecret());
-				
-				if(!((String)session.getAttribute("uidtoken")).equals("null")){
-						
-					if(DEBUG)System.out.println(serviceName+"Connect Twitter account to existing Modissense user.");
-					
-					token = (String) session.getAttribute("uidtoken");
-					if(DEBUG)System.out.println(serviceName+"user token = "+token);
-					
-					int usid = hashClient.getUserId(token);
-						
-					socialNetworkInfo.setUserId(usid);
-					if(socialNetworkInfo.create()){
-						System.out.println(serviceName+"sn_list entry created successfully.");
-					}
-					else
-						System.out.println(serviceName+"sn_list entry failed");
-				}else{
-					if(DEBUG)System.out.println(serviceName+"Create new Modissense user.");
-					
-					User newUser = new User();
-					newUser.setUsername(userTwitter.getScreenName());
-					if(newUser.create()){
-						if(DEBUG)System.out.println(serviceName+"user entry created successfully.");
-						User theSameUser = new User(userTwitter.getScreenName());
-						socialNetworkInfo.setUserId(theSameUser.getId());
-						
-						token = Utils.randomStringGenerator(32); 
-						if(DEBUG)System.out.println(serviceName+"New user acquired token = "+token);
-						hashClient.add(token, theSameUser.getId());
-						
-						if(socialNetworkInfo.create()){
-							System.out.println(serviceName+"sn_list entry created successfully.");
-						}
-						else
-							System.out.println(serviceName+"sn_list entry failed");
-					}
-					else
-						System.out.println(serviceName+"user entry failed.");
-				}
-			} else{
-				/*
-				 * In the DB there is a twitter account with the specific user id. Update twitter access token
-				 * as it may have expired and check if there is a valid session token for this user.
-				 */
-				HashMap<String, String> tokensToUpdate = new HashMap<String, String>();
-				tokensToUpdate.put("sn_token", "'"+accessToken.getToken()+"'");
-				tokensToUpdate.put("sn_token_secret", "'"+accessToken.getTokenSecret()+"'");
-				socialNetworkInfo.update(tokensToUpdate);
-				
-				if(hashClient.getSession(socialNetworkInfo.getUserId()).equals("null")){
-						
-					token = Utils.randomStringGenerator(32); 
-					hashClient.add(token, socialNetworkInfo.getUserId());
-					if(DEBUG)System.out.println(serviceName+"Existing user got new token. Token = "+token);
-				}else{
-					token=hashClient.getSession(socialNetworkInfo.getUserId());
-					if(DEBUG)System.out.println(serviceName+"Existing user logged in. Valid token= "+token);
-				}
-			}
-			
-			
-		    response.sendRedirect(response.encodeRedirectURL((String)session.getAttribute("callback")+"&uid="+token));
-			
-			
-		} catch (TwitterException e) {
-		
-			e.printStackTrace();
-		}
-	}
-
-	/**
-	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-	}
-
-}
Index: /er/src/supportingClasses/FbCallback.java
===================================================================
--- /user/src/supportingClasses/FbCallback.java (revision 757)
+++  (revision )
@@ -1,210 +1,0 @@
-package supportingClasses;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.List;
-
-import javax.net.ssl.HttpsURLConnection;
-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 org.json.simple.JSONObject;
-import org.json.simple.JSONValue;
-
-import datastore.client.PersistentHashMapClient;
-
-import services.users.Register;
-import session.SessionManagementIM;
-
-import entites.SNDetails;
-import entites.User;
-
-/**
- * 
- * Servlet implementation class CallbackServlet
- * 
- * @author Giagkos Mytilinis
- */
-
-public class FbCallback extends RunningWebService {
-	
-	private static final long serialVersionUID = 1L;
-    
-    /**
-     * @see HttpServlet#HttpServlet()
-     */
-    public FbCallback() {
-        super();
-        serviceName = "Facebook Callback: ";
-        DEBUG = true;
-    }
-
-	/**
-	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-		
-		HttpSession session = request.getSession();
-		
-		String code = request.getParameter("code");
-		
-		if(DEBUG){
-			System.out.println(serviceName+"-----Parameters-----");
-			Enumeration<String> enm = request.getParameterNames();
-			while(enm.hasMoreElements()){
-				String pname = enm.nextElement();
-				System.out.print(serviceName+"param: "+pname);
-				if(request.getParameter(pname)!=null)
-					System.out.println(serviceName+"value: "+request.getParameter(pname));
-			}
-		}
-		
-		if(((String) session.getAttribute("state")).equals(request.getParameter("state"))){
-			 if(request.getParameter("error")!=null){
-				 System.out.println(serviceName+"The user denied to authorize your app");
-				 System.out.println(serviceName+request.getParameter("error_reason"));
-				 System.out.println(serviceName+request.getParameter("error"));
-				 System.out.println(serviceName+request.getParameter("error_description"));
-			 }else{
-				 URL url = new URL("https://graph.facebook.com/oauth/access_token?client_id="+Register.getFbAppID()+"&" +
-							"redirect_uri="+OAuth.percentEncode(Register.getFbRedirectURI())+"&client_secret="+Register.getFbAppSecret() +
-							"&code="+code);
-				 HttpsURLConnection conn =(HttpsURLConnection) url.openConnection();conn.setRequestMethod("GET");
-				 conn.setDoOutput(true);
-			     
-			     StringBuffer answer = new StringBuffer();
-			     BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
-			     String line;
-			     while ((line = reader.readLine()) != null) {
-			            answer.append(line);
-			     }
-			     reader.close();
-			   
-			     String accessAnswer = answer.toString();
-			     String [] answerParts = accessAnswer.split("=");
-			     String accessToken = answerParts[1];
-			     
-			     if(DEBUG)System.out.println(serviceName+"fb access token = "+accessToken);
-			     url = new URL("https://graph.facebook.com/me?access_token="+accessToken);
-				 conn =(HttpsURLConnection) url.openConnection();conn.setRequestMethod("GET");
-				 conn.setDoOutput(true);
-			     
-			     answer = new StringBuffer();
-			     reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
-			     
-			     while ((line = reader.readLine()) != null) {
-			            answer.append(line);
-			     }
-			     reader.close();
-			    
-			     String userAnswer = answer.toString();
-			     
-			     JSONObject userObj =(JSONObject) JSONValue.parse(userAnswer);
-			     String userId = (String) userObj.get("id");
-			     String username = (String) userObj.get("username");
-			     
-			     /*
-				  * Search in the DB according to social network and userID. This search can return
-				  * either one or none results.
-				  */
-			     
-			     SNDetails socialNetworkInfo = new SNDetails();
-				 socialNetworkInfo.setSnName("facebook");
-				 socialNetworkInfo.setSnIdentifier(userId);
-					
-				 List<HashMap<String, String>> results = socialNetworkInfo.read();
-				 String token = null;
-				 
-				 PersistentHashMapClient hashClient = new PersistentHashMapClient();
-				 	
-				 if(results.size()==0){
-					 	/*
-	    				 *  If there are no results, we can either add a new Modissense account
-	    				 *  or connect a social network account to an existing one.
-	    				 */
-						
-						socialNetworkInfo.setSnToken(accessToken);
-						
-						if(!((String)session.getAttribute("uidtoken")).equals("null")){
-					
-							if(DEBUG)System.out.println(serviceName+"Connect Facebook account to existing Modissense user.");
-							
-							token = (String) session.getAttribute("uidtoken");
-							if(DEBUG)System.out.println(serviceName+"user token = "+token);
-							
-							int usid = hashClient.getUserId(token);
-							
-							socialNetworkInfo.setUserId(usid);
-							if(socialNetworkInfo.create()){
-								System.out.println(serviceName+"sn_list entry created successfully.");
-							}
-							else
-								System.out.println(serviceName+"sn_list entry failed");
-						}else{
-							if(DEBUG)System.out.println(serviceName+"Create new Modissense user.");
-							
-							User newUser = new User();
-							newUser.setUsername(username);
-							if(newUser.create()){
-								if(DEBUG)System.out.println(serviceName+"user entry created successfully.");
-								User theSameUser = new User(username);
-								socialNetworkInfo.setUserId(theSameUser.getId());			
-								
-								token = Utils.randomStringGenerator(32); 
-								if(DEBUG)System.out.println(serviceName+"New user acquired token = "+token);
-								hashClient.add(token, theSameUser.getId());
-									
-								if(socialNetworkInfo.create()){
-									System.out.println(serviceName+"sn_list entry created successfully.");
-								}
-								else
-									System.out.println(serviceName+"sn_list entry failed");
-							}
-							else
-								System.out.println(serviceName+"user entry failed.");
-						}
-					} else{
-						/*
-	    				 * In the DB there is a twitter account with the specific user id. Update twitter access token
-	    				 * as it may have expired and check if there is a valid session token for this user.
-	    				 */
-						
-						HashMap<String, String> tokensToUpdate = new HashMap<String, String>();
-						tokensToUpdate.put("sn_token", "'"+accessToken+"'");
-						socialNetworkInfo.update(tokensToUpdate);
-						
-						if(hashClient.getSession(socialNetworkInfo.getUserId()).equals("null")){
-							
-							token = Utils.randomStringGenerator(32); 
-							hashClient.add(token, socialNetworkInfo.getUserId());
-							if(DEBUG)System.out.println(serviceName+"Existing user got new token. Token = "+token);
-						}else{
-							token=hashClient.getSession(socialNetworkInfo.getUserId());
-							if(DEBUG)System.out.println(serviceName+"Existing user logged in. Valid token= "+token);
-						}
-					}
-					
-				 
-				 response.sendRedirect(response.encodeRedirectURL((String)session.getAttribute("callback")+"&uid="+token));
-			     
-			 }
-		}
-	
-		
-	}
-
-	/**
-	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-	}
-
-}
-
Index: /er/src/supportingClasses/FoursquareCallback.java
===================================================================
--- /user/src/supportingClasses/FoursquareCallback.java (revision 757)
+++  (revision )
@@ -1,172 +1,0 @@
-package supportingClasses;
-
-import java.io.IOException;
-
-import java.util.HashMap;
-import java.util.List;
-
-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 datastore.client.PersistentHashMapClient;
-
-import services.users.Register;
-import session.SessionManagementIM;
-
-
-import entites.SNDetails;
-import entites.User;
-import fi.foyt.foursquare.api.FoursquareApi;
-import fi.foyt.foursquare.api.FoursquareApiException;
-import fi.foyt.foursquare.api.Result;
-import fi.foyt.foursquare.api.entities.CompleteUser;
-
-/**
- * Servlet implementation class CallbackServlet
- * 
- * @author Giagkos Mytilinis
- */
-
-public class FoursquareCallback extends RunningWebService {
-	
-private static final long serialVersionUID = 1L;
-    
-    /**
-     * @see HttpServlet#HttpServlet()
-     */
-    public FoursquareCallback() {
-        super();
-        serviceName = "Foursquare Callback: ";
-        DEBUG = true;
-    }
-
-	/**
-	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-		
-        FoursquareApi foursquareApi = new FoursquareApi(Register.getFoursquareClientID()
-        		,Register.getFoursquareClientSecret(),Register.getFoursquareCallback());
-
-        String code=request.getParameter("code");
-        
-        HttpSession session = request.getSession();
-        
-        try {
-
-              foursquareApi.authenticateCode(code);
-              String accessToken = foursquareApi.getOAuthToken();
-               
-              Result<CompleteUser> result = foursquareApi.user("self");
-              if (result.getMeta().getCode() == 200) {
-                  CompleteUser fUser = result.getResult();
-                  if(DEBUG)System.out.println(serviceName+"foursquare user "+fUser.getId());
-                  
-                /*
-      			 * Search in the DB according to social network and userID. This search can return
-      			 * either one or none results.
-      			 */
-
-                  SNDetails socialNetworkInfo = new SNDetails();
-      			socialNetworkInfo.setSnName("foursquare");
-      			socialNetworkInfo.setSnIdentifier(fUser.getId());
-      			    			
-      			List<HashMap<String, String>> results = socialNetworkInfo.read();
-      			
-
-    			String token = null;
-    			PersistentHashMapClient hashClient = new PersistentHashMapClient();
-    			
-    			if(results.size()==0){
-    				
-    				/*
-    				 *  If there are no results, we can either add a new Modissense account
-    				 *  or connect a social network account to an existing one.
-    				 */
-    				
-    				socialNetworkInfo.setSnToken(accessToken);
-    				
-    				if(!((String)session.getAttribute("uidtoken")).equals("null")){
-    					
-    					if(DEBUG)System.out.println(serviceName+"Connect Foursquare account to existing Modissense user.");
-    					
-    					token = (String) session.getAttribute("uidtoken");
-    					if(DEBUG)System.out.println(serviceName+"user token = "+token);
-    					
-    					int usid = hashClient.getUserId(token);
-    					
-    					socialNetworkInfo.setUserId(usid);
-    					if(socialNetworkInfo.create()){
-    						System.out.println(serviceName+"sn_list entry created successfully.");
-    					}
-    					else
-    						System.out.println(serviceName+"sn_list entry failed");
-    				}else{
-    					if(DEBUG)System.out.println(serviceName+"Create new Modissense user.");
-    					
-    					User newUser = new User();
-    					newUser.setUsername(fUser.getFirstName()+fUser.getLastName());
-    					if(newUser.create()){
-    						if(DEBUG)System.out.println(serviceName+"user entry created successfully.");
-    					
-    						User theSameUser = new User(fUser.getFirstName()+fUser.getLastName());
-    						socialNetworkInfo.setUserId(theSameUser.getId());
-    						
-    						token = Utils.randomStringGenerator(32); 
-    						if(DEBUG)System.out.println(serviceName+"New user acquired token = "+token);
-    						hashClient.add(token, theSameUser.getId());
-    						
-    						if(socialNetworkInfo.create()){
-    							System.out.println(serviceName+"sn_list entry created successfully.");
-    						}
-    						else
-    							System.out.println(serviceName+"sn_list entry failed");
-    					}
-    					else
-    						System.out.println(serviceName+"user entry failed.");
-    				}
-    			} else{
-    				/*
-    				 * In the DB there is a twitter account with the specific user id. Update twitter access token
-    				 * as it may have expired and check if there is a valid session token for this user.
-    				 */
-    				
-    				HashMap<String, String> tokensToUpdate = new HashMap<String, String>();
-					tokensToUpdate.put("sn_token", "'"+accessToken+"'");
-					socialNetworkInfo.update(tokensToUpdate);
-    				
-    				if(hashClient.getSession(socialNetworkInfo.getUserId()).equals("null")){
-    					token = Utils.randomStringGenerator(32); 
-    					hashClient.add(token, socialNetworkInfo.getUserId());
-    					if(DEBUG)System.out.println(serviceName+"Existing user got new token. Token = "+token);
-					}else{
-						token=hashClient.getSession(socialNetworkInfo.getUserId());
-						if(DEBUG)System.out.println(serviceName+"Existing user logged in. Valid token= "+token);
-					}
-    			}
-    			
-    			response.sendRedirect(response.encodeRedirectURL((String)session.getAttribute("callback")+"&uid="+token));
-
-              }
-              else {
-                  System.out.println(serviceName+"Error occured: ");
-                  System.out.println(serviceName+"  code: " + result.getMeta().getCode());
-                  System.out.println(serviceName+"  type: " + result.getMeta().getErrorType());
-                  System.out.println(serviceName+"  detail: " + result.getMeta().getErrorDetail());
-              }
-              
-        } catch (FoursquareApiException e) {
-              e.printStackTrace();
-        }		
-	}
-
-	/**
-	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-	}
-
-}
Index: /er/src/supportingClasses/GetNetworks.java
===================================================================
--- /user/src/supportingClasses/GetNetworks.java (revision 436)
+++  (revision )
@@ -1,96 +1,0 @@
-package supportingClasses;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.HashMap;
-import java.util.List;
-
-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 session.SessionManagementIM;
-
-import datastore.client.PersistentHashMapClient;
-import description.HTMLDescription;
-import description.ServiceDescription;
-
-import entites.SNDetails;
-
-
-
-public class GetNetworks extends HttpServlet {
-	
-private static final long serialVersionUID = 1L;
-private ServiceDescription description;
-    
-    /**
-     * @see HttpServlet#HttpServlet()
-     */
-    public GetNetworks() {
-        super();
-        
-        description = new HTMLDescription("Get Networks");
-        description.addParameter("String", "token");
-        description.addParameter("String", "format");
-        description.setReturnValue("json String");
-        description.setDescription("This service is used to get the networks, a user has connected to ModisSense platform.");   
-   
-    }
-
-	/**
-	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
-		
-		if(request.getParameter("info")!=null)
-			response.getOutputStream().print(this.description.serialize());
-		else{
-			
-			String token = (String) request.getParameter("token");
-			
-			System.out.println("token = "+token);
-			PersistentHashMapClient hashClient = new PersistentHashMapClient();
-			int usid = hashClient.getUserId(token);
-			
-			System.out.println("phra uid = "+usid);
-			
-			SNDetails loggedInUserSN = new SNDetails();
-			loggedInUserSN.setUserId(usid);
-			
-			List<HashMap<String, String>> results = loggedInUserSN.read();
-			int rSize= results.size();
-			
-			String jsonToSend = "{\"networks\":[";
-		
-			for(int i=0;i<rSize;i++){
-				if(i!=rSize-1)
-					jsonToSend+="{\"Name\":\""+results.get(i).get("sn_name")+"\"},";
-				else
-					jsonToSend+="{\"Name\":\""+results.get(i).get("sn_name")+"\"}";
-			}
-			
-			jsonToSend+="]}";
-			
-			response.setHeader("Content-Type", "application/json");
-			response.setHeader("charset", "utf-8");
-			response.setCharacterEncoding("UTF-8");
-			PrintWriter out = response.getWriter();
-			
-			String format = request.getParameter("format");
-			if(format.equals("jsonp"))
-				out.write("callback("+jsonToSend+")");
-			else if(format.equals("json"))
-				out.write(jsonToSend);
-		}
-	}
-
-	/**
-	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-	 */
-	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException{
-	}
-
-}
