1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 00:16:09 +00:00

Prepare wrapper to handle response bodies after exceptions

This commit is contained in:
davigonz 2018-08-28 17:41:49 +02:00
parent e3db308825
commit eb7cac064f
11 changed files with 202 additions and 113 deletions

View File

@ -148,7 +148,7 @@ public class OwnCloudClient extends HttpClient {
}
private void checkFirstRedirection(HttpBaseMethod method) {
final String location = method.getResponseHeader("location");
final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER);
if(location != null && !location.isEmpty()) {
mRedirectedLocation = location;
}
@ -182,9 +182,9 @@ public class OwnCloudClient extends HttpClient {
status == HttpConstants.HTTP_TEMPORARY_REDIRECT)
) {
final String location = method.getResponseHeader("Location") != null
? method.getResponseHeader("Location")
: method.getResponseHeader("location");
final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER) != null
? method.getResponseHeader(HttpConstants.LOCATION_HEADER)
: method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER);
if (location != null) {
Log_OC.d(TAG + " #" + mInstanceNumber,
@ -458,7 +458,7 @@ public class OwnCloudClient extends HttpClient {
mRedirectedLocation.toLowerCase().contains("wayf")));
}
public boolean isFollowRedirects() {
public boolean followRedirects() {
return mFollowRedirects;
}

View File

@ -44,6 +44,8 @@ public class HttpConstants {
public static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
public static final String PARAM_SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header";
public static final String OC_X_REQUEST_ID = "X-Request-ID";
public static final String LOCATION_HEADER = "Location";
public static final String LOCATION_HEADER_LOWER = "location";
/***********************************************************************************************************
************************************************ STATUS CODES *********************************************

View File

@ -84,7 +84,22 @@ public abstract class HttpBaseMethod {
// Getter
//////////////////////////////
// Request
public Headers getRequestHeaders() {
return mRequest.headers();
}
public String getRequestHeader(String name) {
return mRequest.header(name);
}
public HttpUrl getUrl() {
return mRequest.url();
}
// Response
public int getStatusCode() {
return mResponse.code();
}
@ -109,28 +124,16 @@ public abstract class HttpBaseMethod {
return mResponse.header(headerName);
}
public HttpUrl getUrl() {
return mRequest.url();
}
public boolean getRetryOnConnectionFailure() {
return mOkHttpClient.retryOnConnectionFailure();
}
// Request
public String getRequestHeader(String name) {
return mRequest.header(name);
}
public Headers getRequestHeaders() {
return mRequest.headers();
}
//////////////////////////////
// Setter
//////////////////////////////
// Connection parameters
public void setReadTimeout(long readTimeout, TimeUnit timeUnit) {
mOkHttpClient = mOkHttpClient.newBuilder()
.readTimeout(readTimeout, timeUnit)
@ -155,12 +158,20 @@ public abstract class HttpBaseMethod {
.build();
}
// Request
public void addRequestHeader(String name, String value) {
mRequest = mRequest.newBuilder()
.addHeader(name, value)
.build();
}
/**
* Sets a header and replace it if already exists with that name
*
* @param name header name
* @param value header value
*/
public void setRequestHeader(String name, String value) {
mRequest = mRequest.newBuilder()
.header(name, value)

View File

@ -1,13 +1,38 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.common.http.methods.webdav;
import java.net.URL;
import at.bitfire.dav4android.exception.UnauthorizedException;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import okhttp3.HttpUrl;
import okhttp3.Response;
/**
* Copy calls wrapper
* @author Christian Schabesberger
* @author David González Verdugo
*/
public class CopyMethod extends DavMethod {
final String destinationUrl;
@ -21,15 +46,11 @@ public class CopyMethod extends DavMethod {
@Override
public int onExecute() throws Exception {
try {
mDavResource.copy(destinationUrl, forceOverride, response -> {
mResponse = response;
return Unit.INSTANCE;
});
} catch (UnauthorizedException davException) {
// Do nothing, we will use the 401 code to handle the situation
}
return super.getStatusCode();
}
}

View File

@ -24,6 +24,7 @@
package com.owncloud.android.lib.common.http.methods.webdav;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
import java.net.URL;
@ -31,8 +32,12 @@ import java.util.concurrent.TimeUnit;
import at.bitfire.dav4android.Constants;
import at.bitfire.dav4android.DavOCResource;
import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.RedirectException;
import okhttp3.HttpUrl;
import okhttp3.Protocol;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* Wrapper to perform WebDAV (dav4android) calls
@ -59,13 +64,36 @@ public abstract class DavMethod extends HttpBaseMethod {
public int execute() throws Exception {
try {
return onExecute();
} catch (RedirectException e) {
return getStatusCode();
} catch (HttpException httpException) {
// Modify responses with information gathered from exceptions
if (httpException instanceof RedirectException) {
mResponse = new Response.Builder()
.header(
HttpConstants.LOCATION_HEADER, ((RedirectException) httpException).getRedirectLocation()
)
.code(httpException.getCode())
.request(mRequest)
.message(httpException.getMessage())
.protocol(Protocol.HTTP_1_1)
.build();
} else if (mResponse != null) {
ResponseBody responseBody = ResponseBody.create(
mResponse.body().contentType(),
httpException.getResponseBody()
);
mResponse = mResponse.newBuilder()
.body(responseBody)
.build();
}
return httpException.getCode();
}
}
//////////////////////////////
// setter
// Setter
//////////////////////////////
// Connection parameters
@ -106,7 +134,7 @@ public abstract class DavMethod extends HttpBaseMethod {
}
//////////////////////////////
// getter
// Getter
//////////////////////////////
@Override

View File

@ -1,12 +1,38 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.common.http.methods.webdav;
import java.net.URL;
import at.bitfire.dav4android.exception.UnauthorizedException;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import okhttp3.Response;
/**
* MkCol calls wrapper
* @author Christian Schabesberger
* @author David González Verdugo
*/
public class MkColMethod extends DavMethod {
public MkColMethod(URL url) {
super(url);
@ -14,15 +40,11 @@ public class MkColMethod extends DavMethod {
@Override
public int onExecute() throws Exception {
try {
mDavResource.mkCol(null, response -> {
mResponse = response;
return Unit.INSTANCE;
});
} catch (UnauthorizedException davException) {
// Do nothing, we will use the 401 code to handle the situation
}
return super.getStatusCode();
}
}

View File

@ -1,14 +1,40 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2018 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.common.http.methods.webdav;
import com.owncloud.android.lib.common.http.HttpConstants;
import java.net.URL;
import at.bitfire.dav4android.exception.UnauthorizedException;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import okhttp3.Response;
/**
* Move calls wrapper
* @author Christian Schabesberger
* @author David González Verdugo
*/
public class MoveMethod extends DavMethod {
final String destinationUrl;
final boolean forceOverride;
@ -21,7 +47,6 @@ public class MoveMethod extends DavMethod {
@Override
public int onExecute() throws Exception {
try {
mDavResource.move(
destinationUrl,
forceOverride,
@ -31,9 +56,6 @@ public class MoveMethod extends DavMethod {
return Unit.INSTANCE;
});
} catch (UnauthorizedException davException) {
// Do nothing, we will use the 401 code to handle the situation
}
return super.getStatusCode();
}
}

View File

@ -32,7 +32,6 @@ import java.util.List;
import at.bitfire.dav4android.Property;
import at.bitfire.dav4android.Response;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.UnauthorizedException;
import kotlin.Unit;
/**
@ -59,7 +58,6 @@ public class PropfindMethod extends DavMethod {
@Override
public int onExecute() throws IOException, DavException{
try {
mDavResource.propfind(mDepth, mPropertiesToRequest,
(Response response, Response.HrefRelation hrefRelation) -> {
switch (hrefRelation) {
@ -77,10 +75,6 @@ public class PropfindMethod extends DavMethod {
mResponse = response;
return Unit.INSTANCE;
});
} catch (UnauthorizedException davException) {
// Do nothing, we will use the 401 code to handle the situation
return davException.getCode();
}
return getStatusCode();
}

View File

@ -29,14 +29,8 @@ import com.owncloud.android.lib.common.http.HttpConstants;
import java.io.IOException;
import java.net.URL;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.UnauthorizedException;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import okhttp3.HttpUrl;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Put calls wrapper
@ -50,7 +44,6 @@ public class PutMethod extends DavMethod {
@Override
public int onExecute() throws IOException, HttpException {
try {
mDavResource.put(
mRequestBody,
super.getRequestHeader(HttpConstants.IF_MATCH_HEADER),
@ -61,10 +54,6 @@ public class PutMethod extends DavMethod {
return Unit.INSTANCE;
});
} catch (UnauthorizedException davException) {
// Do nothing, we will use the 401 code to handle the situation
}
return super.getStatusCode();
}
}

View File

@ -82,8 +82,8 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
PropfindMethod propfindMethod = new PropfindMethod(
new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)),
0,
DavUtils.getAllPropset());
DavUtils.getAllPropset()
);
propfindMethod.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
propfindMethod.setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS);