Example usage for org.apache.http.client.methods HttpPatch setEntity

List of usage examples for org.apache.http.client.methods HttpPatch setEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPatch setEntity.

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testPatchBinaryNameAndType() throws IOException {
    final String pid = getRandomUniqueId();

    createDatastream(pid, "x", "some content");

    final String location = serverAddress + pid + "/x/fcr:metadata";
    final HttpPatch patch = new HttpPatch(location);
    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    patch.setEntity(new StringEntity("DELETE { " + "<" + serverAddress + pid + "/x> <" + HAS_MIME_TYPE
            + "> ?any . } " + "WHERE {" + "<" + serverAddress + pid + "/x> <" + HAS_MIME_TYPE + "> ?any . } ; "
            + "INSERT {" + "<" + serverAddress + pid + "/x> <" + HAS_MIME_TYPE + "> \"text/awesome\" ." + "<"
            + serverAddress + pid + "/x> <" + HAS_ORIGINAL_NAME + "> \"x.txt\" }" + "WHERE {}"));

    try (final CloseableHttpResponse response = client.execute(patch)) {
        assertEquals(NO_CONTENT.getStatusCode(), getStatus(response));
        try (final CloseableDataset dataset = getDataset(new HttpGet(location))) {
            final DatasetGraph graphStore = dataset.asDatasetGraph();
            final Node subject = createURI(serverAddress + pid + "/x");
            assertTrue(/*from w  w  w . ja va2  s .c o m*/
                    graphStore.contains(ANY, subject, HAS_MIME_TYPE.asNode(), createLiteral("text/awesome")));
            assertTrue(graphStore.contains(ANY, subject, HAS_ORIGINAL_NAME.asNode(), createLiteral("x.txt")));
            assertFalse("Should not contain old mime type property",
                    graphStore.contains(ANY, subject, createURI(REPOSITORY_NAMESPACE + "mimeType"), ANY));
        }
    }

    // Ensure binary can be downloaded (test against regression of: https://jira.duraspace.org/browse/FCREPO-1720)
    assertEquals(OK.getStatusCode(), getStatus(getDSMethod(pid, "x")));
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testPatchWithBlankNode() throws Exception {
    final String id = getRandomUniqueId();
    createObjectAndClose(id);/*from   ww w.  ja v  a  2  s  . c  o  m*/

    final String location = serverAddress + id;
    final HttpPatch updateObjectGraphMethod = patchObjMethod(id);
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    updateObjectGraphMethod
            .setEntity(new StringEntity("INSERT { <" + location + "> <info:some-predicate> _:a .\n "
                    + "_:a <http://purl.org/dc/elements/1.1/title> \"this is a title\"\n" + " } WHERE {}"));
    assertEquals(NO_CONTENT.getStatusCode(), getStatus(updateObjectGraphMethod));

    try (final CloseableDataset dataset = getDataset(new HttpGet(location))) {
        final DatasetGraph graphStore = dataset.asDatasetGraph();
        assertTrue(graphStore.contains(ANY, createURI(location), createURI("info:some-predicate"), ANY));
        final Node bnode = graphStore.find(ANY, createURI(location), createURI("info:some-predicate"), ANY)
                .next().getObject();
        try (final CloseableDataset dataset2 = getDataset(new HttpGet(bnode.getURI()))) {
            assertTrue(
                    dataset2.asDatasetGraph().contains(ANY, bnode, DCTITLE, createLiteral("this is a title")));
        }
    }
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

private String createAcl() throws UnsupportedEncodingException {
    final String aclPid = "acl" + getRandomUniqueId();
    final String aclURI = serverAddress + aclPid;
    createObjectAndClose(aclPid);//from w ww  . j a va 2  s .  c  o m
    final HttpPatch patch = patchObjMethod(aclPid);
    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    // add webac:Acl type to aclURI
    patch.setEntity(new StringEntity("INSERT { <> a <http://fedora.info/definitions/v4/webac#Acl> } WHERE {}"));
    assertEquals("Couldn't add webac:Acl type", NO_CONTENT.getStatusCode(), getStatus(patch));
    return aclURI;
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testBinaryEtags() throws IOException, InterruptedException {
    final String id = getRandomUniqueId();
    createObjectAndClose(id);/*ww w. j a va  2 s .  c  o  m*/
    final String location = serverAddress + id + "/binary";
    final HttpPut method = putDSMethod(id, "binary", "foo");

    final String binaryEtag1, binaryEtag2, binaryEtag3, descEtag1, descEtag2, descEtag3;
    final String binaryLastModed1, binaryLastModed2, binaryLastModed3;
    final String descLastModed1, descLastModed2, descLastModed3;
    final String descLocation;

    try (final CloseableHttpResponse response = execute(method)) {
        binaryEtag1 = response.getFirstHeader("ETag").getValue();
        binaryLastModed1 = response.getFirstHeader("Last-Modified").getValue();
        descLocation = Link.valueOf(response.getFirstHeader(LINK).getValue()).getUri().toString();
    }

    // First check ETags and Last-Modified headers for the binary
    final HttpGet get1 = new HttpGet(location);
    get1.addHeader("If-None-Match", binaryEtag1);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get1));

    final HttpGet get2 = new HttpGet(location);
    get2.addHeader("If-Modified-Since", binaryLastModed1);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get2));

    // Next, check ETags and Last-Modified headers on the description
    final HttpGet get3 = new HttpGet(descLocation);
    try (final CloseableHttpResponse response = execute(get3)) {
        descEtag1 = response.getFirstHeader("ETag").getValue();
        descLastModed1 = response.getFirstHeader("Last-Modified").getValue();
    }
    assertNotEquals("Binary, description ETags should be different", binaryEtag1, descEtag1);

    final HttpGet get4 = new HttpGet(descLocation);
    get4.addHeader("If-None-Match", descEtag1);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get4));

    final HttpGet get5 = new HttpGet(descLocation);
    get5.addHeader("If-Modified-Since", descLastModed1);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get5));

    // Pause two seconds before updating the description
    sleep(2000);

    // Next, update the description
    final HttpPatch httpPatch = patchObjMethod(id + "/binary/fcr:metadata");
    assertTrue("Expected weak ETag", descEtag1.startsWith("W/"));
    httpPatch.addHeader(CONTENT_TYPE, "application/sparql-update");
    httpPatch.addHeader("If-Match", descEtag1.substring(2));
    httpPatch.setEntity(new StringEntity(
            "INSERT { <> <http://purl.org/dc/elements/1.1/title> 'this is a title' } WHERE {}"));
    assertEquals(NO_CONTENT.getStatusCode(), getStatus(httpPatch));

    // Next, check headers for the binary; they should not have changed
    final HttpHead head1 = new HttpHead(location);
    try (final CloseableHttpResponse response = execute(head1)) {
        binaryEtag2 = response.getFirstHeader("ETag").getValue();
        binaryLastModed2 = response.getFirstHeader("Last-Modified").getValue();
    }

    assertEquals("ETags should be the same", binaryEtag1, binaryEtag2);
    assertEquals("Last-Modified should be the same", binaryLastModed1, binaryLastModed2);

    final HttpGet get6 = new HttpGet(location);
    get6.addHeader("If-None-Match", binaryEtag1);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get6));

    final HttpGet get7 = new HttpGet(location);
    get7.addHeader("If-Modified-Since", binaryLastModed1);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get7));

    // Next, check headers for the description; they should have changed
    final HttpHead head2 = new HttpHead(descLocation);
    try (final CloseableHttpResponse response = execute(head2)) {
        descEtag2 = response.getFirstHeader("ETag").getValue();
        descLastModed2 = response.getFirstHeader("Last-Modified").getValue();
    }

    assertNotEquals("ETags should not be the same", descEtag1, descEtag2);
    assertNotEquals("Last-Modified should not be the same", descLastModed1, descLastModed2);

    final HttpGet get8 = new HttpGet(descLocation);
    get8.addHeader("If-None-Match", descEtag2);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get8));

    final HttpGet get9 = new HttpGet(descLocation);
    get9.addHeader("If-Modified-Since", descLastModed2);
    assertEquals("Expected 304 Not Modified", NOT_MODIFIED.getStatusCode(), getStatus(get9));

    sleep(1000);

    // Next, update the binary itself
    final HttpPut method2 = new HttpPut(location);
    assertFalse("Expected strong ETag", binaryEtag1.startsWith("W/"));
    method2.addHeader("If-Match", binaryEtag1);
    method2.setEntity(new StringEntity("foobar"));
    try (final CloseableHttpResponse response = execute(method2)) {
        assertEquals(NO_CONTENT.getStatusCode(), getStatus(response));
        binaryEtag3 = response.getFirstHeader("ETag").getValue();
        binaryLastModed3 = response.getFirstHeader("Last-Modified").getValue();
    }

    final HttpGet get10 = new HttpGet(location);
    get10.addHeader("If-None-Match", binaryEtag1);
    assertEquals("Expected 200 OK", OK.getStatusCode(), getStatus(get10));

    final HttpGet get11 = new HttpGet(location);
    get11.addHeader("If-Modified-Since", binaryLastModed1);
    assertEquals("Expected 200 OK", OK.getStatusCode(), getStatus(get11));

    assertNotEquals("ETags should have changed", binaryEtag1, binaryEtag3);
    assertNotEquals("Last-Modified should have changed", binaryLastModed1, binaryLastModed3);

    // Next, check headers for the description; they should have changed
    final HttpHead head3 = new HttpHead(descLocation);
    try (final CloseableHttpResponse response = execute(head3)) {
        descEtag3 = response.getFirstHeader("ETag").getValue();
        descLastModed3 = response.getFirstHeader("Last-Modified").getValue();
    }

    assertNotEquals("ETags should have changed", descEtag2, descEtag3);
    assertNotEquals("Last-Modified should have changed", descLastModed2, descLastModed3);
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testContentDispositionHeader() throws ParseException, IOException {
    final HttpPost method = postObjMethod();
    final File img = new File("src/test/resources/test-objects/img.png");
    final String filename = "some-file.png";
    method.addHeader(CONTENT_TYPE, "application/png");
    method.addHeader(CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
    method.setEntity(new FileEntity(img));
    method.addHeader(LINK, NON_RDF_SOURCE_LINK_HEADER);

    // Create a binary resource with content-disposition
    final String location;
    try (final CloseableHttpResponse response = execute(method)) {
        assertEquals("Should be a 201 Created!", CREATED.getStatusCode(), getStatus(response));
        location = getLocation(response);
    }/*w w w. j ava  2  s  .com*/

    // Retrieve the new resource and verify the content-disposition
    verifyContentDispositionFilename(location, filename);

    // Update the filename
    final String filename1 = "new-file.png";
    final HttpPatch patch = new HttpPatch(location + "/" + FCR_METADATA);
    patch.setHeader(CONTENT_TYPE, "application/sparql-update");
    final String updateString = "PREFIX ebucore: <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#>\n"
            + "DELETE { <> ebucore:filename ?x}\n" + "INSERT { <> ebucore:filename \"" + filename1 + "\"}\n"
            + "WHERE { <> ebucore:filename ?x}";

    patch.setEntity(new StringEntity(updateString));
    assertEquals(location, NO_CONTENT.getStatusCode(), getStatus(patch));

    // Retrieve the new resource and verify the content-disposition
    verifyContentDispositionFilename(location, filename1);
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testPatchToCreateDirectContainerInSparqlUpdate() throws IOException {
    final String id = getRandomUniqueId();
    createObjectAndClose(id);//  w ww  .j  a  v  a  2 s. c  om
    final HttpPatch patch = patchObjMethod(id);
    patch.setHeader(CONTENT_TYPE, "application/sparql-update");
    final String updateString = "INSERT DATA { <> a <" + DIRECT_CONTAINER.getURI() + "> ; <"
            + MEMBERSHIP_RESOURCE.getURI() + "> <> ; <" + HAS_MEMBER_RELATION + "> <" + LDP_NAMESPACE
            + "member> .}";
    patch.setEntity(new StringEntity(updateString));
    assertEquals("Patch with sparql update created direct container from basic container!",
            CONFLICT.getStatusCode(), getStatus(patch));
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testPatchToDeleteNonRdfSourceInteractionModel() throws IOException {
    final String pid = getRandomUniqueId();

    createDatastream(pid, "x", "some content");

    final String location = serverAddress + pid + "/x/fcr:metadata";
    final HttpPatch patchDeleteMethod = new HttpPatch(location);
    patchDeleteMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    patchDeleteMethod.setEntity(new StringEntity("PREFIX ldp: <http://www.w3.org/ns/ldp#> "
            + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> DELETE { "
            + "<> rdf:type  ldp:NonRDFSource .} WHERE {}"));
    assertEquals("Delete interaction model got status 409!\n", CONFLICT.getStatusCode(),
            getStatus(patchDeleteMethod));
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testGetObjectReferences() throws IOException {
    final String id = getRandomUniqueId();
    final String resource = serverAddress + id;
    final String resourcea = resource + "/a";
    final String resourceb = resource + "/b";

    createObjectAndClose(id);/*from  w ww.ja  va  2  s.  c  om*/
    createObjectAndClose(id + "/a");
    createObjectAndClose(id + "/b");
    final HttpPatch updateObjectGraphMethod = patchObjMethod(id + "/a");
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    updateObjectGraphMethod.setEntity(new StringEntity(
            "INSERT { <" + resourcea + "> <http://purl.org/dc/terms/isPartOf> <" + resourceb + "> . \n <"
                    + resourcea + "> <info:xyz#some-other-property> <" + resourceb + "> } WHERE {}"));
    executeAndClose(updateObjectGraphMethod);

    final HttpGet getObjMethod = new HttpGet(resourceb);

    getObjMethod.addHeader("Prefer", "return=representation; include=\"" + INBOUND_REFERENCES + "\"");
    try (final CloseableDataset dataset = getDataset(getObjMethod)) {
        final DatasetGraph graph = dataset.asDatasetGraph();
        assertTrue(graph.contains(ANY, createURI(resourcea), createURI("http://purl.org/dc/terms/isPartOf"),
                createURI(resourceb)));

        assertTrue(graph.contains(ANY, createURI(resourcea), createURI("info:xyz#some-other-property"),
                createURI(resourceb)));
    }
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testLinkToNonExistent() throws IOException {
    final HttpPatch patch = new HttpPatch(getLocation(postObjMethod()));
    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    patch.setEntity(new StringEntity("INSERT { " + "<> <http://some-vocabulary#isMemberOfCollection> <"
            + serverAddress + "non-existant> } WHERE {}"));
    assertEquals(BAD_REQUEST.getStatusCode(), getStatus(patch));
}

From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java

License:asdf

@Test
public void testUpdateAndReplaceObjectGraph() throws IOException {
    final String subjectURI = getLocation(postObjMethod());
    final HttpPatch updateObjectGraphMethod = new HttpPatch(subjectURI);
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    updateObjectGraphMethod.setEntity(new StringEntity(
            "INSERT {<" + subjectURI + "> <info:test#label> \"foo\" ; " + " <info:test#number> 42 ; "
                    + " <info:test#date> \"1953?\"^^<http://id.loc.gov/datatypes/edtf/EDTF> }" + " WHERE {}"));
    executeAndClose(updateObjectGraphMethod);
    try (CloseableDataset dataset = getDataset(new HttpGet(subjectURI))) {
        final DatasetGraph graph = dataset.asDatasetGraph();
        assertTrue("Didn't find a triple we thought we added.",
                graph.contains(ANY, createURI(subjectURI), createURI("info:test#label"), createLiteral("foo")));
        assertTrue("Didn't find a triple we thought we added.", graph.contains(ANY, createURI(subjectURI),
                createURI("info:test#number"), createLiteral("42", XSDinteger)));
        assertTrue("Didn't find a triple we thought we added.",
                graph.contains(ANY, createURI(subjectURI), createURI("info:test#date"), createLiteral("1953?",
                        getInstance().getSafeTypeByName("http://id.loc.gov/datatypes/edtf/EDTF"))));
    }/* ww w. j a  v  a 2  s .  c  o m*/
    updateObjectGraphMethod.setEntity(new StringEntity("DELETE WHERE { " + "<" + subjectURI
            + "> <info:test#label> \"foo\"." + "<" + subjectURI + "> <info:test#number> 42 . " + "<"
            + subjectURI + "> <info:test#date> \"1953?\"^^<http://id.loc.gov/datatypes/edtf/EDTF> . }; \n"
            + "INSERT {<" + subjectURI + "> <info:test#label> \"qwerty\" ; " + "<info:test#number> 43 ; "
            + "<info:test#date> \"1953\"^^<http://id.loc.gov/datatypes/edtf/EDTF> . } \n" + "WHERE { }"));

    try (final CloseableHttpResponse response = execute(updateObjectGraphMethod)) {
        assertEquals(NO_CONTENT.getStatusCode(), getStatus(response));
        assertTrue("Didn't find Last-Modified header!", response.containsHeader("Last-Modified"));
        assertTrue("Didn't find ETag header!", response.containsHeader("ETag"));
    }
    try (CloseableDataset dataset = getDataset(new HttpGet(subjectURI))) {
        final DatasetGraph graph = dataset.asDatasetGraph();
        assertFalse("Found a triple we thought we deleted.",
                graph.contains(ANY, createURI(subjectURI), createURI("info:test#label"), createLiteral("foo")));
        assertFalse("Found a triple we thought we deleted.", graph.contains(ANY, createURI(subjectURI),
                createURI("info:test#number"), createLiteral("42", XSDinteger)));
        assertFalse("Found a triple we thought we deleted.",
                graph.contains(ANY, createURI(subjectURI), createURI("info:test#date"), createLiteral("1953?",
                        getInstance().getSafeTypeByName("http://id.loc.gov/datatypes/edtf/EDTF"))));
    }
}