use of org.apache.wicket.util.file.File in project midpoint by Evolveum.
the class ImportReportPopupPanel method importConfirmPerformed.
private void importConfirmPerformed(AjaxRequestTarget target, Model<String> nameModel, Model<String> fileStringImport) {
String dataName;
if (nameModel == null || StringUtils.isEmpty(nameModel.getObject())) {
dataName = getModelObject().getName().getOrig() + "-IMPORT " + getDataTime();
} else {
dataName = nameModel.getObject();
}
// Create new file
MidPointApplication application = getPageBase().getMidpointApplication();
WebApplicationConfiguration config = application.getWebApplicationConfiguration();
String midpointHome = System.getProperty(MidpointConfiguration.MIDPOINT_HOME_PROPERTY);
File importDir = new File(midpointHome, "import");
if (!importDir.exists() || !importDir.isDirectory()) {
if (!importDir.mkdir()) {
LOGGER.error("Couldn't create import dir {}", importDir);
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportDir", importDir).getString());
target.add(feedback);
return;
}
}
FileUpload uploadedFile = getUploadedFile();
if (uploadedFile == null && StringUtils.isEmpty(fileStringImport.getObject())) {
LOGGER.error("Please upload file for import");
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.uploadFile", importDir).getString());
target.add(feedback);
return;
}
String newFilePath;
if (uploadedFile != null) {
String fileName = FilenameUtils.removeExtension(uploadedFile.getClientFileName()) + " " + getDataTime() + "." + FilenameUtils.getExtension(uploadedFile.getClientFileName());
File newFile = new File(importDir, fileName);
// Check new file, delete if it already exists
if (newFile.exists()) {
newFile.delete();
}
try {
newFile.createNewFile();
FileUtils.copyInputStreamToFile(uploadedFile.getInputStream(), newFile);
newFilePath = newFile.getAbsolutePath();
} catch (IOException e) {
LOGGER.error("Couldn't create new file " + newFile.getAbsolutePath(), e);
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportFile", newFile.getAbsolutePath()).getString());
target.add(feedback);
return;
}
} else {
newFilePath = new File(importDir, dataName).getAbsolutePath();
try {
Files.write(Paths.get(newFilePath), fileStringImport.getObject().getBytes());
} catch (IOException e) {
LOGGER.error("Couldn't create new file " + newFilePath, e);
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportFile", newFilePath).getString());
target.add(feedback);
return;
}
}
ReportDataType reportImportData = null;
try {
@NotNull PrismObject<ReportDataType> prismObject = getPrismContext().getSchemaRegistry().findObjectDefinitionByCompileTimeClass(ReportDataType.class).instantiate();
reportImportData = prismObject.asObjectable();
} catch (SchemaException e) {
LOGGER.error("Couldn't instantiate new Report Data from definition", e);
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createInstantiateReportData").getString());
target.add(feedback);
return;
}
reportImportData.setName(new PolyStringType(dataName));
reportImportData.setFilePath(newFilePath);
ObjectReferenceType reportRef = new ObjectReferenceType();
reportRef.setType(ReportType.COMPLEX_TYPE);
reportRef.setOid(getModelObject().getOid());
reportImportData.setReportRef(reportRef);
Collection<ObjectDelta<? extends ObjectType>> deltas = Collections.singleton(reportImportData.asPrismObject().createAddDelta());
Task task = getPageBase().createSimpleTask(OPERATION_CREATE_REPORT_DATA);
try {
Collection<ObjectDeltaOperation<? extends ObjectType>> retDeltas = getPageBase().getModelService().executeChanges(deltas, null, task, task.getResult());
reportImportData = (ReportDataType) retDeltas.iterator().next().getObjectDelta().getObjectToAdd().asObjectable();
} catch (ObjectAlreadyExistsException e) {
LOGGER.error("Report Data with name " + dataName + " already exists", e);
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.importReportDataAlreadyExists", dataName).getString());
target.add(feedback);
return;
} catch (Exception e) {
LOGGER.error("Couldn't create new Report Data with name " + dataName, e);
FeedbackAlerts feedback = getFeedbackPanel();
feedback.error(getPageBase().createStringResource("ImportReportPopupPanel.message.error.createImportReportData", dataName).getString());
target.add(feedback);
return;
}
importConfirmPerformed(target, reportImportData);
}
use of org.apache.wicket.util.file.File in project midpoint by Evolveum.
the class PageDebugDownloadBehaviour method initFile.
@Override
protected File initFile() {
PageBase page = getPage();
OperationResult result = new OperationResult(OPERATION_CREATE_DOWNLOAD_FILE);
MidPointApplication application = page.getMidpointApplication();
WebApplicationConfiguration config = application.getWebApplicationConfiguration();
File folder = new File(config.getExportFolder());
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdir();
}
String suffix = isUseZip() ? "zip" : "xml";
String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_m_s"));
String fileName = "ExportedData_" + getType().getSimpleName() + "_" + currentTime + "." + suffix;
File file = new File(folder, fileName);
LOGGER.debug("Creating file '{}'.", file.getAbsolutePath());
try (Writer writer = createWriter(file)) {
LOGGER.debug("Exporting objects.");
dumpHeader(writer);
dumpObjectsToStream(writer, result);
dumpFooter(writer);
LOGGER.debug("Export finished.");
result.recomputeStatus();
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't init download link", ex);
result.recordFatalError(getPage().createStringResource("PageDebugDownloadBehaviour.message.initFile.fatalError").getString(), ex);
}
if (!WebComponentUtil.isSuccessOrHandledError(result)) {
page.showResult(result);
page.getSession().error(page.getString("pageDebugList.message.createFileException"));
LOGGER.debug("Removing file '{}'.", file.getAbsolutePath());
Files.remove(file);
throw new RestartResponseException(PageDebugList.class);
}
return file;
}
use of org.apache.wicket.util.file.File in project midpoint by Evolveum.
the class AjaxDownloadBehaviorFromFile method onRequest.
public void onRequest() {
final File file = initFile();
IResourceStream resourceStream = new FileResourceStream(new File(file));
getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream) {
@Override
public void respond(IRequestCycle requestCycle) {
try {
super.respond(requestCycle);
} finally {
if (removeFile) {
LOGGER.debug("Removing file '{}'.", file.getAbsolutePath());
Files.remove(file);
}
}
}
}.setFileName(file.getName()).setContentDisposition(ContentDisposition.ATTACHMENT).setCacheDuration(Duration.ofSeconds(1)));
}
Aggregations