Commit 8fa2f1f0 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Connection with Nemopay

Add difference beetween post and get args
Optimization of HTTPRequests
Secure public class variables
Bug: cant get casUrl from NemopaySession
Add "_" before all tags to regex the logcat
parent a551d6cf
......@@ -16,13 +16,14 @@ import android.widget.Toast;
import java.io.IOException;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.NFCActivity;
import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Dialog;
import fr.utc.simde.payutc.tools.NemopaySession;
public class MainActivity extends NFCActivity {
private static final String LOG_TAG = "MainActivity";
private static final String LOG_TAG = "_MainActivity";
private static Boolean registered = false;
private static Dialog dialog;
......@@ -102,19 +103,34 @@ public class MainActivity extends NFCActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (casConnexion.isServiceAdded()) {
try {
nemopaySession.loginCas(casConnexion.getTicket(), casConnexion.getService());
} catch (IOException e) {
e.printStackTrace();
}
}
else
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));
else {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_adding));
}
}
});
if (casConnexion.isServiceAdded()) {
try {
HTTPRequest request = nemopaySession.loginCas(casConnexion.getTicket(), casConnexion.getService());
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
/*
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));*/
}
});
}
}
}
}.start();
......
......@@ -7,12 +7,12 @@ import android.util.Log;
*/
public class CASConnexion {
private static final String LOG_TAG = "CASConnexion";
private static final String service = "http://api.nemopay.net/";
private String url;
private String username;
private String location;
private String ticket;
private static final String LOG_TAG = "_CASConnexion";
private static final String service = "http://localhost";
private static String url;
private static String username;
private static String location;
private static String ticket;
public CASConnexion(final NemopaySession nemopaySession) {
this.url = "";
......@@ -24,8 +24,9 @@ public class CASConnexion {
@Override
public void run() {
try {
HTTPRequest request = nemopaySession.getCasUrl();
url = request.getResponse();
HTTPRequest http = new HTTPRequest("https://api.nemopay.net/services/POSS3/getCasUrl?system_id=payutc");
http.post();
url = http.getResponse();
url = url.substring(1, url.length() - 1);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
......@@ -51,8 +52,8 @@ public class CASConnexion {
throw new RuntimeException("Elements required");
HTTPRequest request = new HTTPRequest(this.url + "v1/tickets/");
request.setArg("username", username);
request.setArg("password", password);
request.addPost("username", username);
request.addPost("password", password);
if (request.post() == 201)
this.location = request.getHeader("Location");
......@@ -72,7 +73,7 @@ public class CASConnexion {
throw new RuntimeException("Elements required");
HTTPRequest request = new HTTPRequest(this.location);
request.setArg("service", this.service);
request.addPost("service", this.service);
if (request.post() == 200)
this.ticket = request.getResponse();
......
......@@ -18,7 +18,7 @@ import fr.utc.simde.payutc.R;
*/
public class Dialog {
private static final String LOG_TAG = "Dialog";
private static final String LOG_TAG = "_Dialog";
private static Activity activity;
private static AlertDialog alertDialog;
private static AlertDialog.Builder alertDialogBuilder;
......
......@@ -18,27 +18,29 @@ import java.util.List;
import java.util.Map;
public class HTTPRequest {
public static final String LOG_TAG = "HTTPRequest";
public String url;
public HttpURLConnection request;
public String response;
private static final String LOG_TAG = "_HTTPRequest";
private String url;
private HttpURLConnection request;
private String response;
public Map<String, String> args;
public static Map<String, String> cookies;
private Map<String, String> postArgs;
private Map<String, String> getArgs;
private static Map<String, String> cookies;
public HTTPRequest(final String url) {
this.url = url;
this.request = null;
this.response = "";
this.args = new HashMap<String, String>();
this.postArgs = new HashMap<String, String>();
this.getArgs = new HashMap<String, String>();
this.cookies = new HashMap<String, String>();
}
public int get() throws IOException {
String data = args2String(this.args);
Log.d(LOG_TAG, "get: " + this.url + data);
String get = args2String(this.getArgs, true);
Log.d(LOG_TAG, "get: " + this.url + get);
this.request = (HttpURLConnection) (new URL(this.url + "?" + data)).openConnection();
this.request = (HttpURLConnection) (new URL(this.url + get)).openConnection();
this.request.setRequestMethod("GET");
this.request.setRequestProperty("Cookie", getCookiesHeader());
this.request.setUseCaches(false);
......@@ -46,25 +48,28 @@ public class HTTPRequest {
updateCookies(this.request.getHeaderFields().get("Set-Cookie"));
generateResponse();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
return this.request.getResponseCode();
}
public int post() throws IOException {
String data = args2String(this.args);
Log.d(LOG_TAG, "post: " + this.url + ", data: " + data);
String get = args2String(this.getArgs, true);
String post = args2String(this.postArgs);
Log.d(LOG_TAG, "post: " + this.url + get + ", data: " + post);
this.request = (HttpURLConnection) (new URL(this.url)).openConnection();
this.request = (HttpURLConnection) (new URL(this.url + get)).openConnection();
this.request.setRequestMethod("POST");
this.request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
this.request.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
this.request.setRequestProperty("Content-Length", Integer.toString(post.getBytes().length));
this.request.setRequestProperty("Cookie", getCookiesHeader());
this.request.setUseCaches(false);
this.request.setDoInput(true);
this.request.setDoOutput(true);
this.request.getOutputStream().write(data.getBytes());
this.request.getOutputStream().write(post.getBytes());
updateCookies(this.request.getHeaderFields().get("Set-Cookie"));
generateResponse();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
return this.request.getResponseCode();
}
......@@ -107,35 +112,34 @@ public class HTTPRequest {
in.close();
this.response = builder.toString();
Log.d(LOG_TAG, this.response);
}
public String getResponse() throws IOException { return response; }
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException {
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); }
protected String args2String(Map<String, String> args, Boolean isGet) throws UnsupportedEncodingException {
String data = "";
for (String arg : args.keySet())
data += (URLEncoder.encode(arg, "UTF-8") + "=" + URLEncoder.encode(args.get(arg), "UTF-8") + "&");
return data.substring(0, data.equals("") ? 0 : data.length() - 1);
return data.equals("") ? "" : (isGet ? "?" : "") + data.substring(0, data.length() - 1);
}
public void setArgs(Map<String, String> args) {
this.args = args;
public void setGet(Map<String, String> args) {
this.getArgs = args;
}
public void setArg(final String key, final String value) {
this.args.put(key, value);
public void setPost(Map<String, String> args) {
this.postArgs = args;
}
public Boolean delArg(final String key) {
if (this.args.containsKey(key))
this.args.remove(key);
else
return false;
public void addGet(final String key, final String value) {
this.getArgs.put(key, value);
}
return true;
public void addPost(final String key, final String value) {
this.postArgs.put(key, value);
}
synchronized String getCookiesHeader() {
......
......@@ -23,7 +23,7 @@ import android.widget.Toast;
import fr.utc.simde.payutc.R;
public abstract class NFCActivity extends Activity {
private static final String LOG_TAG = "NFCActivity";
private static final String LOG_TAG = "_NFCActivity";
private static NfcAdapter NFCAdapter;
private static AlertDialog.Builder alertDialogBuilder;
......
......@@ -12,13 +12,13 @@ import java.util.Map;
*/
public class NemopaySession {
private static final String LOG_TAG = "NemopaySession";
private static final String LOG_TAG = "_NemopaySession";
private static final String url = "https://api.nemopay.net/services/";
private String key;
private String session;
private String username;
private final Map<String, String> defaultArgs = new HashMap<String, String>() {{
private final Map<String, String> getArgs = new HashMap<String, String>() {{
put("system_id", "payutc");
}};
......@@ -38,15 +38,14 @@ public class NemopaySession {
}});
}
public HTTPRequest construct(final String method, final String service) throws IOException { return construct(method, service, new HashMap<String, String>()); }
public HTTPRequest construct(final String method, final String service, final Map<String, String> args) throws IOException {
protected HTTPRequest construct(final String method, final String service) throws IOException { return construct(method, service, new HashMap<String, String>()); }
protected HTTPRequest construct(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);
request.setArgs(defaultArgs);
for (String arg : args.keySet())
request.setArg(arg, args.get(arg));
request.setGet(getArgs);
request.setPost(postArgs);
request.post();
return request;
}
}
......@@ -33,4 +33,5 @@
<string name="cas_error_service_connection">Erreur lors de la connexion au service depuis le CAS</string>
<string name="badge_connection">Connexion via badge</string>
<string name="badge_app_not_registered">Il est nécessaire que l\'application soit enregistrée pour permettre la connexion via badge</string>
<string name="nemopay_connection">Connexion à Nemopay</string>
</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