Skip to content

Commit

Permalink
Added client side asking for a sessionID and using sessionID on requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrosado committed May 17, 2017
1 parent ec65fa8 commit cc3cd48
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
Expand Down Expand Up @@ -334,7 +333,7 @@ protected Boolean doInBackground(Void... params) {

// Simulate network access.
final ServerActions serverActions = new ServerActions(mActivity);
serverActions.goodLogin(mUsername, mPassword, new OnResponseListener<Boolean>() {
serverActions.login(mUsername, mPassword, new OnResponseListener<Boolean>() {
@Override
public void onHTTPResponse(Boolean response) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@
*/
public class ServerActions {

private final static String addr = "194.210.223.59";
private final static String addr = "194.210.220.237";
private final static String port = "8443";
private final static String endpoint = "https://"+addr+":"+port+"/api";
private static RequestQueue queue;
private static String username = "";
private static String password = "";
private static String sessionID = "";
private static String sessionIdURL = "";



public ServerActions(Context context) {
Expand Down Expand Up @@ -82,7 +85,8 @@ public void onErrorResponse(VolleyError error) {


public void insertProfile( Profile p,final OnResponseListener listener){
String url = endpoint+"/profile/create";

String url = generateURL("/profile/create");
try{
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(gson.toJson(p));
Expand All @@ -97,7 +101,7 @@ public void insertProfile( Profile p,final OnResponseListener listener){


public void removeProfile( Profile p,OnResponseListener listener){
String url = endpoint+"/profile/delete";
String url = generateURL("/profile/delete");
try{
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(gson.toJson(p));
Expand All @@ -110,7 +114,7 @@ public void removeProfile( Profile p,OnResponseListener listener){
}

public List<Profile> getMyProfileKeys(final OnResponseListener listener) {
String url = endpoint + "/profile/myList";
String url = generateURL("/profile/myList");

final List<Profile> profiles = new ArrayList<>();
JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,username,password, new Response.Listener<JSONArray>() {
Expand Down Expand Up @@ -148,7 +152,7 @@ public void onErrorResponse(VolleyError error) {
}

public List<Profile> getProfileKeys(final OnResponseListener listener) {
String url = endpoint + "/profile/listAll";
String url = generateURL("/profile/listAll");

final List<Profile> profiles = new ArrayList<>();
HttpsTrustManager.allowAllSSL();
Expand Down Expand Up @@ -204,7 +208,7 @@ public void onErrorResponse(VolleyError error) {
}

public void createUser(String username,String password,final OnResponseListener listener){
String url = endpoint +"/user/create";
String url = generateURL("/user/create");

try{
JSONObject jsonObject = new JSONObject();
Expand All @@ -221,7 +225,7 @@ public void createUser(String username,String password,final OnResponseListener
}

public void updatePassword(String username,String password,final OnResponseListener listener) throws Exception {
String url = endpoint+"/user/updatePassword";
String url = generateURL("/user/updatePassword");

Log.e(TAG, "updatePassword: Still need update" );

Expand All @@ -238,7 +242,7 @@ public void updatePassword(String username,String password,final OnResponseListe
}

public void createLocation(Location location,final OnResponseListener listener){
String url = endpoint+"/location/create";
String url = generateURL("/location/create");

try{
Gson gson = new Gson();
Expand All @@ -255,7 +259,7 @@ public void createLocation(Location location,final OnResponseListener listener){
}

public void getMyMessages(final OnResponseListener listener){
String url = endpoint+"/message/myMessages";
String url = generateURL("/message/myMessages");

final List<Message> messages = new ArrayList<>();
final Gson gson = new Gson();
Expand Down Expand Up @@ -288,7 +292,7 @@ public void onErrorResponse(VolleyError error) {
}

public static List<Message> getMessagesFromLocation(Location location, final OnResponseListener listener){
String url = endpoint+"/message/getMessagesByLocation";
String url = generateURL("/message/getMessagesByLocation");

final List<Message> messages = new ArrayList<>();
Gson gson = new Gson();
Expand Down Expand Up @@ -335,7 +339,7 @@ public void onErrorResponse(VolleyError error) {
}

public List<Location> getAllLocations(final OnResponseListener listener) {
String url = endpoint + "/location/list";
String url = generateURL("/location/list");

final List<Location> locations = new ArrayList<>();
HttpsTrustManager.allowAllSSL();
Expand Down Expand Up @@ -371,7 +375,7 @@ public void onErrorResponse(VolleyError error) {
}

public void getListLocationHash(final OnResponseListener<String> listener){
String url = endpoint + "/location/list/hash";
String url = generateURL("/location/list/hash");
HttpsTrustManager.allowAllSSL();
JsonObjectAuthenticatedRequest request = new JsonObjectAuthenticatedRequest(Request.Method.GET,url,username,password,null,new Response.Listener<JSONObject>() {

Expand All @@ -397,7 +401,7 @@ public void onErrorResponse(VolleyError error) {


public List<Location> getNearLocations(LocationQuery query, final OnResponseListener listener){
String url = endpoint+"/location/nearbyLocations";
String url = generateURL("/location/nearbyLocations");

final List<Location> locations = new ArrayList<>();
//Log.d(TAG, "request: "+query.toJSON());
Expand Down Expand Up @@ -436,7 +440,7 @@ public void onErrorResponse(VolleyError error) {


public void createMessage(MessageServer message,final OnResponseListener listener) {
String url = endpoint+"/message/create";
String url = generateURL("/message/create");
try {
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(gson.toJson(message));
Expand All @@ -448,7 +452,7 @@ public void createMessage(MessageServer message,final OnResponseListener listene
}

public void removeMessage( MessageServer m,OnResponseListener listener){
String url = endpoint+"/message/delete";
String url = generateURL("/message/delete");
try {
url += "?id="+URLEncoder.encode(m.getId().toString(),"UTF-8");
//Log.d(TAG, "removeMessage:"+m.getId());
Expand All @@ -462,16 +466,16 @@ public void removeMessage( MessageServer m,OnResponseListener listener){

public void removeLocation(String name,OnResponseListener listener){
try {
String url = endpoint+"/location/delete";
url+="?name="+URLEncoder.encode(name,"UTF-8");
String url = generateURL("/location/delete");
url+="&name="+URLEncoder.encode(name,"UTF-8");
makeAuthenticatedRequest(Request.Method.DELETE, url, null, listener);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

public void goodLogin(final String username, final String password, final OnResponseListener<Boolean> listener){
String url = endpoint+"/profile/myList";
public void login(final String username, final String password, final OnResponseListener<Boolean> listener){
String url = endpoint+"/user/login";

final boolean[] loggedin = {false};
HttpsTrustManager.allowAllSSL();
Expand All @@ -480,7 +484,13 @@ public void goodLogin(final String username, final String password, final OnResp
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
Log.d(TAG,"String session id: "+response.toString());
try {
sessionID = URLEncoder.encode(response,"UTF-8");
sessionIdURL = "?sessionID="+sessionID;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
listener.onHTTPResponse(true);
}
},
Expand Down Expand Up @@ -512,5 +522,9 @@ public Map<String, String> getHeaders() throws AuthFailureError {
queue.add(strReq);
}

private static String generateURL(String path){
return endpoint+path+sessionIdURL;
}

}

0 comments on commit cc3cd48

Please sign in to comment.