Commit 741a5f80 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Finnalisation

Add json response checker
Handle http errors
Change connectDialog to casDialog (for better understanding)
parent 9cda9714
...@@ -14,14 +14,10 @@ import android.widget.EditText; ...@@ -14,14 +14,10 @@ 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 java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import fr.utc.simde.payutc.tools.HTTPRequest;
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;
import fr.utc.simde.payutc.tools.Dialog; import fr.utc.simde.payutc.tools.Dialog;
...@@ -73,7 +69,7 @@ public class MainActivity extends NFCActivity { ...@@ -73,7 +69,7 @@ public class MainActivity extends NFCActivity {
usernameButton.setOnClickListener(new View.OnClickListener() { usernameButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
connectDialog(); casDialog();
} }
}); });
} }
...@@ -265,7 +261,7 @@ public class MainActivity extends NFCActivity { ...@@ -265,7 +261,7 @@ public class MainActivity extends NFCActivity {
} }
if (nemopaySession.isConnected()) { if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected)); dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return; return;
} }
...@@ -300,9 +296,9 @@ public class MainActivity extends NFCActivity { ...@@ -300,9 +296,9 @@ public class MainActivity extends NFCActivity {
dialog.createDialog(alertDialogBuilder, pinInput); dialog.createDialog(alertDialogBuilder, pinInput);
} }
protected void connectDialog() { protected void casDialog() {
if (nemopaySession.isConnected()) { if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected)); dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return; return;
} }
...@@ -325,7 +321,7 @@ public class MainActivity extends NFCActivity { ...@@ -325,7 +321,7 @@ public class MainActivity extends NFCActivity {
Toast.makeText(MainActivity.this, R.string.username_and_password_required, Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, R.string.username_and_password_required, Toast.LENGTH_SHORT).show();
dialogInterface.cancel(); dialogInterface.cancel();
connectDialog(); casDialog();
} }
else { else {
try { try {
......
...@@ -78,7 +78,12 @@ public class HTTPRequest { ...@@ -78,7 +78,12 @@ public class HTTPRequest {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse()); Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
} }
catch (Exception e) { catch (Exception e) {
Log.e(LOG_TAG, e.getMessage()); try {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", error: " + e.getMessage());
}
catch (Exception e2) {
Log.e(LOG_TAG, e.getMessage());
}
} }
return getResponseCode(); return getResponseCode();
...@@ -115,7 +120,12 @@ public class HTTPRequest { ...@@ -115,7 +120,12 @@ public class HTTPRequest {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse()); Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
} }
catch (Exception e) { catch (Exception e) {
Log.e(LOG_TAG, e.getMessage()); try {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", error: " + e.getMessage());
}
catch (Exception e2) {
Log.e(LOG_TAG, e.getMessage());
}
} }
return getResponseCode(); return getResponseCode();
...@@ -179,6 +189,17 @@ public class HTTPRequest { ...@@ -179,6 +189,17 @@ public class HTTPRequest {
public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); } public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); }
public String getResponse() throws IOException { return response; } public String getResponse() throws IOException { return response; }
public Boolean isJsonResponse() {
try {
jsonToMap(response);
}
catch (Exception e) {
return false;
}
return true;
}
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); } protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); }
protected String args2String(Map<String, String> args, Boolean isGet) throws UnsupportedEncodingException { protected String args2String(Map<String, String> args, Boolean isGet) throws UnsupportedEncodingException {
String data = ""; String data = "";
......
...@@ -64,6 +64,7 @@ public class NemopaySession { ...@@ -64,6 +64,7 @@ public class NemopaySession {
public String getName() { return this.name; } public String getName() { return this.name; }
public String getKey() { return this.key; } public String getKey() { return this.key; }
public String getUsername() { return username; }
public HTTPRequest getRequest() { return this.request; } public HTTPRequest getRequest() { return this.request; }
public void getCasUrl() throws IOException { public void getCasUrl() throws IOException {
...@@ -77,7 +78,7 @@ public class NemopaySession { ...@@ -77,7 +78,7 @@ public class NemopaySession {
put("app_desc", description); put("app_desc", description);
}}); }});
if (request.getResponseCode() == 200) if (request.getResponseCode() == 200 && request.isJsonResponse())
this.key = request.getJsonResponse().get("app_key"); this.key = request.getJsonResponse().get("app_key");
this.request = request; this.request = request;
...@@ -88,7 +89,11 @@ public class NemopaySession { ...@@ -88,7 +89,11 @@ public class NemopaySession {
put("key", key); put("key", key);
}}); }});
Map<String, String> response = request.getJsonResponse(); Map<String, String> response;
if (request.getResponseCode() == 200 && request.isJsonResponse())
response = request.getJsonResponse();
else
throw new Exception("Not authentified");
if (response.containsKey("sessionid") && response.containsKey("name")) { if (response.containsKey("sessionid") && response.containsKey("name")) {
this.session = response.get("sessionid"); this.session = response.get("sessionid");
...@@ -96,7 +101,7 @@ public class NemopaySession { ...@@ -96,7 +101,7 @@ public class NemopaySession {
this.key = key; this.key = key;
} }
else else
throw new Exception("Not authentified"); throw new Exception("No correct informations");
} }
public void loginBadge(final String idBadge, final String pin) throws Exception { public void loginBadge(final String idBadge, final String pin) throws Exception {
...@@ -105,15 +110,18 @@ public class NemopaySession { ...@@ -105,15 +110,18 @@ public class NemopaySession {
put("pin", pin); put("pin", pin);
}}); }});
Map<String, String> response = request.getJsonResponse(); Map<String, String> response;
if (request.getResponseCode() == 200 && request.isJsonResponse())
response = request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) { if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid"); this.session = response.get("sessionid");
this.username = response.get("username"); this.username = response.get("username");
} }
else { else
throw new Exception("Not connected"); throw new Exception("No correct informations");
}
} }
public void loginCas(final String ticket, final String service) throws Exception { public void loginCas(final String ticket, final String service) throws Exception {
...@@ -122,14 +130,18 @@ public class NemopaySession { ...@@ -122,14 +130,18 @@ public class NemopaySession {
put("service", service); put("service", service);
}}); }});
Map<String, String> response = request.getJsonResponse(); Map<String, String> response;
if (request.getResponseCode() == 200 && request.isJsonResponse())
response = request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) { if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid"); this.session = response.get("sessionid");
this.username = response.get("username"); this.username = response.get("username");
} }
else else
throw new Exception("Not connected"); throw new Exception("No correct informations");
} }
protected void request(final String method, final String service) throws IOException { request(method, service, new HashMap<String, String>()); } protected void request(final String method, final String service) throws IOException { request(method, service, new HashMap<String, String>()); }
......
...@@ -51,5 +51,5 @@ ...@@ -51,5 +51,5 @@
<string name="key_name_required">Nom requis !</string> <string name="key_name_required">Nom requis !</string>
<string name="key_add_explication">Il est possible d\'ajouter directement une clé d\'application si celle-ci est connue</string> <string name="key_add_explication">Il est possible d\'ajouter directement une clé d\'application si celle-ci est connue</string>
<string name="key_remove_temp">L\'application a été désauthentifiée temporairement. Il suffit de relancer l\'application pour qu\'elle soit de nouveau connectée</string> <string name="key_remove_temp">L\'application a été désauthentifiée temporairement. Il suffit de relancer l\'application pour qu\'elle soit de nouveau connectée</string>
<string name="already_connected">Déjà connecté !</string> <string name="already_connected">Déjà connecté sous</string>
</resources> </resources>
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