You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by re...@apache.org on 2023/04/07 10:29:09 UTC
[wicket] branch reiern70/WICKET-7033 created (now 04c838f0d7)
This is an automated email from the ASF dual-hosted git repository.
reiern70 pushed a change to branch reiern70/WICKET-7033
in repository https://gitbox.apache.org/repos/asf/wicket.git
at 04c838f0d7 [WICKET-7033] WIP
This branch includes the following new commits:
new aab23d5752 [WICKET-7033] WIP
new 04c838f0d7 [WICKET-7033] WIP
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[wicket] 01/02: [WICKET-7033] WIP
Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
reiern70 pushed a commit to branch reiern70/WICKET-7033
in repository https://gitbox.apache.org/repos/asf/wicket.git
commit aab23d5752cc69839918be28c7e4330132a6538f
Author: reiern70 <re...@gmail.com>
AuthorDate: Thu Apr 6 10:20:25 2023 +0300
[WICKET-7033] WIP
---
.../resource/AbstractFileUploadResource.java | 194 +++++++++++++++++++++
.../resource/FileUploadResourceReference.java | 107 ++++++++++++
.../form/upload/resource/IUploadsFileManager.java | 57 ++++++
.../upload/resource/ResourceFileUploadField.java | 62 +++++++
.../upload/resource/ResourceFileUploadField.js | 91 ++++++++++
5 files changed, 511 insertions(+)
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/AbstractFileUploadResource.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/AbstractFileUploadResource.java
new file mode 100644
index 0000000000..6b9d35a356
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/AbstractFileUploadResource.java
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload.resource;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.resource.AbstractResource;
+import org.apache.wicket.util.lang.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import com.github.openjson.JSONObject;
+
+/**
+ * The resource that handles the file uploads.
+ * Reads the file items from the request parameters and uses {@link IUploadsFileManager}
+ * to store them.
+ * Additionally, cares about the response's content type and body.
+ * <p>
+ * This code was adapted from
+ * <p>
+ * <a href="https://github.com/martin-g/blogs/blob/master/file-upload/src/main/java/com/mycompany/fileupload/AbstractFileUploadResource.java">AbstractFileUploadResource.java</a>
+ * <p>
+ * The main difference is that there some JQuery plugin is used at client side (and it supports multiple uploads +
+ * some UI allowing to delete/preview files and so on).
+ * Here we are just using plain jQuery code at client side to upload a single file.
+ */
+public abstract class AbstractFileUploadResource extends AbstractResource
+{
+ private static final Logger LOG = LoggerFactory.getLogger(AbstractFileUploadResource.class);
+
+ public static final String PARAM_NAME = "FILE-UPLOAD";
+
+ private final IUploadsFileManager fileManager;
+
+ public AbstractFileUploadResource(IUploadsFileManager fileManager)
+ {
+ this.fileManager = fileManager;
+ }
+
+ /**
+ * Reads and stores the uploaded files
+ *
+ * @param attributes
+ * Attributes
+ * @return ResourceResponse
+ */
+ @Override
+ protected ResourceResponse newResourceResponse(Attributes attributes)
+ {
+ final ResourceResponse resourceResponse = new ResourceResponse();
+
+ final ServletWebRequest webRequest = (ServletWebRequest) attributes.getRequest();
+
+ String identifier = webRequest.getRequestParameters().getParameterValue("uploadId").toString("resource");
+
+ try
+ {
+ MultipartServletWebRequest multiPartRequest = webRequest.newMultipartWebRequest(getMaxSize(), identifier);
+ multiPartRequest.parseFileParts();
+
+ RequestCycle.get().setRequest(multiPartRequest);
+
+ Map<String, List<FileItem>> files = multiPartRequest.getFiles();
+ List<FileItem> fileItems = files.get(PARAM_NAME);
+
+ if (fileItems != null)
+ {
+ List<FileUpload> fileUploads = new ArrayList<>();
+ for (FileItem fileItem : fileItems)
+ {
+ fileUploads.add(new FileUpload(fileItem));
+ }
+ saveFiles(fileUploads, identifier);
+ prepareResponse(resourceResponse, webRequest, fileUploads);
+ }
+ else
+ {
+ resourceResponse.setContentType("application/json");
+ resourceResponse.setWriteCallback(new WriteCallback()
+ {
+ @Override
+ public void writeData(Attributes attributes) throws IOException
+ {
+ JSONObject json = new JSONObject();
+ json.put("error", true);
+ json.put("errorMessage", "No files selected!");
+ String error = json.toString();
+ attributes.getResponse().write(error);
+ }
+ });
+ }
+
+ }
+ catch (Exception fux)
+ {
+ LOG.error("An error occurred while uploading a file", fux);
+ resourceResponse.setContentType("application/json");
+ JSONObject json = new JSONObject();
+ json.put("error", true);
+ json.put("errorMessage", fux.getMessage());
+ String error = json.toString();
+ resourceResponse.setWriteCallback(new WriteCallback()
+ {
+ @Override
+ public void writeData(Attributes attributes) throws IOException
+ {
+ attributes.getResponse().write(error);
+ }
+ });
+ }
+
+ return resourceResponse;
+ }
+
+ /**
+ * Sets the response's content type and body
+ *
+ * @param resourceResponse
+ * ResourceResponse
+ * @param webRequest
+ * ServletWebRequest
+ * @param fileItems
+ * List<FileUpload>
+ */
+ protected void prepareResponse(ResourceResponse resourceResponse, ServletWebRequest webRequest, List<FileUpload> fileItems)
+ {
+ resourceResponse.setContentType("application/json");
+ final String responseContent = generateJsonResponse(resourceResponse, webRequest, fileItems);
+
+ resourceResponse.setWriteCallback(new WriteCallback()
+ {
+ @Override
+ public void writeData(Attributes attributes) throws IOException
+ {
+ attributes.getResponse().write(responseContent);
+ }
+ });
+ }
+
+ /**
+ * Delegates to FileManager to store the uploaded files
+ *
+ * @param fileItems
+ * List<FileUpload>
+ */
+ protected void saveFiles(List<FileUpload> fileItems, String identifier) {
+ for (FileUpload fileItem : fileItems) {
+ fileManager.save(fileItem, identifier + "_" + fileItem.getClientFileName());
+ }
+ }
+
+ /**
+ * Defines what is the maximum size of the uploaded files.
+ *
+ * @return Bytes
+ */
+ protected abstract Bytes getMaxSize();
+
+ /**
+ * Should generate the response's body in JSON format
+ *
+ * @param resourceResponse
+ * ResourceResponse
+ * @param webRequest
+ * ServletWebRequest
+ * @param files
+ * List<FileUpload>
+ * @return The generated JSON
+ */
+ protected abstract String generateJsonResponse(ResourceResponse resourceResponse,
+ ServletWebRequest webRequest, List<FileUpload> files);
+
+}
\ No newline at end of file
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FileUploadResourceReference.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FileUploadResourceReference.java
new file mode 100644
index 0000000000..a62e49125e
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FileUploadResourceReference.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload.resource;
+
+import java.util.List;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
+import org.apache.wicket.request.resource.IResource;
+import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.util.lang.Bytes;
+import com.github.openjson.JSONArray;
+import com.github.openjson.JSONException;
+import com.github.openjson.JSONObject;
+
+/**
+ * A resource reference that provides default implementation of AbstractFileUploadResource.
+ * The implementation generates JSON response with data from the upload (this data is
+ * re-routed to page for things like getting the client file name and file size).
+ */
+public class FileUploadResourceReference extends ResourceReference
+{
+
+ private final IUploadsFileManager uploadFileManager;
+
+ private static FileUploadResourceReference i;
+
+ public static FileUploadResourceReference getInstance() {
+ return i;
+ }
+
+ public static FileUploadResourceReference createNewInstance(IUploadsFileManager fileManager, Bytes maxSize)
+ {
+ if (i == null)
+ {
+ i = new FileUploadResourceReference(fileManager, maxSize);
+ }
+ return i;
+ }
+
+ private final Bytes maxSize;
+
+ private FileUploadResourceReference(IUploadsFileManager uploadFileManager, Bytes maxSize)
+ {
+ super(FileUploadResourceReference.class, "file-uploads");
+
+ this.uploadFileManager = uploadFileManager;
+ this.maxSize = maxSize;
+ }
+
+ @Override
+ public IResource getResource()
+ {
+ return new AbstractFileUploadResource(uploadFileManager)
+ {
+ @Override
+ protected Bytes getMaxSize()
+ {
+ return maxSize;
+ }
+
+ @Override
+ protected String generateJsonResponse(ResourceResponse resourceResponse, ServletWebRequest webRequest, List<FileUpload> files)
+ {
+ JSONArray json = new JSONArray();
+
+ for (FileUpload fileItem : files)
+ {
+ JSONObject fileJson = new JSONObject();
+
+ try
+ {
+ generateFileInfo(fileJson, fileItem);
+ json.put(fileJson);
+ }
+ catch (JSONException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ return json.toString();
+ }
+
+ };
+ }
+
+ protected void generateFileInfo(JSONObject fileJson, FileUpload fileItem)
+ {
+ fileJson.put("clientFileName", fileItem.getClientFileName());
+ fileJson.put("size", fileItem.getSize());
+ fileJson.put("contentType", fileItem.getContentType());
+ }
+
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java
new file mode 100644
index 0000000000..9527460cfd
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload.resource;
+
+import java.io.File;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+
+/**
+ * This interface defines the bridge between a file uploaded to a resource and the wicket component.
+ * Wicket component uses some identifier (passed as a request parameter) to instruct the resource
+ * how to store file. Same identifier is used to retrieve the file, once uploaded, from component in page.
+ * Mind that uploader resource is a singleton => identifier needs to be unique among different sessions
+ * (and pages in a session).
+ */
+public interface IUploadsFileManager {
+
+ /**
+ * Saves an uploaded files into some persistent storage (e,g, disk).
+ *
+ * @param fileItem
+ * The {@link FileUpload}
+ * @param identifier
+ * The identifier used for this file.
+ */
+ void save(FileUpload fileItem, String identifier);
+
+ /**
+ * Retrieves the file based on an identifier.
+ *
+ * @param identifier
+ * The identifier used for this file.
+ * @return File the file
+ */
+ File getFile(String identifier);
+
+ /**
+ * Deletes a file based on identifier.
+ *
+ * @param identifier
+ * The identifier
+ */
+ void deleteFile(String identifier);
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.java
new file mode 100644
index 0000000000..105766700b
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload.resource;
+
+import java.io.File;
+import org.apache.wicket.Session;
+import org.apache.wicket.markup.html.form.upload.FileUploadField;
+import org.apache.wicket.model.IModel;
+
+public abstract class ResourceFileUploadField extends FileUploadField {
+
+ private static abstract class FileModel implements IModel<File> {
+
+ @Override
+ public File getObject() {
+ return fileManager().getFile(getIdentifier());
+ }
+
+ @Override
+ public void setObject(File object) {
+ // do thing
+ }
+
+ protected abstract IUploadsFileManager fileManager();
+
+ protected abstract String getIdentifier();
+ }
+
+ public ResourceFileUploadField(String id) {
+ super(id);
+ setOutputMarkupId(true);
+ setMarkupId("WRFUF" + Session.get().getId() + System.currentTimeMillis());
+ setDefaultModel(new FileModel() {
+ @Override
+ protected IUploadsFileManager fileManager() {
+ return ResourceFileUploadField.this.fileManager();
+ }
+
+ @Override
+ protected String getIdentifier()
+ {
+ return ResourceFileUploadField.this.getMarkupId();
+ }
+ });
+ }
+
+ protected abstract IUploadsFileManager fileManager();
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.js b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.js
new file mode 100644
index 0000000000..861352eef5
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.js
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+;(function (undefined) {
+
+ 'use strict';
+
+ if (typeof(Wicket.ResourceFileUploadField) === 'object') {
+ return;
+ }
+
+ Wicket.ResourceFileUploadField = function (settings)
+ {
+ this.link = '#' + settings.link;
+ this.inputName = settings.inputName;
+ this.input = document.getElementById(this.inputName)
+ this.resourceUrl = settings.resourceUrl + "?uploadId=" + this.inputName;
+ this.ajaxCallBackUrl = settings.ajaxCallBackUrl;
+ this.onErrorCallBack = settings.onErrorCallBack;
+ this.connectionErrorCallBack = settings.connectionErrorCallBack;
+ this.uploadFinished = settings.uploadFinished;
+ }
+
+ Wicket.ResourceFileUploadField.prototype.upload = function()
+ {
+ // we add the file (yes only one) to a FormData object.
+ let formData = new FormData();
+ let file = this.input.files[0];
+ formData.append("FILE-UPLOAD", file);
+ // we need to pass to mounted resource the ID of the file
+ // where the uploaded file will be stored. Later on JAVA code will
+ // use IFileManager#getFile(inputName) to retrieve the uploaded file.
+ // Java side makes sure this inputName is UNIQUE
+ // this is needed as the mounted resource is a singleton and have no state
+ formData.append("uploadId", this.inputName);
+ const self = this;
+ // we use jQuery to post the file to the resource (this.url)
+ // and we keep a reference to the request in order to be
+ // to cancel the upload
+ this.xhr = $.ajax({
+ url: this.resourceUrl,
+ type: "POST",
+ data: formData,
+ processData: false,
+ contentType: false,
+ success: function (res) {
+ // do clean up on success
+ self.uploadFinished();
+ if (res.error) {
+ const ep = {'upload-success': false};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ self.onErrorCallBack(res);
+ return;
+ }
+ // resource sent a JSON object with file client name
+ // we need this in page this we schedule a wicket (AJAX) request to the component
+ // UploadPanel passing the clientFileName and success = true
+ const ep = {'upload-success': true, 'clientFileName': res[0]['clientFileName']};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ },
+ error: function (jqXHR, textStatus, errorThrown) {
+ // the thing failed. Tell UploadPanel about this.
+ self.uploadFinished();
+ if (textStatus === "abort") {
+ const ep = {'upload-success': false, 'info':''};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ } else if (textStatus === "error"){
+ // this happens if there is some connection error
+ self.connectionErrorCallBack();
+ } else if (textStatus === "parsererror"){
+ var data = jqXHR.responseText;
+ Wicket.Log.log(data);
+ }
+ }
+ });
+ }
+})();
\ No newline at end of file
[wicket] 02/02: [WICKET-7033] WIP
Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
reiern70 pushed a commit to branch reiern70/WICKET-7033
in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 04c838f0d74b2907183cfdc16ae9ff38dba5d265
Author: reiern70 <re...@gmail.com>
AuthorDate: Fri Apr 7 13:28:55 2023 +0300
[WICKET-7033] WIP
---
.../java/org/apache/wicket/ajax/AjaxUtils.java | 52 ++++++
.../upload/resource/FolderUploadsFileManager.java | 78 +++++++++
.../form/upload/resource/IUploadsFileManager.java | 3 +-
.../upload/resource/ResourceFileUploadField.java | 62 -------
.../resource/SingleFileUploadToResourceField.java | 180 +++++++++++++++++++++
...Field.js => SingleFileUploadToResourceField.js} | 60 ++++---
.../wicket/examples/upload/UploadApplication.java | 19 +++
.../apache/wicket/examples/upload/UploadPage.java | 2 +-
.../examples/upload/UploadToResourcePage.html | 33 ++++
.../examples/upload/UploadToResourcePage.java | 142 ++++++++++++++++
.../apache/wicket/examples/homepage/HomePage.html | 1 +
.../resources/org/apache/wicket/examples/style.css | 2 +-
12 files changed, 547 insertions(+), 87 deletions(-)
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxUtils.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxUtils.java
new file mode 100644
index 0000000000..918055e8a0
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.ajax;
+
+import java.util.Optional;
+import java.util.function.Consumer;
+import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.danekja.java.util.function.serializable.SerializableConsumer;
+
+public class AjaxUtils {
+
+ /**
+ * Runs action if current request is of type "AJAX". Otherwise, nothing is done.
+ *
+ * @param ajaxAction
+ * The action to run a {@link SerializableConsumer}
+ */
+ public static void executeIfAjax(SerializableConsumer<AjaxRequestTarget> ajaxAction)
+ {
+ Optional<AjaxRequestTarget> target = RequestCycle.get().find(AjaxRequestTarget.class);
+ target.ifPresent(ajaxAction);
+ }
+
+
+ /**
+ * Runs action if current request is of type "AJAX" or a Websockets request. Otherwise, nothing is done.
+ *
+ * @param ajaxAction
+ * The action to run a {@link SerializableConsumer}
+ */
+ public static void executeIfAjaxOrWebSockets(SerializableConsumer<IPartialPageRequestHandler> ajaxAction)
+ {
+ Optional<IPartialPageRequestHandler> target = RequestCycle.get().find(IPartialPageRequestHandler.class);
+ target.ifPresent(ajaxAction);
+ }
+
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FolderUploadsFileManager.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FolderUploadsFileManager.java
new file mode 100644
index 0000000000..1fd868c085
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/FolderUploadsFileManager.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload.resource;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.util.file.File;
+import org.apache.wicket.util.io.IOUtils;
+import org.apache.wicket.util.lang.Args;
+
+public class FolderUploadsFileManager implements IUploadsFileManager
+{
+
+ private final File folder;
+
+
+ public FolderUploadsFileManager(File folder)
+ {
+ Args.notNull(folder, "folder");
+ if (!folder.exists()) {
+ try
+ {
+ Files.createDirectories(folder.toPath());
+ }
+ catch (IOException e)
+ {
+ throw new WicketRuntimeException(e);
+ }
+ }
+ else if (folder.exists() && !folder.isDirectory())
+ {
+ throw new IllegalArgumentException("Not a folder : " + folder.getAbsolutePath());
+ }
+ this.folder = folder;
+ }
+
+ @Override
+ public void save(FileUpload fileItem, String identifier)
+ {
+ try
+ {
+ IOUtils.copy(fileItem.getInputStream(), new FileOutputStream(new File(folder, identifier)));
+ }
+ catch (IOException e)
+ {
+ throw new WicketRuntimeException(e);
+ }
+ }
+
+ @Override
+ public java.io.File getFile(String identifier)
+ {
+ return new File(folder, identifier);
+ }
+
+ @Override
+ public void deleteFile(String identifier)
+ {
+ new File(folder, identifier).remove();
+ }
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java
index 9527460cfd..477bb30551 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/IUploadsFileManager.java
@@ -26,7 +26,8 @@ import org.apache.wicket.markup.html.form.upload.FileUpload;
* Mind that uploader resource is a singleton => identifier needs to be unique among different sessions
* (and pages in a session).
*/
-public interface IUploadsFileManager {
+public interface IUploadsFileManager
+{
/**
* Saves an uploaded files into some persistent storage (e,g, disk).
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.java
deleted file mode 100644
index 105766700b..0000000000
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.wicket.markup.html.form.upload.resource;
-
-import java.io.File;
-import org.apache.wicket.Session;
-import org.apache.wicket.markup.html.form.upload.FileUploadField;
-import org.apache.wicket.model.IModel;
-
-public abstract class ResourceFileUploadField extends FileUploadField {
-
- private static abstract class FileModel implements IModel<File> {
-
- @Override
- public File getObject() {
- return fileManager().getFile(getIdentifier());
- }
-
- @Override
- public void setObject(File object) {
- // do thing
- }
-
- protected abstract IUploadsFileManager fileManager();
-
- protected abstract String getIdentifier();
- }
-
- public ResourceFileUploadField(String id) {
- super(id);
- setOutputMarkupId(true);
- setMarkupId("WRFUF" + Session.get().getId() + System.currentTimeMillis());
- setDefaultModel(new FileModel() {
- @Override
- protected IUploadsFileManager fileManager() {
- return ResourceFileUploadField.this.fileManager();
- }
-
- @Override
- protected String getIdentifier()
- {
- return ResourceFileUploadField.this.getMarkupId();
- }
- });
- }
-
- protected abstract IUploadsFileManager fileManager();
-}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/SingleFileUploadToResourceField.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/SingleFileUploadToResourceField.java
new file mode 100644
index 0000000000..005bbc198a
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/SingleFileUploadToResourceField.java
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload.resource;
+
+import java.io.File;
+import org.apache.wicket.Session;
+import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.AjaxUtils;
+import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+import org.apache.wicket.markup.html.form.upload.FileUploadField;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.request.resource.JavaScriptResourceReference;
+import org.apache.wicket.resource.CoreLibrariesContributor;
+import com.github.openjson.JSONObject;
+
+public abstract class SingleFileUploadToResourceField extends FileUploadField {
+
+ private static final JavaScriptResourceReference JS = new JavaScriptResourceReference(SingleFileUploadToResourceField.class, "SingleFileUploadToResourceField.js");
+
+
+ private static abstract class FileModel implements IModel<File>
+ {
+
+ @Override
+ public File getObject()
+ {
+ return fileManager().getFile(getIdentifier());
+ }
+
+ @Override
+ public void setObject(File object)
+ {
+ // do thing
+ }
+
+ protected abstract IUploadsFileManager fileManager();
+
+ protected abstract String getIdentifier();
+ }
+
+ private final AbstractDefaultAjaxBehavior ajaxBehavior;
+
+ public SingleFileUploadToResourceField(String id) {
+ super(id);
+ setOutputMarkupId(true);
+ setMarkupId("WRFUF" + getUserId() + System.currentTimeMillis());
+ setDefaultModel(new FileModel() {
+ @Override
+ protected IUploadsFileManager fileManager() {
+ return SingleFileUploadToResourceField.this.fileManager();
+ }
+
+ @Override
+ protected String getIdentifier()
+ {
+ return SingleFileUploadToResourceField.this.getMarkupId();
+ }
+ });
+ ajaxBehavior = new AbstractDefaultAjaxBehavior()
+ {
+ @Override
+ protected void respond(AjaxRequestTarget target)
+ {
+ boolean success = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("success").toBoolean(false);
+ if (success)
+ {
+ String clientFileName = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("clientFileName").toString(null);
+ onUploadSuccess(target, clientFileName);
+ }
+ else
+ {
+ String errorInfo = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("errorMessage").toString(null);
+ onUploadFailure(target, errorInfo);
+ }
+ }
+ };
+ add(ajaxBehavior);
+ }
+
+ public File getFile()
+ {
+ return (File)getDefaultModel().getObject();
+ }
+
+ @Override
+ protected void onRemove()
+ {
+ super.onRemove();
+ // we clean any client side mess if component is removed via "partial" page replacement
+ AjaxUtils.executeIfAjaxOrWebSockets(target -> target.appendJavaScript("delete window." + getMarkupId() + ";"));
+ }
+
+ protected void onUploadSuccess(AjaxRequestTarget target, String clientFileName)
+ {
+ // do nothing by default
+ }
+
+ protected void onUploadFailure(AjaxRequestTarget target, String errorInfo)
+ {
+ // do nothing by default
+ }
+
+ protected String getUserId() {
+ return Session.get().getId() != null ? Session.get().getId() : "Annon";
+ }
+
+
+ @Override
+ public void renderHead(IHeaderResponse response)
+ {
+ CoreLibrariesContributor.contributeAjax(getApplication(), response);
+ response.render(JavaScriptHeaderItem.forReference(JS));
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("inputName", getMarkupId());
+ jsonObject.put("resourceUrl", urlFor(FileUploadResourceReference.getInstance(), new PageParameters()).toString());
+ jsonObject.put("ajaxCallBackUrl", ajaxBehavior.getCallbackUrl());
+ response.render(OnDomReadyHeaderItem.forScript("window."
+ + getMarkupId() + " = new Wicket.SingleFileUploadToResourceField("
+ + jsonObject + ","
+ + getClientSideSuccessCallBack() + "," + getClientSideErrorCallBack() + ","
+ + getConnectionErrorCallBack() + "); console.log(" + getMarkupId() + ");"));
+ }
+
+ public String getTriggerUploadScript()
+ {
+ return "console.log(window." + getMarkupId() + ");" + " window." + getMarkupId() + ".upload();";
+ }
+
+ public void startUpload(IPartialPageRequestHandler target)
+ {
+ target.appendJavaScript(getTriggerUploadScript());
+ }
+
+ public String getTriggerCancelUploadScript()
+ {
+ return "window." + getMarkupId() + ".cancel();";
+ }
+
+ public void cancelUpload(IPartialPageRequestHandler target)
+ {
+ target.appendJavaScript(getTriggerCancelUploadScript());
+ }
+
+ protected String getClientSideSuccessCallBack()
+ {
+ return "function () {}";
+ }
+
+ protected String getClientSideErrorCallBack()
+ {
+ return "function () {}";
+ }
+
+ protected String getConnectionErrorCallBack()
+ {
+ return "function () {}";
+ }
+
+ protected abstract IUploadsFileManager fileManager();
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.js b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/SingleFileUploadToResourceField.js
similarity index 57%
rename from wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.js
rename to wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/SingleFileUploadToResourceField.js
index 861352eef5..d3ba8777fc 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/ResourceFileUploadField.js
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/SingleFileUploadToResourceField.js
@@ -19,27 +19,30 @@
'use strict';
- if (typeof(Wicket.ResourceFileUploadField) === 'object') {
+ if (typeof(Wicket.SingleFileUploadToResourceField) === 'object') {
return;
}
- Wicket.ResourceFileUploadField = function (settings)
+ Wicket.CurrentUpload = null;
+
+ Wicket.SingleFileUploadToResourceField = function (settings, clientSideSuccessCallBack, clientSideErrorCallBack, connectionErrorCallBack)
{
- this.link = '#' + settings.link;
+ console.log(settings);
this.inputName = settings.inputName;
- this.input = document.getElementById(this.inputName)
+ this.input = document.getElementById(this.inputName);
this.resourceUrl = settings.resourceUrl + "?uploadId=" + this.inputName;
this.ajaxCallBackUrl = settings.ajaxCallBackUrl;
- this.onErrorCallBack = settings.onErrorCallBack;
- this.connectionErrorCallBack = settings.connectionErrorCallBack;
- this.uploadFinished = settings.uploadFinished;
+ this.clientSideSuccessCallBack = clientSideSuccessCallBack;
+ this.clientSideErrorCallBack = clientSideErrorCallBack;
+ this.connectionErrorCallBack = connectionErrorCallBack;
}
- Wicket.ResourceFileUploadField.prototype.upload = function()
+ Wicket.SingleFileUploadToResourceField.prototype.upload = function()
{
+ console.log("Wicket.SingleFileUploadToResourceField.prototype.upload");
// we add the file (yes only one) to a FormData object.
- let formData = new FormData();
- let file = this.input.files[0];
+ var formData = new FormData();
+ var file = this.input.files[0];
formData.append("FILE-UPLOAD", file);
// we need to pass to mounted resource the ID of the file
// where the uploaded file will be stored. Later on JAVA code will
@@ -47,7 +50,7 @@
// Java side makes sure this inputName is UNIQUE
// this is needed as the mounted resource is a singleton and have no state
formData.append("uploadId", this.inputName);
- const self = this;
+ var self = this;
// we use jQuery to post the file to the resource (this.url)
// and we keep a reference to the request in order to be
// to cancel the upload
@@ -59,24 +62,24 @@
contentType: false,
success: function (res) {
// do clean up on success
- self.uploadFinished();
+ self.clientSideSuccessCallBack();
if (res.error) {
- const ep = {'upload-success': false};
+ var ep = {'success': false, 'errorMessage': res.errorMessage};
+ Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
+ self.clientSideErrorCallBack(res);
+ } else {
+ // resource sent a JSON object with file client name
+ // we need this in page this we schedule a wicket (AJAX) request to the component
+ // UploadPanel passing the clientFileName and success = true
+ var ep = {'success': true, 'clientFileName': res[0]['clientFileName']};
Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
- self.onErrorCallBack(res);
- return;
}
- // resource sent a JSON object with file client name
- // we need this in page this we schedule a wicket (AJAX) request to the component
- // UploadPanel passing the clientFileName and success = true
- const ep = {'upload-success': true, 'clientFileName': res[0]['clientFileName']};
- Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
},
error: function (jqXHR, textStatus, errorThrown) {
// the thing failed. Tell UploadPanel about this.
- self.uploadFinished();
if (textStatus === "abort") {
- const ep = {'upload-success': false, 'info':''};
+ self.clientSideErrorCallBack();
+ var ep = {'success': false, 'errorMessage': 'Upload failed because aborted request!'};
Wicket.Ajax.get({"u": self.ajaxCallBackUrl, "ep": ep});
} else if (textStatus === "error"){
// this happens if there is some connection error
@@ -88,4 +91,17 @@
}
});
}
+
+ // cancel the upload
+ Wicket.SingleFileUploadToResourceField.prototype.cancel = function () {
+ // we have a reference to the request we can cancel it.
+ Wicket.CurrentUpload = null;
+ if (this.xhr) {
+ this.xhr.abort();
+ Wicket.Log.log("upload canceled!");
+ delete (this.xhr);
+ } else {
+ Wicket.Log.log("Too late to cancel: upload already finished.");
+ }
+ }
})();
\ No newline at end of file
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
index 0826393842..42c28212f9 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadApplication.java
@@ -18,7 +18,11 @@ package org.apache.wicket.examples.upload;
import org.apache.wicket.Page;
import org.apache.wicket.examples.WicketExampleApplication;
+import org.apache.wicket.markup.html.form.upload.resource.FileUploadResourceReference;
+import org.apache.wicket.markup.html.form.upload.resource.FolderUploadsFileManager;
+import org.apache.wicket.markup.html.form.upload.resource.IUploadsFileManager;
import org.apache.wicket.util.file.Folder;
+import org.apache.wicket.util.lang.Bytes;
/**
@@ -30,6 +34,8 @@ public class UploadApplication extends WicketExampleApplication
{
private Folder uploadFolder = null;
+ private IUploadsFileManager uploadsFileManager;
+
@Override
public Class<? extends Page> getHomePage()
{
@@ -55,9 +61,22 @@ public class UploadApplication extends WicketExampleApplication
// Ensure folder exists
uploadFolder.mkdirs();
+ uploadsFileManager = new FolderUploadsFileManager(uploadFolder);
+
+ mountResource("/uploads", FileUploadResourceReference.createNewInstance(uploadsFileManager, Bytes.megabytes(100)));
+
mountPage("/multi", MultiUploadPage.class);
mountPage("/single", UploadPage.class);
+ mountPage("/singleUploadToResource", UploadToResourcePage.class);
getApplicationSettings().setUploadProgressUpdatesEnabled(true);
}
+
+ public IUploadsFileManager getUploadsFileManager() {
+ return uploadsFileManager;
+ }
+
+ public static UploadApplication getInstance() {
+ return (UploadApplication)get();
+ }
}
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadPage.java
index 3fe09b7eef..6e98748d2c 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadPage.java
@@ -216,6 +216,6 @@ public class UploadPage extends WicketExamplePage
private Folder getUploadFolder()
{
- return ((UploadApplication)Application.get()).getUploadFolder();
+ return UploadApplication.getInstance().getUploadFolder();
}
}
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html
new file mode 100644
index 0000000000..1980715d9c
--- /dev/null
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.html
@@ -0,0 +1,33 @@
+<html xmlns:wicket="http://wicket.apache.org">
+<head>
+ <title>Wicket Examples - upload</title>
+ <style>
+ legend { border: 1px solid #e9601a; background-color: #bbb; color: #fff; padding: 4px;}
+ fieldset { border: 1px solid #e9601a; padding: 10px; margin-top: 10px;}
+ </style>
+</head>
+<body>
+ <wicket:extend>
+
+ <p>Wicket can now upload mounted to a resource.</p>
+ <br/>
+
+ <div>
+ <fieldset>
+ <legend>Upload a single file to a resourde</legend>
+ <p>
+ <label wicket:for="fileInput">File</label>
+ <input wicket:id="fileInput" type="file"/>
+ </p>
+ <a wicket:id="upload" type="button">Upload to a resource</a>
+ <input wicket:id="cancelUpload" type="button" value="Cancel upload"/>
+ </fieldset>
+ </div>
+
+ <div>
+ <span wicket:id="uploadFeedback"/>
+ </div>
+
+</wicket:extend>
+</body>
+</html>
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java
new file mode 100644
index 0000000000..edb68eec69
--- /dev/null
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/upload/UploadToResourcePage.java
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.examples.upload;
+
+import java.io.File;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.markup.html.AjaxLink;
+import org.apache.wicket.examples.WicketExamplePage;
+import org.apache.wicket.markup.html.form.upload.resource.IUploadsFileManager;
+import org.apache.wicket.markup.html.form.upload.resource.SingleFileUploadToResourceField;
+import org.apache.wicket.markup.html.panel.FeedbackPanel;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.util.file.Files;
+
+/**
+ * Upload example.
+ *
+ * @author Eelco Hillenius
+ */
+@SuppressWarnings("serial")
+public class UploadToResourcePage extends WicketExamplePage
+{
+
+ private final FeedbackPanel uploadFeedback;
+
+ private final SingleFileUploadToResourceField singleFileUploadToResourceField;
+
+ private boolean uploading;
+
+ /**
+ * Constructor.
+ *
+ * @param parameters
+ * Page parameters
+ */
+ public UploadToResourcePage(final PageParameters parameters)
+ {
+ singleFileUploadToResourceField = new SingleFileUploadToResourceField("fileInput")
+ {
+
+ @Override
+ protected void onUploadSuccess(AjaxRequestTarget target, String clientFileName)
+ {
+ info("File " + clientFileName + " successfully uploaded. Stored at " + getFile().getAbsolutePath());
+ target.add(uploadFeedback);
+ uploading = false;
+ }
+
+ @Override
+ protected void onUploadFailure(AjaxRequestTarget target, String errorInfo)
+ {
+ info("Upload failed with: " + errorInfo);
+ target.add(uploadFeedback);
+ uploading = false;
+ }
+
+ @Override
+ protected IUploadsFileManager fileManager()
+ {
+ return getUploadsFileManager();
+ }
+ };
+ add(singleFileUploadToResourceField);
+
+ add(new AjaxLink<Void>("upload") {
+ @Override
+ public void onClick(AjaxRequestTarget target) {
+ uploading = true;
+ singleFileUploadToResourceField.startUpload(target);
+ }
+
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+ setVisible(!uploading);
+ }
+ });
+
+ add(new AjaxLink<Void>("cancelUpload") {
+ @Override
+ public void onClick(AjaxRequestTarget target) {
+ singleFileUploadToResourceField.cancelUpload(target);
+ }
+
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+ setVisible(uploading);
+ }
+ });
+ // Create feedback panels
+ uploadFeedback = new FeedbackPanel("uploadFeedback");
+ uploadFeedback.setOutputMarkupId(true);
+
+ // Add uploadFeedback to the page itself
+ add(uploadFeedback);
+
+ }
+
+ @Override
+ protected void onBeforeRender() {
+ super.onBeforeRender();
+ uploading = false;
+ }
+
+ /**
+ * Check whether the file allready exists, and if so, try to delete it.
+ *
+ * @param newFile
+ * the file to check
+ */
+ private void checkFileExists(File newFile)
+ {
+ if (newFile.exists())
+ {
+ // Try to delete the file
+ if (!Files.remove(newFile))
+ {
+ throw new IllegalStateException("Unable to overwrite " + newFile.getAbsolutePath());
+ }
+ }
+ }
+
+ private IUploadsFileManager getUploadsFileManager()
+ {
+ return UploadApplication.getInstance().getUploadsFileManager();
+ }
+}
diff --git a/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html b/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
index 46631905f3..4c339f81c5 100644
--- a/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
+++ b/wicket-examples/src/main/resources/org/apache/wicket/examples/homepage/HomePage.html
@@ -23,6 +23,7 @@
<tr><td align="right"><a href="authorization">authorization</a></td><td> - Demonstrates authorization for pages and components.</td></tr>
<tr><td align="right"><a href="upload/single">upload</a></td><td> - Single file upload.</td></tr>
<tr><td align="right"><a href="upload/multi">upload</a></td><td> - Multiple file upload.</td></tr>
+ <tr><td align="right"><a href="upload/singleUploadToResource">upload</a></td><td> - Single AJAX file upload to a resource.</td></tr>
<tr><td align="right"><a href="template">template</a></td><td> - Templating example.</td></tr>
<tr><td align="right"><a href="mailtemplate">mail template</a></td><td> - Generate mail templates out of Page, Panel or TextTemplate.</td></tr>
<tr><td align="right"><a href="stateless">stateless</a></td><td> - Demonstrates stateless pages/sessions.</td></tr>
diff --git a/wicket-examples/src/main/resources/org/apache/wicket/examples/style.css b/wicket-examples/src/main/resources/org/apache/wicket/examples/style.css
index f27f2c2dac..81fcf9216f 100644
--- a/wicket-examples/src/main/resources/org/apache/wicket/examples/style.css
+++ b/wicket-examples/src/main/resources/org/apache/wicket/examples/style.css
@@ -305,7 +305,7 @@ button.button--alert {
input[type=submit],
input[type=reset],
input[type=button],
-button {
+button, a.button {
background: #FF9925;
color: #fff; }
.button:hover,