Commit a6975918 authored by Ricki Hirner's avatar Ricki Hirner

Add GET, PUT, DELETE

parent 84a2cf0b
......@@ -18,13 +18,6 @@ import java.util.List;
public class HttpUtilsTest extends TestCase {
public void testHttpUrl() {
assertEquals(
HttpUrl.parse("http://example.com/my@dav/"),
HttpUrl.parse("http://example.com/my%40dav/")
);
}
public void testParseWwwAuthenticate() {
List<HttpUtils.AuthScheme> schemes = HttpUtils.parseWwwAuthenticate(new String[]{ "Basic realm=\"test\"" });
assertEquals(1, schemes.size());
......
......@@ -6,6 +6,16 @@ import junit.framework.TestCase;
public class UrlUtilsTest extends TestCase {
public void testEquals() {
assertTrue(UrlUtils.equals(HttpUrl.parse("http://host/resource"), HttpUrl.parse("http://host/resource")));
assertTrue(UrlUtils.equals(HttpUrl.parse("http://host:80/resource"), HttpUrl.parse("http://host/resource")));
assertTrue(UrlUtils.equals(HttpUrl.parse("https://HOST:443/resource"), HttpUrl.parse("https://host/resource")));
assertTrue(UrlUtils.equals(HttpUrl.parse("https://host:443/my@dav/"), HttpUrl.parse("https://host/my%40dav/")));
assertFalse(UrlUtils.equals(HttpUrl.parse("http://host/resource"), HttpUrl.parse("http://host/resource/")));
assertFalse(UrlUtils.equals(HttpUrl.parse("http://host/resource"), HttpUrl.parse("http://host:81/resource")));
}
public void testOmitTrailingSlash() {
assertEquals(HttpUrl.parse("http://host/resource"), UrlUtils.omitTrailingSlash(HttpUrl.parse("http://host/resource")));
assertEquals(HttpUrl.parse("http://host/resource"), UrlUtils.omitTrailingSlash(HttpUrl.parse("http://host/resource/")));
......
......@@ -9,6 +9,7 @@
package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
......@@ -24,6 +25,11 @@ import at.bitfire.dav4android.exception.HttpException;
public class DavAddressBook extends DavResource {
public static final MediaType
MIME_VCARD3_UTF8 = MediaType.parse("text/vcard;charset=utf-8"),
MIME_VCARD4 = MediaType.parse("text/vcard;version=4.0");
public DavAddressBook(OkHttpClient httpClient, HttpUrl location) {
super(httpClient, location);
}
......@@ -47,7 +53,7 @@ public class DavAddressBook extends DavResource {
Response response = httpClient.newCall(new Request.Builder()
.url(location)
.method("REPORT", RequestBody.create(MEDIA_TYPE_XML, writer.toString()))
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
.header("Depth", "1")
.build()).execute();
......
......@@ -8,6 +8,7 @@
package at.bitfire.dav4android;
import android.text.TextUtils;
import android.util.Log;
import com.squareup.okhttp.HttpUrl;
......@@ -36,11 +37,13 @@ import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.InvalidDavResponseException;
import at.bitfire.dav4android.exception.UnsupportedDavException;
import at.bitfire.dav4android.property.GetETag;
import at.bitfire.dav4android.property.ResourceType;
import lombok.NonNull;
public class DavResource {
public final MediaType MEDIA_TYPE_XML = MediaType.parse("application/xml; charset=utf-8");
public final MediaType MIME_XML = MediaType.parse("application/xml; charset=utf-8");
protected final OkHttpClient httpClient;
protected static final int MAX_REDIRECTS = 5;
......@@ -57,7 +60,13 @@ public class DavResource {
}
public ResponseBody get(String accept) throws IOException, HttpException {
public String fileName() {
List<String> pathSegments = location.pathSegments();
return pathSegments.get(pathSegments.size()-1);
}
public ResponseBody get(String accept) throws IOException, HttpException, DavException {
Response response = httpClient.newCall(new Request.Builder()
.get()
.url(location)
......@@ -65,15 +74,40 @@ public class DavResource {
.build()).execute();
checkStatus(response);
// put Content-Type to getcontenttype DAV property
String eTag = response.header("ETag");
if (TextUtils.isEmpty(eTag))
throw new DavException("Server didn't send ETag in GET response");
properties.put(GetETag.NAME, new GetETag(eTag));
ResponseBody body = response.body();
if (body == null)
throw new HttpException(500, "Expected GET response body");
throw new HttpException(599, "Expected GET response body");
// TODO properties.put(GetContentType.NAME, body.contentType());
return body;
}
public void put(@NonNull RequestBody body, String ifMatchETag, String ifNoneMatchETag) throws IOException {
Request.Builder builder = new Request.Builder()
.put(body)
.url(location);
if (ifMatchETag != null)
builder.header("If-Match", ifMatchETag);
if (ifNoneMatchETag != null)
builder.header("If-None-Match", ifNoneMatchETag);
httpClient.newCall(builder.build()).execute();
}
public void delete(@NonNull String ifMatchETag) throws IOException, HttpException {
httpClient.newCall(new Request.Builder()
.delete()
.url(location)
.header("If-Match", ifMatchETag)
.build()).execute();
}
public void propfind(int depth, Property.Name... reqProp) throws IOException, HttpException, DavException {
// build XML request body
XmlSerializer serializer = XmlUtils.newSerializer();
......@@ -97,7 +131,7 @@ public class DavResource {
for (int attempt = 0; attempt < MAX_REDIRECTS; attempt++) {
response = httpClient.newCall(new Request.Builder()
.url(location)
.method("PROPFIND", RequestBody.create(MEDIA_TYPE_XML, writer.toString()))
.method("PROPFIND", RequestBody.create(MIME_XML, writer.toString()))
.header("Depth", String.valueOf(depth))
.build()).execute();
......@@ -275,7 +309,7 @@ public class DavResource {
// Which resource does this <response> represent?
DavResource target = null;
if (UrlUtils.omitTrailingSlash(href).equals(UrlUtils.omitTrailingSlash(location))) {
if (UrlUtils.equals(UrlUtils.omitTrailingSlash(href), UrlUtils.omitTrailingSlash(location))) {
// it's about ourselves
target = this;
} else if (location.scheme().equals(href.scheme()) && location.host().equals(href.host()) && location.port() == href.port()) {
......@@ -367,5 +401,4 @@ public class DavResource {
return prop;
}
}
......@@ -2,9 +2,28 @@ package at.bitfire.dav4android;
import com.squareup.okhttp.HttpUrl;
import java.net.URI;
import java.net.URISyntaxException;
public class UrlUtils {
static public HttpUrl omitTrailingSlash(HttpUrl url) {
public static boolean equals(HttpUrl url1, HttpUrl url2) {
// if okhttp thinks the two URLs are equal, they're in any case
// (and it's a simple String comparison)
if (url1.equals(url2))
return true;
URI uri1 = url1.uri(), uri2 = url2.uri();
try {
URI decoded1 = new URI(uri1.getScheme(), uri1.getSchemeSpecificPart(), uri1.getFragment()),
decoded2 = new URI(uri2.getScheme(), uri2.getSchemeSpecificPart(), uri2.getFragment());
return decoded1.equals(decoded2);
} catch (URISyntaxException e) {
return false;
}
}
public static HttpUrl omitTrailingSlash(HttpUrl url) {
int idxLast = url.pathSize() - 1;
boolean hasTrailingSlash = "".equals(url.pathSegments().get(idxLast));
......@@ -14,7 +33,7 @@ public class UrlUtils {
return url;
}
static public HttpUrl withTrailingSlash(HttpUrl url) {
public static HttpUrl withTrailingSlash(HttpUrl url) {
int idxLast = url.pathSize() - 1;
boolean hasTrailingSlash = "".equals(url.pathSegments().get(idxLast));
......
......@@ -19,12 +19,20 @@ import at.bitfire.dav4android.Constants;
import at.bitfire.dav4android.Property;
import at.bitfire.dav4android.PropertyFactory;
import at.bitfire.dav4android.XmlUtils;
import lombok.ToString;
@ToString
public class GetETag implements Property {
public static final Name NAME = new Name(XmlUtils.NS_WEBDAV, "getetag");
public String eTag;
private GetETag() {}
public GetETag(String eTag) {
this.eTag = eTag;
}
public static class Factory implements PropertyFactory {
@Override
......
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