Commit e08db2bd authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Optimization

Autohandle HTTP errors and send exceptions
Change idBadge to badgeId
parent fb5a2313
......@@ -47,7 +47,7 @@ public class FoundationListActivity extends BaseActivity {
}
@Override
protected void onIdentification(String idBadge) {}
protected void onIdentification(final String badgeId) {}
@Override
protected void onDestroy() {
......
......@@ -78,9 +78,9 @@ public class MainActivity extends BaseActivity {
}
@Override
protected void onIdentification(final String idBadge) {
protected void onIdentification(final String badgeId) {
if (!dialog.isShowing())
badgeDialog(idBadge);
badgeDialog(badgeId);
}
@Override
......@@ -142,13 +142,9 @@ public class MainActivity extends BaseActivity {
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");
nemopaySession.getCASUrl();
String url = nemopaySession.getRequest().getResponse();
casConnexion.setUrl(url.substring(1, url.length() - 1));
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
......@@ -237,7 +233,7 @@ public class MainActivity extends BaseActivity {
}.start();
}
protected void connectWithBadge(final String idBadge, final String pin) {
protected void connectWithBadge(final String badgeId, final String pin) {
if (!nemopaySession.isRegistered() || nemopaySession.isConnected())
return;
......@@ -246,24 +242,32 @@ public class MainActivity extends BaseActivity {
@Override
public void run() {
try {
nemopaySession.loginBadge(idBadge, pin);
nemopaySession.loginBadge(badgeId, pin);
Thread.sleep(100);
} catch (Exception e) {
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
if (nemopaySession.getRequest().getResponseCode() == 400)
dialog.errorDialog(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_pin_error_not_recognized));
else
dialog.errorDialog(MainActivity.this, getString(R.string.badge_dialog), e.getMessage());
}
});
}
if (!nemopaySession.isConnected())
return;
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
try {
if (nemopaySession.isConnected())
startFoundationListActivity(MainActivity.this);
else if (nemopaySession.getRequest().getResponseCode() == 400)
dialog.errorDialog(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_pin_error_not_recognized));
else
dialog.errorDialog(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_error_no_rights) + ".\n" + nemopaySession.needRights(MainActivity.this));
startFoundationListActivity(MainActivity.this);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
......@@ -273,7 +277,7 @@ public class MainActivity extends BaseActivity {
}).start();
}
protected void badgeDialog(final String idBadge) {
protected void badgeDialog(final String badgeId) {
if (!nemopaySession.isRegistered()) {
dialog.errorDialog(MainActivity.this, getString(R.string.badge_connection), getString(R.string.badge_app_not_registered));
return;
......@@ -298,10 +302,10 @@ public class MainActivity extends BaseActivity {
if (pinInput.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, R.string.pin_required, Toast.LENGTH_SHORT).show();
dialogInterface.cancel();
badgeDialog(idBadge);
badgeDialog(badgeId);
}
else {
connectWithBadge(idBadge, pinInput.getText().toString());
connectWithBadge(badgeId, pinInput.getText().toString());
dialogInterface.cancel();
}
}
......
......@@ -53,7 +53,7 @@ public abstract class NFCActivity extends Activity {
this.registerReceiver(NFCReceiver, filter);
}
protected abstract void onIdentification(final String idBadge);
protected abstract void onIdentification(final String badgeId);
protected Boolean identifierIsAvailable() { return NFCAdapter != null; }
......@@ -71,9 +71,9 @@ public abstract class NFCActivity extends Activity {
protected void onNewIntent(Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(NFCAdapter.ACTION_TAG_DISCOVERED)) {
String idBadge = ByteArrayToHexString(((Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)).getId());
Log.d(LOG_TAG, "ID: " + idBadge);
onIdentification(idBadge);
String badgeId = ByteArrayToHexString(((Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)).getId());
Log.d(LOG_TAG, "ID: " + badgeId);
onIdentification(badgeId);
}
}
......
......@@ -29,14 +29,20 @@ public class NemopaySession {
private String foundationName;
private HTTPRequest request;
private String[] rightsNeeded;
private String noRight;
private String noRights;
private String noRightsNeeded;
private String serviceText;
private String notFound;
private String badRequest;
private Map<String, String> cookies = new HashMap<String, String>();
private final Map<String, String> getArgs = new HashMap<String, String>() {{
put("system_id", "payutc");
}};
public NemopaySession(Activity activity) {
public NemopaySession(final Activity activity) {
this.name = "";
this.key = "";
this.session = "";
......@@ -48,6 +54,13 @@ public class NemopaySession {
String[] values = activity.getResources().getStringArray(R.array.rights_values);
for (int i = 0; i < Math.min(keys.length, values.length); ++i)
this.allRights.put(keys[i], values[i]);
this.noRightsNeeded = activity.getString(R.string.no_need_rights);
this.noRight = activity.getString(R.string.no_right);
this.noRights = activity.getString(R.string.no_rights);
this.serviceText = activity.getString(R.string.service);
this.notFound = activity.getString(R.string.not_found);
this.badRequest = activity.getString(R.string.bad_request);
}
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
......@@ -79,6 +92,24 @@ public class NemopaySession {
public int getFoundationId() { return foundationId; }
public String getFoundationName() { return foundationName; }
public int getBuyerInfo(final String badgeId) throws Exception {
if (!isConnected())
throw new Exception("Not connected");
return request(
"POSS3",
"getBuyerInfo",
new HashMap<String, String>() {{
put("badge_id", badgeId);
if (foundationId != -1)
put("fun_id", Integer.toString(foundationId));
}},
new String[]{
"sale"
}
);
}
public int getArticles() throws Exception {
if (!isConnected())
throw new Exception("Not connected");
......@@ -130,7 +161,7 @@ public class NemopaySession {
);
}
public int getCASUrl() throws IOException {
public int getCASUrl() throws Exception {
return request(
"POSS3",
"getCasUrl"
......@@ -200,7 +231,7 @@ public class NemopaySession {
return reponseCode;
}
public int loginBadge(final String idBadge, final String pin) throws Exception {
public int loginBadge(final String badgeId, final String pin) throws Exception {
if (!isRegistered())
throw new Exception("Not registered");
......@@ -208,7 +239,7 @@ public class NemopaySession {
"POSS3",
"loginBadge2",
new HashMap<String, String>() {{
put("badge_id", idBadge);
put("badge_id", badgeId);
put("pin", pin);
}},
new String[]{
......@@ -258,16 +289,16 @@ public class NemopaySession {
return reponseCode;
}
public String needRights(Activity activity) {
public String forbidden(final String[] rightsNeeded) {
String result;
if (this.rightsNeeded.length == 0)
return activity.getString(R.string.no_need_rights);
else if (this.rightsNeeded.length == 1)
result = activity.getString(R.string.no_right);
if (rightsNeeded.length == 0)
result = this.noRightsNeeded;
else if (rightsNeeded.length == 1)
result = this.noRight;
else
result = activity.getString(R.string.no_rights);
result = this.noRights;
for (String right : this.rightsNeeded) {
for (String right : rightsNeeded) {
if (allRights.containsKey(right))
result += " " + allRights.get(right) + ",";
else {
......@@ -279,20 +310,26 @@ public class NemopaySession {
return result.substring(0, result.length() - 1) + ".";
}
protected int request(final String method, final String service) throws IOException { return request(method, service, new HashMap<String, String>(), new String[]{}); }
protected int request(final String method, final String service, final String[] rightsNeeded) throws IOException { return request(method, service, new HashMap<String, String>(), rightsNeeded); }
protected int request(final String method, final String service, final Map<String, String> postArgs) throws IOException { return request(method, service, postArgs, new String[]{}); }
protected int request(final String method, final String service, final Map<String, String> postArgs, final String[] rightsNeeded) throws IOException {
protected int request(final String method, final String service) throws Exception { return request(method, service, new HashMap<String, String>(), new String[]{}); }
protected int request(final String method, final String service, final String[] rightsNeeded) throws Exception { return request(method, service, new HashMap<String, String>(), rightsNeeded); }
protected int request(final String method, final String service, final Map<String, String> postArgs) throws Exception { return request(method, service, postArgs, new String[]{}); }
protected int request(final String method, final String service, final Map<String, String> postArgs, final String[] rightsNeeded) throws Exception {
Log.d(LOG_TAG, "url: " + url + method + "/" + service);
this.request = new HTTPRequest(url + method + "/" + service);
this.request.setGet(getArgs);
this.request.setPost(postArgs);
this.request.setCookies(this.cookies);
int reponseCode = this.request.post();
int responseCode = this.request.post();
this.cookies = request.getCookies();
this.rightsNeeded = rightsNeeded;
return reponseCode;
if (responseCode == 500)
throw new Exception(forbidden(rightsNeeded));
else if (responseCode == 404)
throw new Exception(this.serviceText + " " + service + " " + this.notFound);
else if (responseCode == 400)
throw new Exception(this.serviceText + " " + service + " " + this.badRequest);
return responseCode;
}
}
Markdown is supported
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