Add exception class for 401 Unauthorized

parent 6fc7f30b
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.http.HttpDate;
import junit.framework.TestCase;
import java.util.Calendar;
import java.util.Date;
public class ServiceUnavailableExceptionTest extends TestCase {
public void testRetryAfter() {
Response response = new Response.Builder()
.request(new Request.Builder()
.url("http://www.example.com")
.get()
.build())
.protocol(Protocol.HTTP_1_1)
.code(503)
.build();
ServiceUnavailableException e = new ServiceUnavailableException(response);
assertNull(e.retryAfter);
response = response.newBuilder()
.header("Retry-After", "120")
.build();
e = new ServiceUnavailableException(response);
assertNotNull(e.retryAfter);
assertTrue(withinTimeRange(e.retryAfter, 120));
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 30);
response = response.newBuilder()
.header("Retry-After", HttpDate.format(cal.getTime()))
.build();
e = new ServiceUnavailableException(response);
assertNotNull(e.retryAfter);
assertTrue(withinTimeRange(e.retryAfter, 30*60));
}
private boolean withinTimeRange(Date d, int seconds) {
final long msCheck = d.getTime(), msShouldBe = new Date().getTime() + seconds*1000;
// assume max. 5 seconds difference for test running
return Math.abs(msCheck - msShouldBe) < 5000;
}
}
......@@ -36,6 +36,7 @@ import java.util.Set;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.InvalidDavResponseException;
import at.bitfire.dav4android.exception.UnauthorizedException;
import at.bitfire.dav4android.exception.PreconditionFailedException;
import at.bitfire.dav4android.exception.ServiceUnavailableException;
import at.bitfire.dav4android.exception.UnsupportedDavException;
......@@ -189,10 +190,12 @@ public class DavResource {
return;
switch (code) {
case 401:
throw response != null ? new UnauthorizedException(response) : new UnauthorizedException(message);
case 412:
throw response != null ? new PreconditionFailedException(response) : new PreconditionFailedException(code, message);
throw response != null ? new PreconditionFailedException(response) : new PreconditionFailedException(message);
case 503:
throw response != null ? new ServiceUnavailableException(response) : new ServiceUnavailableException(code, message);
throw response != null ? new ServiceUnavailableException(response) : new ServiceUnavailableException(message);
default:
throw response != null ? new HttpException(response) : new HttpException(code, message);
}
......
......@@ -16,8 +16,8 @@ public class PreconditionFailedException extends HttpException {
super(response);
}
public PreconditionFailedException(int status, String message) {
super(status, message);
public PreconditionFailedException(String message) {
super(412, message);
}
}
......@@ -20,8 +20,8 @@ public class ServiceUnavailableException extends HttpException {
public Date retryAfter;
public ServiceUnavailableException(int status, String message) {
super(status, message);
public ServiceUnavailableException(String message) {
super(503, message);
retryAfter = null;
}
......
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
public class UnauthorizedException extends HttpException {
public UnauthorizedException(Response response) {
super(response);
}
public UnauthorizedException(String message) {
super(401, message);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment