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
// TODO there is no actual use of the JCR namespace in this test-- what is it testing?
public void testUpdateWithSparqlQueryJcrNS() throws IOException {
    final String subjectURI = getLocation(postObjMethod());
    final HttpPatch updateObjectGraphMethod = new HttpPatch(subjectURI);
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    updateObjectGraphMethod.setEntity(new StringEntity("PREFIX fcr: <http://xmlns.com/my-fcr/> " + "INSERT { <"
            + subjectURI + "> <info:test#label> \"asdfg\" } WHERE {}"));
    assertNotEquals("Got updated response with jcr namspace prefix!\n", NO_CONTENT.getStatusCode(),
            getStatus(updateObjectGraphMethod));
}

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

License:asdf

@Test
public void testUpdateObjectGraphWithProblems() throws IOException {
    final String subjectURI = getLocation(postObjMethod());
    final Link ex = fromUri(URI.create(serverAddress + "static/constraints/ServerManagedPropertyException.rdf"))
            .rel(CONSTRAINED_BY.getURI()).build();

    final HttpPatch patchObjMethod = new HttpPatch(subjectURI);
    patchObjMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    patchObjMethod.setEntity(new StringEntity("INSERT { <" + subjectURI + "> <" + REPOSITORY_NAMESPACE
            + "uuid> \"value-doesn't-matter\" } WHERE {}\n"));
    try (final CloseableHttpResponse response = execute(patchObjMethod)) {
        assertEquals(CONFLICT.getStatusCode(), getStatus(response));
        assertEquals(ex.toString(), response.getFirstHeader(LINK).getValue().toString());
    }//from  w ww.  j  a  va2  s  .com
}

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

License:asdf

@Test
public void testBinarySetBadMimeType() throws IOException {
    final String subjectURI = serverAddress + getRandomUniqueId();
    final HttpPut createMethod = new HttpPut(subjectURI);
    createMethod.addHeader(CONTENT_TYPE, "text/plain");
    createMethod.setEntity(new StringEntity("Some text here."));
    createMethod.addHeader(LINK, NON_RDF_SOURCE_LINK_HEADER);

    assertEquals(CREATED.getStatusCode(), getStatus(createMethod));

    final HttpPatch patch = new HttpPatch(subjectURI + "/fcr:metadata");
    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    patch.setEntity(new StringEntity("PREFIX ebucore: <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#> "
            + "INSERT { <" + subjectURI + "> ebucore:hasMimeType> \"-- invalid syntax! --\" } WHERE {}"));

    assertEquals(BAD_REQUEST.getStatusCode(), getStatus(patch));

    // make sure it's still retrievable
    final HttpGet getMethod = new HttpGet(subjectURI);
    try (final CloseableHttpResponse response = execute(getMethod)) {
        assertEquals(OK.getStatusCode(), getStatus(response));
        final Collection<String> contentTypes = getHeader(response, CONTENT_TYPE);
        final String contentType = contentTypes.iterator().next();
        assertTrue("GET: Expected 'text/plain' instead got: '" + contentType + "'",
                contentType.contains("text/plain"));
    }//ww  w  .ja  v a 2 s .c  om

    final HttpHead httpHead = new HttpHead(subjectURI);
    try (final CloseableHttpResponse response = execute(httpHead)) {
        final Collection<String> contentTypes = getHeader(response, CONTENT_TYPE);
        final String contentType = contentTypes.iterator().next();
        assertTrue("HEAD: Expected 'text/plain' instead got: '" + contentType + "'",
                contentType.contains("text/plain"));
    }
}

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

License:asdf

@Test
public void testLinkedDeletion() {
    final String linkedFrom = getRandomUniqueId();
    final String linkedTo = getRandomUniqueId();
    createObjectAndClose(linkedFrom);/*from   w w w .ja  v  a2 s .c  o  m*/
    createObjectAndClose(linkedTo);

    final String sparql = "INSERT DATA { <" + serverAddress + linkedFrom + "> "
            + "<http://some-vocabulary#isMemberOfCollection> <" + serverAddress + linkedTo + "> . }";
    final HttpPatch patch = patchObjMethod(linkedFrom);
    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    patch.setEntity(new ByteArrayEntity(sparql.getBytes(UTF_8)));
    assertEquals("Couldn't link resources!", NO_CONTENT.getStatusCode(), getStatus(patch));
    assertEquals("Error deleting linked-to!", NO_CONTENT.getStatusCode(), getStatus(deleteObjMethod(linkedTo)));
    assertEquals("Linked to should still exist!", OK.getStatusCode(), getStatus(getObjMethod(linkedFrom)));
}

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

License:asdf

@Test
public void testUpdateObjectWithSpaces() throws IOException {
    final String id = getRandomUniqueId() + " 2";
    try (final CloseableHttpResponse createResponse = createObject(id)) {
        final String subjectURI = getLocation(createResponse);
        final HttpPatch updateObjectGraphMethod = new HttpPatch(subjectURI);
        updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
        updateObjectGraphMethod.setEntity(
                new StringEntity("INSERT { <> <http://purl.org/dc/elements/1.1/title> \"test\" } WHERE {}"));
        assertEquals(NO_CONTENT.getStatusCode(), getStatus(updateObjectGraphMethod));
    }//from w w w .  j  a  va 2s .c o  m
}

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

License:asdf

@Test
public void testEmbeddedContainedResources() throws IOException {
    final String id = getRandomUniqueId();
    final String binaryId = "binary0";
    final String preferEmbed = "return=representation; include=\"http://www.w3.org/ns/oa#PreferContainedDescriptions\"";

    assertEquals(CREATED.getStatusCode(), getStatus(putObjMethod(id)));
    assertEquals(CREATED.getStatusCode(), getStatus(putDSMethod(id, binaryId, "some test content")));

    final HttpPatch httpPatch = patchObjMethod(id + "/" + binaryId + "/fcr:metadata");
    httpPatch.addHeader(CONTENT_TYPE, "application/sparql-update");
    httpPatch.setEntity(new StringEntity(
            "INSERT { <> <http://purl.org/dc/elements/1.1/title> 'this is a title' } WHERE {}"));
    assertEquals(NO_CONTENT.getStatusCode(), getStatus(httpPatch));

    final HttpGet httpGet = getObjMethod(id);
    httpGet.setHeader("Prefer", preferEmbed);
    try (final CloseableHttpResponse response = execute(httpGet)) {
        final Collection<String> preferenceApplied = getHeader(response, "Preference-Applied");
        assertTrue("Preference-Applied header doesn't match", preferenceApplied.contains(preferEmbed));

        final DatasetGraph graphStore = getDataset(response).asDatasetGraph();
        assertTrue("Property on child binary should be found!" + graphStore,
                graphStore.contains(ANY, createURI(serverAddress + id + "/" + binaryId),
                        createURI("http://purl.org/dc/elements/1.1/title"), createLiteral("this is a title")));
    }/*from  w  w  w.jav  a 2  s  . co  m*/
}

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

License:asdf

@Test
public void testUpdateObjectGraphWithNonLocalTriples() throws IOException {
    final String pid = getRandomUniqueId();
    createObject(pid);/*from w  w  w. ja v a2 s . com*/
    final String otherPid = getRandomUniqueId();
    createObject(otherPid);
    final String location = serverAddress + pid;
    final String otherLocation = serverAddress + otherPid;
    final HttpPatch updateObjectGraphMethod = new HttpPatch(location);
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    updateObjectGraphMethod.setEntity(new StringEntity("INSERT { <" + location
            + "> <http://purl.org/dc/elements/1.1/identifier> \"this is an identifier\". " + "<" + otherLocation
            + "> <http://purl.org/dc/elements/1.1/identifier> \"this is an identifier\"" + " } WHERE {}"));
    assertEquals("It ought not be possible to use PATCH to create non-local triples!",
            FORBIDDEN.getStatusCode(), getStatus(updateObjectGraphMethod));
}

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

License:asdf

@Test
public void testDeleteLargeLiteralStoredAsBinary() throws IOException {

    final String pid = getRandomUniqueId();
    createObject(pid);/*from w  ww  . j  av  a 2 s  . c  o m*/

    final Node DC_TITLE = title.asNode();
    final String location = serverAddress + pid;
    final HttpPatch patch = new HttpPatch(location);
    final HttpPatch delPatch = new HttpPatch(location);

    final String longLiteral = // minimumBinaryInByteSize is currently 40 bytes
            "01234567890123456789012345678901234567890123456789"
                    + "01234567890123456789012345678901234567890123456789"
                    + "01234567890123456789012345678901234567890123456789"
                    + "01234567890123456789012345678901234567890123456789";

    LOGGER.info("BINARY LITERAL TEST");

    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    patch.setEntity(
            new StringEntity("INSERT { <> <" + DC_TITLE + "> \"\"\"" + longLiteral + "\"\"\" } WHERE {}"));

    assertEquals("Unable to add property value", NO_CONTENT.getStatusCode(), getStatus(patch));

    delPatch.addHeader(CONTENT_TYPE, "application/sparql-update");
    delPatch.setEntity(
            new StringEntity("DELETE WHERE { <> <" + DC_TITLE + "> \"\"\"" + longLiteral + "\"\"\"}"));

    // delete that triple, or at least try to.
    assertEquals("Unable to complete delete property HTTP request", NO_CONTENT.getStatusCode(),
            getStatus(delPatch));

    // now test if property exists anymore (it shouldn't).
    try (final CloseableDataset dataset = getDataset(getObjMethod(pid))) {
        assertFalse("Found the literal we tried to delete!", dataset.asDatasetGraph().contains(ANY,
                createURI(location), DC_TITLE, createLiteral(longLiteral)));
    }
}

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

License:asdf

@Test
public void testInboundLinksDoNotUpdateEtag() throws IOException {
    final String id1 = getRandomUniqueId();
    final HttpPut httpPut = putObjMethod(id1);
    final String oldETag;
    final String oldMod;
    try (final CloseableHttpResponse response = execute(httpPut)) {
        assertEquals(CREATED.getStatusCode(), getStatus(response));
        oldETag = response.getFirstHeader("ETag").getValue();
        oldMod = response.getFirstHeader("Last-Modified").getValue();
    }/* ww  w .j a  va  2 s  .c o  m*/

    final String id2 = getRandomUniqueId();
    createObject(id2).close();

    final HttpPatch patch = patchObjMethod(id2);
    patch.addHeader(CONTENT_TYPE, "application/sparql-update");
    patch.setEntity(new StringEntity(
            "INSERT { <> <http://purl.org/dc/elements/1.1/relation> <" + serverAddress + id1 + "> } WHERE {}"));
    assertEquals(NO_CONTENT.getStatusCode(), getStatus(execute(patch)));

    try (final CloseableHttpResponse response = execute(getObjMethod(id1))) {
        final String etag = response.getFirstHeader("ETag").getValue();
        final String lastmod = response.getFirstHeader("Last-Modified").getValue();
        assertEquals(oldMod, lastmod);
        assertEquals(oldETag, etag);
    }
}

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

License:asdf

private int patchWithSparql(final String path, final String sparqlUpdate) {
    try {/*from   w ww.  ja va2  s  .  c o  m*/
        final HttpPatch patch = new HttpPatch(serverAddress + path);
        patch.addHeader(CONTENT_TYPE, "application/sparql-update");
        patch.setEntity(new StringEntity(sparqlUpdate));
        final CloseableHttpResponse r = client.execute(patch);
        final int code = r.getStatusLine().getStatusCode();
        r.close();
        return code;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}