use of edu.harvard.iq.dataverse.dataaccess.OptionalAccessService in project dataverse by IQSS.
the class DownloadInstance method getServiceFormatType.
public String getServiceFormatType(String serviceArg, String serviceArgValue) {
if (downloadInfo == null || serviceArg == null) {
return null;
}
List<OptionalAccessService> servicesAvailable = downloadInfo.getServicesAvailable();
for (OptionalAccessService dataService : servicesAvailable) {
if (dataService != null) {
// Special case for the subsetting parameter (variables=<LIST>):
if (serviceArg.equals("variables")) {
if ("subset".equals(dataService.getServiceName())) {
conversionParam = "subset";
conversionParamValue = serviceArgValue;
return dataService.getMimeType();
}
} else if (serviceArg.equals("imageThumb")) {
return "image/png";
} else {
String argValuePair = serviceArg + "=" + serviceArgValue;
if (argValuePair.equals(dataService.getServiceArguments())) {
conversionParam = serviceArg;
conversionParamValue = serviceArgValue;
return dataService.getMimeType();
}
}
}
}
return null;
}
use of edu.harvard.iq.dataverse.dataaccess.OptionalAccessService 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.dataaccess.OptionalAccessService in project dataverse by IQSS.
the class DownloadInstance method isDownloadServiceSupported.
// Move this method into the DownloadInfo instead -- ?
public Boolean isDownloadServiceSupported(String serviceArg, String serviceArgValue) {
if (downloadInfo == null || serviceArg == null) {
return false;
}
List<OptionalAccessService> servicesAvailable = downloadInfo.getServicesAvailable();
for (OptionalAccessService dataService : servicesAvailable) {
if (dataService != null) {
// Special case for the subsetting parameter (variables=<LIST>):
if (serviceArg.equals("variables")) {
if ("subset".equals(dataService.getServiceName())) {
conversionParam = "subset";
conversionParamValue = serviceArgValue;
return true;
}
} else if ("imageThumb".equals(serviceArg)) {
if ("true".equals(serviceArgValue)) {
this.conversionParam = serviceArg;
this.conversionParamValue = "";
} else {
this.conversionParam = serviceArg;
this.conversionParamValue = serviceArgValue;
}
return true;
}
String argValuePair = serviceArg + "=" + serviceArgValue;
if (argValuePair.startsWith(dataService.getServiceArguments())) {
conversionParam = serviceArg;
conversionParamValue = serviceArgValue;
return true;
}
// }
}
}
return false;
}
use of edu.harvard.iq.dataverse.dataaccess.OptionalAccessService in project dataverse by IQSS.
the class Access method tabularDatafileMetadataPreprocessed.
/*
* "Preprocessed data" metadata format:
* (this was previously provided as a "format conversion" option of the
* file download form of the access API call)
*/
@Path("datafile/{fileId}/metadata/preprocessed")
@GET
@Produces({ "text/xml" })
public DownloadInstance tabularDatafileMetadataPreprocessed(@PathParam("fileId") Long fileId, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) throws ServiceUnavailableException {
DataFile df = dataFileService.find(fileId);
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);
DownloadInfo dInfo = new DownloadInfo(df);
if (df.isTabularData()) {
dInfo.addServiceAvailable(new OptionalAccessService("preprocessed", "application/json", "format=prep", "Preprocessed data in JSON"));
} else {
throw new ServiceUnavailableException("Preprocessed Content Metadata requested on a non-tabular data file.");
}
DownloadInstance downloadInstance = new DownloadInstance(dInfo);
if (downloadInstance.isDownloadServiceSupported("format", "prep")) {
logger.fine("Preprocessed data for tabular file " + fileId);
}
response.setHeader("Access-Control-Allow-Origin", "*");
return downloadInstance;
}
Aggregations