reiern70 commented on code in PR #571: URL: https://github.com/apache/wicket/pull/571#discussion_r1163719410
########## wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/AbstractFileUploadResource.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.commons.fileupload.FileUploadBase; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.wicket.WicketRuntimeException; +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 (FileUploadException e) + { + if (e instanceof FileUploadBase.FileSizeLimitExceededException || e instanceof FileUploadBase.SizeLimitExceededException) + { + resourceResponse.setError(413, "File size exceeded!"); + return resourceResponse; + } + throw new WicketRuntimeException(e); + } + 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()); Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
