Repository: wicket Updated Branches: refs/heads/WICKET-6517-multipart-ajax 6ad44176a -> c92f1cc6a
WICKET-6517 moved behavior out of markup package Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/c92f1cc6 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/c92f1cc6 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/c92f1cc6 Branch: refs/heads/WICKET-6517-multipart-ajax Commit: c92f1cc6ae48a7cb43d596113be2139e1d3f68f4 Parents: 6ad4417 Author: Sven Meier <svenme...@apache.org> Authored: Fri Jan 19 09:22:35 2018 +0100 Committer: Sven Meier <svenme...@apache.org> Committed: Fri Jan 19 09:22:35 2018 +0100 ---------------------------------------------------------------------- .../examples/ajax/builtin/FileUploadPage.java | 2 +- .../extensions/ajax/AjaxFileDropBehavior.java | 211 +++++++++++++++++++ .../ajax/markup/html/AjaxFileDropBehavior.java | 211 ------------------- .../extensions/ajax/markup/html/datatransfer.js | 54 ----- .../wicket/extensions/ajax/wicket-ajaxupload.js | 54 +++++ 5 files changed, 266 insertions(+), 266 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/c92f1cc6/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java index f7a473c..598efee 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java @@ -22,7 +22,7 @@ import org.apache.commons.fileupload.FileUploadException; import org.apache.wicket.Component; import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.ajax.markup.html.form.AjaxButton; -import org.apache.wicket.extensions.ajax.markup.html.AjaxFileDropBehavior; +import org.apache.wicket.extensions.ajax.AjaxFileDropBehavior; import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; http://git-wip-us.apache.org/repos/asf/wicket/blob/c92f1cc6/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java new file mode 100644 index 0000000..0501610 --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java @@ -0,0 +1,211 @@ +/* + * 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.extensions.ajax; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.wicket.Component; +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.ajax.AjaxEventBehavior; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.attributes.AjaxCallListener; +import org.apache.wicket.ajax.attributes.AjaxRequestAttributes; +import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method; +import org.apache.wicket.core.util.string.CssUtils; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.markup.head.JavaScriptHeaderItem; +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.resource.PackageResourceReference; +import org.apache.wicket.request.resource.ResourceReference; +import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.lang.Bytes; + +/** + * Uploads files from a drop event. + * + * @author Andrew Kondratev + * @author svenmeier + */ +public class AjaxFileDropBehavior extends AjaxEventBehavior +{ + + public static final String DRAG_OVER_CLASS_KEY = CssUtils.key(AjaxFileDropBehavior.class, "dragover"); + + private static final ResourceReference JS = new PackageResourceReference( + AjaxFileDropBehavior.class, "wicket-ajaxupload.js"); + + /** + * Maximum size of all uploaded files in bytes in a request. + */ + private Bytes maxSize; + + /** + * Maximum size of file of upload in bytes (if there are more than one) in a request. + */ + private Bytes fileMaxSize; + + private String parameterName = "f"; + + /** + * Listen for 'dragover' and 'drop' events and prevent them, only 'drop' will initiate + * an Ajax request. + */ + public AjaxFileDropBehavior() + { + super("dragenter dragover dragleave drop"); + } + + @Override + public void renderHead(Component component, IHeaderResponse response) + { + super.renderHead(component, response); + + response.render(JavaScriptHeaderItem.forReference(JS)); + } + + @Override + protected void updateAjaxAttributes(AjaxRequestAttributes attributes) + { + super.updateAjaxAttributes(attributes); + + attributes.setMultipart(true); + attributes.setMethod(Method.POST); + // default must be prevented, otherwise browser will consume the dataTransfer + attributes.setPreventDefault(true); + + attributes.getAjaxCallListeners().add(new AjaxCallListener() { + @Override + public CharSequence getPrecondition(Component component) + { + String css = getComponent().getString(DRAG_OVER_CLASS_KEY); + + return String.format("jQuery('#' + attrs.c).toggleClass('%s', attrs.event.type === 'dragover'); return (attrs.event.type === 'drop');", css); + } + }); + + attributes.getDynamicExtraParameters() + .add(String.format( + "return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", + parameterName)); + } + + @Override + protected void onEvent(AjaxRequestTarget target) + { + try + { + ServletWebRequest request = (ServletWebRequest)getComponent().getRequest(); + final MultipartServletWebRequest multipartWebRequest = request + .newMultipartWebRequest(getMaxSize(), getComponent().getPage().getId()); + multipartWebRequest.setFileMaxSize(getFileMaxSize()); + multipartWebRequest.parseFileParts(); + + // TODO: Can't this be detected from header? + getComponent().getRequestCycle().setRequest(multipartWebRequest); + + ArrayList<FileUpload> fileUploads = new ArrayList<>(); + + // Get the item for the path + final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName); + + if (fileItems != null) + { + for (FileItem item : fileItems) + { + fileUploads.add(new FileUpload(item)); + } + } + + onFileUpload(target, fileUploads); + } + catch (final FileUploadException fux) + { + onError(target, fux); + } + } + + public Bytes getMaxSize() + { + if (maxSize == null) + { + maxSize = getComponent().getApplication().getApplicationSettings() + .getDefaultMaximumUploadSize(); + } + return maxSize; + } + + /** + * Set the maximum upload size. + * + * @param maxSize maximum size, must not be null + */ + public void setMaxSize(Bytes maxSize) + { + Args.notNull(maxSize, "maxSize"); + this.maxSize = maxSize; + } + + public Bytes getFileMaxSize() + { + return fileMaxSize; + } + + /** + * Set an optional maximum size per file. + * + * @param fileMaxSize maximum size for each uploaded file + */ + public void setFileMaxSize(Bytes fileMaxSize) + { + this.fileMaxSize = fileMaxSize; + } + + /** + * Hook method called after a file was uploaded. + * <p> + * Note: {@link #onError(AjaxRequestTarget, FileUploadException)} is called instead when + * uploading failed + * + * @param target + * the current request handler + * @param files + * uploaded files + */ + protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files) + { + } + + /** + * Hook method called to handle any error during uploading of the file. + * <p> + * Default implementation re-throws the exception. + * + * @param target + * the current request handler + * @param e + * the error that occurred + */ + protected void onError(AjaxRequestTarget target, FileUploadException fux) + { + throw new WicketRuntimeException(fux); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/c92f1cc6/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java deleted file mode 100644 index 817c2f4..0000000 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java +++ /dev/null @@ -1,211 +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.extensions.ajax.markup.html; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.fileupload.FileUploadException; -import org.apache.wicket.Component; -import org.apache.wicket.WicketRuntimeException; -import org.apache.wicket.ajax.AjaxEventBehavior; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.ajax.attributes.AjaxCallListener; -import org.apache.wicket.ajax.attributes.AjaxRequestAttributes; -import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method; -import org.apache.wicket.core.util.string.CssUtils; -import org.apache.wicket.markup.head.IHeaderResponse; -import org.apache.wicket.markup.head.JavaScriptHeaderItem; -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.resource.PackageResourceReference; -import org.apache.wicket.request.resource.ResourceReference; -import org.apache.wicket.util.lang.Args; -import org.apache.wicket.util.lang.Bytes; - -/** - * Uploads files from a drop event. - * - * @author Andrew Kondratev - * @author svenmeier - */ -public class AjaxFileDropBehavior extends AjaxEventBehavior -{ - - public static final String DRAG_OVER_CLASS_KEY = CssUtils.key(AjaxFileDropBehavior.class, "dragover"); - - private static final ResourceReference JS = new PackageResourceReference( - AjaxFileDropBehavior.class, "datatransfer.js"); - - /** - * Maximum size of all uploaded files in bytes in a request. - */ - private Bytes maxSize; - - /** - * Maximum size of file of upload in bytes (if there are more than one) in a request. - */ - private Bytes fileMaxSize; - - private String parameterName = "f"; - - /** - * Listen for 'dragover' and 'drop' events and prevent them, only 'drop' will initiate - * an Ajax request. - */ - public AjaxFileDropBehavior() - { - super("dragenter dragover dragleave drop"); - } - - @Override - public void renderHead(Component component, IHeaderResponse response) - { - super.renderHead(component, response); - - response.render(JavaScriptHeaderItem.forReference(JS)); - } - - @Override - protected void updateAjaxAttributes(AjaxRequestAttributes attributes) - { - super.updateAjaxAttributes(attributes); - - attributes.setMultipart(true); - attributes.setMethod(Method.POST); - // default must be prevented, otherwise browser will consume the dataTransfer - attributes.setPreventDefault(true); - - attributes.getAjaxCallListeners().add(new AjaxCallListener() { - @Override - public CharSequence getPrecondition(Component component) - { - String css = getComponent().getString(DRAG_OVER_CLASS_KEY); - - return String.format("jQuery('#' + attrs.c).toggleClass('%s', attrs.event.type === 'dragover'); return (attrs.event.type === 'drop');", css); - } - }); - - attributes.getDynamicExtraParameters() - .add(String.format( - "return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", - parameterName)); - } - - @Override - protected void onEvent(AjaxRequestTarget target) - { - try - { - ServletWebRequest request = (ServletWebRequest)getComponent().getRequest(); - final MultipartServletWebRequest multipartWebRequest = request - .newMultipartWebRequest(getMaxSize(), getComponent().getPage().getId()); - multipartWebRequest.setFileMaxSize(getFileMaxSize()); - multipartWebRequest.parseFileParts(); - - // TODO: Can't this be detected from header? - getComponent().getRequestCycle().setRequest(multipartWebRequest); - - ArrayList<FileUpload> fileUploads = new ArrayList<>(); - - // Get the item for the path - final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName); - - if (fileItems != null) - { - for (FileItem item : fileItems) - { - fileUploads.add(new FileUpload(item)); - } - } - - onFileUpload(target, fileUploads); - } - catch (final FileUploadException fux) - { - onError(target, fux); - } - } - - public Bytes getMaxSize() - { - if (maxSize == null) - { - maxSize = getComponent().getApplication().getApplicationSettings() - .getDefaultMaximumUploadSize(); - } - return maxSize; - } - - /** - * Set the maximum upload size. - * - * @param maxSize maximum size, must not be null - */ - public void setMaxSize(Bytes maxSize) - { - Args.notNull(maxSize, "maxSize"); - this.maxSize = maxSize; - } - - public Bytes getFileMaxSize() - { - return fileMaxSize; - } - - /** - * Set an optional maximum size per file. - * - * @param fileMaxSize maximum size for each uploaded file - */ - public void setFileMaxSize(Bytes fileMaxSize) - { - this.fileMaxSize = fileMaxSize; - } - - /** - * Hook method called after a file was uploaded. - * <p> - * Note: {@link #onError(AjaxRequestTarget, FileUploadException)} is called instead when - * uploading failed - * - * @param target - * the current request handler - * @param files - * uploaded files - */ - protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files) - { - } - - /** - * Hook method called to handle any error during uploading of the file. - * <p> - * Default implementation re-throws the exception. - * - * @param target - * the current request handler - * @param e - * the error that occurred - */ - protected void onError(AjaxRequestTarget target, FileUploadException fux) - { - throw new WicketRuntimeException(fux); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/c92f1cc6/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js deleted file mode 100644 index 05ff68a..0000000 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js +++ /dev/null @@ -1,54 +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. - */ - -;(function (undefined) { - 'use strict'; - - if (typeof(Wicket) === "undefined") { - window.Wicket = {}; - } - - if (Wicket.DataTransfer) { - return; - } - - Wicket.DataTransfer = { - getFilesAsParamArray : function(ev, name) { - var files = []; - - function pushFile(file) { - files.push({'name' : name, 'value' : file}); - }; - - var dataTransfer = ev.dataTransfer; - var i; - if (dataTransfer.items) { - for (i = 0; i < dataTransfer.items.length; i++) { - if (dataTransfer.items[i].kind == 'file') { - pushFile(dataTransfer.items[i].getAsFile()); - } - } - } else { - for (i = 0; i < dataTransfer.files.length; i++) { - pushFile(dataTransfer.files[i]); - } - } - - return files; - } - }; -})(); http://git-wip-us.apache.org/repos/asf/wicket/blob/c92f1cc6/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js new file mode 100644 index 0000000..05ff68a --- /dev/null +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js @@ -0,0 +1,54 @@ +/* + * 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) === "undefined") { + window.Wicket = {}; + } + + if (Wicket.DataTransfer) { + return; + } + + Wicket.DataTransfer = { + getFilesAsParamArray : function(ev, name) { + var files = []; + + function pushFile(file) { + files.push({'name' : name, 'value' : file}); + }; + + var dataTransfer = ev.dataTransfer; + var i; + if (dataTransfer.items) { + for (i = 0; i < dataTransfer.items.length; i++) { + if (dataTransfer.items[i].kind == 'file') { + pushFile(dataTransfer.items[i].getAsFile()); + } + } + } else { + for (i = 0; i < dataTransfer.files.length; i++) { + pushFile(dataTransfer.files[i]); + } + } + + return files; + } + }; +})();