@Override public List<URI> getChildren(URI resource) { try (final FcrepoResponse response = client.get(resource) .accept("application/n-triples") .preferRepresentation(asList(PREFER_CONTAINMENT), emptyList()).perform()) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getBody(), UTF_8))) { final List<URI> children = new ArrayList<>(); for (String line = reader.readLine(); line != null; line = reader.readLine()) { final Matcher aclFinder = childPattern.matcher(line); if (aclFinder.matches()) { children.add(URI.create(aclFinder.group(1))); } } return children; } } catch (final Exception e) { throw new RuntimeException("Error getting children of " + resource, e); } } }
/** * @see org.dataconservancy.pass.v2_3.client.PassClient#deleteResource(URI) * @param uri uri. */ public void deleteResource(URI uri) { try (FcrepoResponse response = new DeleteBuilder(uri, client).perform()) { LOG.info("Resource deletion status for {}: {}", uri, response.getStatusCode()); } catch (IOException | FcrepoOperationFailedException e) { throw new RuntimeException("A problem occurred while attempting to delete a Resource", e); } }
/** * location getter * * @return the location of a related resource */ public URI getLocation() { if (location == null && headers != null) { // Retrieve the value from the location header if available final String value = getHeaderValue(LOCATION); if (value != null) { location = URI.create(getHeaderValue(LOCATION)); } // Fall back to retrieving from the described by link if (location == null) { final List<URI> links = getLinkHeaders(DESCRIBEDBY_REL); if (links != null && links.size() == 1) { location = links.get(0); } } } return location; }
.perform()) { LOG.info("Resource read status for {}: {}", uri, response.getStatusCode()); T model = adapter.toModel(response.getBody(), modelClass); String etag = response.getHeaderValue(ETAG_HEADER); if (etag!=null && etag.contains(ETAG_WEAK_PREFIX)) { etag = etag.replace(ETAG_WEAK_PREFIX, "");
.perform()) { LOG.info("Resource read status: for {}: {}", passEntityUri, response.getStatusCode()); JsonNode raw = new ObjectMapper().readTree(response.getBody()); JsonNode graph = raw.withArray("@graph");
case PATCH: response = fcrepoClient.patch(getMetadataUri(url)).body(in.getBody(InputStream.class)).perform(); exchange.getIn().setBody(extractResponseBodyAsStream(response.getBody(), exchange)); break; case PUT: response = fcrepoClient.put(URI.create(url)).body(in.getBody(InputStream.class), contentType).perform(); exchange.getIn().setBody(extractResponseBodyAsStream(response.getBody(), exchange)); break; case POST: response = fcrepoClient.post(URI.create(url)).body(in.getBody(InputStream.class), contentType).perform(); exchange.getIn().setBody(extractResponseBodyAsStream(response.getBody(), exchange)); break; case DELETE: response = fcrepoClient.delete(URI.create(url)).perform(); exchange.getIn().setBody(extractResponseBodyAsStream(response.getBody(), exchange)); break; case HEAD: exchange.getIn().setBody(extractResponseBodyAsStream(response.getBody(), exchange)); exchange.getIn().setHeader(CONTENT_TYPE, response.getContentType()); exchange.getIn().setHeader(HTTP_RESPONSE_CODE, response.getStatusCode());
/** * Retrieve the resource location from a HEAD request. */ private URI getMetadataUri(final String url) throws FcrepoOperationFailedException { final FcrepoResponse headResponse = fcrepoClient.head(URI.create(url)).perform(); if (headResponse.getLocation() != null) { return headResponse.getLocation(); } else { return URI.create(url); } }
/** * contentType getter * * @return the mime-type of response */ public String getContentType() { if (contentType == null && headers != null) { contentType = getHeaderValue(CONTENT_TYPE); } return contentType; }
/** * Handle the general case with responses. */ private FcrepoResponse fcrepoGenericResponse(final URI url, final CloseableHttpResponse response, final Boolean throwExceptionOnFailure) throws FcrepoOperationFailedException { final int status = response.getStatusLine().getStatusCode(); final Map<String, List<String>> headers = getHeaders(response); if ((status >= HttpStatus.SC_OK && status < HttpStatus.SC_BAD_REQUEST) || !throwExceptionOnFailure) { return new FcrepoResponse(url, status, headers, getEntityContent(response)); } else { free(response); throw new FcrepoOperationFailedException(url, status, response.getStatusLine().getReasonPhrase()); } }
.perform()) { LOG.info("Resource read status for {}: {}", uri, response.getStatusCode()); T model = adapter.toModel(response.getBody(), modelClass); String etag = response.getHeaderValue(ETAG_HEADER); if (etag!=null && etag.contains(ETAG_WEAK_PREFIX)) { etag = etag.replace(ETAG_WEAK_PREFIX, "");
.perform()) { LOG.info("Resource read status: for {}: {}", passEntityUri, response.getStatusCode()); JsonNode raw = new ObjectMapper().readTree(response.getBody()); JsonNode graph = raw.withArray("@graph");
@Override protected void doBegin(final Object transaction, final TransactionDefinition definition) { final FcrepoResponse response; final InputStream is = null; final String contentType = null; final FcrepoTransactionObject tx = (FcrepoTransactionObject)transaction; if (tx.getSessionId() == null) { try { response = getClient().post(URI.create(baseUrl + TRANSACTION)) .body(is, contentType).perform(); } catch (final FcrepoOperationFailedException ex) { LOGGER.debug("HTTP Operation failed: ", ex); throw new CannotCreateTransactionException("Could not create fcrepo transaction"); } if (response != null && response.getLocation() != null) { tx.setSessionId(response.getLocation().toString().substring(baseUrl.length() + 1)); } else { throw new CannotCreateTransactionException("Invalid response while creating transaction"); } } }
if (response.getStatusCode() == 404) { try (FcrepoResponse createResp = client.put(getAclBase()).perform()) { System.err.println(createResp.getStatusCode()); if (response.getStatusCode() == 404) { try (FcrepoResponse putResponse = client.put(container).perform()) { if (!(putResponse.getStatusCode() < 299)) { System.out.println(IOUtils.toString(putResponse.getBody(), UTF_8)); } else { System.out.println("Created new resource " + IOUtils.toString(putResponse.getBody(), UTF_8));
/** * @see org.dataconservancy.pass.client.PassClient#deleteResource(URI) * @param uri uri. */ public void deleteResource(URI uri) { try (FcrepoResponse response = new DeleteBuilder(uri, client).perform()) { LOG.info("Resource deletion status for {}: {}", uri, response.getStatusCode()); } catch (IOException | FcrepoOperationFailedException e) { throw new RuntimeException("A problem occurred while attempting to delete a Resource", e); } }
@Override public List<URI> getChildren(URI resource) { try (final FcrepoResponse response = client.get(resource) .accept("application/n-triples") .preferRepresentation(asList(PREFER_CONTAINMENT), emptyList()).perform()) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getBody(), UTF_8))) { final List<URI> children = new ArrayList<>(); for (String line = reader.readLine(); line != null; line = reader.readLine()) { final Matcher aclFinder = childPattern.matcher(line); if (aclFinder.matches()) { children.add(URI.create(aclFinder.group(1))); } } return children; } } catch (final Exception e) { throw new RuntimeException("Error getting children of " + resource, e); } } }
return response.getLocation(); } catch (Exception e) { throw new RuntimeException("An problem occurred while POSTing binary content to Resource " +
/** * @param destinationURI URI * @param resourceFile File * @param contentType String * @throws ModellerClientFailedException Throwable */ public static void doBinaryPut(final URI destinationURI, final File resourceFile, final String contentType) throws ModellerClientFailedException { final FcrepoClient testClient; testClient = FcrepoClient.client().throwExceptionOnFailure().build(); try { final FcrepoResponse response = testClient.put(destinationURI).body(resourceFile, contentType).perform(); log.info(String.valueOf(response.getStatusCode())); } catch (FcrepoOperationFailedException e) { log.info(getMessage(e)); throw new ModellerClientFailedException(e); } catch (IOException e) { e.printStackTrace(); } }
/** * @param destinationURI URI * @throws ModellerClientFailedException Throwable */ public static void doPut(final URI destinationURI) throws ModellerClientFailedException { final FcrepoClient testClient; testClient = FcrepoClient.client().throwExceptionOnFailure().build(); try { final FcrepoResponse response = testClient.put(destinationURI).perform(); try { log.info(IOUtils.toString(response.getBody(), UTF_8)); } catch (IOException e) { log.info(getMessage(e)); } } catch (FcrepoOperationFailedException e) { log.info(getMessage(e)); throw new ModellerClientFailedException(e); } }
return response.getLocation(); } catch (Exception e) { throw new RuntimeException("An problem occurred while POSTing binary content to Resource " +
/** * @param destinationURI URI * @param rdfBody InputStream * @throws ModellerClientFailedException Throwable */ public static void doPatch(final URI destinationURI, final InputStream rdfBody) throws ModellerClientFailedException { final FcrepoClient testClient; testClient = FcrepoClient.client().throwExceptionOnFailure().build(); try { final FcrepoResponse response = testClient.patch(destinationURI).body(rdfBody).perform(); log.info(String.valueOf(response.getStatusCode())); } catch (FcrepoOperationFailedException e) { log.info(getMessage(e)); throw new ModellerClientFailedException(e); } }