Search in sources :

Example 26 with HttpHead

use of org.apache.http.client.methods.HttpHead 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);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpMkcol(org.apache.jackrabbit.webdav.client.methods.HttpMkcol) HttpVersionControl(org.apache.jackrabbit.webdav.client.methods.HttpVersionControl) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) RebindInfo(org.apache.jackrabbit.webdav.bind.RebindInfo) HttpHead(org.apache.http.client.methods.HttpHead) HttpRebind(org.apache.jackrabbit.webdav.client.methods.HttpRebind)

Example 27 with HttpHead

use of org.apache.http.client.methods.HttpHead in project jackrabbit by apache.

the class BindTest method testBindOverwrite.

public void testBindOverwrite() 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 resource R' with path 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 to create new binding of R with path bindtest2/res2 and Overwrite:F
        HttpBind bind = new HttpBind(subcol2, new BindInfo(testres1, "res2"));
        bind.addHeader("Overwrite", "F");
        status = this.client.execute(bind, this.context).getStatusLine().getStatusCode();
        assertEquals(412, status);
        // verify that 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()));
        // create new binding of R with path bindtest2/res2
        bind = new HttpBind(subcol2, new BindInfo(testres1, "res2"));
        status = this.client.execute(bind, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204);
        // verify that 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 still there
        HttpHead head = new HttpHead(testres1);
        status = this.client.execute(head, this.context).getStatusLine().getStatusCode();
        assertEquals(200, status);
    } finally {
        delete(testcol);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpMkcol(org.apache.jackrabbit.webdav.client.methods.HttpMkcol) HttpBind(org.apache.jackrabbit.webdav.client.methods.HttpBind) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) BindInfo(org.apache.jackrabbit.webdav.bind.BindInfo)

Example 28 with HttpHead

use of org.apache.http.client.methods.HttpHead in project jackrabbit by apache.

the class BindTest method testUnbind.

// will fail until <https://issues.apache.org/jira/browse/JCR-1773> is fixed
public void testUnbind() throws Exception {
    String testcol = this.root + "testUnbind/";
    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);
        // create new binding of R with path testSimpleBind/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));
        // remove new path
        HttpUnbind unbind = new HttpUnbind(subcol2, new UnbindInfo("res2"));
        status = this.client.execute(unbind, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204);
        // verify that the new binding is gone
        HttpHead head = new HttpHead(testres2);
        status = this.client.execute(head, this.context).getStatusLine().getStatusCode();
        assertEquals(404, status);
        // verify that the initial binding is still there
        head = new HttpHead(testres1);
        status = this.client.execute(head, this.context).getStatusLine().getStatusCode();
        assertEquals(200, status);
    } finally {
        delete(testcol);
    }
}
Also used : UnbindInfo(org.apache.jackrabbit.webdav.bind.UnbindInfo) StringEntity(org.apache.http.entity.StringEntity) HttpMkcol(org.apache.jackrabbit.webdav.client.methods.HttpMkcol) HttpBind(org.apache.jackrabbit.webdav.client.methods.HttpBind) HttpUnbind(org.apache.jackrabbit.webdav.client.methods.HttpUnbind) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) BindInfo(org.apache.jackrabbit.webdav.bind.BindInfo)

Example 29 with HttpHead

use of org.apache.http.client.methods.HttpHead in project jackrabbit by apache.

the class ContentCodingTest method testPutGzipContentCoding.

public void testPutGzipContentCoding() throws IOException {
    String testUri = this.uri.toString() + (this.uri.toString().endsWith("/") ? "" : "/") + "testPutGzipContentCoding";
    int status = -1;
    try {
        byte[] bytes = "foobarfoobarfoobar".getBytes("UTF-8");
        HttpPut put = new HttpPut(testUri);
        byte[] gzbytes = asGzipOctets(bytes);
        assertTrue(gzbytes.length != bytes.length);
        ByteArrayEntity entity = new ByteArrayEntity(gzbytes);
        entity.setContentEncoding(new BasicHeader("Content-Encoding", "gzip"));
        put.setEntity(entity);
        status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
        assertTrue("server create or signal error, got: " + status, status == 201 || status == 415);
        if (status / 2 == 100) {
            // check length
            HttpHead head = new HttpHead(testUri);
            HttpResponse response = this.client.execute(head, this.context);
            assertEquals(200, response.getStatusLine().getStatusCode());
            assertEquals(bytes.length, Integer.parseInt(response.getFirstHeader("Content-Length").getValue()));
        }
    } finally {
        if (status / 2 == 100) {
            delete(testUri);
        }
    }
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) BasicHeader(org.apache.http.message.BasicHeader) HttpHead(org.apache.http.client.methods.HttpHead)

Example 30 with HttpHead

use of org.apache.http.client.methods.HttpHead in project jackrabbit by apache.

the class RFC4918DestinationHeaderTest method testMove.

public void testMove() throws IOException, URISyntaxException {
    String testuri = this.root + "movetest";
    String destinationuri = testuri + "2";
    String destinationpath = new URI(destinationuri).getRawPath();
    // make sure the scheme is removed
    assertFalse(destinationpath.contains(":"));
    HttpRequestBase requestBase = null;
    try {
        requestBase = new HttpPut(testuri);
        int status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 201 || status == 204);
        requestBase.releaseConnection();
        // try to move outside the servlet's name space
        requestBase = new HttpMove(testuri, "/foobar", true);
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 502);
        requestBase.releaseConnection();
        // try a relative path
        requestBase = new HttpMove(testuri, "foobar", true);
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 400);
        requestBase.releaseConnection();
        requestBase = new HttpMove(testuri, destinationpath, true);
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 201 || status == 204);
        requestBase.releaseConnection();
        requestBase = new HttpHead(destinationuri);
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200);
        requestBase.releaseConnection();
        requestBase = new HttpHead(testuri);
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 404);
    } finally {
        requestBase.releaseConnection();
        requestBase = new HttpDelete(testuri);
        int status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204 || status == 404);
        requestBase.releaseConnection();
        requestBase = new HttpDelete(destinationuri);
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204 || status == 404);
        requestBase.releaseConnection();
    }
}
Also used : HttpMove(org.apache.jackrabbit.webdav.client.methods.HttpMove) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)100 HttpResponse (org.apache.http.HttpResponse)40 HttpGet (org.apache.http.client.methods.HttpGet)28 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)25 Test (org.junit.Test)24 IOException (java.io.IOException)23 URI (java.net.URI)22 HttpPut (org.apache.http.client.methods.HttpPut)22 Header (org.apache.http.Header)21 HttpPost (org.apache.http.client.methods.HttpPost)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)15 HttpDelete (org.apache.http.client.methods.HttpDelete)13 InputStream (java.io.InputStream)12 HttpEntity (org.apache.http.HttpEntity)10 File (java.io.File)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpOptions (org.apache.http.client.methods.HttpOptions)8 URISyntaxException (java.net.URISyntaxException)6 URL (java.net.URL)6