aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/connector/oc/OkapiClient.java')
-rw-r--r--main/src/cgeo/geocaching/connector/oc/OkapiClient.java100
1 files changed, 74 insertions, 26 deletions
diff --git a/main/src/cgeo/geocaching/connector/oc/OkapiClient.java b/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
index 2175935..712bb26 100644
--- a/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
+++ b/main/src/cgeo/geocaching/connector/oc/OkapiClient.java
@@ -34,7 +34,6 @@ import cgeo.geocaching.utils.SynchronizedDateFormat;
import ch.boye.httpclientandroidlib.HttpResponse;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@@ -61,7 +60,7 @@ final class OkapiClient {
private static final char SEPARATOR = '|';
private static final String SEPARATOR_STRING = Character.toString(SEPARATOR);
- private static final FastDateFormat LOG_DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSZ", TimeZone.getTimeZone("UTC"), Locale.US);
+ private static final SynchronizedDateFormat LOG_DATE_FORMAT = new SynchronizedDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", TimeZone.getTimeZone("UTC"), Locale.US);
private static final SynchronizedDateFormat ISO8601DATEFORMAT = new SynchronizedDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
private static final String CACHE_ATTRNAMES = "attrnames";
@@ -75,7 +74,8 @@ final class OkapiClient {
private static final String CACHE_STATUS_ARCHIVED = "Archived";
private static final String CACHE_STATUS_DISABLED = "Temporarily unavailable";
private static final String CACHE_IS_FOUND = "is_found";
- private static final String CACHE_SIZE = "size";
+ private static final String CACHE_SIZE_DEPRECATED = "size";
+ private static final String CACHE_SIZE2 = "size2";
private static final String CACHE_VOTES = "rating_votes";
private static final String CACHE_NOTFOUNDS = "notfounds";
private static final String CACHE_FOUNDS = "founds";
@@ -112,7 +112,7 @@ final class OkapiClient {
// the several realms of possible fields for cache retrieval:
// Core: for livemap requests (L3 - only with level 3 auth)
// Additional: additional fields for full cache (L3 - only for level 3 auth, current - only for connectors with current api)
- private static final String SERVICE_CACHE_CORE_FIELDS = "code|name|location|type|status|difficulty|terrain|size|date_hidden";
+ private static final String SERVICE_CACHE_CORE_FIELDS = "code|name|location|type|status|difficulty|terrain|size|size2|date_hidden";
private static final String SERVICE_CACHE_CORE_L3_FIELDS = "is_found";
private static final String SERVICE_CACHE_ADDITIONAL_FIELDS = "owner|founds|notfounds|rating|rating_votes|recommendations|description|hint|images|latest_logs|alt_wpts|attrnames|req_passwd";
private static final String SERVICE_CACHE_ADDITIONAL_CURRENT_FIELDS = "gc_code|attribution_note|attr_acodes";
@@ -148,23 +148,18 @@ final class OkapiClient {
valueMap.put("limit", "20");
valueMap.put("radius", "200");
- return requestCaches(connector, params, valueMap);
+ return requestCaches(connector, params, valueMap, false);
}
public static List<Geocache> getCachesByOwner(final String username, final OCApiConnector connector) {
- final Parameters params = new Parameters("search_method", METHOD_SEARCH_ALL);
- final Map<String, String> valueMap = new LinkedHashMap<String, String>();
- final @Nullable
- String uuid = getUserUUID(connector, username);
- if (StringUtils.isEmpty(uuid)) {
- return Collections.emptyList();
- }
- valueMap.put("owner_uuid", uuid);
-
- return requestCaches(connector, params, valueMap);
+ return getCachesByUser(username, connector, "owner_uuid");
}
public static List<Geocache> getCachesByFinder(final String username, final OCApiConnector connector) {
+ return getCachesByUser(username, connector, "found_by");
+ }
+
+ private static List<Geocache> getCachesByUser(final String username, final OCApiConnector connector, final String userRequestParam) {
final Parameters params = new Parameters("search_method", METHOD_SEARCH_ALL);
final Map<String, String> valueMap = new LinkedHashMap<String, String>();
final @Nullable
@@ -172,9 +167,9 @@ final class OkapiClient {
if (StringUtils.isEmpty(uuid)) {
return Collections.emptyList();
}
- valueMap.put("found_by", uuid);
+ valueMap.put(userRequestParam, uuid);
- return requestCaches(connector, params, valueMap);
+ return requestCaches(connector, params, valueMap, connector.isSearchForMyCaches(username));
}
public static List<Geocache> getCachesNamed(final Geopoint center, final String namePart, final OCApiConnector connector) {
@@ -195,16 +190,16 @@ final class OkapiClient {
// full wildcard search, maybe we need to change this after some testing and evaluation
valueMap.put("name", "*" + namePart + "*");
- return requestCaches(connector, params, valueMap);
+ return requestCaches(connector, params, valueMap, false);
}
- private static List<Geocache> requestCaches(final OCApiConnector connector, final Parameters params, final Map<String, String> valueMap) {
+ private static List<Geocache> requestCaches(final OCApiConnector connector, final Parameters params, final Map<String, String> valueMap, final boolean my) {
// if a global type filter is set, and OKAPI does not know that type, then return an empty list instead of all caches
if (Settings.getCacheType() != CacheType.ALL && StringUtils.isBlank(getFilterFromType())) {
return Collections.emptyList();
}
- addFilterParams(valueMap, connector);
+ addFilterParams(valueMap, connector, my);
params.add("search_params", new JSONObject(valueMap).toString());
addRetrieveParams(params, connector);
@@ -234,7 +229,7 @@ final class OkapiClient {
final Map<String, String> valueMap = new LinkedHashMap<String, String>();
valueMap.put("bbox", bboxString);
- return requestCaches(connector, params, valueMap);
+ return requestCaches(connector, params, valueMap, false);
}
public static boolean setWatchState(final Geocache cache, final boolean watched, final OCApiConnector connector) {
@@ -386,6 +381,7 @@ final class OkapiClient {
}
if (!response.isNull(CACHE_MY_NOTES)) {
cache.setPersonalNote(response.getString(CACHE_MY_NOTES));
+ cache.parseWaypointsFromNote();
}
cache.setLogPasswordRequired(response.getBoolean(CACHE_REQ_PASSWORD));
@@ -489,6 +485,30 @@ final class OkapiClient {
if ("Didn't find it".equalsIgnoreCase(logType)) {
return LogType.DIDNT_FIND_IT;
}
+ if ("Will attend".equalsIgnoreCase(logType)) {
+ return LogType.WILL_ATTEND;
+ }
+ if ("Attended".equalsIgnoreCase(logType)) {
+ return LogType.ATTENDED;
+ }
+ if ("Temporarily unavailable".equalsIgnoreCase(logType)) {
+ return LogType.TEMP_DISABLE_LISTING;
+ }
+ if ("Ready to search".equalsIgnoreCase(logType)) {
+ return LogType.ENABLE_LISTING;
+ }
+ if ("Archived".equalsIgnoreCase(logType)) {
+ return LogType.ARCHIVE;
+ }
+ if ("Needs maintenance".equalsIgnoreCase(logType)) {
+ return LogType.NEEDS_MAINTENANCE;
+ }
+ if ("Moved".equalsIgnoreCase(logType)) {
+ return LogType.UPDATE_COORDINATES;
+ }
+ if ("OC Team comment".equalsIgnoreCase(logType)) {
+ return LogType.POST_REVIEWER_NOTE;
+ }
return LogType.NOTE;
}
@@ -567,12 +587,25 @@ final class OkapiClient {
}
private static CacheSize getCacheSize(final JSONObject response) {
- if (response.isNull(CACHE_SIZE)) {
+ if (response.isNull(CACHE_SIZE2)) {
+ return getCacheSizeDeprecated(response);
+ }
+ try {
+ final String size = response.getString(CACHE_SIZE2);
+ return CacheSize.getById(size);
+ } catch (JSONException e) {
+ Log.e("OkapiClient.getCacheSize", e);
+ return getCacheSizeDeprecated(response);
+ }
+ }
+
+ private static CacheSize getCacheSizeDeprecated(final JSONObject response) {
+ if (response.isNull(CACHE_SIZE_DEPRECATED)) {
return CacheSize.NOT_CHOSEN;
}
double size = 0;
try {
- size = response.getDouble(CACHE_SIZE);
+ size = response.getDouble(CACHE_SIZE_DEPRECATED);
} catch (final JSONException e) {
Log.e("OkapiClient.getCacheSize", e);
}
@@ -586,7 +619,7 @@ final class OkapiClient {
case 4:
return CacheSize.LARGE;
case 5:
- return CacheSize.LARGE;
+ return CacheSize.VERY_LARGE;
default:
break;
}
@@ -687,11 +720,11 @@ final class OkapiClient {
return "en";
}
- private static void addFilterParams(final Map<String, String> valueMap, final OCApiConnector connector) {
+ private static void addFilterParams(final Map<String, String> valueMap, final OCApiConnector connector, final boolean my) {
if (!Settings.isExcludeDisabledCaches()) {
valueMap.put("status", "Available|Temporarily unavailable");
}
- if (Settings.isExcludeMyCaches() && connector.getSupportedAuthLevel() == OAuthLevel.Level3) {
+ if (!my && Settings.isExcludeMyCaches() && connector.getSupportedAuthLevel() == OAuthLevel.Level3) {
valueMap.put("exclude_my_own", "true");
valueMap.put("found_status", "notfound_only");
}
@@ -789,6 +822,21 @@ final class OkapiClient {
}
/**
+ * Retrieves error information from an unsuccessful Okapi-response
+ *
+ * @param response
+ * response containing an error object
+ * @return OkapiError object with detailed information
+ */
+ public static OkapiError decodeErrorResponse(HttpResponse response) {
+ final JSONResult result = new JSONResult(response);
+ if (!result.isSuccess) {
+ return new OkapiError(result.data);
+ }
+ return new OkapiError(new JSONObject());
+ }
+
+ /**
* Encapsulates response state and content of an HTTP-request that expects a JSON result. <code>isSuccess</code> is
* only true, if the response state was success and <code>data</code> is not null.
*/