Permalink
Browse files

Add logout button

  • Loading branch information...
1 parent e820ff9 commit eb812cf07dc17049493655e70f18dc1eace94e99 @pawitp committed May 15, 2012
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="pref_login_now">เข้าสู่ระบบเดี๋ยวนี้!</string>
+ <string name="pref_logout_now">ออกจากระบบเดี๋ยวนี้!</string>
<string name="pref_preferences">การตั้งค่า</string>
<string name="pref_username">ชื่อผู้ใช้</string>
<string name="pref_username_summary">กดเพื่อเปลี่ยนชื่อผู้ใช้</string>
@@ -26,11 +27,14 @@
<string name="wifi_disabled">สัญญาณอินเตอร์เน็ตไร้สายถูกปิดอยู่ เปิดก่อนใช้งาน</string>
<string name="no_login_required">เซิฟเวอร์ไม่ร้องขอการเข้าสู่ระบบ</string>
<string name="login_successful">การเข้าสู่ระบบสมบูรณ์</string>
+ <string name="logout_successful">การออกจากระบบสมบูรณ์</string>
<string name="ticker_login_error">ไม่สามารถเข้าสู่ระบบ MU-WiFi ได้</string>
- <string name="notify_login_ongoing_title">กำลังเข้าสู่ระบบ MU-WiFi...</string>
- <string name="notify_login_ongoing_text_determine_requirement">ตรวจสอบสถานะการเชื่อมต่อ...</string>
- <string name="notify_login_ongoing_text_logging_in">กำลังเข้าสู่ระบบ...</string>
+ <string name="notify_login_ongoing_title">กำลังเข้าสู่ระบบ MU-WiFi…</string>
+ <string name="notify_login_ongoing_text_determine_requirement">ตรวจสอบสถานะการเชื่อมต่อ…</string>
+ <string name="notify_login_ongoing_text_logging_in">กำลังเข้าสู่ระบบ…</string>
+ <string name="notify_logout_ongoing_text">กำลังออกจากระบบ…</string>
<string name="notify_login_error_title">ไม่สามารถเข้าสู่ระบบ MU-WiFi ได้</string>
+ <string name="notify_logout_error_title">ไม่สามารถออกจากระบบ MU-WiFi ได้</string>
<string name="notify_login_error_text">กดเพื่อลองอีกครั้ง</string>
<string name="notify_login_error_invalid_credentials_text">ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง</string>
</resources>
@@ -2,6 +2,7 @@
<resources>
<string name="app_name">MU-WiFi Autologin</string>
<string name="pref_login_now">Login Now!</string>
+ <string name="pref_logout_now">Logout Now!</string>
<string name="pref_preferences">Preferences</string>
<string name="pref_username">Username</string>
<string name="pref_username_summary">Click to set username</string>
@@ -29,11 +30,14 @@
<string name="wifi_disabled">WiFi needs to be enabled to use autologin.</string>
<string name="no_login_required">No login required.</string>
<string name="login_successful">Automatic login to MU-WiFi successful.</string>
+ <string name="logout_successful">Logout successful.</string>
<string name="ticker_login_error">Unable to login to MU-WiFi.</string>
<string name="notify_login_ongoing_title">MU-WiFi Autologin</string>
- <string name="notify_login_ongoing_text_determine_requirement">Determining if login is required...</string>
- <string name="notify_login_ongoing_text_logging_in">Logging in...</string>
+ <string name="notify_login_ongoing_text_determine_requirement">Determining if login is required…</string>
+ <string name="notify_login_ongoing_text_logging_in">Logging in…</string>
+ <string name="notify_logout_ongoing_text">Logging out…</string>
<string name="notify_login_error_title">Unable to login to MU-WiFi</string>
+ <string name="notify_logout_error_title">Unable to logout from MU-WiFi</string>
<string name="notify_login_error_text">Click to retry.</string>
<string name="notify_login_error_invalid_credentials_text">Invalid username or password.</string>
</resources>
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference android:title="@string/pref_login_now" android:key="login_now"></Preference>
+ <Preference android:title="@string/pref_logout_now" android:key="logout_now"></Preference>
<PreferenceCategory android:title="@string/pref_preferences">
<CheckBoxPreference
android:defaultValue="false"
@@ -2,9 +2,9 @@
public class LoginException extends Exception {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- public LoginException(String strRes) {
- super(strRes);
- }
+ public LoginException(String strRes) {
+ super(strRes);
+ }
}
@@ -1,16 +1,8 @@
package org.dyndns.pawitp.muwifiautologin;
-import java.io.IOException;
-import java.net.ConnectException;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.net.ssl.SSLException;
-import javax.net.ssl.SSLHandshakeException;
-
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
+import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
@@ -22,84 +14,121 @@
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
+import java.io.IOException;
+import java.net.ConnectException;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLHandshakeException;
+
public class MuWifiClient {
- // These are not regex
- static final String LOGIN_SUCCESSFUL_PATTERN = "External Welcome Page";
-
- static final String FORM_USERNAME = "user";
- static final String FORM_PASSWORD = "password";
- static final String FORM_URL = "https://securelogin.arubanetworks.com/auth/index.html/u";
- static final int CONNECTION_TIMEOUT = 2000;
- static final int SOCKET_TIMEOUT = 2000;
- static final int RETRY_COUNT = 2;
-
- private String mUsername;
- private String mPassword;
- private DefaultHttpClient mHttpClient;
-
- public MuWifiClient(String username, String password) {
- mUsername = username;
- mPassword = password;
-
- mHttpClient = new DefaultHttpClient();
- HttpParams params = mHttpClient.getParams();
- HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
- HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
-
- // Also retry POST requests (normally not retried because it is not regarded idempotent)
- mHttpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
- @Override
- public boolean retryRequest(IOException exception, int executionCount,
- HttpContext context) {
- if (executionCount >= RETRY_COUNT) {
- // Do not retry if over max retry count
- return false;
- }
- if (exception instanceof UnknownHostException) {
- // Unknown host
- return false;
- }
- if (exception instanceof ConnectException) {
- // Connection refused
- return false;
- }
- if (exception instanceof SSLHandshakeException) {
- // SSL handshake exception
- return false;
- }
-
- return true;
- }
- });
- }
-
- public boolean loginRequired() throws IOException {
- try {
- HttpGet httpget = new HttpGet("https://www.google.com/");
- mHttpClient.execute(httpget);
- }
- catch (SSLException e) {
- return true; // If login is required, the certificate sent will be securelogin.arubanetworks.com
- }
- return false;
- }
-
- public void login() throws IOException, LoginException {
- List<NameValuePair> formparams = new ArrayList<NameValuePair>();
- formparams.add(new BasicNameValuePair(FORM_USERNAME, mUsername));
- formparams.add(new BasicNameValuePair(FORM_PASSWORD, mPassword));
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
- HttpPost httppost = new HttpPost(FORM_URL);
- httppost.setEntity(entity);
- HttpResponse response = mHttpClient.execute(httppost);
- String strRes = EntityUtils.toString(response.getEntity());
-
- if (strRes.contains(LOGIN_SUCCESSFUL_PATTERN)) {
- // login successful
- } else {
- throw new LoginException(strRes);
- }
- }
-
+ // These are not regex
+ static final String LOGIN_SUCCESSFUL_PATTERN = "External Welcome Page";
+ static final String LOGOUT_SUCCESSFUL_PATTERN = "Logout Successful";
+
+ static final String FORM_USERNAME = "user";
+ static final String FORM_PASSWORD = "password";
+ static final String FORM_URL = "https://securelogin.arubanetworks.com/auth/index.html/u";
+ static final String LOGOUT_URL = "https://securelogin.arubanetworks.com/auth/logout.html";
+ static final int CONNECTION_TIMEOUT = 2000;
+ static final int SOCKET_TIMEOUT = 2000;
+ static final int RETRY_COUNT = 2;
+
+ private String mUsername;
+ private String mPassword;
+ private DefaultHttpClient mHttpClient;
+
+ public MuWifiClient(String username, String password) {
+ mUsername = username;
+ mPassword = password;
+
+ mHttpClient = new DefaultHttpClient();
+ HttpParams params = mHttpClient.getParams();
+ HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
+ HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
+
+ // Also retry POST requests (normally not retried because it is not regarded idempotent)
+ mHttpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
+ @Override
+ public boolean retryRequest(IOException exception, int executionCount,
+ HttpContext context) {
+ if (executionCount >= RETRY_COUNT) {
+ // Do not retry if over max retry count
+ return false;
+ }
+ if (exception instanceof UnknownHostException) {
+ // Unknown host
+ return false;
+ }
+ if (exception instanceof ConnectException) {
+ // Connection refused
+ return false;
+ }
+ if (exception instanceof SSLHandshakeException) {
+ // SSL handshake exception
+ return false;
+ }
+
+ return true;
+ }
+ });
+ }
+
+ public boolean loginRequired() throws IOException {
+ try {
+ HttpGet httpget = new HttpGet("https://www.google.com/");
+ mHttpClient.execute(httpget);
+ }
+ catch (SSLException e) {
+ return true; // If login is required, the certificate sent will be securelogin.arubanetworks.com
+ }
+ return false;
+ }
+
+ public void login() throws IOException, LoginException {
+ try {
+ List<NameValuePair> formparams = new ArrayList<NameValuePair>();
+ formparams.add(new BasicNameValuePair(FORM_USERNAME, mUsername));
+ formparams.add(new BasicNameValuePair(FORM_PASSWORD, mPassword));
+ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
+ HttpPost httppost = new HttpPost(FORM_URL);
+ httppost.setEntity(entity);
+ HttpResponse response = mHttpClient.execute(httppost);
+ String strRes = EntityUtils.toString(response.getEntity());
+
+ if (strRes.contains(LOGIN_SUCCESSFUL_PATTERN)) {
+ // login successful
+ } else {
+ throw new LoginException("Unexpected reply from server.");
+ }
+ } catch (ClientProtocolException e) {
+ // If there is an error, the server will send an illegal reply (containing space)
+ if (e.getCause() == null || e.getCause().getCause() == null ||
+ !(e.getCause().getCause() instanceof URISyntaxException)) {
+ throw new LoginException("Unknown error.");
+ }
+ URISyntaxException orig = (URISyntaxException) e.getCause().getCause();
+ String url = orig.getInput();
+ String message = url.substring(url.indexOf("errmsg=") + "errmsg=".length());
+ throw new LoginException(message);
+ }
+ }
+
+ public void logout() throws IOException, LoginException {
+ HttpGet httpget = new HttpGet(LOGOUT_URL);
+ HttpResponse response = mHttpClient.execute(httpget);
+ String strRes = EntityUtils.toString(response.getEntity());
+
+ if (strRes.contains(LOGOUT_SUCCESSFUL_PATTERN)) {
+ // logout successful
+ } else {
+ // TODO
+ throw new LoginException("Unexpected reply from server.");
+ }
+ }
+
}
Oops, something went wrong.

0 comments on commit eb812cf

Please sign in to comment.