Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring: separate Cookies into their own file

  • Loading branch information...
commit 8074ce7eb0097e646093346881521b02a70d709f 1 parent 73dda01
Samuel Tardieu samueltardieu authored
6 main/src/cgeo/geocaching/activity/AbstractActivity.java
View
@@ -6,7 +6,7 @@
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.compatibility.Compatibility;
-import cgeo.geocaching.network.Network;
+import cgeo.geocaching.network.Cookies;
import android.app.Activity;
import android.content.Context;
@@ -88,7 +88,7 @@ public void onCreate(Bundle savedInstanceState) {
cgBase.initialize(app);
// Restore cookie store if needed
- Network.restoreCookieStore(Settings.getCookieStore());
+ Cookies.restoreCookieStore(Settings.getCookieStore());
ActivityMixin.keepScreenOn(this, keepScreenOn);
}
@@ -131,7 +131,7 @@ public static LinearLayout createStarRating(final float value, final int count,
/**
* insert text into the EditText at the current cursor position
- *
+ *
* @param editText
* @param insertText
* @param moveCursor
5 main/src/cgeo/geocaching/cgeocaches.java
View
@@ -17,6 +17,7 @@
import cgeo.geocaching.filter.IFilter;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.maps.CGeoMap;
+import cgeo.geocaching.network.Cookies;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.sorting.CacheComparator;
@@ -186,14 +187,14 @@ public void handleMessage(Message msg) {
dialog.setNegativeButton(res.getString(R.string.license_dismiss), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- Network.clearCookies();
+ Cookies.clearCookies();
dialog.cancel();
}
});
dialog.setPositiveButton(res.getString(R.string.license_show), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- Network.clearCookies();
+ Cookies.clearCookies();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/software/agreement.aspx?ID=0")));
}
});
3  main/src/cgeo/geocaching/cgeoinit.java
View
@@ -7,6 +7,7 @@
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.maps.MapProviderFactory;
+import cgeo.geocaching.network.Cookies;
import cgeo.geocaching.network.Login;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
@@ -822,7 +823,7 @@ public void onClick(View arg0) {
loginDialog.setCancelable(false);
Settings.setLogin(username, password);
- Network.clearCookies();
+ Cookies.clearCookies();
(new Thread() {
47 main/src/cgeo/geocaching/network/Cookies.java
View
@@ -0,0 +1,47 @@
+package cgeo.geocaching.network;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.CookieStore;
+import org.apache.http.cookie.Cookie;
+import org.apache.http.impl.client.BasicCookieStore;
+import org.apache.http.impl.cookie.BasicClientCookie;
+
+public abstract class Cookies {
+
+ private static boolean cookieStoreRestored = false;
+ final static CookieStore cookieStore = new BasicCookieStore();
+
+ public static void restoreCookieStore(final String oldCookies) {
+ if (!cookieStoreRestored) {
+ clearCookies();
+ if (oldCookies != null) {
+ for (final String cookie : StringUtils.split(oldCookies, ';')) {
+ final String[] split = StringUtils.split(cookie, "=", 3);
+ if (split.length == 3) {
+ final BasicClientCookie newCookie = new BasicClientCookie(split[0], split[1]);
+ newCookie.setDomain(split[2]);
+ cookieStore.addCookie(newCookie);
+ }
+ }
+ }
+ cookieStoreRestored = true;
+ }
+ }
+
+ public static String dumpCookieStore() {
+ StringBuilder cookies = new StringBuilder();
+ for (final Cookie cookie : cookieStore.getCookies()) {
+ cookies.append(cookie.getName());
+ cookies.append('=');
+ cookies.append(cookie.getValue());
+ cookies.append('=');
+ cookies.append(cookie.getDomain());
+ cookies.append(';');
+ }
+ return cookies.toString();
+ }
+
+ public static void clearCookies() {
+ cookieStore.clear();
+ }
+}
6 main/src/cgeo/geocaching/network/Login.java
View
@@ -86,7 +86,7 @@ public static StatusCode login() {
return StatusCode.NO_ERROR; // logged in
}
- Network.clearCookies();
+ Cookies.clearCookies();
Settings.setCookieStore(null);
final Parameters params = new Parameters(
@@ -111,7 +111,7 @@ public static StatusCode login() {
Log.i("Successfully logged in Geocaching.com as " + login.left);
Login.switchToEnglish(loginData);
- Settings.setCookieStore(Network.dumpCookieStore());
+ Settings.setCookieStore(Cookies.dumpCookieStore());
return StatusCode.NO_ERROR; // logged in
} else {
@@ -137,7 +137,7 @@ public static StatusCode logout() {
return StatusCode.MAINTENANCE;
}
- Network.clearCookies();
+ Cookies.clearCookies();
Settings.setCookieStore(null);
return StatusCode.NO_ERROR;
}
42 main/src/cgeo/geocaching/network/Network.java
View
@@ -15,17 +15,13 @@
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.NameValuePair;
-import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.cookie.Cookie;
import org.apache.http.entity.HttpEntityWrapper;
-import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
@@ -72,8 +68,6 @@ public long getContentLength() {
private static final String PATTERN_PASSWORD = "(?<=[\\?&])[Pp]ass(w(or)?d)?=[^&#$]+";
private final static HttpParams clientParams = new BasicHttpParams();
- private static boolean cookieStoreRestored = false;
- private final static CookieStore cookieStore = new BasicCookieStore();
static {
Network.clientParams.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8);
@@ -87,7 +81,7 @@ private static String hidePassword(final String message) {
private static HttpClient getHttpClient() {
final DefaultHttpClient client = new DefaultHttpClient();
- client.setCookieStore(cookieStore);
+ client.setCookieStore(Cookies.cookieStore);
client.setParams(clientParams);
client.addRequestInterceptor(new HttpRequestInterceptor() {
@@ -128,40 +122,6 @@ public void process(
return client;
}
- public static void restoreCookieStore(final String oldCookies) {
- if (!cookieStoreRestored) {
- Network.clearCookies();
- if (oldCookies != null) {
- for (final String cookie : StringUtils.split(oldCookies, ';')) {
- final String[] split = StringUtils.split(cookie, "=", 3);
- if (split.length == 3) {
- final BasicClientCookie newCookie = new BasicClientCookie(split[0], split[1]);
- newCookie.setDomain(split[2]);
- cookieStore.addCookie(newCookie);
- }
- }
- }
- cookieStoreRestored = true;
- }
- }
-
- public static String dumpCookieStore() {
- StringBuilder cookies = new StringBuilder();
- for (final Cookie cookie : cookieStore.getCookies()) {
- cookies.append(cookie.getName());
- cookies.append('=');
- cookies.append(cookie.getValue());
- cookies.append('=');
- cookies.append(cookie.getDomain());
- cookies.append(';');
- }
- return cookies.toString();
- }
-
- public static void clearCookies() {
- cookieStore.clear();
- }
-
/**
* POST HTTP request
*
Please sign in to comment.
Something went wrong with that request. Please try again.