[ 
https://issues.apache.org/jira/browse/WICKET-7033?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17712871#comment-17712871
 ] 

ASF GitHub Bot commented on WICKET-7033:
----------------------------------------

solomax commented on code in PR #571:
URL: https://github.com/apache/wicket/pull/571#discussion_r1168122329


##########
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/AbstractFileUploadResource.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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 javax.servlet.http.HttpServletResponse;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadBase;
+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 = "WICKET-FILE-UPLOAD";
+
+       /**
+        * This resource is usually an application singleton. Thus, client side 
pass
+        * to the resource a unique ID identifying the upload field performing 
the upload.
+        * The upload file makes sure this is a unique identifier at 
application level, See
+        * {@link 
FileUploadToResourceField#generateAUniqueApplicationWiseId()}. So that, there 
are no clashes between
+        * different users/pages/sessions performing an upload.
+        */
+       public static final String UPLOAD_ID = "uploadId";
+       /**
+        * i18n key for case no files were selected.
+        */
+       public static final String NO_FILE_SELECTED = 
"wicket.no.files.selected";
+       /**
+        * i18n key for the case selcted files exceed size limit.
+        */
+       public static final String REQUEST_SIZE_LIMIT_EXCEEDED = 
"wicket.multipart.size.exceeded";
+
+       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();
+
+               // get the ID of the upload field (it should be unique per 
application)
+               String uploadId = 
webRequest.getRequestParameters().getParameterValue(UPLOAD_ID).toString("resource");
+
+               try
+               {
+                       MultipartServletWebRequest multiPartRequest = 
webRequest.newMultipartWebRequest(getMaxSize(), uploadId);

Review Comment:
   FileCountLimitExceededException exception should also be handled,
   
   Please see ade93326346e20587242b501422e43ae6937a843





> add support to uploading to a resource
> --------------------------------------
>
>                 Key: WICKET-7033
>                 URL: https://issues.apache.org/jira/browse/WICKET-7033
>             Project: Wicket
>          Issue Type: New Feature
>          Components: wicket
>            Reporter: Ernesto Reinaldo Barreiro
>            Assignee: Ernesto Reinaldo Barreiro
>            Priority: Major
>             Fix For: 10.0.0, 9.14.0
>
>
> Add support for the following:
> * Upload to a resource in an asynchronous non page blocking request
> * Add an optional way to block the user from leaving the page while the 
> upload is happening 
> * Ways to cancel the upload
> * Adapt the upload progress bar to work with this new "component" and improve 
> its code as in some corner cases it is producing client side errors (I 
> created an issue for that some time ago). 
> * Maybe useful too:  create a web socket based progress bar, as the upload 
> progress bar now works pulling the server every second. 
> * Also to add an example to wicket-examples that uses a smart JS uploader, 
> like in the blog 
> (https://github.com/martin-g/blogs/blob/master/file-upload/). This way you 
> will verify that the new APIs are easily extendable.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to