Make uploads to a resource fail cleanly, keep files of one name apart, and add a drop zone and a file list (#1671)
* Make uploads to a resource fail cleanly and keep files of one name apart
GitHub issue #1670
FileUploadToResourceField and the resource it uploads to had several
problems:
- A failed upload ended on the error page instead of in
onUploadFailure(). For a failure other than a limit the resource answers
the key uploadFailed, whose text expects an exception in
${exception.localizedMessage}; the field put the key, a String, in its
place, and the interpolation threw. Such failures now have a text of
their own, uploadToResourceFailed ("The upload failed.").
- The field took whatever the browser reported as a resource key. The
status text of an HTTP error, or the exception message the resource sent
with debug logging on, threw MissingResourceException, and an empty one
(HTTP/2, a network failure) read "File must be less than ...". The field
now accepts the keys of the limits, of no files selected and of a lost
connection, and reads anything else as a failed upload. The script
reports HTTP errors, answers that are not JSON and requests that time
out as uploadFailed, and a request that got no answer as
uploadConnectionLost, which the field
words "The upload failed: the connection to the server was lost.". The
check is the protected getUploadErrorMessage(String), so a subclass
whose own script reports other keys can word them; the field still
never looks up a key it does not know, since the browser can send any.
The resource no longer sends exception messages to the browser; it logs
the exception as an error, which it did not do at all before.
- The script checks the limits of the field before sending: files beyond
them are reported with the key of the limit and not sent. Before, the
server refused them, often by resetting the connection, which the
browser saw as a network failure.
- A second upload started while one ran sent a second request, which
shared the progress information of the first in the session. Whichever
ended first removed it, and the other failed with an
IllegalStateException. The script now refuses to start an upload while
one of the field runs.
- The script reported the received files to the field by GET, with their
JSON in the URL. Beyond some 65 files that exceeded the 8 KB request line
of Jetty and Tomcat: the server answered 414 and onUploadSuccess() was
never called, although the files were stored. They are reported by POST
now.
- FolderUploadsFileManager stored each file under its client file name, so
two files of one name, in one upload or in two, overwrote each other
while the field was told of both. Files are now stored under a random
name, which the resource sends to the browser with the client file name
and UploadInfo carries back to the field.
- cancel() after an upload had ended still claimed to cancel it, the
return value of the before-send callback was ignored, and an answer that
was not JSON left the field waiting, as did a timeout the before-send
callback set on the request. Now cancel() does nothing once the upload
has ended, false from the before-send callback keeps the upload from
being sent and calls onUploadCanceled(), and an answer that is not JSON
or a timeout fails the upload.
The Javadoc of AbstractFileUploadResource said the field uploads a single
file with jQuery; it uploads all the files of its input with a plain
XMLHttpRequest.
Keeping files of one name apart needs a name for each stored file that
travels from the file manager through the browser back to the field, so
the API changes:
- IUploadsFileManager gets store(FileUpload, String), which returns the
name the file was stored under, and getFile(String, String) takes that
name. save() is deprecated, and Wicket no longer calls it. The default
store() calls save() and returns the client file name, so an
implementation written for save() keeps working, with the old behaviour
for files of one name. The default save() throws an
UnsupportedOperationException saying to implement store(): save() used
to be abstract, and with two defaults calling each other an
implementation overriding neither would compile and then fail with a
StackOverflowError.
- AbstractFileUploadResource.saveFiles() returns the stored names, and
prepareResponse() and generateJsonResponse() receive them. These are
hooks for subclasses: an old signature kept as deprecated would no
longer be called, so the signatures change and an override fails to
compile rather than being skipped silently.
- FileUploadResourceReference.generateFileInfo() receives the stored name
and puts it into the JSON as fileName.
- UploadInfo carries the stored name (getFileName() and a new
constructor), and equals() and hashCode() use it instead of the client
file name. The old constructor is deprecated. A JSON answer without
fileName, from a custom resource, falls back to the client file name.
- FileUploadToResourceField gets the constants UPLOAD_FAILED_RESOURCE_KEY
and CONNECTION_LOST_RESOURCE_KEY, and getUploadErrorMessage(String)
becomes protected. Both are additions.
What an application sees after the change: files stored by
FolderUploadsFileManager no longer have their client file name on disk,
so code looking them up with getFile(fieldId, clientFileName) has to use
UploadInfo#getFile() or #getFileName() instead. A failed upload calls
onUploadFailure() with "The upload failed."; an application that had
changed the text of uploadFailed to work around the error page changes
uploadToResourceFailed instead. An upload whose connection breaks off
reads "The upload failed: the connection to the server was lost.", and
so does one the server refuses by resetting the connection, for a limit
the field does not check before sending. The client-side error callback
receives only resource keys as errorMessage, uploadConnectionLost among
them. A before-send callback returning false now keeps the upload from
being sent, and an upload that times out calls the error callback and
onUploadFailure() with uploadFailed. Code calling the deprecated save()
on a manager that implements only store() gets an
UnsupportedOperationException, and calls store() instead.
migration-to-11.md describes the migration in detail: for each change,
what it was before, what it is now, who has to change something and how.
The comment of the MigrateToWicket11 recipe has the short version, as for
the other changes of Wicket 11 that need manual migration. Draft for the
migration guide on the wiki:
Uploads to a resource (FileUploadToResourceField): implement
IUploadsFileManager#store(FileUpload, String) instead of the deprecated
save(). It stores the file under a name of its own and returns that
name, which getFile(uploadFieldId, fileName) then receives; the name
goes to the browser and back, so getFile() must not return a file of
another upload field. FolderUploadsFileManager stores files under random
names: find them with UploadInfo#getFile() or UploadInfo#getFileName(),
not by client file name. Subclasses of AbstractFileUploadResource
receive the stored names in saveFiles(), prepareResponse() and
generateJsonResponse(), subclasses of FileUploadResourceReference in
generateFileInfo(). A failed upload reads "The upload failed." (resource
key uploadToResourceFailed) instead of ending on the error page.
* Add a drop zone and a file list to uploads to a resource
GitHub issue #1669
FileUploadToResourceField uploaded the files of its input as the
browser's dialog left them: each choice replaced the one before, files
could not be dropped, and a file could not be taken out again before the
upload.
Two behaviors add that:
- FileDropZoneBehavior makes a component a zone files are dropped on.
The input receives the dropped files it accepts by its accept
attribute, all of them if it is multiple, else the first; folders are
left out. It then fires its input and change events, as for files
chosen in the dialog. While files are dragged over the zone, it has the
CSS class wicket-upload-dragover.
- FileListBehavior, on a <ul> or <ol>, lists the files chosen for the
next upload, each with a button removing it, whose text is the resource
uploadToResourceRemove. With a list the field collects files: if its
input is multiple, files chosen or dropped are added to the listed ones
instead of replacing them. The files an upload sent leave the list once
it succeeded, while files chosen meanwhile stay; during an upload the
remove buttons are disabled. A file name is written as text, never as
markup.
The script keeps the chosen files and writes them back into the input
through a DataTransfer, so the upload, the progress bar and other
listeners of the input see all of them. It hears of a choice in the
capture phase, before the input's other change listeners run.
A field works with the input it was created for, and draws the lists
only while that input is in the page. The running upload of a field is
kept by the script, not by the field: an Ajax rendering of the field
creates a new field, which takes the upload over. It refuses a second
upload until the first one ended, cancelUpload() cancels it, and when it
ends, the files chosen in the new input stay and the remove buttons are
enabled again. Before, the new field knew nothing of the running upload:
cancelUpload() did not reach it, and a second upload could run next to
it.
The registries of the script, of file lists and of running uploads, are
kept by field id in objects without a prototype, and a list or a drop
zone takes from Wicket.Timer only a field. A field whose markup id names
a property every object has, such as constructor or toString, therefore
works like any other.
A field without these behaviors works as before: a choice replaces the
files of the input, and the input keeps them after the upload.
The script's guard against being evaluated twice tested for an object,
but Wicket.FileUploadToResourceField is a function, so a second
evaluation replaced it. It tests for a function now, as the registries
of file lists and of running uploads live in the script. A test runs the
script a second time and checks that the field and its lists survive it.
The upload-to-resource example shows a drop zone and a file list. Its
styles never applied: they were in a <head> the page ignores, since it
extends WicketExamplePage, and inline, which the Content Security Policy
of the examples blocks. They are in UploadToResourcePage.css now, under
a class name that does not clash with the drop-zone of the examples'
style.css. Its guard against leaving the page during an upload used
jQuery, which the plain JavaScript engine does not load; it uses
Wicket.Event now. Its explanation also says that the limits of the field
are checked before anything is sent, and that files of the same name are
kept apart, each stored under a name of its own.
migration-to-11.md lists the two behaviors among the changes of Wicket
11 that need no migration, and says that a field rendered anew during an
upload refuses a second one and that cancelUpload() cancels it.
Apache Wicket is an open source, java, component based, web application framework. With proper mark-up/logic separation, a POJO data model, and a refreshing lack of XML, Apache Wicket makes developing web-apps simple and enjoyable again. Swap the boilerplate, complex debugging and brittle code for powerful, reusable components written with plain Java and HTML.
Apache Wicket can be found at https://wicket.apache.org and is licensed under the Apache Software Foundation license, version 2.0.
The Wicket project has several resources and projects where you can learn from, and get started quickly:
The Wicket user guide - https://wicket.apache.org/learn/#guide:
learn Wicket from scratch reading its user guide which gradually introduces you to the various features of the framework with many real-world examples.
The Wicket JavaDoc:
the API Docs are available on the main site of the project: https://wicket.apache.org/learn/#javadoc
Wicket Examples:
shows all components in short usage examples, also available live on: https://examples10x.wicket.apache.org/index.html
Wicket Quickstart - https://wicket.apache.org/start/quickstart.html:
provides a skeleton project for use in NetBeans, Eclipse, IntelliJ IDEA and other major IDE‘s, without having to configure anything yourself. Just copy’n'paste the generated command line and Maven will do the job.
You can download Wicket's source package here: https://wicket.apache.org/start/wicket-10.x.html . It contains the source code and the jars of the core projects of Wicket. If you are just starting out, you probably only need to include wicket-util-x.jar, wicket-request-x.jar and wicket-core-x.jar, where x stands for the version. As a rule, use just the jars you need.
You will find the source code here:
|-- apidocs
| |-- org
| `-- resources
|-- lib
|-- licenses
`-- src
|-- archetypes
|-- testing
|-- wicket
|-- wicket-auth-roles
|-- wicket-bean-validation
|-- wicket-cdi
|-- wicket-cdi-tests
|-- wicket-core
|-- wicket-tester
|-- wicket-core-tests
|-- wicket-devutils
|-- wicket-eclipse-settings
|-- wicket-examples
|-- wicket-experimental
| |-- wicket-metrics
| |-- wicket-http2
|-- wicket-extensions
|-- wicket-guice
|-- wicket-ioc
|-- wicket-jmx
|-- wicket-native-websocket
|-- wicket-objectssizeof-agent
|-- wicket-request
|-- wicket-spring
|-- wicket-util
|-- wicket-user-guide
`-- wicket-velocity
Here is a list of projects in the distribution and what they do.
The easiest way of getting the dependencies of your Wicket based projects right is to use Apache Maven (https://maven.apache.org) with your projects and include the wicket dependencies you want as outlined in the wicket-quickstart. Maven will then take care of including the appropriate dependencies.
If you do not want to use Maven, here is a break-down of the dependencies you need. For the complete and precise reference see the wicket-parent pom.xml in the root folder.
wicket and wicket-extensions:
You only need to include the Jakarta Servlet API (5, just for compiling), SLF4J API and the SLF4J logging implementation you want. You cannot use Wicket without adding a SLF4J logging implementation to your classpath. Please see the SLF4J site (https://www.slf4j.org/) for more information.
As the following projects all depend on Wicket, they inherit these dependencies.
wicket-velocity:
Apache Velocity 1.7 (https://velocity.apache.org/) and it's dependencies (it ships a velocity-deps jar for convenience)
wicket-ioc:
byte-buddy 1.11.12 (https://bytebuddy.net/) and asm-util 9.1 (https://asm.ow2.io/)
wicket-spring:
wicket-ioc and Spring (https://spring.io/projects/spring-framework/) and it's dependencies
wicket-guice:
Google Guice (https://github.com/google/guice)
wicket-cdi: Component Dependency Injection 2.0 (https://cdi-spec.org/)
wicket-examples:
All of the above.
Wicket's source distribution (download package mentioned above) contains also the binaries (jar files) for each of its modules (subprojects). You can use these directly in your applications. The Wicket project uploads the source and JavaDoc jars to the Maven repository used by the Maven build tool as well. So there is actually no specific need to build Wicket yourself from the distribution.
When building using Maven 3, execute one of the following in the root folder:
mvn package
creates wicket-(subproject)-x.y.z.jar(s) in according target subdirectories.
mvn install
creates wicket-(subproject)-x.y.z.jar(s) in according target subdirectories and installs the jar files into your local Maven repository for use in other projects.
Coverage is measured with JaCoCo and aggregated into a single report, because most Wicket tests live in a module other than the code they exercise (the tests for wicket-core are in wicket-core-tests, for example). To produce it:
mvn clean verify -Pcoverage
writes the aggregated report to wicket-coverage/target/site/jacoco-aggregate/index.html
To build only the modules that feed the report, which is considerably faster:
Note that a build without “clean” merges the previous run's data into the new report, because the JaCoCo agent appends by default.
Every push and pull request also uploads this report to https://app.codecov.io/gh/apache/wicket. Coverage is reported there, never enforced: no coverage check can fail a build or block a merge.
This file is a copy of the migration guide available on our Wiki:
https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+10.0
Read the online documentation available on our website (https://wicket.apache.org)
Read the migration guide above
Read the mailing archives available on Nabble, GMane and Apache
Send a complete message containing your problem, stacktrace and problem you're trying to solve to our user list (users@wicket.apache.org)
Ask a question on IRC at freenode.net, channel ##wicket
Wicket is distributed under the terms of the Apache Software Foundation license, version 2.0. The text is included in the file LICENSE in the root of the project.
Wicket 10 requires at least Java 17. The application server for running your web application should adhere to the Jakarta Servlet 5 specification or newer.
This distribution includes cryptographic software. The country in which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check your country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted. See http://www.wassenaar.org for more information.
The U.S. Government Department of Commerce, Bureau of Industry and Security (BIS), has classified this software as Export Commodity Control Number (ECCN) 5D002.C.1, which includes information security software using or performing cryptographic functions with asymmetric algorithms. The form and manner of this Apache Software Foundation distribution makes it eligible for export under the License Exception ENC Technology Software Unrestricted (TSU) exception (see the BIS Export Administration Regulations, Section 740.13) for both object code and source code.
The following provides more details on the included cryptographic software:
For encoding HTTP URL data (see org.apache.wicket.core.request.mapper.CryptoMapper) Wicket requires the Java Cryptography extensions (http://java.sun.com/javase/technologies/security/). Wicket does not include these libraries itself, but is designed to use them.