Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Represents an asynchronous login/registration task used to authenticate
- * the user.
- */
- public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
- private final String mEmail;
- private final String mPassword;
- private boolean found = false;
- ManagedUser user = null;
- UserLoginTask(String email, String password) {
- mEmail = email;
- mPassword = password;
- }
- private String readStream(InputStream is) {
- try {
- ByteArrayOutputStream bo = new ByteArrayOutputStream();
- int i = is.read();
- while(i != -1) {
- bo.write(i);
- i = is.read();
- }
- return bo.toString();
- } catch (IOException e) {
- return "";
- }
- }
- private void setCookies(String url) throws IOException
- {
- URL web = new URL(url);
- HttpURLConnection connect = (HttpURLConnection)web.openConnection();
- connect.getHeaderFields().get("Set-Cookie");
- connect.disconnect();
- }
- @Override
- protected Boolean doInBackground(Void... params) {
- HttpURLConnection urlConnection = null;
- HttpURLConnection conn = null;
- InputStream in = null;
- String COOKIES_HEADER = "Set-Cookie";
- CookieManager cm = null;
- try {
- URL urlCookie = new URL("http://10.0.2.2:8080/");
- conn = (HttpURLConnection) urlCookie.openConnection();
- } catch (IOException e) {
- Log.e("LoginActivity", e.getMessage(), e);
- e.printStackTrace();
- }
- Map<String, List<String>> headerFields = conn.getHeaderFields();
- List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
- String POST_PARAMS = "j_username=admin&j_password=admin&remember-me=false&submit=Login";
- int resCode = -1;
- try {
- URL url = new URL("http://10.0.2.2:8080/api/authentication");
- // URL url = new URL("http://10.0.2.2:8080/api/users/user");
- urlConnection = (HttpURLConnection) url.openConnection();
- //Turn on cookies.
- if(cookiesHeader != null)
- {
- cm = new CookieManager();
- CookieHandler.setDefault(cm);
- for (String cookie : cookiesHeader)
- {
- cm.getCookieStore().add(null,HttpCookie.parse(cookie).get(0));
- }
- }
- urlConnection.setRequestMethod("POST");
- urlConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
- urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
- urlConnection.setRequestProperty("Accept-Language", "pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4");
- urlConnection.setRequestProperty("Connection", "keep-alive");
- urlConnection.setRequestProperty("Content-Length", "" + Integer.toString(POST_PARAMS.getBytes().length));
- urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- // urlConnection.setRequestProperty("Cookie", "JSESSIONID=4ECF6F8B210FC930A09B922AFB502D20; CSRF-TOKEN=61502dcb-bbcb-4391-a41e-3223ac2f7418");
- if(cm.getCookieStore().getCookies().size() > 0)
- {
- //While joining the Cookies, use ',' or ';' as needed. Most of the server are using ';'
- urlConnection.setRequestProperty("Cookie",
- TextUtils.join(";", cm.getCookieStore().getCookies()));
- }
- urlConnection.setRequestProperty("Host", "10.0.2.2:8080");
- urlConnection.setRequestProperty("Origin", "http://10.0.2.2:8080");
- urlConnection.setRequestProperty("Referer", "http://10.0.2.2:8080/");
- 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");
- // urlConnection.setRequestProperty("X-CSRF-TOKEN", "61502dcb-bbcb-4391-a41e-3223ac2f7418");
- urlConnection.setDoOutput(true);
- urlConnection.setDoInput(true);
- byte[] outputInBytes = POST_PARAMS.getBytes("UTF-8");
- OutputStream os = urlConnection.getOutputStream();
- os.write( outputInBytes );
- os.close();
- urlConnection.connect();
- resCode = urlConnection.getResponseCode();
- System.out.println("RESCODE: " + resCode);
- if (resCode == HttpURLConnection.HTTP_OK) {
- in = new BufferedInputStream(urlConnection.getInputStream());
- readStream(in);
- found = true;
- }
- urlConnection.disconnect();
- } catch (MalformedURLException e) {
- Log.e("LoginActivity", e.getMessage(), e);
- e.printStackTrace();
- } catch (IOException e) {
- Log.e("LoginActivity", e.getMessage(), e);
- e.printStackTrace();
- }
- // In this context email = login
- if (found && user != null){
- if (mEmail == user.getLogin()){
- if (mPassword == user.getPassword()){
- // Account exists, return true if the password matches.
- return true;
- }
- }
- }
- return false;
- }
- @Override
- protected void onPostExecute(final Boolean success) {
- mAuthTask = null;
- showProgress(false);
- if (success) {
- finish();
- } else {
- mPasswordView.setError(getString(R.string.error_incorrect_password));
- mPasswordView.requestFocus();
- }
- }
- @Override
- protected void onCancelled() {
- mAuthTask = null;
- showProgress(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement