List of usage examples for org.apache.http.entity BasicHttpEntity setContent
public void setContent(InputStream inputStream)
From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java
License:asdf
@Test public void testJsonLdProfileCompacted() throws IOException { // Create a resource final HttpPost method = postObjMethod(); method.addHeader(CONTENT_TYPE, "text/n3"); final BasicHttpEntity entity = new BasicHttpEntity(); final String rdf = "<> <http://purl.org/dc/elements/1.1/title> \"ceci n'est pas un titre franais\"@fr ." + "<> <http://purl.org/dc/elements/1.1/title> \"this is an english title\"@en ."; entity.setContent(new ByteArrayInputStream(rdf.getBytes(UTF_8))); method.setEntity(entity);/* w ww .j a v a2 s. c om*/ final String location; try (final CloseableHttpResponse response = execute(method)) { assertEquals("Didn't get a CREATED response!", CREATED.getStatusCode(), getStatus(response)); location = response.getFirstHeader("Location").getValue(); } // GET the resource with a JSON profile final HttpGet httpGet = new HttpGet(location); httpGet.setHeader(ACCEPT, "application/ld+json; profile=\"http://www.w3.org/ns/json-ld#compacted\""); final JsonNode json; try (final CloseableHttpResponse responseGET = execute(httpGet)) { // Inspect the response final ObjectMapper mapper = new ObjectMapper(); json = mapper.readTree(responseGET.getEntity().getContent()); } final JsonNode titles = json.get("title"); assertNotNull(titles); assertTrue("Should be a list", titles.isArray()); assertEquals("Should be two langs!", 2, titles.findValues("@language").size()); assertEquals("Should be two values!", 2, titles.findValues("@value").size()); }
From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java
License:asdf
@Test public void testJsonLdProfileExpanded() throws IOException { // Create a resource final HttpPost method = postObjMethod(); method.addHeader(CONTENT_TYPE, "text/n3"); final BasicHttpEntity entity = new BasicHttpEntity(); final String rdf = "<> <http://purl.org/dc/elements/1.1/title> \"ceci n'est pas un titre franais\"@fr ." + "<> <http://purl.org/dc/elements/1.1/title> \"this is an english title\"@en ."; entity.setContent(new ByteArrayInputStream(rdf.getBytes(UTF_8))); method.setEntity(entity);//from w ww. j a va 2s.c om final String location; try (final CloseableHttpResponse response = execute(method)) { assertEquals("Didn't get a CREATED response!", CREATED.getStatusCode(), getStatus(response)); location = response.getFirstHeader("Location").getValue(); } // GET the resource with a JSON profile final HttpGet httpGet = new HttpGet(location); httpGet.setHeader(ACCEPT, "application/ld+json; profile=\"http://www.w3.org/ns/json-ld#expanded\""); final JsonNode json; try (final CloseableHttpResponse responseGET = execute(httpGet)) { // Inspect the response final ObjectMapper mapper = new ObjectMapper(); json = mapper.readTree(responseGET.getEntity().getContent()); } final List<JsonNode> titlesList = json.findValues("http://purl.org/dc/elements/1.1/title"); assertNotNull(titlesList); assertEquals("Should be list of lists", 1, titlesList.size()); final JsonNode titles = titlesList.get(0); assertEquals("Should be two langs!", 2, titles.findValues("@language").size()); assertEquals("Should be two values!", 2, titles.findValues("@value").size()); }
From source file:org.fcrepo.integration.http.api.FedoraLdpIT.java
License:asdf
@Test public void testJsonLdProfileFlattened() throws IOException { // Create a resource final HttpPost method = postObjMethod(); method.addHeader(CONTENT_TYPE, "text/n3"); final BasicHttpEntity entity = new BasicHttpEntity(); final String rdf = "<> <http://purl.org/dc/elements/1.1/title> \"ceci n'est pas un titre franais\"@fr ." + "<> <http://purl.org/dc/elements/1.1/title> \"this is an english title\"@en ."; entity.setContent(new ByteArrayInputStream(rdf.getBytes(UTF_8))); method.setEntity(entity);/*from ww w.j a va2s. co m*/ final String location; try (final CloseableHttpResponse response = execute(method)) { assertEquals("Didn't get a CREATED response!", CREATED.getStatusCode(), getStatus(response)); location = response.getFirstHeader("Location").getValue(); } // GET the resource with a JSON profile final HttpGet httpGet = new HttpGet(location); httpGet.setHeader(ACCEPT, "application/ld+json; profile=\"http://www.w3.org/ns/json-ld#flattened\""); final JsonNode json; try (final CloseableHttpResponse responseGET = execute(httpGet)) { // Inspect the response final ObjectMapper mapper = new ObjectMapper(); json = mapper.readTree(responseGET.getEntity().getContent()); } final List<JsonNode> titlesList = json.get("@graph").findValues("title"); assertNotNull(titlesList); assertEquals("Should be list of lists", 1, titlesList.size()); final JsonNode titles = titlesList.get(0); assertEquals("Should be two langs!", 2, titles.findValues("@language").size()); assertEquals("Should be two values!", 2, titles.findValues("@value").size()); }
From source file:org.fcrepo.integration.http.api.FedoraTransactionsIT.java
/** * Tests whether a Sparql update is visible within a transaction * and if the update is made persistent along with the commit. * @throws Exception//from w w w. j a va 2s.c om */ @Test public void testIngestNewWithSparqlPatchWithinTransaction() throws Exception { final String objectInTxCommit = randomUUID().toString(); /* create new tx */ final String txLocation = createTransaction(); client = createClient(); final HttpPost postNew = new HttpPost(txLocation); postNew.addHeader("Slug", objectInTxCommit); HttpResponse resp = execute(postNew); assertEquals(201, resp.getStatusLine().getStatusCode()); final String newObjectLocation = resp.getFirstHeader("Location").getValue(); /* update sparql */ final HttpPatch method = new HttpPatch(newObjectLocation); method.addHeader("Content-Type", "application/sparql-update"); final BasicHttpEntity entity = new BasicHttpEntity(); final String title = "this is a new title"; entity.setContent(new ByteArrayInputStream( ("INSERT { <> <http://purl.org/dc/elements/1.1/title> \"" + title + "\" } WHERE {}").getBytes())); method.setEntity(entity); final HttpResponse responseFromPatch = client.execute(method); final int status = responseFromPatch.getStatusLine().getStatusCode(); assertEquals("Didn't get a 204 status! Got status:\n" + status, NO_CONTENT.getStatusCode(), status); /* make sure the change was made within the tx */ final HttpGet httpGet = new HttpGet(newObjectLocation); final GraphStore graphStore = getGraphStore(httpGet); assertTrue("The sparql update did not succeed within a transaction", graphStore.contains(ANY, createResource(newObjectLocation).asNode(), DC_TITLE.asNode(), createPlainLiteral(title).asNode())); /* commit */ client = createClient(); final HttpPost commitTx = new HttpPost(txLocation + "/fcr:tx/fcr:commit"); resp = execute(commitTx); assertEquals(204, resp.getStatusLine().getStatusCode()); /* it must exist after commit */ client = createClient(); final HttpGet getObjCommitted = new HttpGet(serverAddress + objectInTxCommit); final GraphStore graphStoreAfterCommit = getGraphStore(getObjCommitted); assertTrue("The inserted triple does not exist after the transaction has committed", graphStoreAfterCommit.contains(ANY, ANY, DC_TITLE.asNode(), createPlainLiteral(title).asNode())); }
From source file:org.fcrepo.integration.ldp.LdpTestSuiteIT.java
@Test public void runLDPBasicContainerTestSuite() throws IOException { final String pid = "ldp-test-basic-" + UUID.randomUUID().toString(); final HttpPut request = new HttpPut(serverAddress + pid); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(IOUtils.toInputStream("<> a <" + BASIC_CONTAINER.getURI() + "> .")); request.setEntity(entity);/* ww w . j ava2s . co m*/ request.setHeader("Content-Type", "text/turtle"); final HttpResponse response = client.execute(request); assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode()); final HashMap<String, String> options = new HashMap<>(); options.put("server", serverAddress + pid); options.put("output", "report-basic"); options.put("basic", "true"); options.put("non-rdf", "true"); options.put("read-only-prop", "http://fedora.info/definitions/v4/repository#uuid"); final LdpTestSuite testSuite = new LdpTestSuite(options); testSuite.run(); assertTrue("The LDP test suite is only informational", true); }
From source file:org.fcrepo.integration.ldp.LdpTestSuiteIT.java
@Test public void runLDPDirectContainerTestSuite() throws IOException { final String pid = "ldp-test-direct-" + UUID.randomUUID().toString(); final HttpPut request = new HttpPut(serverAddress + pid); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(IOUtils.toInputStream( "<> a <" + DIRECT_CONTAINER.getURI() + "> ;" + " <" + LDP_NAMESPACE + "membershipResource> <> ;" + " <" + LDP_NAMESPACE + "hasMemberRelation> <" + LDP_NAMESPACE + "member> .")); request.setEntity(entity);//from w w w . j av a 2 s . c o m request.setHeader("Content-Type", "text/turtle"); final HttpResponse response = client.execute(request); assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode()); final HashMap<String, String> options = new HashMap<>(); options.put("server", serverAddress + pid); options.put("output", "report-direct"); options.put("direct", "true"); options.put("non-rdf", "true"); options.put("read-only-prop", "http://fedora.info/definitions/v4/repository#uuid"); final LdpTestSuite testSuite = new LdpTestSuite(options); testSuite.run(); assertTrue("The LDP test suite is only informational", true); }
From source file:org.fcrepo.integration.ldp.LdpTestSuiteIT.java
@Test public void runLDPIndirectContainerTestSuite() throws IOException { final String pid = "ldp-test-indirect-" + UUID.randomUUID().toString(); final HttpPut request = new HttpPut(serverAddress + pid); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(IOUtils.toInputStream("<> a <" + INDIRECT_CONTAINER.getURI() + ">;" + " <" + LDP_NAMESPACE + "membershipResource> <> ;" + " <" + LDP_NAMESPACE + "insertedContentRelation> <" + LDP_NAMESPACE + "MemberSubject> ;" + " <" + LDP_NAMESPACE + "hasMemberRelation> <" + LDP_NAMESPACE + "member> .")); request.setEntity(entity);//from w w w.j ava2 s . c o m request.setHeader("Content-Type", "text/turtle"); final HttpResponse response = client.execute(request); assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode()); final HashMap<String, String> options = new HashMap<>(); options.put("server", serverAddress + pid); options.put("output", "report-indirect"); options.put("indirect", "true"); options.put("non-rdf", "true"); options.put("read-only-prop", "http://fedora.info/definitions/v4/repository#uuid"); final LdpTestSuite testSuite = new LdpTestSuite(options); testSuite.run(); assertTrue("The LDP test suite is only informational", true); }
From source file:org.fcrepo.integration.rdf.AbstractIntegrationRdfIT.java
protected HttpResponse createLDPRSAndCheckResponse(final String pid, final String body) { try {/*w ww .jav a2 s.c o m*/ final HttpPut httpPut = new HttpPut(serverAddress + pid); httpPut.addHeader("Slug", pid); httpPut.addHeader("Content-Type", "text/turtle"); final BasicHttpEntity e = new BasicHttpEntity(); e.setContent(IOUtils.toInputStream(body)); httpPut.setEntity(e); final HttpResponse response = client.execute(httpPut); checkResponse(response, CREATED); final String location = response.getFirstHeader("Location").getValue(); final HttpGet httpGet = new HttpGet(location); httpGet.addHeader("Prefer", "return=representation; " + "include=\"http://www.w3.org/ns/ldp#PreferMinimalContainer\"; " + "omit=\"http://fedora.info/definitions/v4/repository#ServerManaged\""); final GraphStore graphStore = getGraphStore(httpGet); assertFalse(graphStore.isEmpty()); final Graph tidiedGraph = getTidiedGraph(graphStore); final Model expected = ModelFactory.createDefaultModel().read(IOUtils.toInputStream(body), location, "TTL"); final boolean isomorphicWith = tidiedGraph.isIsomorphicWith(getTidiedGraph(expected.getGraph())); final String description; if (!isomorphicWith) { final ByteArrayOutputStream o = new ByteArrayOutputStream(); final Model tidiedModel = ModelFactory.createModelForGraph(tidiedGraph); tidiedModel.setNsPrefixes(expected.getNsPrefixMap()); o.write("Expected: ".getBytes()); RDFDataMgr.write(o, expected, RDFLanguages.TTL); o.write("to be isomorphic with: ".getBytes()); RDFDataMgr.write(o, tidiedModel, RDFLanguages.TTL); description = IOUtils.toString(o.toByteArray(), "UTF-8"); } else { description = ""; } assertTrue(description, isomorphicWith); return response; } catch (final IOException e) { assertTrue("Got IOException " + e, false); return null; } }
From source file:org.fcrepo.integration.transform.http.FedoraSparqlIT.java
private String getResponseContent(final String sparql) throws IOException { final HttpPost sparqlRequest = new HttpPost(serverAddress + "/fcr:sparql"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(IOUtils.toInputStream(sparql)); sparqlRequest.setEntity(entity);//from w ww.j a v a2s . c o m final HttpResponse response = client.execute(sparqlRequest); assertEquals(200, response.getStatusLine().getStatusCode()); final String content = EntityUtils.toString(response.getEntity()); logger.trace("Retrieved sparql feed:\n" + content); return content; }
From source file:org.jenkinsci.plugins.skytap.ConnectToVPNTunnelStep.java
private String connectVPNToConfiguration(String confId, String networkId, String vpnId) { // build url/*from w w w .j av a2 s . c om*/ String reqUrl = this.buildConnectRequestURL(confId, networkId, vpnId); // create request HttpPut hp = SkytapUtils.buildHttpPutRequest(reqUrl, this.authCredentials); // add content to request - vpn identifier BasicHttpEntity he = new BasicHttpEntity(); he.setContentEncoding("gzip"); he.setContentType("application/json"); // json string for connected attribute String jsonString = "{\"connected\" :true}"; InputStream stream; try { stream = new ByteArrayInputStream(jsonString.getBytes("UTF-8")); Integer len = jsonString.getBytes("UTF-8").length; long llen = len.longValue(); he.setContent(stream); he.setContentLength(llen); } catch (UnsupportedEncodingException e) { JenkinsLogger.error("Error encoding json string for connected attribute: " + e.getMessage()); } hp.setEntity(he); String response = ""; try { response = SkytapUtils.executeHttpRequest(hp); } catch (SkytapException e) { JenkinsLogger.error("Skytap Exception: " + e.getMessage()); } return response; }