Search in sources :

Example 1 with AuxiliaryFile

use of edu.harvard.iq.dataverse.AuxiliaryFile in project dataverse by IQSS.

the class Access method saveAuxiliaryFileWithVersion.

/* 
        removing: 
    private String getWebappImageResource(String imageName) {
        String imageFilePath = null;
        String persistenceFilePath = null;
        java.net.URL persistenceFileUrl = Thread.currentThread().getContextClassLoader().getResource("META-INF/persistence.xml");
        
        if (persistenceFileUrl != null) {
            persistenceFilePath = persistenceFileUrl.getDataFile();
            if (persistenceFilePath != null) {
                persistenceFilePath = persistenceFilePath.replaceFirst("/[^/]*$", "/");
                imageFilePath = persistenceFilePath + "../../../resources/images/" + imageName;
                return imageFilePath; 
            }
            logger.warning("Null file path representation of the location of persistence.xml in the webapp root directory!"); 
        } else {
            logger.warning("Could not find the location of persistence.xml in the webapp root directory!");
        }

        return null;
    }
    */
/**
 * @param fileId
 * @param formatTag
 * @param formatVersion
 * @param origin
 * @param isPublic
 * @param type
 * @param fileInputStream
 * @param contentDispositionHeader
 * @param formDataBodyPart
 * @return
 */
@Path("datafile/{fileId}/auxiliary/{formatTag}/{formatVersion}")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response saveAuxiliaryFileWithVersion(@PathParam("fileId") Long fileId, @PathParam("formatTag") String formatTag, @PathParam("formatVersion") String formatVersion, @FormDataParam("origin") String origin, @FormDataParam("isPublic") boolean isPublic, @FormDataParam("type") String type, @FormDataParam("file") final FormDataBodyPart formDataBodyPart, @FormDataParam("file") InputStream fileInputStream) {
    AuthenticatedUser authenticatedUser;
    try {
        authenticatedUser = findAuthenticatedUserOrDie();
    } catch (WrappedResponse ex) {
        return error(FORBIDDEN, "Authorized users only.");
    }
    DataFile dataFile = dataFileService.find(fileId);
    if (dataFile == null) {
        return error(BAD_REQUEST, "File not found based on id " + fileId + ".");
    }
    if (!permissionService.userOn(authenticatedUser, dataFile.getOwner()).has(Permission.EditDataset)) {
        return error(FORBIDDEN, "User not authorized to edit the dataset.");
    }
    MediaType mediaType = null;
    if (formDataBodyPart != null) {
        mediaType = formDataBodyPart.getMediaType();
    }
    AuxiliaryFile saved = auxiliaryFileService.processAuxiliaryFile(fileInputStream, dataFile, formatTag, formatVersion, origin, isPublic, type, mediaType);
    if (saved != null) {
        return ok(json(saved));
    } else {
        return error(BAD_REQUEST, "Error saving Auxiliary file.");
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) AuxiliaryFile(edu.harvard.iq.dataverse.AuxiliaryFile) MediaType(javax.ws.rs.core.MediaType) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 2 with AuxiliaryFile

use of edu.harvard.iq.dataverse.AuxiliaryFile in project dataverse by IQSS.

the class Access method listAuxiliaryFiles.

private Response listAuxiliaryFiles(String fileId, String origin, String apiToken, UriInfo uriInfo, HttpHeaders headers, HttpServletResponse response) {
    DataFile df = findDataFileOrDieWrapper(fileId);
    if (apiToken == null || apiToken.equals("")) {
        apiToken = headers.getHeaderString(API_KEY_HEADER);
    }
    List<AuxiliaryFile> auxFileList = auxiliaryFileService.findAuxiliaryFiles(df, origin);
    if (auxFileList == null || auxFileList.isEmpty()) {
        throw new NotFoundException("No Auxiliary files exist for datafile " + fileId + (origin == null ? "" : " and the specified origin"));
    }
    boolean isAccessAllowed = isAccessAuthorized(df, apiToken);
    JsonArrayBuilder jab = Json.createArrayBuilder();
    auxFileList.forEach(auxFile -> {
        if (isAccessAllowed || auxFile.getIsPublic()) {
            NullSafeJsonBuilder job = NullSafeJsonBuilder.jsonObjectBuilder();
            job.add("formatTag", auxFile.getFormatTag());
            job.add("formatVersion", auxFile.getFormatVersion());
            job.add("fileSize", auxFile.getFileSize());
            job.add("contentType", auxFile.getContentType());
            job.add("isPublic", auxFile.getIsPublic());
            job.add("type", auxFile.getType());
            jab.add(job);
        }
    });
    return ok(jab);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) AuxiliaryFile(edu.harvard.iq.dataverse.AuxiliaryFile) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) JsonArrayBuilder(javax.json.JsonArrayBuilder) NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder)

Example 3 with AuxiliaryFile

use of edu.harvard.iq.dataverse.AuxiliaryFile in project dataverse by IQSS.

the class Access method downloadAuxiliaryFile.

/*
     * GET method for retrieving various auxiliary files associated with 
     * a tabular datafile.
     *
     */
@Path("datafile/{fileId}/auxiliary/{formatTag}/{formatVersion}")
@GET
public DownloadInstance downloadAuxiliaryFile(@PathParam("fileId") String fileId, @PathParam("formatTag") String formatTag, @PathParam("formatVersion") String formatVersion, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) throws ServiceUnavailableException {
    DataFile df = findDataFileOrDieWrapper(fileId);
    if (apiToken == null || apiToken.equals("")) {
        apiToken = headers.getHeaderString(API_KEY_HEADER);
    }
    DownloadInfo dInfo = new DownloadInfo(df);
    boolean publiclyAvailable = false;
    DownloadInstance downloadInstance;
    AuxiliaryFile auxFile = null;
    /* 
          The special case for "preprocessed" metadata should not be here at all. 
          Access to the format should be handled by the /api/access/datafile/{id}?format=prep
          form exclusively (this is what Data Explorer has been using all along).
          We may have advertised /api/access/datafile/{id}/metadata/preprocessed
          in the past - but it has been broken since 5.3 anyway, since the /{formatVersion}
          element was added to the @Path. 
          Now that the api method has been renamed /api/access/datafile/{id}/auxiliary/..., 
          nobody should be using it to access the "preprocessed" format. 
          Leaving the special case below commented-out, for now. - L.A.
        
        // formatTag=preprocessed is handled as a special case. 
        // This is (as of now) the only aux. tabular metadata format that Dataverse
        // can generate (and cache) itself. (All the other formats served have 
        // to be deposited first, by the @POST version of this API).
        
        if ("preprocessed".equals(formatTag)) {
            dInfo.addServiceAvailable(new OptionalAccessService("preprocessed", "application/json", "format=prep", "Preprocessed data in JSON"));
            downloadInstance = new DownloadInstance(dInfo);
            if (downloadInstance.checkIfServiceSupportedAndSetConverter("format", "prep")) {
                logger.fine("Preprocessed data for tabular file "+fileId);
            }
        } else { */
    // All other (deposited) formats:
    auxFile = auxiliaryFileService.lookupAuxiliaryFile(df, formatTag, formatVersion);
    if (auxFile == null) {
        throw new NotFoundException("Auxiliary metadata format " + formatTag + " is not available for datafile " + fileId);
    }
    // Don't consider aux file public unless data file is published.
    if (auxFile.getIsPublic() && df.getPublicationDate() != null) {
        publiclyAvailable = true;
    }
    downloadInstance = new DownloadInstance(dInfo);
    downloadInstance.setAuxiliaryFile(auxFile);
    // if access is denied:
    if (!publiclyAvailable) {
        checkAuthorization(df, apiToken);
    }
    return downloadInstance;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) AuxiliaryFile(edu.harvard.iq.dataverse.AuxiliaryFile) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

AuxiliaryFile (edu.harvard.iq.dataverse.AuxiliaryFile)3 DataFile (edu.harvard.iq.dataverse.DataFile)3 FileNotFoundException (java.io.FileNotFoundException)2 NotFoundException (javax.ws.rs.NotFoundException)2 Path (javax.ws.rs.Path)2 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 NullSafeJsonBuilder (edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 MediaType (javax.ws.rs.core.MediaType)1