Use HttpUrl to construct and validate URLs

This commit is contained in:
Daniel Schaal 2015-09-25 07:18:13 +02:00
parent 0b68e30d4f
commit 3bdfcdd4fe
6 changed files with 85 additions and 76 deletions

View file

@ -145,9 +145,9 @@ public class HttpJsonRequest {
return imageClient;
}
public InputStream PerformJsonRequest(String urlString, HashMap<String,String> nameValuePairs) throws Exception
public InputStream PerformJsonRequest(HttpUrl url, HashMap<String,String> 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))

View file

@ -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("(?<!:)\\/\\/");
public API(Context cont) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(cont);
@ -83,27 +85,23 @@ public abstract class API {
return api;
}
protected abstract String getItemUrl();
protected abstract String getItemUpdatedUrl();
public abstract String getFeedUrl();
protected abstract String getFolderUrl();
public abstract HttpUrl getItemUrl();
public abstract HttpUrl getItemUpdatedUrl();
public abstract HttpUrl getFeedUrl();
public abstract HttpUrl getFolderUrl();
protected abstract String getTagBaseUrl();
public abstract HttpUrl getTagBaseUrl();
/**
*
* @return http(s)://url_to_server
*/
protected String getOcRootPath() {
protected HttpUrl getAPIUrl(String format, String... urlSegments) {
String oc_root_path = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, "");
oc_root_path = RemoveAllDoubleSlashes.matcher(oc_root_path).replaceAll("/");
HttpUrl basePath = HttpUrl.parse(oc_root_path);
//if(!oc_root_path.endsWith("/"))
// oc_root_path += "/";
//while(oc_root_path.endsWith("/"))
// oc_root_path += oc_root_path.substring(0, oc_root_path.length() - 2);
HttpUrl.Builder apiUrlBuilder = basePath.resolve(StringUtils.join(urlSegments, "/")).newBuilder();
return oc_root_path;
if(format != null)
apiUrlBuilder.addQueryParameter("format", format);
return apiUrlBuilder.build();
}
public int[] GetFeeds(Context cont, API api) throws Exception {
@ -122,9 +120,5 @@ public abstract class API {
return OwnCloudReaderMethods.GetUpdatedItems(tag, cont, lastSync, api);
}
public static String validateURL(String url) {
return RemoveAllDoubleSlashes.matcher(url).replaceAll("/");
}
public abstract boolean PerformTagExecution(List<String> itemIds, FeedItemTags.TAGS tag, Context context, API api);
}

View file

@ -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";
}

View file

@ -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);

View file

@ -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

View file

@ -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