List of usage examples for org.apache.wicket.protocol.http.servlet MultipartServletWebRequest MultipartServletWebRequest
public MultipartServletWebRequest(HttpServletRequest httpServletRequest, String filterPrefix, Url url)
From source file:com.servoy.j2db.server.headlessclient.MediaUploadPage.java
License:Open Source License
@SuppressWarnings({ "nls", "unchecked" })
public MediaUploadPage(IPageMap pageMap, final IMediaUploadCallback callback, boolean multiSelect,
final IApplication application) {
super(pageMap);
add(new PageContributor(application, "contribution"));
Form form = new Form("form") {
private static final long serialVersionUID = 1L;
private boolean close = false;
@Override//w w w . j ava 2s .c om
protected void onSubmit() {
close = true;
if (fuf != null) {
final FileUpload fu = fuf.getFileUpload();
if (fu != null) {
IUploadData ud = new FileUploadData(fu);
callback.uploadComplete(new IUploadData[] { ud });
} else {
callback.uploadComplete(new IUploadData[0]);
}
} else if (mfuf != null) {
Collection<FileUpload> uploads = mfuf.getModelObject();
if (uploads != null) {
IUploadData[] data = new IUploadData[uploads.size()];
int counter = 0;
for (FileUpload fileUpload : uploads) {
data[counter++] = new FileUploadData(fileUpload);
}
callback.uploadComplete(data);
} else {
callback.uploadComplete(new IUploadData[0]);
}
}
callback.onSubmit();
if (fuf != null) {
remove(fuf);
} else if (mfuf != null) {
remove(mfuf);
}
add(new Label("panel",
new Model<String>(application.getI18NMessage("servoy.filechooser.upload.finished"))));
}
@Override
public void renderHead(HtmlHeaderContainer container) {
super.renderHead(container);
if (close) {
container.getHeaderResponse().renderOnLoadJavascript("window.parent.triggerAjaxUpdate();");
}
}
@Override
protected boolean handleMultiPart() {
// Change the request to a multipart web request that is able to work with multiple file uploads for the same field nmae
// so parameters are parsed out correctly as well
try {
final WebRequest req = ((WebRequest) getRequest());
MultipartServletWebRequest multipart;
try {
Settings settings = Settings.getInstance();
File fileUploadDir = null;
String uploadDir = settings.getProperty("servoy.ng_web_client.temp.uploadir");
if (uploadDir != null) {
fileUploadDir = new File(uploadDir);
if (!fileUploadDir.exists() && !fileUploadDir.mkdirs()) {
fileUploadDir = null;
Debug.error(
"Couldn't use the property 'servoy.ng_web_client.temp.uploadir' value: '"
+ uploadDir
+ "', directory could not be created or doesn't exists");
}
}
int tempFileThreshold = Utils.getAsInteger(
settings.getProperty("servoy.ng_web_client.tempfile.threshold", "50"), false)
* 1000;
multipart = new MultipartServletWebRequest(req.getHttpServletRequest(), getMaxSize(),
new DiskFileItemFactory(tempFileThreshold, fileUploadDir) {
private final HashSet<String> fieldNames = new HashSet<String>();
@Override
public FileItem createItem(String fieldName, String contentType,
boolean isFormField, String fileName) {
String adjustedFieldName = fieldName;
int i = 1;
while (fieldNames.contains(adjustedFieldName)) {
adjustedFieldName = fieldName + "_additionalFile_" + (i++);
}
String timestampStr = req
.getParameter("last_modified_" + fieldName + "_" + fileName);
long timestamp = System.currentTimeMillis();
if (timestampStr != null) {
try {
timestamp = Long.parseLong(timestampStr);
} catch (NumberFormatException ex) {
timestamp = System.currentTimeMillis();
}
}
fieldNames.add(adjustedFieldName);
return new ServoyDiskFileItem(adjustedFieldName, contentType, isFormField,
fileName, getSizeThreshold(), getRepository(), timestamp);
}
});
} catch (FileUploadException e) {
throw new WicketRuntimeException(e);
}
multipart.setRequestParameters(req.getRequestParameters());
getRequestCycle().setRequest(multipart);
return true;
} catch (WicketRuntimeException wre) {
if (wre.getCause() == null || !(wre.getCause() instanceof FileUploadException)) {
throw wre;
}
FileUploadException e = (FileUploadException) wre.getCause();
// Create model with exception and maximum size values
final Map<String, Object> model = new HashMap<String, Object>();
model.put("exception", e);
model.put("maxSize", getMaxSize());
onFileUploadException((FileUploadException) wre.getCause(), model);
// don't process the form if there is a FileUploadException
return false;
}
}
};
if (multiSelect) {
fuf = null;
IModel<Collection<FileUpload>> model = new Model(new ArrayList());
mfuf = new MultiFileUpload("panel", model, application);
form.add(mfuf);
} else {
mfuf = null;
fuf = new SingleFileUpload("panel", application);
form.add(fuf);
}
form.add(new Label("uploadtitle",
new Model<String>(application.getI18NMessage("servoy.filechooser.upload.title"))));
form.setMultiPart(true);
add(form);
FeedbackPanel panel = new FeedbackPanel("feedback");
add(panel);
add(CSSPackageResource.getHeaderContribution(
"/servoy-webclient/templates/" + application.getClientProperty(WEBCONSTANTS.WEBCLIENT_TEMPLATES_DIR)
+ "/servoy_web_client_default.css"));
}