use of edu.harvard.iq.dataverse.datasetutility.DataFileTagException in project dataverse by IQSS.
the class Datasets method addFileToDataset.
/**
* Add a File to an existing Dataset
*
* @param idSupplied
* @param jsonData
* @param fileInputStream
* @param contentDispositionHeader
* @param formDataBodyPart
* @return
*/
@POST
@Path("{id}/add")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addFileToDataset(@PathParam("id") String idSupplied, @FormDataParam("jsonData") String jsonData, @FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader, @FormDataParam("file") final FormDataBodyPart formDataBodyPart) {
// -------------------------------------
// (1) Get the user from the API key
// -------------------------------------
User authUser;
try {
authUser = findUserOrDie();
} catch (WrappedResponse ex) {
return error(Response.Status.FORBIDDEN, ResourceBundle.getBundle("Bundle").getString("file.addreplace.error.auth"));
}
if (DataCaptureModuleUtil.rsyncSupportEnabled(settingsSvc.getValueForKey(SettingsServiceBean.Key.UploadMethods))) {
return error(Response.Status.METHOD_NOT_ALLOWED, SettingsServiceBean.Key.UploadMethods + " contains " + SystemConfig.FileUploadMethods.RSYNC + ". Please use rsync file upload.");
}
// -------------------------------------
// (2) Get the Dataset Id
//
// -------------------------------------
Dataset dataset;
Long datasetId;
try {
dataset = findDatasetOrDie(idSupplied);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
// -------------------------------------
// (3) Get the file name and content type
// -------------------------------------
String newFilename = contentDispositionHeader.getFileName();
String newFileContentType = formDataBodyPart.getMediaType().toString();
// (2a) Load up optional params via JSON
// ---------------------------------------
OptionalFileParams optionalFileParams = null;
msgt("(api) jsonData: " + jsonData);
try {
optionalFileParams = new OptionalFileParams(jsonData);
} catch (DataFileTagException ex) {
return error(Response.Status.BAD_REQUEST, ex.getMessage());
}
// -------------------
// (3) Create the AddReplaceFileHelper object
// -------------------
msg("ADD!");
DataverseRequest dvRequest2 = createDataverseRequest(authUser);
AddReplaceFileHelper addFileHelper = new AddReplaceFileHelper(dvRequest2, ingestService, datasetService, fileService, permissionSvc, commandEngine, systemConfig);
// -------------------
// (4) Run "runAddFileByDatasetId"
// -------------------
addFileHelper.runAddFileByDataset(dataset, newFilename, newFileContentType, fileInputStream, optionalFileParams);
if (addFileHelper.hasError()) {
return error(addFileHelper.getHttpErrorCode(), addFileHelper.getErrorMessagesAsString("\n"));
} else {
String successMsg = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.add");
try {
// msgt("as String: " + addFileHelper.getSuccessResult());
/**
* @todo We need a consistent, sane way to communicate a human
* readable message to an API client suitable for human
* consumption. Imagine if the UI were built in Angular or React
* and we want to return a message from the API as-is to the
* user. Human readable.
*/
logger.fine("successMsg: " + successMsg);
return ok(addFileHelper.getSuccessResultAsJsonObjectBuilder());
// "Look at that! You added a file! (hey hey, it may have worked)");
} catch (NoFilesException ex) {
Logger.getLogger(Files.class.getName()).log(Level.SEVERE, null, ex);
return error(Response.Status.BAD_REQUEST, "NoFileException! Serious Error! See administrator!");
}
}
}
use of edu.harvard.iq.dataverse.datasetutility.DataFileTagException in project dataverse by IQSS.
the class Files method replaceFileInDataset.
/**
* Replace an Existing File
*
* @param datasetId
* @param testFileInputStream
* @param contentDispositionHeader
* @param formDataBodyPart
* @return
*/
@POST
@Path("{id}/replace")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response replaceFileInDataset(@PathParam("id") Long fileToReplaceId, @FormDataParam("jsonData") String jsonData, @FormDataParam("file") InputStream testFileInputStream, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader, @FormDataParam("file") final FormDataBodyPart formDataBodyPart) {
// -------------------------------------
// (1) Get the user from the API key
// -------------------------------------
User authUser;
try {
authUser = this.findUserOrDie();
} catch (AbstractApiBean.WrappedResponse ex) {
return error(Response.Status.FORBIDDEN, ResourceBundle.getBundle("Bundle").getString("file.addreplace.error.auth"));
}
// -------------------------------------
// (2) Check/Parse the JSON (if uploaded)
// -------------------------------------
Boolean forceReplace = false;
OptionalFileParams optionalFileParams = null;
if (jsonData != null) {
JsonObject jsonObj = null;
try {
jsonObj = new Gson().fromJson(jsonData, JsonObject.class);
// -------------------------------------
if ((jsonObj.has("forceReplace")) && (!jsonObj.get("forceReplace").isJsonNull())) {
forceReplace = jsonObj.get("forceReplace").getAsBoolean();
if (forceReplace == null) {
forceReplace = false;
}
}
try {
// (2b) Load up optional params via JSON
// - Will skip extra attributes which includes fileToReplaceId and forceReplace
// ---------------------------------------
optionalFileParams = new OptionalFileParams(jsonData);
} catch (DataFileTagException ex) {
return error(Response.Status.BAD_REQUEST, ex.getMessage());
}
} catch (ClassCastException ex) {
logger.info("Exception parsing string '" + jsonData + "': " + ex);
}
}
// -------------------------------------
// (3) Get the file name and content type
// -------------------------------------
String newFilename = contentDispositionHeader.getFileName();
String newFileContentType = formDataBodyPart.getMediaType().toString();
// -------------------
// (4) Create the AddReplaceFileHelper object
// -------------------
msg("REPLACE!");
DataverseRequest dvRequest2 = createDataverseRequest(authUser);
AddReplaceFileHelper addFileHelper = new AddReplaceFileHelper(dvRequest2, this.ingestService, this.datasetService, this.fileService, this.permissionSvc, this.commandEngine, this.systemConfig);
if (forceReplace) {
addFileHelper.runForceReplaceFile(fileToReplaceId, newFilename, newFileContentType, testFileInputStream, optionalFileParams);
} else {
addFileHelper.runReplaceFile(fileToReplaceId, newFilename, newFileContentType, testFileInputStream, optionalFileParams);
}
msg("we're back.....");
if (addFileHelper.hasError()) {
msg("yes, has error");
return error(addFileHelper.getHttpErrorCode(), addFileHelper.getErrorMessagesAsString("\n"));
} else {
msg("no error");
String successMsg = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.replace");
try {
msgt("as String: " + addFileHelper.getSuccessResult());
/**
* @todo We need a consistent, sane way to communicate a human
* readable message to an API client suitable for human
* consumption. Imagine if the UI were built in Angular or React
* and we want to return a message from the API as-is to the
* user. Human readable.
*/
logger.fine("successMsg: " + successMsg);
return ok(addFileHelper.getSuccessResultAsJsonObjectBuilder());
// return okResponseGsonObject(successMsg,
// addFileHelper.getSuccessResultAsGsonObject());
// "Look at that! You added a file! (hey hey, it may have worked)");
} catch (NoFilesException ex) {
Logger.getLogger(Files.class.getName()).log(Level.SEVERE, null, ex);
return error(Response.Status.BAD_REQUEST, "NoFileException! Serious Error! See administrator!");
}
}
}
Aggregations