Search in sources :

Example 1 with GuestbookResponse

use of edu.harvard.iq.dataverse.GuestbookResponse 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;
}
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 2 with GuestbookResponse

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

the class Access method datafiles.

/* 
     * API method for downloading zipped bundles of multiple files:
    */
@Path("datafiles/{fileIds}")
@GET
@Produces({ "application/zip" })
public Response datafiles(@PathParam("fileIds") String fileIds, @QueryParam("gbrecs") Boolean gbrecs, @QueryParam("key") String apiTokenParam, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) throws WebApplicationException /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    long setLimit = systemConfig.getZipDownloadLimit();
    if (!(setLimit > 0L)) {
        setLimit = DataFileZipper.DEFAULT_ZIPFILE_LIMIT;
    }
    long zipDownloadSizeLimit = setLimit;
    logger.fine("setting zip download size limit to " + zipDownloadSizeLimit + " bytes.");
    if (fileIds == null || fileIds.equals("")) {
        throw new BadRequestException();
    }
    String apiToken = (apiTokenParam == null || apiTokenParam.equals("")) ? headers.getHeaderString(API_KEY_HEADER) : apiTokenParam;
    // for use in adding gb records if necessary
    User apiTokenUser = findAPITokenUser(apiToken);
    StreamingOutput stream = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            String[] fileIdParams = fileIds.split(",");
            DataFileZipper zipper = null;
            boolean accessToUnrestrictedFileAuthorized = false;
            String fileManifest = "";
            long sizeTotal = 0L;
            if (fileIdParams != null && fileIdParams.length > 0) {
                logger.fine(fileIdParams.length + " tokens;");
                for (int i = 0; i < fileIdParams.length; i++) {
                    logger.fine("token: " + fileIdParams[i]);
                    Long fileId = null;
                    try {
                        fileId = new Long(fileIdParams[i]);
                    } catch (NumberFormatException nfe) {
                        fileId = null;
                    }
                    if (fileId != null) {
                        logger.fine("attempting to look up file id " + fileId);
                        DataFile file = dataFileService.find(fileId);
                        if (file != null) {
                            if ((accessToUnrestrictedFileAuthorized && !file.isRestricted()) || isAccessAuthorized(file, apiToken)) {
                                if (!file.isRestricted()) {
                                    accessToUnrestrictedFileAuthorized = true;
                                }
                                logger.fine("adding datafile (id=" + file.getId() + ") to the download list of the ZippedDownloadInstance.");
                                // downloadInstance.addDataFile(file);
                                if (gbrecs == null && file.isReleased()) {
                                    GuestbookResponse gbr = guestbookResponseService.initAPIGuestbookResponse(file.getOwner(), file, session, apiTokenUser);
                                    guestbookResponseService.save(gbr);
                                }
                                if (zipper == null) {
                                    // This is the first file we can serve - so we now know that we are going to be able
                                    // to produce some output.
                                    zipper = new DataFileZipper(os);
                                    zipper.setFileManifest(fileManifest);
                                    response.setHeader("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
                                    response.setHeader("Content-Type", "application/zip; name=\"dataverse_files.zip\"");
                                }
                                if (sizeTotal + file.getFilesize() < zipDownloadSizeLimit) {
                                    sizeTotal += zipper.addFileToZipStream(file);
                                } else {
                                    String fileName = file.getFileMetadata().getLabel();
                                    String mimeType = file.getContentType();
                                    zipper.addToManifest(fileName + " (" + mimeType + ") " + " skipped because the total size of the download bundle exceeded the limit of " + zipDownloadSizeLimit + " bytes.\r\n");
                                }
                            } else {
                                if (zipper == null) {
                                    fileManifest = fileManifest + file.getFileMetadata().getLabel() + " IS RESTRICTED AND CANNOT BE DOWNLOADED\r\n";
                                } else {
                                    zipper.addToManifest(file.getFileMetadata().getLabel() + " IS RESTRICTED AND CANNOT BE DOWNLOADED\r\n");
                                }
                            }
                        } else {
                            // Or should we just drop it and make a note in the Manifest?
                            throw new NotFoundException();
                        }
                    }
                }
            } else {
                throw new BadRequestException();
            }
            if (zipper == null) {
                // just give them a 403:
                throw new ForbiddenException();
            }
            // This will add the generated File Manifest to the zipped output,
            // then flush and close the stream:
            zipper.finalizeZipStream();
        // os.flush();
        // os.close();
        }
    };
    return Response.ok(stream).build();
}
Also used : DataFileZipper(edu.harvard.iq.dataverse.dataaccess.DataFileZipper) ForbiddenException(javax.ws.rs.ForbiddenException) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NotFoundException(javax.ws.rs.NotFoundException) StreamingOutput(javax.ws.rs.core.StreamingOutput) DataFile(edu.harvard.iq.dataverse.DataFile) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with GuestbookResponse

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

the class Access method datafileBundle.

// @EJB
// TODO:
// versions? -- L.A. 4.0 beta 10
@Path("datafile/bundle/{fileId}")
@GET
@Produces({ "application/zip" })
public BundleDownloadInstance datafileBundle(@PathParam("fileId") Long fileId, @QueryParam("gbrecs") Boolean gbrecs, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    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 (apiToken == null || apiToken.equals("")) {
        apiToken = headers.getHeaderString(API_KEY_HEADER);
    }
    // This will throw a ForbiddenException if access isn't authorized:
    checkAuthorization(df, apiToken);
    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);
        guestbookResponseService.save(gbr);
    }
    DownloadInfo dInfo = new DownloadInfo(df);
    BundleDownloadInstance downloadInstance = new BundleDownloadInstance(dInfo);
    FileMetadata fileMetadata = df.getFileMetadata();
    DatasetVersion datasetVersion = df.getOwner().getLatestVersion();
    downloadInstance.setFileCitationEndNote(datasetService.createCitationXML(datasetVersion, fileMetadata));
    downloadInstance.setFileCitationRIS(datasetService.createCitationRIS(datasetVersion, fileMetadata));
    downloadInstance.setFileCitationBibtex(new BibtexCitation(datasetVersion).toString());
    ByteArrayOutputStream outStream = null;
    outStream = new ByteArrayOutputStream();
    try {
        ddiExportService.exportDataFile(fileId, outStream, null, null);
        downloadInstance.setFileDDIXML(outStream.toString());
    } catch (Exception ex) {
    // if we can't generate the DDI, it's ok;
    // we'll just generate the bundle without it.
    }
    return downloadInstance;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) 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) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) NotFoundException(javax.ws.rs.NotFoundException) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) BibtexCitation(edu.harvard.iq.dataverse.BibtexCitation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) WebApplicationException(javax.ws.rs.WebApplicationException) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with GuestbookResponse

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

the class MoveDatasetCommandTest method setUp.

@Before
public void setUp() {
    auth = makeAuthenticatedUser("Super", "User");
    auth.setSuperuser(true);
    nobody = makeAuthenticatedUser("Nick", "Nobody");
    nobody.setSuperuser(false);
    root = new Dataverse();
    root.setName("root");
    root.setId(1l);
    root.setPublicationDate(new Timestamp(new Date().getTime()));
    childA = new Dataverse();
    childA.setName("childA");
    childA.setId(2l);
    childA.setPublicationDate(new Timestamp(new Date().getTime()));
    childB = new Dataverse();
    childB.setName("childB");
    childB.setId(3l);
    childB.setPublicationDate(new Timestamp(new Date().getTime()));
    grandchildAA = new Dataverse();
    grandchildAA.setName("grandchildAA");
    grandchildAA.setId(4l);
    grandchildAA.setPublicationDate(new Timestamp(new Date().getTime()));
    childDraft = new Dataverse();
    childDraft.setName("childDraft");
    childDraft.setId(5l);
    grandchildBB = new Dataverse();
    grandchildBB.setName("grandchildBB");
    grandchildBB.setId(6l);
    grandchildBB.setPublicationDate(new Timestamp(new Date().getTime()));
    moved = new Dataset();
    moved.setOwner(root);
    moved.setPublicationDate(new Timestamp(new Date().getTime()));
    moved.setId(1l);
    movedResponses = new Dataset();
    movedResponses.setOwner(root);
    movedResponses.setPublicationDate(new Timestamp(new Date().getTime()));
    movedResponses.setId(2l);
    childA.setOwner(root);
    childB.setOwner(root);
    grandchildAA.setOwner(childA);
    grandchildBB.setOwner(childA);
    childDraft.setOwner(childA);
    gbA = new Guestbook();
    gbA.setId(1l);
    gbB = new Guestbook();
    gbB.setId(2l);
    gbC = new Guestbook();
    gbC.setId(3l);
    moved.setGuestbook(gbA);
    movedResponses.setGuestbook(gbA);
    GuestbookResponse gbResp = new GuestbookResponse();
    gbResp.setGuestbook(gbA);
    gbResp.setDataset(movedResponses);
    List<Guestbook> includeA = new ArrayList();
    includeA.add(gbA);
    includeA.add(gbB);
    grandchildAA.setGuestbooks(includeA);
    List<Guestbook> notIncludeA = new ArrayList();
    notIncludeA.add(gbC);
    notIncludeA.add(gbB);
    childB.setGuestbooks(notIncludeA);
    List<Guestbook> none = new ArrayList();
    root.setGuestbooks(none);
    grandchildBB.setGuestbooks(none);
    grandchildBB.setGuestbookRoot(false);
    childA.setGuestbooks(includeA);
    testEngine = new TestDataverseEngine(new TestCommandContext() {

        @Override
        public DataverseServiceBean dataverses() {
            return new DataverseServiceBean() {

                @Override
                public Dataverse save(Dataverse dataverse) {
                    // no-op. The superclass accesses databases which we don't have.
                    return dataverse;
                }
            };
        }

        @Override
        public GuestbookServiceBean guestbooks() {
            return new GuestbookServiceBean() {

                @Override
                public Long findCountResponsesForGivenDataset(Long guestbookId, Long datasetId) {
                    // We're going to fake a response for a dataset with responses
                    if (datasetId == 1) {
                        return new Long(0);
                    } else {
                        return new Long(1);
                    }
                }
            };
        }

        @Override
        public IndexServiceBean index() {
            return new IndexServiceBean() {

                @Override
                public Future<String> indexDataset(Dataset dataset, boolean doNormalSolrDocCleanUp) {
                    return null;
                }
            };
        }

        @Override
        public EntityManager em() {
            return new MockEntityManager() {
            };
        }
    });
}
Also used : TestCommandContext(edu.harvard.iq.dataverse.engine.TestCommandContext) GuestbookServiceBean(edu.harvard.iq.dataverse.GuestbookServiceBean) GuestbookResponse(edu.harvard.iq.dataverse.GuestbookResponse) Dataset(edu.harvard.iq.dataverse.Dataset) Guestbook(edu.harvard.iq.dataverse.Guestbook) ArrayList(java.util.ArrayList) Dataverse(edu.harvard.iq.dataverse.Dataverse) Timestamp(java.sql.Timestamp) DataverseServiceBean(edu.harvard.iq.dataverse.DataverseServiceBean) Date(java.util.Date) TestDataverseEngine(edu.harvard.iq.dataverse.engine.TestDataverseEngine) IndexServiceBean(edu.harvard.iq.dataverse.search.IndexServiceBean) Before(org.junit.Before)

Aggregations

GuestbookResponse (edu.harvard.iq.dataverse.GuestbookResponse)4 DataFile (edu.harvard.iq.dataverse.DataFile)3 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3 GuestUser (edu.harvard.iq.dataverse.authorization.users.GuestUser)3 PrivateUrlUser (edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser)3 User (edu.harvard.iq.dataverse.authorization.users.User)3 GET (javax.ws.rs.GET)3 NotFoundException (javax.ws.rs.NotFoundException)3 Path (javax.ws.rs.Path)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BadRequestException (javax.ws.rs.BadRequestException)2 ForbiddenException (javax.ws.rs.ForbiddenException)2 Produces (javax.ws.rs.Produces)2 BibtexCitation (edu.harvard.iq.dataverse.BibtexCitation)1 Dataset (edu.harvard.iq.dataverse.Dataset)1 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 DataverseServiceBean (edu.harvard.iq.dataverse.DataverseServiceBean)1 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)1 Guestbook (edu.harvard.iq.dataverse.Guestbook)1