papegaaij commented on code in PR #1834: URL: https://github.com/apache/wicket/pull/1834#discussion_r4523026177
########## wicket-experimental/wicket-islands/src/main/java/org/apache/wicket/islands/IslandCallRejectedException.java: ########## @@ -0,0 +1,53 @@ +/* + * 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.islands; + +import java.util.Arrays; +import java.util.List; + +/** + * Thrown to reject an island call. The client's call fails with the kind <code>invalid</code> and + * the given messages; whatever the handler added to the Ajax request target before rejecting is + * still sent. + * + * @see IslandCall#reject(String...) + * @since 12.0.0 + */ +public class IslandCallRejectedException extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + private final List<String> messages; + + /** + * @param messages + * the messages for the client + */ + public IslandCallRejectedException(String... messages) + { + super(String.join("; ", messages), null, false, false); + this.messages = List.copyOf(Arrays.asList(messages)); Review Comment: Fixed in da6964b04b: `null` messages are dropped. ########## wicket-experimental/wicket-islands/src/main/java/org/apache/wicket/islands/FormIsland.java: ########## @@ -0,0 +1,374 @@ +/* + * 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.islands; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.feedback.FeedbackMessage; +import org.apache.wicket.islands.serializer.IslandSerializationException; +import org.apache.wicket.markup.ComponentTag; +import org.apache.wicket.markup.MarkupStream; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.markup.html.form.FormComponent; +import org.apache.wicket.model.IModel; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.string.Strings; +import org.apache.wicket.validation.ValidationError; + +/** + * An island that is a form component: its client-side peer edits a value of type {@code T}, which + * takes part in the form like the value of any other form component, with conversion, validators, + * {@code required}, feedback messages, and normal and Ajax submits. + * <p> + * The value travels as JSON in a hidden input inside the host element. The peer reads and writes + * it through its adapter (e.g. {@code useFormValue()} in React), which also fires + * <code>input</code> and <code>change</code> events, so Ajax behaviors on this component work as + * on any other. Besides its own props, the peer gets the form state: whether the value is + * required, whether the component is enabled, its label, and its feedback messages. + * <p> + * <strong>The submitted value comes from the browser and is untrusted.</strong> It is converted + * into {@code T} by the island serializer, so it can only hold what {@code T} declares, and it is + * validated like any other input. A value that is markup, such as the HTML of a rich text + * editor, must be sanitized before it is ever rendered without escaping; override + * {@link #sanitize(Object)} to apply a sanitizer of your choice. Wicket bundles none. + * <p> + * After an Ajax request that validated it, a form island adds itself to the request target, so + * its feedback messages reach the peer; see {@link #setUpdateOnValidation(boolean)}. + * + * @param <T> + * the type of the value + * @param <P> + * the type of the props + * @since 12.0.0 + */ +public class FormIsland<T, P> extends FormComponent<T> +{ + private static final long serialVersionUID = 1L; + + /** + * The attribute marking the hidden input that holds the value. + */ + public static final String ATTR_VALUE = "data-wicket-island-value"; + + private final IslandSupport island; + + private final Class<T> valueType; + + private final IModel<P> props; + + private boolean updateOnValidation = true; + + /** + * Creates a form island without props of its own. + * + * @param id + * the component id + * @param type + * the island type, naming the client-side module; letters, digits, '-' and '_' + * @param model + * the model of the value + * @param valueType + * the type of the value, which submitted JSON is converted into + */ + public FormIsland(String id, String type, IModel<T> model, Class<T> valueType) + { + this(id, type, model, valueType, null); + } + + /** + * @param id + * the component id + * @param type + * the island type, naming the client-side module; letters, digits, '-' and '_' + * @param model + * the model of the value + * @param valueType + * the type of the value, which submitted JSON is converted into + * @param props + * the model of the props, {@code null} for none + */ + public FormIsland(String id, String type, IModel<T> model, Class<T> valueType, + IModel<P> props) + { + super(id, model); + this.valueType = Args.notNull(valueType, "valueType"); + setType(valueType); + this.props = props; + island = new IslandSupport(type); + setOutputMarkupId(true); + setEscapeModelStrings(false); + add(island.getBehavior()); + } + + /** + * @return the island type + */ + public final String getIslandType() + { + return island.getType(); + } + + /** + * @return the key identifying this island instance across re-renders + */ + public final String getKey() + { + return island.getKey(); + } + + final IslandSupport getIslandSupport() + { + return island; + } + + /** + * Registers a call that returns a result. + * + * @see Island#onCall(String, Class, Class, IslandCallHandler) + */ + public final <Q, R> FormIsland<T, P> onCall(String name, Class<Q> requestType, + Class<R> resultType, IslandCallHandler<Q, R> handler) + { + island.addEndpoint(name, requestType, resultType, handler); + return this; + } + + /** + * Registers a call that returns no result. + * + * @see Island#onCall(String, Class, IslandCallConsumer) + */ + public final <Q> FormIsland<T, P> onCall(String name, Class<Q> requestType, + IslandCallConsumer<Q> handler) + { + island.addEndpoint(name, requestType, Void.class, IslandSupport.withoutResult(handler)); + return this; + } + + /** + * @param updateOnValidation + * whether this island adds itself to the Ajax request target after it was validated, + * so its peer receives its feedback messages; {@code true} by default + * @return {@code this} + */ + public FormIsland<T, P> setUpdateOnValidation(boolean updateOnValidation) + { + this.updateOnValidation = updateOnValidation; + return this; + } + + /** + * Converts the submitted JSON into the value with the island serializer, and passes it through + * {@link #sanitize(Object)}. JSON that does not match the value type is a conversion error. + */ + @Override + public void convertInput() + { + String input = getInput(); + if (Strings.isEmpty(input)) + { + setConvertedInput(null); + return; + } + try + { + setConvertedInput(sanitize(IslandSupport.serializer().fromJson(input, valueType))); + } + catch (IslandSerializationException e) + { + error(new ValidationError().addKey("FormIsland.ConversionError") Review Comment: Fixed in da6964b04b: `FormIsland.utf8.properties` holds `FormIsland.ConversionError=The value of '${label}' is not valid.` ########## wicket-experimental/wicket-islands/pom.xml: ########## @@ -0,0 +1,105 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.wicket.experimental.wicket12</groupId> + <artifactId>wicket-experimental</artifactId> + <version>12.0.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>wicket-islands</artifactId> + <version>12.0.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>Wicket Islands</name> + <description> + Embeds components of JavaScript frameworks such as React, Vue and Angular in Wicket pages, + as client-side peers of Wicket components. + </description> + + <dependencies> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-core</artifactId> + </dependency> + <dependency> + <groupId>tools.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <optional>false</optional> + </dependency> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-tester</artifactId> + </dependency> + </dependencies> + + <profiles> + <profile> + <!-- Type-checks and tests the JavaScript packages in npm/ --> + <id>js-test</id> + <activation> + <activeByDefault>false</activeByDefault> + </activation> + <build> + <plugins> + <plugin> + <groupId>com.github.eirslett</groupId> + <artifactId>frontend-maven-plugin</artifactId> + <version>2.0.2</version> Review Comment: Done in b376eae5b3: the root pom manages the plugin, with `node.version` and `npm.version` properties, for all three modules. ########## wicket-examples/pom.xml: ########## @@ -294,6 +321,78 @@ </build> <profiles> + <profile> + <!-- Builds the JavaScript of the island examples in src/main/frontend --> + <id>js-test</id> + <activation> + <activeByDefault>false</activeByDefault> + </activation> + <build> + <plugins> + <plugin> + <groupId>com.github.eirslett</groupId> + <artifactId>frontend-maven-plugin</artifactId> + <version>2.0.2</version> + <configuration> + <workingDirectory>src/main/frontend</workingDirectory> + </configuration> + <executions> + <execution> + <id>install node and npm</id> + <goals> + <goal>install-node-and-npm</goal> + </goals> + <phase>generate-resources</phase> + <configuration> + <npmVersion>11.12.1</npmVersion> + <nodeVersion>v24.15.0</nodeVersion> + </configuration> + </execution> + <execution> + <id>npm ci</id> + <goals> + <goal>npm</goal> + </goals> + <phase>generate-resources</phase> + <configuration> + <arguments>ci</arguments> Review Comment: Done in b376eae5b3: the islands module and the examples run `npm ci --ignore-scripts`. Both builds and test suites pass without the scripts (esbuild and rollup ship their binaries as optional dependencies). The `Island` Javadoc now says that the npm dependency tree is part of what is trusted. ########## wicket-core/src/main/java/org/apache/wicket/core/request/handler/IPartialPageRequestHandler.java: ########## @@ -99,6 +99,30 @@ public interface IPartialPageRequestHandler extends IPageRequestHandler */ void prependJavaScript(CharSequence javascript); + /** + * Adds a named data entry to the response, for client-side code that subscribed to it. + * <p> + * The value is delivered to the client as text, exactly as given: Wicket neither interprets, + * escapes nor evaluates it. On the client it is published on the topic + * <code>Wicket.Event.Topic.AJAX_CALL_DATA</code> (<code>'/ajax/call/data'</code>) with the + * arguments <code>(attrs, name, value)</code>, where <code>attrs</code> are the attributes of + * the Ajax call. Data entries are published in the order they were added, after the components + * have been replaced and the JavaScripts evaluated, and before the success handlers of the call + * run. + * <p> + * A subscriber that interprets the value, for example as JSON, has to treat it as data and + * never evaluate it as script or insert it as markup. + * + * @param name + * the name of the entry, used by subscribers to recognise it + * @param value + * the value to deliver + * @throws IllegalStateException + * if the response is already being written + * @since 12.0.0 + */ + void addData(String name, CharSequence value); Review Comment: Keeping the abstract method, as the commit message argues. Outside Wicket, `IPartialPageRequestHandler` is rarely implemented directly, and a default that throws would turn a compile error into a runtime one. -- 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]
