Advertisement
Guest User

Untitled

a guest
May 6th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1.    /**
  2.      * Represents an asynchronous login/registration task used to authenticate
  3.      * the user.
  4.      */
  5.     public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
  6.  
  7.         private final String mEmail;
  8.         private final String mPassword;
  9.         private boolean found = false;
  10.         ManagedUser user = null;
  11.  
  12.         UserLoginTask(String email, String password) {
  13.             mEmail = email;
  14.             mPassword = password;
  15.         }
  16.         private String readStream(InputStream is) {
  17.             try {
  18.                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
  19.                 int i = is.read();
  20.                 while(i != -1) {
  21.                     bo.write(i);
  22.                     i = is.read();
  23.                 }
  24.                 return bo.toString();
  25.             } catch (IOException e) {
  26.                 return "";
  27.             }
  28.         }
  29.  
  30.         private void setCookies(String url) throws IOException
  31.         {
  32.             URL web = new URL(url);
  33.             HttpURLConnection connect = (HttpURLConnection)web.openConnection();
  34.             connect.getHeaderFields().get("Set-Cookie");
  35.             connect.disconnect();
  36.         }
  37.  
  38.         @Override
  39.         protected Boolean doInBackground(Void... params) {
  40.             HttpURLConnection urlConnection = null;
  41.             HttpURLConnection conn = null;
  42.             InputStream in = null;
  43.  
  44.             String COOKIES_HEADER = "Set-Cookie";
  45.             CookieManager cm = null;
  46.  
  47.             try {
  48.                 URL urlCookie = new URL("http://10.0.2.2:8080/");
  49.                 conn = (HttpURLConnection) urlCookie.openConnection();
  50.             } catch (IOException e) {
  51.                 Log.e("LoginActivity", e.getMessage(), e);
  52.                 e.printStackTrace();
  53.             }
  54.             Map<String, List<String>> headerFields = conn.getHeaderFields();
  55.             List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
  56.  
  57.             String POST_PARAMS = "j_username=admin&j_password=admin&remember-me=false&submit=Login";
  58.             int resCode = -1;
  59.             try {
  60.  
  61.                 URL url = new URL("http://10.0.2.2:8080/api/authentication");
  62. //                URL url = new URL("http://10.0.2.2:8080/api/users/user");
  63.  
  64.                 urlConnection = (HttpURLConnection) url.openConnection();
  65.  
  66.                 //Turn on cookies.
  67.                 if(cookiesHeader != null)
  68.                 {
  69.                     cm = new CookieManager();
  70.                     CookieHandler.setDefault(cm);
  71.                     for (String cookie : cookiesHeader)
  72.                     {
  73.                         cm.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
  74.                     }
  75.                 }
  76.  
  77.                 urlConnection.setRequestMethod("POST");
  78.                 urlConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
  79.                 urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
  80.                 urlConnection.setRequestProperty("Accept-Language", "pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4");
  81.                 urlConnection.setRequestProperty("Connection", "keep-alive");
  82.                 urlConnection.setRequestProperty("Content-Length", "" + Integer.toString(POST_PARAMS.getBytes().length));
  83.                 urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  84. //                urlConnection.setRequestProperty("Cookie", "JSESSIONID=4ECF6F8B210FC930A09B922AFB502D20; CSRF-TOKEN=61502dcb-bbcb-4391-a41e-3223ac2f7418");
  85.                 if(cm.getCookieStore().getCookies().size() > 0)
  86.                 {
  87.                     //While joining the Cookies, use ',' or ';' as needed. Most of the server are using ';'
  88.                     urlConnection.setRequestProperty("Cookie",
  89.                             TextUtils.join(";",  cm.getCookieStore().getCookies()));
  90.                 }
  91.                 urlConnection.setRequestProperty("Host", "10.0.2.2:8080");
  92.                 urlConnection.setRequestProperty("Origin", "http://10.0.2.2:8080");
  93.                 urlConnection.setRequestProperty("Referer", "http://10.0.2.2:8080/");
  94.                 urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
  95. //                urlConnection.setRequestProperty("X-CSRF-TOKEN", "61502dcb-bbcb-4391-a41e-3223ac2f7418");
  96.  
  97.                 urlConnection.setDoOutput(true);
  98.                 urlConnection.setDoInput(true);
  99.  
  100.                 byte[] outputInBytes = POST_PARAMS.getBytes("UTF-8");
  101.                 OutputStream os = urlConnection.getOutputStream();
  102.                 os.write( outputInBytes );
  103.                 os.close();
  104.  
  105.  
  106.                 urlConnection.connect();
  107.                 resCode = urlConnection.getResponseCode();
  108.                 System.out.println("RESCODE: " + resCode);
  109.                 if (resCode == HttpURLConnection.HTTP_OK) {
  110.                     in = new BufferedInputStream(urlConnection.getInputStream());
  111.                     readStream(in);
  112.                     found = true;
  113.                 }
  114.                 urlConnection.disconnect();
  115.             } catch (MalformedURLException e) {
  116.                 Log.e("LoginActivity", e.getMessage(), e);
  117.                 e.printStackTrace();
  118.             } catch (IOException e) {
  119.                 Log.e("LoginActivity", e.getMessage(), e);
  120.                 e.printStackTrace();
  121.             }
  122.  
  123.             // In this context email = login
  124.             if (found && user != null){
  125.                 if (mEmail == user.getLogin()){
  126.                     if (mPassword == user.getPassword()){
  127.                         // Account exists, return true if the password matches.
  128.                         return true;
  129.                     }
  130.                 }
  131.             }
  132.             return false;
  133.         }
  134.  
  135.         @Override
  136.         protected void onPostExecute(final Boolean success) {
  137.             mAuthTask = null;
  138.             showProgress(false);
  139.  
  140.             if (success) {
  141.                 finish();
  142.             } else {
  143.                 mPasswordView.setError(getString(R.string.error_incorrect_password));
  144.                 mPasswordView.requestFocus();
  145.             }
  146.         }
  147.  
  148.         @Override
  149.         protected void onCancelled() {
  150.             mAuthTask = null;
  151.             showProgress(false);
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement