use of org.apache.http.client.methods.HttpPut in project jackrabbit by apache.
the class RFC4918IfHeaderTest method testPutIfLockToken.
public void testPutIfLockToken() throws IOException, DavException, URISyntaxException {
String testuri = this.root + "iflocktest";
String locktoken = null;
try {
HttpPut put = new HttpPut(testuri);
put.setEntity(new StringEntity("1"));
int status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 201 || status == 204);
HttpLock lock = new HttpLock(testuri, new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "testcase", 10000, true));
HttpResponse response = this.client.execute(lock, this.context);
status = response.getStatusLine().getStatusCode();
assertEquals("status", 200, status);
locktoken = lock.getLockToken(response);
assertNotNull(locktoken);
System.out.println(locktoken);
System.out.println(response.getFirstHeader("lock-token").getValue());
// try to overwrite without lock token
put = new HttpPut(testuri);
put.setEntity(new StringEntity("2"));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals("status: " + status, 423, status);
// try to overwrite using bad lock token
put = new HttpPut(testuri);
put.setEntity(new StringEntity("2"));
put.setHeader("If", "(<" + "DAV:foobar" + ">)");
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals("status: " + status, 412, status);
// try to overwrite using correct lock token, using No-Tag-list format
put = new HttpPut(testuri);
put.setEntity(new StringEntity("2"));
put.setHeader("If", "(<" + locktoken + ">)");
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 204);
// try to overwrite using correct lock token, using Tagged-list format
// and full URI
put = new HttpPut(testuri);
put.setEntity(new StringEntity("3"));
put.setHeader("If", "<" + testuri + ">" + "(<" + locktoken + ">)");
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 204);
// try to overwrite using correct lock token, using Tagged-list format
// and absolute path only
put = new HttpPut(testuri);
put.setEntity(new StringEntity("4"));
put.setHeader("If", "<" + new URI(testuri).getRawPath() + ">" + "(<" + locktoken + ">)");
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 204);
// try to overwrite using correct lock token, using Tagged-list format
// and bad path
put = new HttpPut(testuri);
put.setEntity(new StringEntity("5"));
put.setHeader("If", "</foobar>" + "(<" + locktoken + ">)");
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 404 || status == 412);
} finally {
HttpDelete delete = new HttpDelete(testuri);
if (locktoken != null) {
delete.setHeader("If", "(<" + locktoken + ">)");
}
int status = this.client.execute(delete, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 204 || status == 404);
}
}
use of org.apache.http.client.methods.HttpPut in project jackrabbit by apache.
the class BindTest method testRebindOverwrite.
public void testRebindOverwrite() throws Exception {
String testcol = this.root + "testSimpleBind/";
String subcol1 = testcol + "bindtest1/";
String testres1 = subcol1 + "res1";
String subcol2 = testcol + "bindtest2/";
String testres2 = subcol2 + "res2";
int status;
try {
HttpMkcol mkcol = new HttpMkcol(testcol);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(subcol1);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(subcol2);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create new resource R with path testSimpleBind/bindtest1/res1
HttpPut put = new HttpPut(testres1);
put.setEntity(new StringEntity("foo", ContentType.create("text/plain", "UTF-8")));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
// enabling version control always makes the resource referenceable
HttpVersionControl versioncontrol = new HttpVersionControl(testres1);
status = this.client.execute(versioncontrol, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 201);
//create new resource R' with path testSimpleBind/bindtest2/res2
put = new HttpPut(testres2);
put.setEntity(new StringEntity("bar", ContentType.create("text/plain", "UTF-8")));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//try rebind R with path testSimpleBind/bindtest2/res2 and Overwrite:F
HttpRebind rebind = new HttpRebind(subcol2, new RebindInfo(testres1, "res2"));
rebind.addHeader("Overwrite", "F");
status = this.client.execute(rebind, this.context).getStatusLine().getStatusCode();
assertEquals(412, status);
//verify that testSimpleBind/bindtest2/res2 still points to R'
HttpGet get = new HttpGet(testres2);
HttpResponse resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("bar", EntityUtils.toString(resp.getEntity()));
//rebind R with path testSimpleBind/bindtest2/res2
rebind = new HttpRebind(subcol2, new RebindInfo(testres1, "res2"));
status = this.client.execute(rebind, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 204);
//verify that testSimpleBind/bindtest2/res2 now points to R
get = new HttpGet(testres2);
resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("foo", EntityUtils.toString(resp.getEntity()));
//verify that the initial binding is gone
HttpHead head = new HttpHead(testres1);
status = this.client.execute(head, this.context).getStatusLine().getStatusCode();
assertEquals(404, status);
} finally {
delete(testcol);
}
}
use of org.apache.http.client.methods.HttpPut in project jackrabbit by apache.
the class BindTest method testBindCollections.
public void testBindCollections() throws Exception {
String testcol = this.root + "testBindCollections/";
String a1 = testcol + "a1/";
String b1 = a1 + "b1/";
String c1 = b1 + "c1/";
String x1 = c1 + "x1";
String a2 = testcol + "a2/";
String b2 = a2 + "b2/";
String c2 = b2 + "c2/";
String x2 = c2 + "x2";
int status;
try {
HttpMkcol mkcol = new HttpMkcol(testcol);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(a1);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(a2);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create collection resource C
mkcol = new HttpMkcol(b1);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(c1);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create plain resource R
HttpPut put = new HttpPut(x1);
put.setEntity(new StringEntity("foo", ContentType.create("text/plain", "UTF-8")));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create new binding of C with path a2/b2
HttpBind bind = new HttpBind(a2, new BindInfo(b1, "b2"));
status = this.client.execute(bind, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//check if both bindings report the same DAV:resource-id
assertEquals(this.getResourceId(b1), this.getResourceId(b2));
mkcol = new HttpMkcol(c2);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create new binding of R with path a2/b2/c2/r2
bind = new HttpBind(c2, new BindInfo(x1, "x2"));
status = this.client.execute(bind, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//check if both bindings report the same DAV:resource-id
assertEquals(this.getResourceId(x1), this.getResourceId(x2));
//verify different path alternatives
URI rid = this.getResourceId(x1);
assertEquals(rid, this.getResourceId(x2));
assertEquals(rid, this.getResourceId(testcol + "a2/b2/c1/x1"));
assertEquals(rid, this.getResourceId(testcol + "a1/b1/c2/x2"));
Object ps = this.getParentSet(x1).getValue();
assertTrue(ps instanceof List);
assertEquals(2, ((List) ps).size());
ps = this.getParentSet(x2).getValue();
assertTrue(ps instanceof List);
assertEquals(2, ((List) ps).size());
} finally {
delete(testcol);
}
}
use of org.apache.http.client.methods.HttpPut in project jackrabbit by apache.
the class RFC4918PropfindTest method testPropfindInclude.
public void testPropfindInclude() throws IOException, DavException, URISyntaxException {
String testuri = this.root + "iftest";
int status;
try {
HttpPut put = new HttpPut(testuri);
put.setEntity(new StringEntity("1"));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals("status: " + status, 201, status);
DavPropertyNameSet names = new DavPropertyNameSet();
names.add(DeltaVConstants.COMMENT);
HttpPropfind propfind = new HttpPropfind(testuri, DavConstants.PROPFIND_ALL_PROP_INCLUDE, names, 0);
HttpResponse resp = this.client.execute(propfind, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(207, status);
MultiStatus multistatus = propfind.getResponseBodyAsMultiStatus(resp);
MultiStatusResponse[] responses = multistatus.getResponses();
assertEquals(1, responses.length);
MultiStatusResponse response = responses[0];
DavPropertySet found = response.getProperties(200);
DavPropertySet notfound = response.getProperties(404);
assertTrue(found.contains(DeltaVConstants.COMMENT) || notfound.contains(DeltaVConstants.COMMENT));
} finally {
delete(testuri);
}
}
use of org.apache.http.client.methods.HttpPut in project jackrabbit by apache.
the class BindTest method testSimpleBind.
public void testSimpleBind() throws Exception {
String testcol = this.root + "testSimpleBind/";
String subcol1 = testcol + "bindtest1/";
String testres1 = subcol1 + "res1";
String subcol2 = testcol + "bindtest2/";
String testres2 = subcol2 + "res2";
int status;
try {
HttpMkcol mkcol = new HttpMkcol(testcol);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(subcol1);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(subcol2);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create new resource R with path bindtest1/res1
HttpPut put = new HttpPut(testres1);
put.setEntity(new StringEntity("foo", ContentType.create("text/plain", "UTF-8")));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//create new binding of R with path bindtest2/res2
HttpBind bind = new HttpBind(subcol2, new BindInfo(testres1, "res2"));
status = this.client.execute(bind, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
//check if both bindings report the same DAV:resource-id
assertEquals(this.getResourceId(testres1), this.getResourceId(testres2));
//compare representations retrieved with both paths
HttpGet get = new HttpGet(testres1);
HttpResponse resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("foo", EntityUtils.toString(resp.getEntity()));
resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("foo", EntityUtils.toString(resp.getEntity()));
//modify R using the new path
put = new HttpPut(testres2);
put.setEntity(new StringEntity("bar", ContentType.create("text/plain", "UTF-8")));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 204);
//compare representations retrieved with both paths
get = new HttpGet(testres1);
resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("bar", EntityUtils.toString(resp.getEntity()));
get = new HttpGet(testres2);
resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("bar", EntityUtils.toString(resp.getEntity()));
} finally {
delete(testcol);
}
}
Aggregations