use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class MediaResourceManagerImpl method deleteMediaResource.
@Override
public void deleteMediaResource(String uri, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
AuthenticatedUser user = swordAuth.auth(authCredentials);
DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
urlManager.processUrl(uri);
String targetType = urlManager.getTargetType();
String fileId = urlManager.getTargetIdentifier();
if (targetType != null && fileId != null) {
if ("file".equals(targetType)) {
String fileIdString = urlManager.getTargetIdentifier();
if (fileIdString != null) {
Long fileIdLong;
try {
fileIdLong = Long.valueOf(fileIdString);
} catch (NumberFormatException ex) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "File id must be a number, not '" + fileIdString + "'. URL was: " + uri);
}
if (fileIdLong != null) {
logger.fine("preparing to delete file id " + fileIdLong);
DataFile fileToDelete = dataFileService.find(fileIdLong);
if (fileToDelete != null) {
Dataset dataset = fileToDelete.getOwner();
Dataset datasetThatOwnsFile = fileToDelete.getOwner();
Dataverse dataverseThatOwnsFile = datasetThatOwnsFile.getOwner();
/**
* @todo it would be nice to have this check higher
* up. Do we really need the file ID? Should the
* last argument to isUserAllowedOn be changed from
* "dataset" to "fileToDelete"?
*/
UpdateDatasetCommand updateDatasetCommand = new UpdateDatasetCommand(dataset, dvReq, fileToDelete);
if (!permissionService.isUserAllowedOn(user, updateDatasetCommand, dataset)) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "User " + user.getDisplayInfo().getTitle() + " is not authorized to modify " + dataverseThatOwnsFile.getAlias());
}
try {
commandEngine.submit(updateDatasetCommand);
} catch (CommandException ex) {
throw SwordUtil.throwSpecialSwordErrorWithoutStackTrace(UriRegistry.ERROR_BAD_REQUEST, "Could not delete file: " + ex);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to find file id " + fileIdLong + " from URL: " + uri);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to find file id in URL: " + uri);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not file file to delete in URL: " + uri);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unsupported file type found in URL: " + uri);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Target or identifer not specified in URL: " + uri);
}
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class StatementManagerImpl method getStatement.
@Override
public Statement getStatement(String editUri, Map<String, String> map, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordServerException, SwordError, SwordAuthException {
AuthenticatedUser user = swordAuth.auth(authCredentials);
DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
urlManager.processUrl(editUri);
String globalId = urlManager.getTargetIdentifier();
if (urlManager.getTargetType().equals("study") && globalId != null) {
logger.fine("request for sword statement by user " + user.getDisplayInfo().getTitle());
Dataset dataset = datasetService.findByGlobalId(globalId);
if (dataset == null) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "couldn't find dataset with global ID of " + globalId);
}
Dataverse dvThatOwnsDataset = dataset.getOwner();
if (!permissionService.isUserAllowedOn(user, new GetDraftDatasetVersionCommand(dvReq, dataset), dataset)) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to view dataset with global ID " + globalId);
}
String feedUri = urlManager.getHostnamePlusBaseUrlPath(editUri) + "/edit/study/" + dataset.getGlobalId();
String author = dataset.getLatestVersion().getAuthorsStr();
String title = dataset.getLatestVersion().getTitle();
// in the statement, the element is called "updated"
Date lastUpdatedFinal = new Date();
Date lastUpdateTime = dataset.getLatestVersion().getLastUpdateTime();
if (lastUpdateTime != null) {
lastUpdatedFinal = lastUpdateTime;
} else {
logger.info("lastUpdateTime was null, trying createtime");
Date createtime = dataset.getLatestVersion().getCreateTime();
if (createtime != null) {
lastUpdatedFinal = createtime;
} else {
logger.info("creatime was null, using \"now\"");
lastUpdatedFinal = new Date();
}
}
AtomDate atomDate = new AtomDate(lastUpdatedFinal);
String datedUpdated = atomDate.toString();
Statement statement = new AtomStatement(feedUri, author, title, datedUpdated);
Map<String, String> states = new HashMap<>();
states.put("latestVersionState", dataset.getLatestVersion().getVersionState().toString());
Boolean isMinorUpdate = dataset.getLatestVersion().isMinorUpdate();
states.put("isMinorUpdate", isMinorUpdate.toString());
if (dataset.isLocked()) {
states.put("locked", "true");
states.put("lockedDetail", dataset.getLocks().stream().map(l -> l.getInfo()).collect(joining(",")));
Optional<DatasetLock> earliestLock = dataset.getLocks().stream().min((l1, l2) -> (int) Math.signum(l1.getStartTime().getTime() - l2.getStartTime().getTime()));
states.put("lockedStartTime", earliestLock.get().getStartTime().toString());
} else {
states.put("locked", "false");
}
statement.setStates(states);
List<FileMetadata> fileMetadatas = dataset.getLatestVersion().getFileMetadatas();
for (FileMetadata fileMetadata : fileMetadatas) {
DataFile dataFile = fileMetadata.getDataFile();
// We are exposing the filename for informational purposes. The file id is what you
// actually operate on to delete a file, etc.
//
// Replace spaces to avoid IRISyntaxException
String fileNameFinal = fileMetadata.getLabel().replace(' ', '_');
String fileUrlString = urlManager.getHostnamePlusBaseUrlPath(editUri) + "/edit-media/file/" + dataFile.getId() + "/" + fileNameFinal;
IRI fileUrl;
try {
fileUrl = new IRI(fileUrlString);
} catch (IRISyntaxException ex) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Invalid URL for file ( " + fileUrlString + " ) resulted in " + ex.getMessage());
}
ResourcePart resourcePart = new ResourcePart(fileUrl.toString());
// default to something that doesn't throw a org.apache.abdera.util.MimeTypeParseException
String finalFileFormat = "application/octet-stream";
String contentType = dataFile.getContentType();
if (contentType != null) {
finalFileFormat = contentType;
}
resourcePart.setMediaType(finalFileFormat);
/**
* @todo: Why are properties set on a ResourcePart not exposed
* when you GET a Statement? Asked about this at
* http://www.mail-archive.com/sword-app-tech@lists.sourceforge.net/msg00394.html
*/
// Map<String, String> properties = new HashMap<String, String>();
// properties.put("filename", studyFile.getFileName());
// properties.put("category", studyFile.getLatestCategory());
// properties.put("originalFileType", studyFile.getOriginalFileType());
// properties.put("id", studyFile.getId().toString());
// properties.put("UNF", studyFile.getUnf());
// resourcePart.setProperties(properties);
statement.addResource(resourcePart);
/**
* @todo it's been noted at
* https://github.com/IQSS/dataverse/issues/892#issuecomment-54159284
* that at the file level the "updated" date is always "now",
* which seems to be set here:
* https://github.com/swordapp/JavaServer2.0/blob/sword2-server-1.0/src/main/java/org/swordapp/server/AtomStatement.java#L70
*/
}
return statement;
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not determine target type or identifier from URL: " + editUri);
}
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class Access method fileCardImage.
@Path("fileCardImage/{fileId}")
@GET
@Produces({ "image/png" })
public InputStream fileCardImage(@PathParam("fileId") Long fileId, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
DataFile df = dataFileService.find(fileId);
if (df == null) {
logger.warning("Preview: datafile service could not locate a DataFile object for id " + fileId + "!");
return null;
}
StorageIO<DataFile> thumbnailDataAccess = null;
try {
StorageIO<DataFile> dataAccess = df.getStorageIO();
if (dataAccess != null) {
// && dataAccess.isLocalFile()) {
dataAccess.open();
if ("application/pdf".equalsIgnoreCase(df.getContentType()) || df.isImage() || "application/zipped-shapefile".equalsIgnoreCase(df.getContentType())) {
thumbnailDataAccess = ImageThumbConverter.getImageThumbnailAsInputStream(dataAccess, 48);
}
}
} catch (IOException ioEx) {
return null;
}
if (thumbnailDataAccess != null && thumbnailDataAccess.getInputStream() != null) {
return thumbnailDataAccess.getInputStream();
}
return null;
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class Admin method getDatasetThumbnailMetadata.
/**
* This method is used in API tests, called from UtilIt.java.
*/
@GET
@Path("datasets/thumbnailMetadata/{id}")
public Response getDatasetThumbnailMetadata(@PathParam("id") Long idSupplied) {
Dataset dataset = datasetSvc.find(idSupplied);
if (dataset == null) {
return error(Response.Status.NOT_FOUND, "Could not find dataset based on id supplied: " + idSupplied + ".");
}
JsonObjectBuilder data = Json.createObjectBuilder();
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
data.add("isUseGenericThumbnail", dataset.isUseGenericThumbnail());
data.add("datasetLogoPresent", DatasetUtil.isDatasetLogoPresent(dataset));
if (datasetThumbnail != null) {
data.add("datasetThumbnailBase64image", datasetThumbnail.getBase64image());
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
/**
* @todo Change this from a String to a long.
*/
data.add("dataFileId", dataFile.getId().toString());
}
}
return ok(data);
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class JsonPrinterTest method testGetFileCategories.
@Test
public void testGetFileCategories() {
FileMetadata fmd = new FileMetadata();
DatasetVersion dsVersion = new DatasetVersion();
DataFile dataFile = new DataFile();
List<DataFileTag> dataFileTags = new ArrayList<>();
DataFileTag tag = new DataFileTag();
tag.setTypeByLabel("Survey");
dataFileTags.add(tag);
dataFile.setTags(dataFileTags);
fmd.setDatasetVersion(dsVersion);
fmd.setDataFile(dataFile);
List<DataFileCategory> fileCategories = new ArrayList<>();
DataFileCategory dataFileCategory = new DataFileCategory();
dataFileCategory.setName("Data");
fileCategories.add(dataFileCategory);
fmd.setCategories(fileCategories);
JsonObjectBuilder job = JsonPrinter.json(fmd);
assertNotNull(job);
JsonObject jsonObject = job.build();
System.out.println("json: " + jsonObject);
assertEquals("", jsonObject.getString("description"));
assertEquals("", jsonObject.getString("label"));
assertEquals("Data", jsonObject.getJsonArray("categories").getString(0));
assertEquals("", jsonObject.getJsonObject("dataFile").getString("filename"));
assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("filesize"));
assertEquals("UNKNOWN", jsonObject.getJsonObject("dataFile").getString("originalFormatLabel"));
assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("rootDataFileId"));
assertEquals("Survey", jsonObject.getJsonObject("dataFile").getJsonArray("tabularTags").getString(0));
}
Aggregations