Ensure trailing slash directly in HttpJsonRequest

This commit is contained in:
Daniel Schaal 2015-10-02 22:30:13 +02:00
parent 63371ae4f2
commit 65b980b7e4
10 changed files with 21 additions and 32 deletions

View file

@ -59,10 +59,10 @@ public class SyncTests extends ActivityInstrumentationTestCase2<NewsReaderListAc
// Schedule some responses.
server.enqueue(new MockResponse().setBody(getSampleVersionInfoV2()));
String versionNumber = OwnCloudReaderMethods.GetVersionNumber(baseUrl.toString());
String versionNumber = OwnCloudReaderMethods.GetVersionNumber(baseUrl);
assertEquals("5.2.3", versionNumber);
API api = API.GetRightApiForVersion(versionNumber, baseUrl.toString());
API api = API.GetRightApiForVersion(versionNumber, baseUrl);
assertTrue(api instanceof APIv2);
}
@ -95,8 +95,8 @@ public class SyncTests extends ActivityInstrumentationTestCase2<NewsReaderListAc
server.enqueue(new MockResponse().setBody(getSampleVersionInfoV2()));
server.enqueue(new MockResponse().setBody(jFeed.toString()));
String versionNumber = OwnCloudReaderMethods.GetVersionNumber(baseUrl.toString());
API api = API.GetRightApiForVersion(versionNumber, baseUrl.toString());
String versionNumber = OwnCloudReaderMethods.GetVersionNumber(baseUrl);
API api = API.GetRightApiForVersion(versionNumber, baseUrl);
assertTrue(api instanceof APIv2);
int[] res = OwnCloudReaderMethods.GetFeeds(mActivity, api);
@ -132,8 +132,8 @@ public class SyncTests extends ActivityInstrumentationTestCase2<NewsReaderListAc
server.enqueue(new MockResponse().setBody(getSampleVersionInfoV2()));
server.enqueue(new MockResponse().setBody(jItem.toString()));
String versionNumber = OwnCloudReaderMethods.GetVersionNumber(baseUrl.toString());
API api = API.GetRightApiForVersion(versionNumber, baseUrl.toString());
String versionNumber = OwnCloudReaderMethods.GetVersionNumber(baseUrl);
API api = API.GetRightApiForVersion(versionNumber, baseUrl);
assertTrue(api instanceof APIv2);
int res2 = OwnCloudReaderMethods.GetItems(FeedItemTags.ALL, mActivity, "0", true, "0", "0", api); //TODO verify params

View file

@ -319,7 +319,7 @@ public class LoginDialogFragment extends DialogFragment implements IAccountImpor
protected Integer doInBackground(Void... params) {
try {
String _version = OwnCloudReaderMethods.GetVersionNumber(oc_root_path);
String _version = OwnCloudReaderMethods.GetVersionNumber(HttpJsonRequest.getInstance().getRootUrl());
if(_version != null)
{
_version = _version.replace(".", "");

View file

@ -216,10 +216,7 @@ public class NewFeedActivity extends AppCompatActivity {
@Override
protected Boolean doInBackground(Void... params) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(NewFeedActivity.this);
String baseUrl = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, "");
API api = new APIv2(baseUrl);
API api = new APIv2(HttpJsonRequest.getInstance().getRootUrl());
try {
int status = HttpJsonRequest.getInstance().performCreateFeedRequest(api.getFeedUrl(),

View file

@ -896,9 +896,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
private class AsyncTaskGetUserInfo extends AsyncTask<Void, Void, UserInfo> {
@Override
protected UserInfo doInBackground(Void... voids) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(NewsReaderListActivity.this);
String baseUrl = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, "");
API api = API.GetRightApiForVersion("6.0.4", baseUrl);
API api = API.GetRightApiForVersion("6.0.4", HttpJsonRequest.getInstance().getRootUrl());
try {
UserInfo ui = new UserInfo();
@ -948,6 +946,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
return ui;
} catch (Exception e) {
if(e.getMessage().equals("Method Not Allowed")) { //Remove if old version is used
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(NewsReaderListActivity.this);
mPrefs.edit().remove("USER_INFO").commit();
}
e.printStackTrace();

View file

@ -114,8 +114,10 @@ public class HttpJsonRequest {
else
credentials = null;
if(oc_root_path != null)
oc_root_url = HttpUrl.parse(oc_root_path);
if(oc_root_path != null) {
// Add empty path segment to ensure trailing slash
oc_root_url = HttpUrl.parse(oc_root_path).newBuilder().addPathSegment("").build();
}
}
public HttpUrl getRootUrl() {

View file

@ -35,16 +35,13 @@ import de.luhmer.owncloudnewsreader.reader.owncloud.apiv1.APIv1;
import de.luhmer.owncloudnewsreader.reader.owncloud.apiv2.APIv2;
public abstract class API {
private String baseUrl;
public API(String baseUrl) {
if(!baseUrl.endsWith("/"))
baseUrl = baseUrl + "/";
private HttpUrl baseUrl;
public API(HttpUrl baseUrl) {
this.baseUrl = baseUrl;
}
public static API GetRightApiForVersion(String appVersion, String baseUrl) {
public static API GetRightApiForVersion(String appVersion, HttpUrl baseUrl) {
API api;
int majorVersion = 0;
int minorVersion = 0;
@ -91,10 +88,8 @@ public abstract class API {
public abstract HttpUrl getTagBaseUrl();
protected HttpUrl getAPIUrl(String format, String... urlSegments) {
HttpUrl basePath = HttpUrl.parse(baseUrl);
String url = "." + StringUtils.join(urlSegments, "/");
HttpUrl.Builder apiUrlBuilder = basePath.resolve(url).newBuilder();
HttpUrl.Builder apiUrlBuilder = baseUrl.resolve(url).newBuilder();
if(format != null)
apiUrlBuilder.addQueryParameter("format", format);

View file

@ -413,10 +413,6 @@ public class OwnCloudReaderMethods {
}
}
public static String GetVersionNumber(String basePath) throws Exception {
return GetVersionNumber(HttpUrl.parse(basePath));
}
public static String GetVersionNumber(HttpUrl basePath) throws Exception
{
//Try APIv2

View file

@ -55,7 +55,7 @@ public class OwnCloud_Reader {
public API call() throws Exception {
HttpUrl oc_root_url = HttpJsonRequest.getInstance().getRootUrl();
String version = OwnCloudReaderMethods.GetVersionNumber(oc_root_url);
return API.GetRightApiForVersion(version, oc_root_url.toString());
return API.GetRightApiForVersion(version, oc_root_url);
}
};

View file

@ -35,7 +35,7 @@ import de.luhmer.owncloudnewsreader.reader.owncloud.OwnCloudReaderMethods;
public class APIv1 extends API {
public APIv1(String baseUrl) {
public APIv1(HttpUrl baseUrl) {
super(baseUrl);
}

View file

@ -34,7 +34,7 @@ import de.luhmer.owncloudnewsreader.reader.owncloud.OwnCloudReaderMethods;
public class APIv2 extends API {
public APIv2(String baseUrl) {
public APIv2(HttpUrl baseUrl) {
super(baseUrl);
}