implementing login against myexperiment
Project: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/commit/6bb42dae Tree: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/tree/6bb42dae Diff: http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/diff/6bb42dae Branch: refs/heads/master Commit: 6bb42dae0b3174b10c7e15b4c9e992e1b63a5d07 Parents: b217240 Author: larrytech7 <larrya...@gmail.com> Authored: Mon Jul 20 18:30:01 2015 +0100 Committer: larrytech7 <larrya...@gmail.com> Committed: Mon Jul 20 18:30:01 2015 +0100 ---------------------------------------------------------------------- .../mobile/activities/LoginActivity.java | 82 ++++++++++---------- app/src/main/res/layout/fragment_login.xml | 3 +- 2 files changed, 44 insertions(+), 41 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/blob/6bb42dae/app/src/main/java/org/apache/taverna/mobile/activities/LoginActivity.java ---------------------------------------------------------------------- diff --git a/app/src/main/java/org/apache/taverna/mobile/activities/LoginActivity.java b/app/src/main/java/org/apache/taverna/mobile/activities/LoginActivity.java index fd6ecb2..3e8466a 100644 --- a/app/src/main/java/org/apache/taverna/mobile/activities/LoginActivity.java +++ b/app/src/main/java/org/apache/taverna/mobile/activities/LoginActivity.java @@ -62,6 +62,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; @@ -130,10 +131,9 @@ public class LoginActivity extends ActionBarActivity { } else if (password.getText().toString().isEmpty()) { password.setError(getString(R.string.passworderr)); } else { - //send login request + // login request new LoginTask(getActivity()).execute(email.getText().toString(), password.getText().toString()); } - //startActivity(new Intent(getActivity(), DashboardMainActivity.class)); } } @@ -156,57 +156,59 @@ public class LoginActivity extends ActionBarActivity { } @Override - protected String doInBackground(String... strings) { + protected String doInBackground(String... params) { //http://sandbox.myexperiment.org/users String whoAmI = "http://www.myexperiment.org/whoami.xml"; - Object response = null; - String responseMessage = null; - User loggedUser; - response = new HttpUtil().doGetRequestResponse(whoAmI, User.class,strings[0], strings[1]); - if(response instanceof User){ - //user is successfully authenticated - loggedUser = (User) response; - //TODO save login state at this level, - - //TODO set any cookies necessary - - //TODO save remember user login at this level - - }else{ - if (response instanceof String){ - responseMessage = (String) response; - if (responseMessage.equals("Unauthorized request")) { - responseMessage = "Invalid username or password"; - } - } + String response = null; + HttpURLConnection con = null; + try { + URL url = new URL(whoAmI); + con = (HttpURLConnection) url.openConnection(); + String userName = params[0]; + String password = params[1]; + + String authentication = userName + ":" + password; + con.setRequestMethod("GET"); + con.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(authentication.getBytes(), Base64.DEFAULT)); + + con.connect(); + response = String.valueOf(con.getResponseCode()); + //response values are: + //401 for an unauthorized or invalid credential and 200 for a valid and authorized user + System.out.println("" + response); + System.out.println(""+con.getResponseMessage()); + con.disconnect(); + return response; + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } - return responseMessage; + + return response; } @Override protected void onPostExecute(String response) { - Log.i("RESULTS", ""+response); + Log.i("STATUS CODE", ""+response); pd.dismiss(); - String responseMessage = response; - if(responseMessage != null) { - if (responseMessage.equals("Unauthorized request")) { - responseMessage = "Invalid username or password"; - Toast.makeText(this.context, responseMessage, Toast.LENGTH_SHORT).show(); - }else{ - Toast.makeText(this.context, responseMessage, Toast.LENGTH_SHORT).show(); + if(response != null) { + switch(Integer.parseInt(response)){ + case 401: + Toast.makeText(getActivity(), getActivity().getString(R.string.auth_err), Toast.LENGTH_LONG).show(); + break; + case 200: + this.context.startActivity(new Intent(this.context, DashboardMainActivity.class)); + getActivity().overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top); + getActivity().finish(); + break; } }else{ - //TODO: save user profile at this stage - - this.context.startActivity(new Intent(this.context, DashboardMainActivity.class)); - getActivity().overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top); - getActivity().finish(); + Toast.makeText(getActivity(), getActivity().getString(R.string.servererr), Toast.LENGTH_LONG).show(); } - this.context.startActivity(new Intent(this.context, DashboardMainActivity.class)); - getActivity().overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top); - getActivity().finish(); } } } http://git-wip-us.apache.org/repos/asf/incubator-taverna-mobile/blob/6bb42dae/app/src/main/res/layout/fragment_login.xml ---------------------------------------------------------------------- diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index 38e1e5f..37ebb53 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -140,7 +140,8 @@ limitations under the License. android:id="@+id/textView7" android:paddingTop="20dp" android:textSize="20dp" - android:gravity="center|center_horizontal" /> + android:gravity="center|center_horizontal" + android:layout_gravity="center_horizontal" /> </LinearLayout>