Android Open Source - Icinga-Mobile Login






From Project

Back to project page Icinga-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project Icinga-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mhst.dreamteam.IcingaClient.SessionMng;
//from  w ww .j av a 2 s. c  o  m
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import mhst.dreamteam.IcingaClient.GlobalConst;
import mhst.dreamteam.IcingaClient.Controller.NetController;
import mhst.dreamteam.IcingaClient.GlobalConfig;
import mhst.dreamteam.IcingaClient.Interface.OnCompleteListener;
import mhst.dreamteam.IcingaClient.Misc.CookieMng;
import mhst.dreamteam.IcingaClient.R;

/**
 * Executes login/logout action...
 *
 * @author MinhNN
 */
public class Login extends AsyncTask<String, Void, Map<String, Object>>{
    private Context mContext;
    private ProgressDialog mProgress;
    private OnCompleteListener mListener;
    private Session currentSs;
    private boolean mExecuting;

    public Login(Context context, OnCompleteListener listener) {
        mContext = context;
        mListener = listener;
        Session.init();
        currentSs = Session.getInstance();
        mExecuting = false;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(mContext != null) {
            mProgress = new ProgressDialog(mContext);
            mProgress.setCancelable(false);
            mProgress.setMessage(mContext.getResources().getString(R.string.message_logging_in) + "...");
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            if (!currentSs.isInProgress()
                    && !mProgress.isShowing()) {
                currentSs.isInProgress(true);
                mProgress.show();
            }
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    protected Map<String, Object> doInBackground(String... loginInfo) {
        // Parse login url
        String sLoginUrl = loginInfo[0] + GlobalConfig.loginUri;

        // Return data
        Map<String, Object> mResult = new HashMap<String, Object>();

        if (!mExecuting) {
            mExecuting = true;
            try {
                // Parse login information to send to server
                String sRequest = "dologin=1" +
                        "&username=" + URLEncoder.encode(loginInfo[1], "utf-8") +
                        "&password=" + URLEncoder.encode(loginInfo[2], "utf-8");

                // Properties for request
                Map<String, String> prop = new HashMap<String, String>();
                if (Session.getInstance().getCookie() != null) {
                    prop.put("Cookie", Session.getInstance().getCookie());
                }

                publishProgress();
                // Send request and get response data
                Map<String, Object> mResponse = NetController.sendRequest("POST", sLoginUrl, sRequest, prop);

                // Check if there is any error
                if (mResponse == null) {
                    mResult.put("Code", GlobalConst.ERROR_UNKNOWN_ERROR);
                    return mResult; // No response data
                }
                if (mResponse.containsKey("Error")) {
                    mResult.put("Code", mResponse.get("Error"));
                    return mResult; // Check if any error was detected
                }
                int sttCode;
                if (mResponse.containsKey("Code")) {
                    sttCode = (Integer) mResponse.get("Code"); // Get response code
                } else {
                    mResult.put("Code", GlobalConst.ERROR_UNKNOWN_ERROR);
                    return mResult; // No response code
                }
                Log.i("Log in", "Status code = " + sttCode);

                // Check login condition
                if (sttCode == HttpStatus.SC_OK) { // 200
                    // This must be the necessary condition to login successful (i.e. response code == 200)
                    if (!mResponse.containsKey("Data")) {
                        mResult.put("Code", GlobalConst.ERROR_NO_RESPONSE_DATA);
                        return mResult; // No response message
                    }
                    // Get response data to check the sufficient condition
                    JSONObject json = new JSONObject((String) mResponse.get("Data"));
                    boolean success = json.getBoolean("success");
                    // success == true <=> login successfully; otherwise, login failed
                    if (!success) { // Something wrong, login failed
                        JSONObject err = json.getJSONObject("errors");
                        if (err.has("username")) {
                            mResult.put("Code", GlobalConst.ERROR_WRONG_USER_PASS);
                            return mResult;
                        } else {
                            mResult.put("Code", GlobalConst.ERROR_UNKNOWN_ERROR);
                            return mResult;
                        }
                    }
                    // Login successfully
                    // Find cookies
                    Map<String, String> headers = (Map<String, String>) mResponse.get("Headers");
                    if (headers.containsKey("Set-Cookie")) {
                        Object obj = headers.get("Set-Cookie");
                        // Parse and build cookie string, make sure no duplicate field
                        if (obj != null) {
                            if (obj instanceof ArrayList) { // Always fails??? No I don't think so
                                String setCookie = "";
                                ArrayList<String> cookie = (ArrayList<String>) obj;
                                for (String field : cookie) {
                                    setCookie += field + ";";
                                }
                                Map<String, String> cookieList = CookieMng.parse(setCookie);
                                cookieList.remove("path");
                                setCookie = CookieMng.build(cookieList);
                                mResult.put("Cookie", setCookie);
                            } else {
                                mResult.put("Cookie", headers.get("Set-Cookie"));
                            }
                        }
                    }
                    mResult.put("Code", GlobalConst.SESSION_LOGGED_IN);
                    return mResult;
                } else { // Error detected
                    if (!mResponse.containsKey("Data")) {
                        mResult.put("Code", GlobalConst.ERROR_NO_RESPONSE_DATA);
                        return mResult; // No response data
                    }
                    // Get response data to find out the reason
                    JSONObject json = new JSONObject((String) mResponse.get("Data"));
                    boolean success = json.getBoolean("success");
                    if (!success) { // Obviously success == false
                        JSONObject err = json.getJSONObject("errors");
                        if (err.has("username")) {
                            mResult.put("Code", GlobalConst.ERROR_WRONG_USER_PASS);
                            return mResult; // Wrong username or password
                        } else {
                            mResult.put("Code", GlobalConst.ERROR_UNKNOWN_ERROR);
                            return mResult; // Unknown error
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            mResult.put("Code", GlobalConst.ERROR_UNKNOWN_ERROR);
        }
        return mResult;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        if (mContext != null && !currentSs.isInProgress() && !mProgress.isShowing()) {
            currentSs.isInProgress(true);
            mProgress.show();
        }
    }

    @Override
    protected void onPostExecute(Map<String, Object> maps) {
        super.onPostExecute(maps);
        if (mProgress != null) {
            mProgress.dismiss();
            currentSs.isInProgress(false);
        }
        if (mListener != null) {
            mListener.onComplete(maps, "Login");
        }
    }

}




Java Source Code List

mhst.dreamteam.ApplicationContext.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.MainActivity.java
mhst.dreamteam.IcingaClient.GlobalConfig.java
mhst.dreamteam.IcingaClient.GlobalConst.java
mhst.dreamteam.IcingaClient.Controller.NetControllerTest.java
mhst.dreamteam.IcingaClient.Controller.NetController.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApiConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApi.java
mhst.dreamteam.IcingaClient.Icinga.IcingaConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaExecutor.java
mhst.dreamteam.IcingaClient.Icinga.IcingaParam.java
mhst.dreamteam.IcingaClient.Icinga.IcingaUdt.java
mhst.dreamteam.IcingaClient.Icinga.package-info.java
mhst.dreamteam.IcingaClient.Interface.OnCompleteListener.java
mhst.dreamteam.IcingaClient.Interface.OnPieChartClickListener.java
mhst.dreamteam.IcingaClient.Json.JsonHelperTest.java
mhst.dreamteam.IcingaClient.Json.JsonHelper.java
mhst.dreamteam.IcingaClient.Misc.CookieMng.java
mhst.dreamteam.IcingaClient.Misc.CookieTest.java
mhst.dreamteam.IcingaClient.SessionMng.LogInTest.java
mhst.dreamteam.IcingaClient.SessionMng.Login.java
mhst.dreamteam.IcingaClient.SessionMng.Logout.java
mhst.dreamteam.IcingaClient.SessionMng.Session.java
mhst.dreamteam.IcingaService.ApplicationContext.java
mhst.dreamteam.IcingaService.DataUpdater.java
mhst.dreamteam.IcingaService.MessageReveicer.java
mhst.dreamteam.IcingaService.NotiBuilder.java
mhst.dreamteam.IcingaService.SQLHelper.java
mhst.dreamteam.IcingaService.SessionProvider.java
mhst.dreamteam.UI.Color.java
mhst.dreamteam.UI.GradientLine.java
mhst.dreamteam.UI.HostDetailsFragment.java
mhst.dreamteam.UI.HostlistAdapter.java
mhst.dreamteam.UI.HostlistFragment.java
mhst.dreamteam.UI.LoginActivity.java
mhst.dreamteam.UI.OverviewFragment.java
mhst.dreamteam.UI.PieGraph.java
mhst.dreamteam.UI.ProgressDialog.java
mhst.dreamteam.UI.ServiceDetailsFragment.java
mhst.dreamteam.UI.ServicelistAdapter.java
mhst.dreamteam.UI.ServicelistFragment.java
org.json.CDL.java
org.json.CookieList.java
org.json.Cookie.java
org.json.HTTPTokener.java
org.json.HTTP.java
org.json.JSONArray.java
org.json.JSONException.java
org.json.JSONML.java
org.json.JSONObject.java
org.json.JSONString.java
org.json.JSONStringer.java
org.json.JSONTokener.java
org.json.JSONWriter.java
org.json.Kim.java
org.json.Property.java
org.json.XMLTokener.java
org.json.XML.java
org.json.zip.BitInputStream.java
org.json.zip.BitOutputStream.java
org.json.zip.BitReader.java
org.json.zip.BitWriter.java
org.json.zip.Huff.java
org.json.zip.JSONzip.java
org.json.zip.Keep.java
org.json.zip.None.java
org.json.zip.PostMortem.java
org.json.zip.Unzipper.java
org.json.zip.Zipper.java