use of org.fcrepo.client.FcrepoOperationFailedException in project box-c by UNC-Libraries.
the class RepositoryObjectDriver method loadModel.
/**
* Loads and assigns the model for direct relationships of the given
* repository object
*
* @param obj
* @param checkForUpdates if true, will reload the model if the object has changed
* @return
* @throws FedoraException
*/
public RepositoryObjectDriver loadModel(RepositoryObject obj, boolean checkForUpdates) throws FedoraException {
long start = System.nanoTime();
URI metadataUri = obj.getMetadataUri();
// Model needs to be loaded if not present, or if checkForUpdates is true and either in a tx or obj has changed
if (obj.hasModel() && !(checkForUpdates && (FedoraTransaction.isStillAlive() || !obj.isUnmodified()))) {
log.debug("Object unchanged, reusing existing model for {}", obj.getPid());
return this;
}
// Need to load the model from fedora
try (FcrepoResponse response = getClient().get(metadataUri).accept(TURTLE_MIMETYPE).perform()) {
Model model = ModelFactory.createDefaultModel();
model.read(response.getBody(), null, Lang.TURTLE.getName());
// Store the fresh model
obj.storeModel(model);
// Store updated modification info to track if the object changes
obj.setEtag(parseEtag(response));
log.debug("Retrieved new model for {} in {}s", obj.getPid(), (System.nanoTime() - start) / 1e9);
return this;
} catch (IOException e) {
throw new FedoraException("Failed to read model for " + metadataUri, e);
} catch (FcrepoOperationFailedException e) {
throw ClientFaultResolver.resolve(e);
}
}
use of org.fcrepo.client.FcrepoOperationFailedException in project box-c by UNC-Libraries.
the class RepositoryObjectCacheLoader method loadObject.
public RepositoryObject loadObject(PID pid) {
String etag;
try (FcrepoResponse response = client.head(pid.getRepositoryUri()).perform()) {
boolean isBinary = response.hasType(BINARY_TYPE_URI);
etag = response.getHeaderValue("ETag");
if (etag != null) {
etag = new EntityTag(etag).getValue();
}
// For binaries, pull out location of content and immediately instantiate binary obj
if (isBinary) {
log.debug("Loading object for binary {}", pid);
String contentLoc = response.getHeaderValue("Content-Location");
URI contentUri = null;
if (contentLoc != null) {
contentUri = URI.create(contentLoc);
}
return instantiateBinaryObject(pid, contentUri, etag);
}
} catch (IOException e) {
throw new FedoraException("Failed to read model for " + pid, e);
} catch (FcrepoOperationFailedException e) {
throw ClientFaultResolver.resolve(e);
}
log.debug("Loading object for RDF resource {}", pid);
// For non-binaries, retrieve the metadata body before instantiation
URI metadataUri = pid.getRepositoryUri();
Model model;
try (FcrepoResponse modelResp = client.get(metadataUri).accept(TURTLE_MIMETYPE).perform()) {
model = ModelFactory.createDefaultModel();
model.read(modelResp.getBody(), null, "TURTLE");
} catch (IOException e) {
throw new FedoraException("Failed to read model for " + pid, e);
} catch (FcrepoOperationFailedException e) {
throw ClientFaultResolver.resolve(e);
}
return instantiateRepositoryObject(pid, model, etag);
}
use of org.fcrepo.client.FcrepoOperationFailedException in project box-c by UNC-Libraries.
the class RepositoryObjectFactoryImpl method updateBinaryDescription.
private void updateBinaryDescription(PID binPid, URI describedBy, Model model) {
// Add in pcdm:File type to model
populateModelTypes(binPid.getRepositoryUri(), model, Arrays.asList(PcdmModels.File));
// If a model was provided, then add the triples to the new binary's
// metadata
// Turn model into sparql update query
String sparqlUpdate = SparqlUpdateHelper.createSparqlInsert(model);
InputStream sparqlStream = new ByteArrayInputStream(sparqlUpdate.getBytes(StandardCharsets.UTF_8));
try (FcrepoResponse response = getClient().patch(describedBy).body(sparqlStream).perform()) {
} catch (IOException e) {
throw new FedoraException("Unable to add triples to binary at " + describedBy, e);
} catch (FcrepoOperationFailedException e) {
throw ClientFaultResolver.resolve(e);
}
}
use of org.fcrepo.client.FcrepoOperationFailedException in project box-c by UNC-Libraries.
the class RepositoryObjectFactoryImpl method createOrUpdateBinary.
/**
* Creates a BinaryObject with the given PID, using the provided storage URI as the proxied external binary
* location.
*
* @param pid pid of the binary
* @param storageUri location where the binary is stored
* @param filename filename for the binary
* @param mimetype mimetype of the binary
* @param sha1Checksum sha1 digest of the content.
* @param md5Checksum md5 digest of the content.
* @param model Model containing any properties to include in the description of this binary
* @return the newly created BinaryObject
* @throws FedoraException
*/
@Override
public BinaryObject createOrUpdateBinary(PID pid, URI storageUri, String filename, String mimetype, String sha1Checksum, String md5Checksum, Model model) {
// Upload the binary and provided metadata
URI resultUri;
// Track the URI where metadata updates would be made to for this binary
URI describedBy;
log.debug("Updating binary {} with fields:\n uri = '{}'\n mimetype = '{}', " + "filename = '{}'\n sha1 = '{}', m5 = '{}'", pid.getRepositoryPath(), storageUri, mimetype, filename, sha1Checksum, md5Checksum);
try (FcrepoResponse response = getClient().put(pid.getRepositoryUri()).externalContent(storageUri, formatMimetype(mimetype), PROXY).addInteractionModel(LDP_NON_RDF_SOURCE).filename(filename).digestSha1(sha1Checksum).digestMd5(md5Checksum).perform()) {
resultUri = response.getLocation();
describedBy = response.getLinkHeaders(DESCRIBEDBY_REL).get(0);
} catch (IOException e) {
throw new FedoraException("Unable to create binary at " + pid.getRepositoryPath(), e);
} catch (FcrepoOperationFailedException e) {
// if one or more checksums don't match
if (e.getStatusCode() == HttpStatus.SC_CONFLICT) {
throw new ChecksumMismatchException(String.format("Failed to create binary for %s" + " from URI '%s', checksum(s) did not match the submitted" + " content according to the repository: md5=%s sha1=%s", pid.getRepositoryPath(), storageUri, md5Checksum, sha1Checksum));
}
throw ClientFaultResolver.resolve(e);
}
if (model != null) {
updateBinaryDescription(pid, describedBy, model);
}
String resultUriString = resultUri.toString();
if (resultUriString.endsWith(FCR_METADATA)) {
resultUriString = resultUriString.replace("/" + FCR_METADATA, "");
}
PID binPid = PIDs.get(resultUriString);
repoObjLoader.invalidate(binPid);
return repoObjLoader.getBinaryObject(binPid);
}
use of org.fcrepo.client.FcrepoOperationFailedException in project box-c by UNC-Libraries.
the class DestroyProxyService method destroyProxy.
/**
* Destroys the membership proxy referencing objPid.
*
* @param objPid pid of the object whose proxy will be destroyed.
* @return the path of the parent object the proxy was removed from.
*/
public String destroyProxy(PID objPid) {
ProxyInfo proxyInfo = getProxyInfo(objPid);
if (proxyInfo == null) {
log.debug("No proxy found for object {}", objPid);
return null;
}
URI proxyUri = proxyInfo.proxyUri;
try (FcrepoResponse resp = fcrepoClient.delete(proxyUri).perform()) {
} catch (FcrepoOperationFailedException | IOException e) {
throw new ServiceException("Unable to clean up proxy for " + objPid, e);
}
URI tombstoneUri = URI.create(proxyUri.toString() + "/" + FCR_TOMBSTONE);
try (FcrepoResponse resp = fcrepoClient.delete(tombstoneUri).perform()) {
} catch (FcrepoOperationFailedException | IOException e) {
throw new ServiceException("Unable to clean up proxy tombstone for " + objPid, e);
}
return proxyInfo.sourcePath;
}
Aggregations