Search in sources :

Example 31 with DataFile

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));
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Test(org.junit.Test)

Example 32 with DataFile

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;
}
Also used : DataverseTheme(edu.harvard.iq.dataverse.DataverseTheme) Properties(java.util.Properties) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File)

Example 33 with DataFile

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=&lt;LIST&gt;", "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;
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) GuestbookResponse(edu.harvard.iq.dataverse.GuestbookResponse) NotFoundException(javax.ws.rs.NotFoundException) OptionalAccessService(edu.harvard.iq.dataverse.dataaccess.OptionalAccessService) DataVariable(edu.harvard.iq.dataverse.datavariable.DataVariable) DataFile(edu.harvard.iq.dataverse.DataFile) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 34 with DataFile

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;
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) NullSafeJsonBuilder.jsonObjectBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder) Arrays(java.util.Arrays) DatasetDistributor(edu.harvard.iq.dataverse.DatasetDistributor) DataFileTag(edu.harvard.iq.dataverse.DataFileTag) Date(java.util.Date) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) User(edu.harvard.iq.dataverse.authorization.users.User) Permission(edu.harvard.iq.dataverse.authorization.Permission) Map(java.util.Map) DataverseFacet(edu.harvard.iq.dataverse.DataverseFacet) ExplicitGroup(edu.harvard.iq.dataverse.authorization.groups.impl.explicit.ExplicitGroup) Collector(java.util.stream.Collector) DatasetField(edu.harvard.iq.dataverse.DatasetField) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) EnumSet(java.util.EnumSet) JsonObject(javax.json.JsonObject) DatasetFieldValue(edu.harvard.iq.dataverse.DatasetFieldValue) Collection(java.util.Collection) DatasetFieldWalker(edu.harvard.iq.dataverse.util.DatasetFieldWalker) Set(java.util.Set) DataFile(edu.harvard.iq.dataverse.DataFile) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) MetadataBlock(edu.harvard.iq.dataverse.MetadataBlock) BinaryOperator(java.util.function.BinaryOperator) DataverseTheme(edu.harvard.iq.dataverse.DataverseTheme) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) List(java.util.List) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonObjectBuilder(javax.json.JsonObjectBuilder) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) StringUtil(edu.harvard.iq.dataverse.util.StringUtil) JsonArrayBuilder(javax.json.JsonArrayBuilder) Util(edu.harvard.iq.dataverse.api.Util) AuthenticationProviderRow(edu.harvard.iq.dataverse.authorization.providers.AuthenticationProviderRow) Deque(java.util.Deque) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) IpAddress(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress) ArrayList(java.util.ArrayList) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue) DatasetFieldCompoundValue(edu.harvard.iq.dataverse.DatasetFieldCompoundValue) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) IpAddressRange(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange) Workflow(edu.harvard.iq.dataverse.workflow.Workflow) BiConsumer(java.util.function.BiConsumer) Json(javax.json.Json) LinkedList(java.util.LinkedList) Dataverse(edu.harvard.iq.dataverse.Dataverse) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) BuiltinUser(edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) Dataset(edu.harvard.iq.dataverse.Dataset) JsonArray(javax.json.JsonArray) TermsOfUseAndAccess(edu.harvard.iq.dataverse.TermsOfUseAndAccess) Collections(edu.emory.mathcs.backport.java.util.Collections) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) Collectors.toList(java.util.stream.Collectors.toList) WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) ShibGroup(edu.harvard.iq.dataverse.authorization.groups.impl.shib.ShibGroup) RoleAssigneeDisplayInfo(edu.harvard.iq.dataverse.authorization.RoleAssigneeDisplayInfo) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 35 with DataFile

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());
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DvObject(edu.harvard.iq.dataverse.DvObject) Dataset(edu.harvard.iq.dataverse.Dataset) ArrayList(java.util.ArrayList) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)111 Dataset (edu.harvard.iq.dataverse.Dataset)39 IOException (java.io.IOException)39 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)30 ArrayList (java.util.ArrayList)25 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)20 File (java.io.File)20 FileNotFoundException (java.io.FileNotFoundException)18 Path (javax.ws.rs.Path)18 Dataverse (edu.harvard.iq.dataverse.Dataverse)17 FileInputStream (java.io.FileInputStream)16 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 Date (java.util.Date)13 GET (javax.ws.rs.GET)13 Test (org.junit.Test)13 Timestamp (java.sql.Timestamp)11 InputStream (java.io.InputStream)10 DataVariable (edu.harvard.iq.dataverse.datavariable.DataVariable)8 FileOutputStream (java.io.FileOutputStream)8