use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class FileUtilTest method testIsThumbnailSupported.
// isThumbnailSuppported() has been moved from DataFileService to FileUtil:
/**
* Expect that {@code null}, a DataFile without content type and a DataFile
* with bogus content type are not files that thumbnails can be created for.
* @throws Exception when the test is in error.
*/
@Test
public void testIsThumbnailSupported() throws Exception {
// null file:
assertFalse(FileUtil.isThumbnailSupported(null));
// file with no content type:
DataFile filewNoContentType = new DataFile("");
filewNoContentType.setStorageIdentifier("");
assertFalse(FileUtil.isThumbnailSupported(filewNoContentType));
DataFile filewBogusContentType = new DataFile("");
filewBogusContentType.setStorageIdentifier("");
assertFalse(FileUtil.isThumbnailSupported(filewBogusContentType));
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class Access method getLogo.
// helper methods:
// What the method below does - going through all the files in the version -
// is too expensive! Instead we are now selecting an available thumbnail and
// giving the dataset card a direct link to that file thumbnail. -- L.A., 4.2.2
/*
private StorageIO getThumbnailForDatasetVersion(DatasetVersion datasetVersion) {
logger.info("entering getThumbnailForDatasetVersion()");
StorageIO thumbnailDataAccess = null;
if (datasetVersion != null) {
List<FileMetadata> fileMetadatas = datasetVersion.getFileMetadatas();
for (FileMetadata fileMetadata : fileMetadatas) {
DataFile dataFile = fileMetadata.getDataFile();
logger.info("looking at file "+fileMetadata.getLabel()+" , file type "+dataFile.getContentType());
if (dataFile != null && dataFile.isImage()) {
try {
StorageIO dataAccess = dataFile.getStorageIO();
if (dataAccess != null && dataAccess.isLocalFile()) {
dataAccess.open();
thumbnailDataAccess = ImageThumbConverter.getImageThumb((FileAccessIO) dataAccess, 48);
}
} catch (IOException ioEx) {
thumbnailDataAccess = null;
}
}
if (thumbnailDataAccess != null) {
logger.info("successfully generated thumbnail, returning.");
break;
}
}
}
return thumbnailDataAccess;
}
*/
// TODO:
// put this method into the dataverseservice; use it there
// -- L.A. 4.0 beta14
private File getLogo(Dataverse dataverse) {
if (dataverse.getId() == null) {
return null;
}
DataverseTheme theme = dataverse.getDataverseTheme();
if (theme != null && theme.getLogo() != null && !theme.getLogo().equals("")) {
Properties p = System.getProperties();
String domainRoot = p.getProperty("com.sun.aas.instanceRoot");
if (domainRoot != null && !"".equals(domainRoot)) {
return new File(domainRoot + File.separator + "docroot" + File.separator + "logos" + File.separator + dataverse.getLogoOwnerId() + File.separator + theme.getLogo());
}
}
return null;
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class Access method datafile.
@Path("datafile/{fileId}")
@GET
public // @Produces({ "application/xml" })
DownloadInstance datafile(@PathParam("fileId") Long fileId, @QueryParam("gbrecs") Boolean gbrecs, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) {
DataFile df = dataFileService.find(fileId);
GuestbookResponse gbr = null;
if (df == null) {
logger.warning("Access: datafile service could not locate a DataFile object for id " + fileId + "!");
throw new NotFoundException();
}
if (df.isHarvested()) {
throw new NotFoundException();
// (nobody should ever be using this API on a harvested DataFile)!
}
if (apiToken == null || apiToken.equals("")) {
apiToken = headers.getHeaderString(API_KEY_HEADER);
}
if (gbrecs == null && df.isReleased()) {
// Write Guestbook record if not done previously and file is released
User apiTokenUser = findAPITokenUser(apiToken);
gbr = guestbookResponseService.initAPIGuestbookResponse(df.getOwner(), df, session, apiTokenUser);
}
// This will throw a ForbiddenException if access isn't authorized:
checkAuthorization(df, apiToken);
DownloadInfo dInfo = new DownloadInfo(df);
logger.fine("checking if thumbnails are supported on this file.");
if (FileUtil.isThumbnailSupported(df)) {
dInfo.addServiceAvailable(new OptionalAccessService("thumbnail", "image/png", "imageThumb=true", "Image Thumbnail (64x64)"));
}
if (df.isTabularData()) {
String originalMimeType = df.getDataTable().getOriginalFileFormat();
dInfo.addServiceAvailable(new OptionalAccessService("original", originalMimeType, "format=original", "Saved original (" + originalMimeType + ")"));
dInfo.addServiceAvailable(new OptionalAccessService("R", "application/x-rlang-transport", "format=RData", "Data in R format"));
dInfo.addServiceAvailable(new OptionalAccessService("preprocessed", "application/json", "format=prep", "Preprocessed data in JSON"));
dInfo.addServiceAvailable(new OptionalAccessService("subset", "text/tab-separated-values", "variables=<LIST>", "Column-wise Subsetting"));
}
DownloadInstance downloadInstance = new DownloadInstance(dInfo);
if (gbr != null) {
downloadInstance.setGbr(gbr);
downloadInstance.setDataverseRequestService(dvRequestService);
downloadInstance.setCommand(engineSvc);
}
for (String key : uriInfo.getQueryParameters().keySet()) {
String value = uriInfo.getQueryParameters().getFirst(key);
if (downloadInstance.isDownloadServiceSupported(key, value)) {
logger.fine("is download service supported? key=" + key + ", value=" + value);
if (downloadInstance.getConversionParam().equals("subset")) {
String subsetParam = downloadInstance.getConversionParamValue();
String[] variableIdParams = subsetParam.split(",");
if (variableIdParams != null && variableIdParams.length > 0) {
logger.fine(variableIdParams.length + " tokens;");
for (int i = 0; i < variableIdParams.length; i++) {
logger.fine("token: " + variableIdParams[i]);
String token = variableIdParams[i].replaceFirst("^v", "");
Long variableId = null;
try {
variableId = new Long(token);
} catch (NumberFormatException nfe) {
variableId = null;
}
if (variableId != null) {
logger.fine("attempting to look up variable id " + variableId);
if (variableService != null) {
DataVariable variable = variableService.find(variableId);
if (variable != null) {
if (downloadInstance.getExtraArguments() == null) {
downloadInstance.setExtraArguments(new ArrayList<Object>());
}
logger.fine("putting variable id " + variable.getId() + " on the parameters list of the download instance.");
downloadInstance.getExtraArguments().add(variable);
// if (!variable.getDataTable().getDataFile().getId().equals(sf.getId())) {
// variableList.add(variable);
// }
}
} else {
logger.fine("variable service is null.");
}
}
}
}
}
logger.fine("downloadInstance: " + downloadInstance.getConversionParam() + "," + downloadInstance.getConversionParamValue());
break;
} else {
// Service unknown/not supported/bad arguments, etc.:
// TODO: throw new ServiceUnavailableException();
}
}
/*
* Provide "Access-Control-Allow-Origin" header:
*/
response.setHeader("Access-Control-Allow-Origin", "*");
// return retValue;
return downloadInstance;
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class JsonPrinter method jsonDataFileList.
public static JsonObjectBuilder jsonDataFileList(List<DataFile> dataFiles) {
if (dataFiles == null) {
throw new NullPointerException("dataFiles cannot be null");
}
JsonObjectBuilder bld = jsonObjectBuilder();
List<FileMetadata> dataFileList = dataFiles.stream().map(x -> x.getFileMetadata()).collect(Collectors.toList());
bld.add("files", jsonFileMetadatas(dataFileList));
return bld;
}
use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.
the class SolrIndexServiceBean method indexPermissionsOnSelfAndChildren.
/**
* We use the database to determine direct children since there is no
* inheritance
*/
public IndexResponse indexPermissionsOnSelfAndChildren(DvObject definitionPoint) {
List<DvObject> dvObjectsToReindexPermissionsFor = new ArrayList<>();
List<DataFile> filesToReindexAsBatch = new ArrayList<>();
// so don't create a Solr "permission" doc either.
if (definitionPoint.isInstanceofDataverse()) {
Dataverse selfDataverse = (Dataverse) definitionPoint;
if (!selfDataverse.equals(dataverseService.findRootDataverse())) {
dvObjectsToReindexPermissionsFor.add(definitionPoint);
}
List<Dataset> directChildDatasetsOfDvDefPoint = datasetService.findByOwnerId(selfDataverse.getId());
for (Dataset dataset : directChildDatasetsOfDvDefPoint) {
dvObjectsToReindexPermissionsFor.add(dataset);
for (DataFile datafile : filesToReIndexPermissionsFor(dataset)) {
filesToReindexAsBatch.add(datafile);
}
}
} else if (definitionPoint.isInstanceofDataset()) {
// index the dataset itself
indexPermissionsForOneDvObject(definitionPoint);
dvObjectsToReindexPermissionsFor.add(definitionPoint);
// index files
Dataset dataset = (Dataset) definitionPoint;
for (DataFile datafile : filesToReIndexPermissionsFor(dataset)) {
filesToReindexAsBatch.add(datafile);
}
} else {
dvObjectsToReindexPermissionsFor.add(definitionPoint);
}
/**
* @todo Error handling? What to do with response?
*
* @todo Should update timestamps, probably, even thought these are
* files, see https://github.com/IQSS/dataverse/issues/2421
*/
String response = reindexFilesInBatches(filesToReindexAsBatch);
List<String> updatePermissionTimeSuccessStatus = new ArrayList<>();
for (DvObject dvObject : dvObjectsToReindexPermissionsFor) {
/**
* @todo do something with this response
*/
IndexResponse indexResponse = indexPermissionsForOneDvObject(dvObject);
DvObject managedDefinitionPoint = dvObjectService.updatePermissionIndexTime(definitionPoint);
boolean updatePermissionTimeSuccessful = false;
if (managedDefinitionPoint != null) {
updatePermissionTimeSuccessful = true;
}
updatePermissionTimeSuccessStatus.add(dvObject + ":" + updatePermissionTimeSuccessful);
}
return new IndexResponse("Number of dvObject permissions indexed for " + definitionPoint + " (updatePermissionTimeSuccessful:" + updatePermissionTimeSuccessStatus + "): " + dvObjectsToReindexPermissionsFor.size());
}
Aggregations