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.
25 files changed
tree: 82c37633bda161cd779a31724a9a845f7f9d6168
  1. .github/
  2. archetypes/
  3. licenses/
  4. testing/
  5. wicket/
  6. wicket-auth-roles/
  7. wicket-bean-validation/
  8. wicket-benchmarks/
  9. wicket-cdi/
  10. wicket-cdi-tests/
  11. wicket-core/
  12. wicket-core-tests/
  13. wicket-coverage/
  14. wicket-devutils/
  15. wicket-eclipse-settings/
  16. wicket-examples/
  17. wicket-experimental/
  18. wicket-extensions/
  19. wicket-extensions-tester/
  20. wicket-guice/
  21. wicket-ioc/
  22. wicket-jmx/
  23. wicket-migration/
  24. wicket-native-websocket/
  25. wicket-objectsizeof-agent/
  26. wicket-request/
  27. wicket-spring/
  28. wicket-tester/
  29. wicket-user-guide/
  30. wicket-util/
  31. wicket-velocity/
  32. .asf.yaml
  33. .gitignore
  34. AGENTS.md
  35. build-changelog.sh
  36. build-versions.py
  37. CHANGELOG-10.x
  38. CLAUDE.md
  39. codecov.yml
  40. JAVASCRIPTTESTING.md
  41. LICENSE
  42. migration-to-11.md
  43. NOTICE
  44. pom.xml
  45. README.md
  46. release.sh
  47. SECURITY.md
  48. wicket-assembly-all.xml
README.md

What is Apache Wicket?

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.

Getting started

The Wicket project has several resources and projects where you can learn from, and get started quickly:

What does Wicket's download package contain?

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.

  • wicket-core: the core project, includes the framework and basic components;
  • wicket-tester: contains common classes for unit testing
  • wicket-core-tests: contains test cases for wicket-core module
  • wicket-extensions: contains utilities and more specialized components;
  • wicket-auth-roles: a basic authorization package based on roles;
  • wicket-jmx: registers JMX beans for managing things like your Wicket configuration and markup cache;
  • wicket-objectssizeof-agent: utility for making better estimates of object sizes in the JVM - most people probably never need this;
  • wicket-ioc: base project for IoC (aka DI) implementations such as Spring and Guice;
  • wicket-spring: support project for using Spring with Wicket and including Spring managed dependencies through using @SpringBean annotations;
  • wicket-guice: support project for using Google Guice with Wicket;
  • wicket-velocity: contains special components for rendering Velocity templates using Wicket components - most people probably don't need this, but it can be neat when you want to do CMS-like things;
  • wicket-examples: contains a basic component reference and many examples of how to use Wicket and Wicket components, including examples for subprojects such as wicket-spring, wicket-velocity and wicket-auth-roles.
  • wicket-devutils: provides small utilities which can help in development phase and during debugging
  • wicket-bean-validation: validates beans with annotation based on javax.validation;
  • wicket-cdi: the context and dependency injection of the jee standard for wicket;
  • wicket-experimental: experimental implementations for wicket;
  • wicket-native-websocket: wicket's native web sockets integration for several servers;
  • wicket-request: lightweight project which contains all classes dealing with request handlers and so on;
  • wicket-util: the util project for wicket;
  • wicket-eclipse-settings: specifies Eclipse settings for a uniform development environment. Most notably the formatting rules;
  • wicket-user-guide: the user guide of wicket
  • wicket-metrics: collects data of a running wicket application
  • wicket-http2: http/2 push support

Dependencies

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.

Building Wicket from source

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.

Code coverage

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:

  • mvn clean verify -Pcoverage -pl wicket-coverage -am

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.

Migrating from 9.x

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

Getting help

  • 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

License

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.

Java/Application server requirements

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.

Cryptographic Software Notice

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.