From 3bdfcdd4fe66f82e7f2b8f6a2247e35ed44e772c Mon Sep 17 00:00:00 2001 From: Daniel Schaal Date: Fri, 25 Sep 2015 07:18:13 +0200 Subject: [PATCH] Use HttpUrl to construct and validate URLs --- .../reader/HttpJsonRequest.java | 16 +++--- .../reader/owncloud/API.java | 40 ++++++-------- .../reader/owncloud/OwnCloudConstants.java | 6 +- .../owncloud/OwnCloudReaderMethods.java | 55 ++++++++++++------- .../reader/owncloud/apiv1/APIv1.java | 22 ++++---- .../reader/owncloud/apiv2/APIv2.java | 22 ++++---- 6 files changed, 85 insertions(+), 76 deletions(-) diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/HttpJsonRequest.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/HttpJsonRequest.java index 030d34e0..2bb93967 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/HttpJsonRequest.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/HttpJsonRequest.java @@ -145,9 +145,9 @@ public class HttpJsonRequest { return imageClient; } - public InputStream PerformJsonRequest(String urlString, HashMap nameValuePairs) throws Exception + public InputStream PerformJsonRequest(HttpUrl url, HashMap nameValuePairs) throws Exception { - HttpUrl.Builder urlBuilder = HttpUrl.parse(API.validateURL(urlString)).newBuilder(); + HttpUrl.Builder urlBuilder = url.newBuilder(); if(nameValuePairs != null) { for(String key: nameValuePairs.keySet()) @@ -177,14 +177,14 @@ public class HttpJsonRequest { } } - public int performCreateFeedRequest(String urlString, String feedUrl, long folderId) throws Exception { - HttpUrl url = HttpUrl.parse(API.validateURL(urlString)).newBuilder() - .setQueryParameter("url", feedUrl) + public int performCreateFeedRequest(HttpUrl url, String feedUrlString, long folderId) throws Exception { + HttpUrl feedUrl = url.newBuilder() + .setQueryParameter("url", feedUrlString) .setQueryParameter("folderId", String.valueOf(folderId)) .build(); Request request = new Request.Builder() - .url(url) + .url(feedUrl) .post(RequestBody.create(JSON, "")) .build(); @@ -193,10 +193,8 @@ public class HttpJsonRequest { return response.code(); } - public int performTagChangeRequest(String urlString, String content) throws Exception + public int performTagChangeRequest(HttpUrl url, String content) throws Exception { - HttpUrl url = HttpUrl.parse(API.validateURL(urlString)); - Request request = new Request.Builder() .url(url) .put(RequestBody.create(JSON, content)) diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/API.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/API.java index 7450f008..98454c0e 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/API.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/API.java @@ -24,6 +24,11 @@ package de.luhmer.owncloudnewsreader.reader.owncloud; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.util.Log; + +import com.squareup.okhttp.HttpUrl; + +import org.apache.commons.lang3.StringUtils; import java.util.List; import java.util.regex.Pattern; @@ -36,9 +41,6 @@ import de.luhmer.owncloudnewsreader.reader.owncloud.apiv2.APIv2; public abstract class API { protected SharedPreferences mPrefs; - //static final Pattern RemoveAllDoubleSlashes = Pattern.compile("[^:](\\/\\/)"); - static final Pattern RemoveAllDoubleSlashes = Pattern.compile("(? itemIds, FeedItemTags.TAGS tag, Context context, API api); } \ No newline at end of file diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudConstants.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudConstants.java index 64f0dba9..2f91d6e3 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudConstants.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudConstants.java @@ -24,12 +24,12 @@ package de.luhmer.owncloudnewsreader.reader.owncloud; public class OwnCloudConstants { //public static final String ROOT_PATH = "/ocs/v1.php/apps/news/"; - public static final String ROOT_PATH_APIv1 = "/ocs/v1.php/apps/news/"; - public static final String ROOT_PATH_APIv2 = "/index.php/apps/news/api/v1-2/"; + public static final String ROOT_PATH_APIv1 = "/ocs/v1.php/apps/news"; + public static final String ROOT_PATH_APIv2 = "/index.php/apps/news/api/v1-2"; public static final String FOLDER_PATH = "folders"; public static final String SUBSCRIPTION_PATH = "feeds"; public static final String FEED_PATH = "items"; public static final String FEED_PATH_UPDATED_ITEMS = "items/updated"; public static final String VERSION_PATH = "version"; - public static final String JSON_FORMAT = "?format=json"; + public static final String JSON_FORMAT = "json"; } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudReaderMethods.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudReaderMethods.java index d15830f7..58cb3cf9 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudReaderMethods.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/OwnCloudReaderMethods.java @@ -22,9 +22,11 @@ package de.luhmer.owncloudnewsreader.reader.owncloud; import android.content.Context; +import android.util.Log; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; +import com.squareup.okhttp.HttpUrl; import org.json.JSONArray; import org.json.JSONException; @@ -47,7 +49,7 @@ import de.luhmer.owncloudnewsreader.reader.owncloud.apiv1.APIv1; import de.luhmer.owncloudnewsreader.reader.owncloud.apiv2.APIv2; public class OwnCloudReaderMethods { - //private static final String TAG = "OwnCloudReaderMethods"; + private static final String TAG = "OwnCloudReaderMethods"; public static String maxSizePerSync = "300"; public static int[] GetUpdatedItems(TAGS tag, Context cont, long lastSync, API api) throws Exception @@ -144,7 +146,7 @@ public class OwnCloudReaderMethods { public static int[] GetFeeds(Context cont, API api) throws Exception { - InputStream inputStream = HttpJsonRequest.getInstance().PerformJsonRequest(api.getFeedUrl() , null); + InputStream inputStream = HttpJsonRequest.getInstance().PerformJsonRequest(api.getFeedUrl(), null); DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(cont); int result[] = new int[2]; @@ -341,16 +343,17 @@ public class OwnCloudReaderMethods { { String jsonIds; - - String url = api.getTagBaseUrl(); + HttpUrl.Builder urlBuilder = api.getTagBaseUrl().newBuilder(); if(tag.equals(TAGS.MARK_ITEM_AS_READ) || tag.equals(TAGS.MARK_ITEM_AS_UNREAD)) { jsonIds = buildIdsToJSONArray(itemIds); if(tag.equals(TAGS.MARK_ITEM_AS_READ)) - url += "read/multiple"; + urlBuilder.addPathSegment("read"); else - url += "unread/multiple"; + urlBuilder.addPathSegment("unread"); + + urlBuilder.addPathSegment("multiple"); } else { DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context); @@ -370,10 +373,11 @@ public class OwnCloudReaderMethods { }*/ if(tag.equals(TAGS.MARK_ITEM_AS_STARRED)) - url += "star/multiple"; + urlBuilder.addPathSegment("star"); else if(tag.equals(TAGS.MARK_ITEM_AS_UNSTARRED)) - url += "unstar/multiple"; + urlBuilder.addPathSegment("unstar"); + urlBuilder.addPathSegment("multiple"); /* url += "/" + guidHash; @@ -387,7 +391,7 @@ public class OwnCloudReaderMethods { } try { - int result = HttpJsonRequest.getInstance().performTagChangeRequest(url, jsonIds); + int result = HttpJsonRequest.getInstance().performTagChangeRequest(urlBuilder.build(), jsonIds); //if(result != -1 || result != 405) return (result == 200); } @@ -400,30 +404,31 @@ public class OwnCloudReaderMethods { public static boolean PerformTagExecutionAPIv1(String itemId, FeedItemTags.TAGS tag, Context context, API api) { - String url = api.getTagBaseUrl(); + HttpUrl.Builder urlBuilder = api.getTagBaseUrl().newBuilder(); if(tag.equals(TAGS.MARK_ITEM_AS_READ) || tag.equals(TAGS.MARK_ITEM_AS_UNREAD)) { + urlBuilder.addPathSegment(itemId); if(tag.equals(TAGS.MARK_ITEM_AS_READ)) - url += itemId + "/read"; + urlBuilder.addPathSegment("read"); else - url += itemId + "/unread"; + urlBuilder.addPathSegment("unread"); } else { DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context); RssItem rssItem = dbConn.getRssItemById(Long.parseLong(itemId)); - url += rssItem.getFeedId(); + urlBuilder.addPathSegment(String.valueOf(rssItem.getFeedId())); + urlBuilder.addPathSegment(rssItem.getGuidHash()); - url += "/" + rssItem.getGuidHash(); if(tag.equals(TAGS.MARK_ITEM_AS_STARRED)) - url += "/star"; + urlBuilder.addPathSegment("star"); else if(tag.equals(TAGS.MARK_ITEM_AS_UNSTARRED)) - url += "/unstar"; + urlBuilder.addPathSegment("unstar"); } try { - int result = HttpJsonRequest.getInstance().performTagChangeRequest(url, null); + int result = HttpJsonRequest.getInstance().performTagChangeRequest(urlBuilder.build(), null); return (result == 200); } catch (Exception ex) @@ -436,11 +441,15 @@ public class OwnCloudReaderMethods { public static String GetVersionNumber(Context cont, String oc_root_path) throws Exception { + HttpUrl basePath = HttpUrl.parse(oc_root_path); //Try APIv2 try { - String requestUrl = oc_root_path + OwnCloudConstants.ROOT_PATH_APIv2 + OwnCloudConstants.VERSION_PATH; - requestUrl = API.validateURL(requestUrl); + HttpUrl requestUrl = basePath.resolve(OwnCloudConstants.ROOT_PATH_APIv2).newBuilder() + .addPathSegment(OwnCloudConstants.VERSION_PATH) + .build(); + InputStream is = HttpJsonRequest.getInstance().PerformJsonRequest(requestUrl, null); + try { GetVersion_v2 gv = new GetVersion_v2(); readJsonStreamSimple(is, gv); @@ -449,9 +458,13 @@ public class OwnCloudReaderMethods { is.close(); } } catch(Exception ex) {//TODO GET HERE THE RIGHT EXCEPTION - String requestUrl = oc_root_path + OwnCloudConstants.ROOT_PATH_APIv1 + OwnCloudConstants.VERSION_PATH + OwnCloudConstants.JSON_FORMAT; - requestUrl = API.validateURL(requestUrl); + HttpUrl requestUrl = basePath.resolve(OwnCloudConstants.ROOT_PATH_APIv1).newBuilder() + .addPathSegment(OwnCloudConstants.VERSION_PATH) + .addQueryParameter("format", "json") + .build(); + InputStream is = HttpJsonRequest.getInstance().PerformJsonRequest(requestUrl, null); + try { GetVersion_v1 gv = new GetVersion_v1(); readJsonStreamSimple(is, gv); diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv1/APIv1.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv1/APIv1.java index 3e73c74e..52d7a5be 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv1/APIv1.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv1/APIv1.java @@ -23,6 +23,8 @@ package de.luhmer.owncloudnewsreader.reader.owncloud.apiv1; import android.content.Context; +import com.squareup.okhttp.HttpUrl; + import java.util.ArrayList; import java.util.List; @@ -38,28 +40,28 @@ public class APIv1 extends API { } @Override - public String getItemUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv1 + OwnCloudConstants.FEED_PATH + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getItemUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv1, OwnCloudConstants.FEED_PATH); } @Override - public String getItemUpdatedUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv1 + OwnCloudConstants.FEED_PATH_UPDATED_ITEMS + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getItemUpdatedUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv1, OwnCloudConstants.FEED_PATH_UPDATED_ITEMS); } @Override - public String getFeedUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv1 + OwnCloudConstants.SUBSCRIPTION_PATH + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getFeedUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv1, OwnCloudConstants.SUBSCRIPTION_PATH); } @Override - public String getFolderUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv1 + OwnCloudConstants.FOLDER_PATH + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getFolderUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv1, OwnCloudConstants.FOLDER_PATH); } @Override - public String getTagBaseUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv1 + OwnCloudConstants.FEED_PATH + "/"; + public HttpUrl getTagBaseUrl() { + return getAPIUrl(null, OwnCloudConstants.ROOT_PATH_APIv1, OwnCloudConstants.FEED_PATH); } @Override diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv2/APIv2.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv2/APIv2.java index c5f2bede..fbdbb56a 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv2/APIv2.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/reader/owncloud/apiv2/APIv2.java @@ -23,6 +23,8 @@ package de.luhmer.owncloudnewsreader.reader.owncloud.apiv2; import android.content.Context; +import com.squareup.okhttp.HttpUrl; + import java.util.List; import de.luhmer.owncloudnewsreader.reader.FeedItemTags.TAGS; @@ -37,28 +39,28 @@ public class APIv2 extends API { } @Override - public String getItemUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv2 + OwnCloudConstants.FEED_PATH + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getItemUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv2, OwnCloudConstants.FEED_PATH); } @Override - public String getItemUpdatedUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv2 + OwnCloudConstants.FEED_PATH_UPDATED_ITEMS + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getItemUpdatedUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv2, OwnCloudConstants.FEED_PATH_UPDATED_ITEMS); } @Override - public String getFeedUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv2 + OwnCloudConstants.SUBSCRIPTION_PATH + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getFeedUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv2, OwnCloudConstants.SUBSCRIPTION_PATH); } @Override - public String getFolderUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv2 + OwnCloudConstants.FOLDER_PATH + OwnCloudConstants.JSON_FORMAT; + public HttpUrl getFolderUrl() { + return getAPIUrl(OwnCloudConstants.JSON_FORMAT, OwnCloudConstants.ROOT_PATH_APIv2, OwnCloudConstants.FOLDER_PATH); } @Override - public String getTagBaseUrl() { - return getOcRootPath() + OwnCloudConstants.ROOT_PATH_APIv2 + OwnCloudConstants.FEED_PATH + "/"; + public HttpUrl getTagBaseUrl() { + return getAPIUrl(null, OwnCloudConstants.ROOT_PATH_APIv2, OwnCloudConstants.FEED_PATH); } @Override