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

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

Introduction

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

Prototype

public HttpPatch(final String uri) 

Source Link

Usage

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 a  va  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 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 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"))));
    }// w  w w.  j  a v a  2s.c om
    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"))));
    }
}

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());
    }// w w w.  ja  v a  2  s.  c om
}

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"));
    }/* w  w  w . ja  v a 2s. 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 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 www . ja va 2 s .  com*/
}

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

License:asdf

@Test
public void testUpdateObjectGraphWithNonLocalTriples() throws IOException {
    final String pid = getRandomUniqueId();
    createObject(pid);/* w  w w  .ja v a2s.c o m*/
    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);// ww  w  .  ja  va2  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)));
    }
}