use of org.apache.wicket.markup.html.form.upload.FileUpload in project midpoint by Evolveum.
the class PageImportObject method getInputStream.
private InputStream getInputStream(boolean raw) throws Exception {
if (raw) {
return IOUtils.toInputStream(xmlEditorModel.getObject(), "utf-8");
}
File newFile = null;
try {
// Create new file
MidPointApplication application = getMidpointApplication();
WebApplicationConfiguration config = application.getWebApplicationConfiguration();
File folder = new File(config.getImportFolder());
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdir();
}
FileUpload uploadedFile = getUploadedFile();
newFile = new File(folder, uploadedFile.getClientFileName());
// Check new file, delete if it already exists
if (newFile.exists()) {
newFile.delete();
}
// Save file
newFile.createNewFile();
uploadedFile.writeTo(newFile);
InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
return new ReaderInputStream(reader, reader.getEncoding());
} finally {
if (newFile != null) {
FileUtils.deleteQuietly(newFile);
}
}
}
use of org.apache.wicket.markup.html.form.upload.FileUpload in project ocvn by devgateway.
the class FileInputBootstrapFormComponentWrapper method addBootstrapFileInputComponent.
private void addBootstrapFileInputComponent() {
// this is where the newly uploaded files are saved
final IModel<List<FileUpload>> internalUploadModel = new ListModel<>();
/*
* some customization of the BootstrapFileInput Component
*/
FileInputConfig fileInputConfig = new FileInputConfig();
fileInputConfig.put(new Key<String>("browseLabel"), new StringResourceModel("browseLabel", FileInputBootstrapFormComponentWrapper.this, null).getString());
fileInputConfig.put(new Key<String>("uploadClass"), "btn btn-blue");
fileInputConfig.put(new Key<String>("browseClass"), "btn btn-blue");
bootstrapFileInput = new BootstrapFileInput("bootstrapFileInput", internalUploadModel, fileInputConfig) {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
@Override
protected void onSubmit(final AjaxRequestTarget target) {
super.onSubmit(target);
List<FileUpload> fileUploads = internalUploadModel.getObject();
if (fileUploads != null) {
// check if we uploaded too many files
if (maxFiles > 0 && filesModel.size() + fileUploads.size() > maxFiles) {
if (maxFiles == 1) {
FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("OneUpload", FileInputBootstrapFormComponentWrapper.this, null).getString());
} else {
FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("tooManyFiles", FileInputBootstrapFormComponentWrapper.this, Model.of(maxFiles)).getString());
}
FileInputBootstrapFormComponentWrapper.this.invalid();
} else {
// and update the model
for (FileUpload upload : fileUploads) {
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.setName(upload.getClientFileName());
fileMetadata.setContentType(upload.getContentType());
fileMetadata.setSize(upload.getSize());
FileContent fileContent = new FileContent();
fileContent.setBytes(upload.getBytes());
fileMetadata.setContent(fileContent);
filesModel.add(fileMetadata);
// don't display the success notification
// FileInputBootstrapFormComponentWrapper.this.success(new
// StringResourceModel("successUpload",
// FileInputBootstrapFormComponentWrapper.this,
// null, new
// Model(upload.getClientFileName())).getString());
}
}
}
FileInputBootstrapFormComponentWrapper.this.getModel().setObject((T) filesModel);
target.add(fileUploadFeedback);
target.add(pendingFiles);
}
};
add(bootstrapFileInput);
/**
* due to an upgrade of FormGroup in wicket7/wicket-bootrap-0.10, the
* visitor that finds inner FormComponentS, will now find two instead of
* one: the FileInputBootstrapFormComponentWrapper and also the
* BootstrapFileInputField. This is the RIGHT result, previously in
* wicket 6.x it only got the first level of children, hence only one
* FormComponent (the FileInputBootstrapFormComponentWrapper). It would
* then read the label from FileInputBootstrapFormComponentWrapper and
* use it for displaying the label of the FormGroup. In
* wicket7/wicket-bootstrap-0.10 this will result in reading the label
* of BootstrapFileInputField which is null. So you will notice no
* labels for FormGroupS. We fix this by forcing the label of the
* underlying fileInput element to the same model as the label used by
* FileInputBootstrapFormComponentWrapper
*/
FormComponent<?> fileInput = (FormComponent<?>) bootstrapFileInput.get("fileInputForm").get("fileInput");
fileInput.setLabel(this.getLabel());
// only to admins
if (visibleOnlyToAdmin) {
MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, SecurityConstants.Roles.ROLE_ADMIN);
}
// want to read only
if (disableDeleteButton) {
MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, MetaDataRoleAuthorizationStrategy.NO_ROLE);
}
}
use of org.apache.wicket.markup.html.form.upload.FileUpload in project midpoint by Evolveum.
the class UploadDownloadPanel method uploadFilePerformed.
public void uploadFilePerformed(AjaxRequestTarget target) {
Component input = get(ID_INPUT_FILE);
try {
FileUpload uploadedFile = getFileUpload();
updateValue(uploadedFile.getBytes());
LOGGER.trace("Upload file success.");
input.success(getString("UploadPanel.message.uploadSuccess"));
} catch (Exception e) {
LOGGER.trace("Upload file error.", e);
input.error(getString("UploadPanel.message.uploadError") + " " + e.getMessage());
}
}
use of org.apache.wicket.markup.html.form.upload.FileUpload in project midpoint by Evolveum.
the class PageNewReport method importReportFromFilePerformed.
private void importReportFromFilePerformed(AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);
FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
final FileUpload uploadedFile = file.getFileUpload();
if (uploadedFile == null) {
error(getString("PageNewReport.message.nullFile"));
target.add(getFeedbackPanel());
return;
}
InputStream stream = null;
File newFile = null;
try {
// Create new file
MidPointApplication application = getMidpointApplication();
WebApplicationConfiguration config = application.getWebApplicationConfiguration();
File folder = new File(config.getImportFolder());
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdir();
}
newFile = new File(folder, uploadedFile.getClientFileName());
// Check new file, delete if it already exists
if (newFile.exists()) {
newFile.delete();
}
// Save file
// Task task = createSimpleTask(OPERATION_IMPORT_FILE);
newFile.createNewFile();
uploadedFile.writeTo(newFile);
InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
// reader.
stream = new ReaderInputStream(reader, reader.getEncoding());
byte[] reportIn = IOUtils.toByteArray(stream);
setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
} catch (Exception ex) {
result.recordFatalError("Couldn't import file.", ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't import file", ex);
} finally {
if (stream != null) {
IOUtils.closeQuietly(stream);
}
if (newFile != null) {
FileUtils.deleteQuietly(newFile);
}
}
showResult(result);
target.add(getFeedbackPanel());
}
Aggregations