Commit 519e6e57 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Optimization

Handle getCASUrl
No more Map objects for http response, only JSONObject objects
Reponse code sent at each request
Handle CAS Url by 2 ways
parent 425c34d7
...@@ -14,9 +14,15 @@ import android.widget.EditText; ...@@ -14,9 +14,15 @@ import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import fr.utc.simde.payutc.tools.NFCActivity; import fr.utc.simde.payutc.tools.NFCActivity;
import fr.utc.simde.payutc.tools.CASConnexion; import fr.utc.simde.payutc.tools.CASConnexion;
...@@ -45,7 +51,7 @@ public class MainActivity extends NFCActivity { ...@@ -45,7 +51,7 @@ public class MainActivity extends NFCActivity {
nemopaySession = new NemopaySession(); nemopaySession = new NemopaySession();
casConnexion = new CASConnexion(nemopaySession); casConnexion = new CASConnexion(nemopaySession);
sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE); sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE);
final String key = sharedPreferences.getString("key", ""); final String key = sharedPreferences.getString("key", "");
if (!key.equals("")) if (!key.equals(""))
setKey(key); setKey(key);
...@@ -114,7 +120,7 @@ public class MainActivity extends NFCActivity { ...@@ -114,7 +120,7 @@ public class MainActivity extends NFCActivity {
@Override @Override
public void run() { public void run() {
try { try {
nemopaySession.loginApp(key); nemopaySession.loginApp(key, casConnexion);
Thread.sleep(100); Thread.sleep(100);
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, e.getMessage()); Log.e(LOG_TAG, e.getMessage());
...@@ -143,11 +149,40 @@ public class MainActivity extends NFCActivity { ...@@ -143,11 +149,40 @@ public class MainActivity extends NFCActivity {
protected void connectWithCAS(final String username, final String password) throws InterruptedException { protected void connectWithCAS(final String username, final String password) throws InterruptedException {
dialog.dismiss(); dialog.dismiss();
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_in_connection), true); final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_in_url), true);
loading.setCancelable(false); loading.setCancelable(false);
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
if (casConnexion.getUrl().equals("")) {
try {
if (nemopaySession.getCASUrl() == 200) {
Log.d(LOG_TAG, nemopaySession.getRequest().getResponse());
String url = nemopaySession.getRequest().getResponse();
casConnexion.setUrl(url.substring(1, url.length() - 1));
}
else
throw new Exception("Impossible to get CAS url");
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (casConnexion.getUrl().equals("")) {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_url));
}
else
loading.setMessage(getResources().getString(R.string.cas_in_connection));
}
});
if (casConnexion.getUrl().equals(""))
return;
try { try {
casConnexion.connect(username, password); casConnexion.connect(username, password);
Thread.sleep(100); Thread.sleep(100);
...@@ -167,50 +202,52 @@ public class MainActivity extends NFCActivity { ...@@ -167,50 +202,52 @@ public class MainActivity extends NFCActivity {
} }
}); });
if (casConnexion.isConnected()) { if (!casConnexion.isConnected())
try { return;
casConnexion.addService(service);
Thread.sleep(100); try {
} catch (Exception e) { casConnexion.addService(service);
Log.e(LOG_TAG, e.getMessage()); Thread.sleep(100);
} } catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
if (casConnexion.isServiceAdded()) if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection)); loading.setMessage(getResources().getString(R.string.nemopay_connection));
else { else {
loading.dismiss(); loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_adding)); dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_adding));
}
} }
}); }
});
if (casConnexion.isServiceAdded()) { if (!casConnexion.isServiceAdded())
try { return;
nemopaySession.loginCas(casConnexion.getTicket(), service);
Thread.sleep(1000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() { try {
@Override nemopaySession.loginCas(casConnexion.getTicket(), service);
public void run() { Thread.sleep(1000);
loading.dismiss(); } catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
if (!nemopaySession.isConnected())
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
}
});
}
} }
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (!nemopaySession.isConnected())
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
}
});
} }
}.start(); }.start();
} }
......
...@@ -18,28 +18,13 @@ public class CASConnexion { ...@@ -18,28 +18,13 @@ public class CASConnexion {
this.username = ""; this.username = "";
this.location = ""; this.location = "";
this.ticket = ""; this.ticket = "";
new Thread(new Runnable(){
@Override
public void run() {
try {
HTTPRequest http = new HTTPRequest("https://api.nemopay.net/services/POSS3/getCasUrl?system_id=payutc"); // Remettre le getCasUrl
http.post();
url = http.getResponse();
url = url.substring(1, url.length() - 1);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
}).start();
} }
public void setUsername(final String username) { this.username = username; } public void setUsername(final String username) { this.username = username; }
public String getUsername() { return this.username; } public String getUsername() { return this.username; }
public String getTicket() { return this.ticket; } public String getTicket() { return this.ticket; }
public String getUrl() { return this.url; } public String getUrl() { return this.url; }
public void setUrl(final String url) { this.url = url; }
public void connect(final String username, final String password) throws Exception { public void connect(final String username, final String password) throws Exception {
this.username = username; this.username = username;
......
...@@ -40,20 +40,6 @@ public class HTTPRequest { ...@@ -40,20 +40,6 @@ public class HTTPRequest {
this.cookies = new HashMap<String, String>(); this.cookies = new HashMap<String, String>();
} }
public static Map<String, String> jsonToMap(String t) throws JSONException {
Map<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while (keys.hasNext()){
String key = (String) keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
return map;
}
public int get() { public int get() {
String get = null; String get = null;
...@@ -186,12 +172,12 @@ public class HTTPRequest { ...@@ -186,12 +172,12 @@ public class HTTPRequest {
this.response = builder.toString(); this.response = builder.toString();
} }
public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); } public JSONObject getJsonResponse() throws IOException, JSONException { return new JSONObject(response); }
public String getResponse() throws IOException { return response; } public String getResponse() throws IOException { return response; }
public Boolean isJsonResponse() { public Boolean isJsonResponse() {
try { try {
jsonToMap(response); new JSONObject(response);
} }
catch (Exception e) { catch (Exception e) {
return false; return false;
......
package fr.utc.simde.payutc.tools; package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.util.Log; import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import fr.utc.simde.payutc.MainActivity;
import fr.utc.simde.payutc.R;
/** /**
* Created by Samy on 24/10/2017. * Created by Samy on 24/10/2017.
...@@ -67,93 +55,111 @@ public class NemopaySession { ...@@ -67,93 +55,111 @@ public class NemopaySession {
public String getUsername() { return username; } public String getUsername() { return username; }
public HTTPRequest getRequest() { return this.request; } public HTTPRequest getRequest() { return this.request; }
public void getCasUrl() throws IOException { public int getCASUrl() throws IOException {
request("POSS3", "getCasUrl"); return request("POSS3", "getCasUrl");
} }
public void registerApp(final String name, final String description, final String service) throws IOException, JSONException { public int registerApp(final String name, final String description, final String service) throws IOException, JSONException {
request("KEY", "registerApplication", new HashMap<String, String>() {{ int reponseCode = request("KEY", "registerApplication", new HashMap<String, String>() {{
put("app_url", service); put("app_url", service);
put("app_name", name); put("app_name", name);
put("app_desc", description); put("app_desc", description);
}}); }});
if (request.getResponseCode() == 200 && request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
this.key = request.getJsonResponse().get("app_key"); this.key = this.request.getJsonResponse().getString("app_key");
this.request = request; return reponseCode;
} }
public void loginApp(final String key) throws Exception { public int loginApp(final String key, CASConnexion casConnexion) throws Exception {
request("POSS3", "loginApp", new HashMap<String, String>() {{ int reponseCode = loginApp(key);
JSONObject response = getRequest().getJsonResponse();
if (response.has("config") && ((JSONObject) response.get("config")).has("cas_url"))
casConnexion.setUrl(((JSONObject) response.get("config")).getString("cas_url"));
else
throw new Exception("No correct informations");
return reponseCode;
}
public int loginApp(final String key) throws Exception {
int reponseCode = request("POSS3", "loginApp", new HashMap<String, String>() {{
put("key", key); put("key", key);
}}); }});
Map<String, String> response; JSONObject response;
if (request.getResponseCode() == 200 && request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
response = request.getJsonResponse(); response = this.request.getJsonResponse();
else else
throw new Exception("Not authentified"); throw new Exception("Not authentified");
if (response.containsKey("sessionid") && response.containsKey("name")) { if (response.has("sessionid") && response.has("name")) {
this.session = response.get("sessionid"); this.session = response.getString("sessionid");
this.name = response.get("name"); this.name = response.getString("name");
this.key = key; this.key = key;
} }
else else
throw new Exception("No correct informations"); throw new Exception("No correct informations");
return reponseCode;
} }
public void loginBadge(final String idBadge, final String pin) throws Exception { public int loginBadge(final String idBadge, final String pin) throws Exception {
request("POSS3", "loginBadge2", new HashMap<String, String>() {{ int reponseCode = request("POSS3", "loginBadge2", new HashMap<String, String>() {{
put("badge_id", idBadge); put("badge_id", idBadge);
put("pin", pin); put("pin", pin);
}}); }});
Map<String, String> response; JSONObject response;
if (request.getResponseCode() == 200 && request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
response = request.getJsonResponse(); response = this.request.getJsonResponse();
else else
throw new Exception("Not connected"); throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) { if (response.has("sessionid") && response.has("username")) {
this.session = response.get("sessionid"); this.session = response.getString("sessionid");
this.username = response.get("username"); this.username = response.getString("username");
} }
else else
throw new Exception("No correct informations"); throw new Exception("No correct informations");
return reponseCode;
} }
public void loginCas(final String ticket, final String service) throws Exception { public int loginCas(final String ticket, final String service) throws Exception {
request("POSS3", "loginCas2", new HashMap<String, String>() {{ int reponseCode = request("POSS3", "loginCas2", new HashMap<String, String>() {{
put("ticket", ticket); put("ticket", ticket);
put("service", service); put("service", service);
}}); }});
Map<String, String> response; JSONObject response;
if (request.getResponseCode() == 200 && request.isJsonResponse()) if (reponseCode == 200 && this.request.isJsonResponse())
response = request.getJsonResponse(); response = this.request.getJsonResponse();
else else
throw new Exception("Not connected"); throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) { if (response.has("sessionid") && response.has("username")) {
this.session = response.get("sessionid"); this.session = response.getString("sessionid");
this.username = response.get("username"); this.username = response.getString("username");
} }
else else
throw new Exception("No correct informations"); throw new Exception("No correct informations");
return reponseCode;
} }
protected void request(final String method, final String service) throws IOException { request(method, service, new HashMap<String, String>()); } protected int request(final String method, final String service) throws IOException { return request(method, service, new HashMap<String, String>()); }
protected void request(final String method, final String service, final Map<String, String> postArgs) throws IOException { protected int request(final String method, final String service, final Map<String, String> postArgs) throws IOException {
HTTPRequest request = new HTTPRequest(url + method + "/" + service);
Log.d(LOG_TAG, "url: " + url + method + "/" + service); Log.d(LOG_TAG, "url: " + url + method + "/" + service);
request.setGet(getArgs); this.request = new HTTPRequest(url + method + "/" + service);
request.setPost(postArgs); this.request.setGet(getArgs);
request.setCookies(this.cookies); this.request.setPost(postArgs);
this.request.setCookies(this.cookies);
request.post(); int reponseCode = this.request.post();
this.cookies = request.getCookies(); this.cookies = request.getCookies();
this.request = request;
return reponseCode;
} }
} }
...@@ -26,9 +26,11 @@ ...@@ -26,9 +26,11 @@
<string name="nfc_availability">L\'application nécessite que le NFC soit disponible sur le téléphone pour que celle-ci fonctionne correctement. Elle ne pourra donc être utilisée sur ce téléphone.</string> <string name="nfc_availability">L\'application nécessite que le NFC soit disponible sur le téléphone pour que celle-ci fonctionne correctement. Elle ne pourra donc être utilisée sur ce téléphone.</string>
<string name="nfc_accessibility">L\'application nécessite que le NFC soit activé pour fonctionner correctement.</string> <string name="nfc_accessibility">L\'application nécessite que le NFC soit activé pour fonctionner correctement.</string>
<string name="cas_connection">Connexion via CAS</string> <string name="cas_connection">Connexion via CAS</string>
<string name="cas_in_url">Récupération du lien CAS...</string>
<string name="cas_in_connection">Connexion au CAS...</string> <string name="cas_in_connection">Connexion au CAS...</string>
<string name="cas_in_service_adding">Ajout du service au CAS...</string> <string name="cas_in_service_adding">Ajout du service au CAS...</string>
<string name="cas_in_service_connection">Connexion au service via le CAS...</string> <string name="cas_in_service_connection">Connexion au service via le CAS...</string>
<string name="cas_error_url">Impossible de joindre le CAS</string>
<string name="cas_error_connection">Login et/ou mot de passe non reconnu par le CAS</string> <string name="cas_error_connection">Login et/ou mot de passe non reconnu par le CAS</string>
<string name="cas_error_service_adding">Une erreur a été détectée lors de l\'ajout du service</string> <string name="cas_error_service_adding">Une erreur a été détectée lors de l\'ajout du service</string>
<string name="cas_error_service_connection">Erreur lors de la connexion au service depuis le CAS</string> <string name="cas_error_service_connection">Erreur lors de la connexion au service depuis le CAS</string>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment