use of org.swordapp.server.MediaResource in project dataverse by IQSS.
the class MediaResourceManagerImpl method getMediaResourceRepresentation.
@Override
public MediaResource getMediaResourceRepresentation(String uri, Map<String, String> map, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
AuthenticatedUser user = swordAuth.auth(authCredentials);
DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
urlManager.processUrl(uri);
String globalId = urlManager.getTargetIdentifier();
if (urlManager.getTargetType().equals("study") && globalId != null) {
logger.fine("looking up dataset with globalId " + globalId);
Dataset dataset = datasetService.findByGlobalId(globalId);
if (dataset != null) {
/**
* @todo: support downloading of files (SWORD 2.0 Profile 6.4. -
* Retrieving the content)
* http://swordapp.github.io/SWORDv2-Profile/SWORDProfile.html#protocoloperations_retrievingcontent
*
* This ticket is mostly about terms of use:
* https://github.com/IQSS/dataverse/issues/183
*/
boolean getMediaResourceRepresentationSupported = false;
if (getMediaResourceRepresentationSupported) {
Dataverse dvThatOwnsDataset = dataset.getOwner();
/**
* @todo Add Dataverse 4 style permission check here. Is
* there a Command we use for downloading files as zip?
*/
boolean authorized = false;
if (authorized) {
/**
* @todo Zip file download is being implemented in
* https://github.com/IQSS/dataverse/issues/338
*/
InputStream fixmeInputStream = new ByteArrayInputStream("FIXME: replace with zip of all dataset files".getBytes());
String contentType = "application/zip";
String packaging = UriRegistry.PACKAGE_SIMPLE_ZIP;
boolean isPackaged = true;
MediaResource mediaResource = new MediaResource(fixmeInputStream, contentType, packaging, isPackaged);
return mediaResource;
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to get a media resource representation of the dataset with global ID " + dataset.getGlobalId());
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Downloading files via the SWORD-based Dataverse Data Deposit API is not (yet) supported: https://github.com/IQSS/dataverse/issues/183");
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't find dataset with global ID of " + globalId);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't dermine target type or identifier from URL: " + uri);
}
}
use of org.swordapp.server.MediaResource in project mycore by MyCoRe-Org.
the class MCRSwordMediaHandler method getMediaResourceRepresentation.
public MediaResource getMediaResourceRepresentation(String derivateID, String requestFilePath, Map<String, String> accept) throws SwordError, SwordServerException {
MediaResource resultRessource;
if (!MCRAccessManager.checkPermission(derivateID, MCRAccessManager.PERMISSION_READ)) {
throw new SwordError(UriRegistry.ERROR_METHOD_NOT_ALLOWED, "You dont have the right to read from the derivate!");
}
if (requestFilePath != null && isValidFilePath(requestFilePath)) {
final MCRPath path = MCRPath.getPath(derivateID, requestFilePath);
checkFile(path);
InputStream is = null;
try {
// MediaResource/Sword2 api should close the stream.
is = Files.newInputStream(path);
resultRessource = new MediaResource(is, Files.probeContentType(path), UriRegistry.PACKAGE_BINARY);
} catch (IOException e) {
LOGGER.error("Error while opening File: {}", path, e);
if (is != null) {
try {
is.close();
} catch (IOException e1) {
LOGGER.error("Could not close Stream after error. ", e);
}
}
throw new SwordError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} else {
// if there is no file path or file is just "/" or "" then send the zipped Derivate
resultRessource = MCRSwordUtil.getZippedDerivateMediaResource(derivateID);
}
MCRSessionMgr.getCurrentSession().commitTransaction();
return resultRessource;
}
use of org.swordapp.server.MediaResource in project mycore by MyCoRe-Org.
the class MCRSwordUtil method getZippedDerivateMediaResource.
public static MediaResource getZippedDerivateMediaResource(String object) {
final Path tempFile;
try {
tempFile = Files.createTempFile("swordv2_", ".temp.zip");
} catch (IOException e) {
throw new MCRException("Could not create temp file!", e);
}
try (final OutputStream tempFileStream = Files.newOutputStream(tempFile)) {
final ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFileStream);
zipOutputStream.setLevel(Deflater.BEST_COMPRESSION);
final MCRPath root = MCRPath.getPath(object, "/");
addDirectoryToZip(zipOutputStream, root);
zipOutputStream.close();
} catch (IOException e) {
throw new MCRException(e);
}
MediaResource resultRessource;
InputStream is;
try {
is = new MCRDeleteFileOnCloseFilterInputStream(Files.newInputStream(tempFile), tempFile);
resultRessource = new MediaResource(is, MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP, UriRegistry.PACKAGE_SIMPLE_ZIP);
} catch (IOException e) {
throw new MCRException("could not read from temp file!", e);
}
return resultRessource;
}
Aggregations