-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
9 changed files
with
540 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
= Standalone multipart/form-data Example | ||
|
||
In https://jakarta.ee/specifications/restful-ws/3.1/[Jakarta RESTful Web Services 3.1] the `SeBootstrap` API was | ||
introduced as well as the `EntityPart` API for multipart data. This example shows how to use these API's with RESTEasy. | ||
|
||
== Building | ||
|
||
To build the `standalone-multipart` quickstart you must have https://maven.apache.org/[Maven] installed and at least | ||
Java 11. Then you simply need to run the following: | ||
|
||
---- | ||
mvn clean verify | ||
---- | ||
|
||
This will create a `standalone-multipart.jar` which can be executed from the command line. A test is also executed as | ||
part of the build. | ||
|
||
== Running the Quickstart | ||
|
||
The `standalone-multipart.jar` created can be executed from the command. | ||
|
||
---- | ||
java -jar target/standalone-multipart.jar | ||
---- | ||
|
||
This will start an Undertow container with RESTEasy and CDI support. Then make a multipart/form-data request and print | ||
the results of the request. You should end up seeing something like: | ||
|
||
--- | ||
Container running at http://localhost:8081/ | ||
OK | ||
{"name":"RESTEasy","data":"test content","entity":"entity-part"} | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ JBoss, Home of Professional Open Source. | ||
~ | ||
~ Copyright 2023 Red Hat, Inc., and individual contributors | ||
~ as indicated by the @author tags. | ||
~ | ||
~ Licensed 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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>dev.resteasy.tools</groupId> | ||
<artifactId>resteasy-parent</artifactId> | ||
<version>2.0.3.Final</version> | ||
<relativePath/> | ||
</parent> | ||
|
||
<groupId>dev.resteasy.examples</groupId> | ||
<artifactId>standalone-multipart</artifactId> | ||
<version>6.1.0.Final-SNAPSHOT</version> | ||
<name>RESTEasy Quick Start: Standalone MultiPart Example</name> | ||
|
||
<properties> | ||
<!-- Dependency versions --> | ||
<version.jakarta.enterprise.cdi-api>4.0.1</version.jakarta.enterprise.cdi-api> | ||
<version.jakarta.ws.rs.api>3.1.0</version.jakarta.ws.rs.api> | ||
<version.org.jboss.logmanager>3.0.2.Final</version.org.jboss.logmanager> | ||
<version.org.jboss.resteasy>6.2.5.Final</version.org.jboss.resteasy> | ||
<version.org.junit>5.10.0</version.org.junit> | ||
|
||
<!-- Plugin Versions --> | ||
<version.jandex.maven.plugin>1.2.3</version.jandex.maven.plugin> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-bom</artifactId> | ||
<version>${version.org.jboss.resteasy}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit</groupId> | ||
<artifactId>junit-bom</artifactId> | ||
<version>${version.org.junit}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>jakarta.ws.rs</groupId> | ||
<artifactId>jakarta.ws.rs-api</artifactId> | ||
<version>${version.jakarta.ws.rs.api}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jboss.logmanager</groupId> | ||
<artifactId>jboss-logmanager</artifactId> | ||
<version>${version.org.jboss.logmanager}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-json-p-provider</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-multipart-provider</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.resteasy</groupId> | ||
<artifactId>resteasy-undertow-cdi</artifactId> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>net.revelc.code.formatter</groupId> | ||
<artifactId>formatter-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>net.revelc.code</groupId> | ||
<artifactId>impsort-maven-plugin</artifactId> | ||
</plugin> | ||
<!-- Use Jandex to --> | ||
<plugin> | ||
<groupId>org.jboss.jandex</groupId> | ||
<artifactId>jandex-maven-plugin</artifactId> | ||
<version>${version.jandex.maven.plugin}</version> | ||
<executions> | ||
<execution> | ||
<id>make-index</id> | ||
<goals> | ||
<goal>jandex</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>dev.resteasy.examples.multipart.Main</mainClass> | ||
<packageName>dev.resteasy.quickstart.bootstrap</packageName> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<configuration> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer"> | ||
<resource>module-info.class</resource> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<systemPropertyVariables> | ||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
82 changes: 82 additions & 0 deletions
82
standalone-multipart/src/main/java/dev/resteasy/examples/multipart/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 dev.resteasy.examples.multipart; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import jakarta.ws.rs.SeBootstrap; | ||
import jakarta.ws.rs.client.Client; | ||
import jakarta.ws.rs.client.ClientBuilder; | ||
import jakarta.ws.rs.client.Entity; | ||
import jakarta.ws.rs.core.EntityPart; | ||
import jakarta.ws.rs.core.GenericEntity; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
/** | ||
* An entry point for starting a REST container | ||
* | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
public class Main { | ||
|
||
public static void main(final String[] args) throws Exception { | ||
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager"); | ||
|
||
// Start the container | ||
final SeBootstrap.Instance instance = SeBootstrap.start(RestActivator.class) | ||
.thenApply(i -> { | ||
System.out.printf("Container running at %s%n", i.configuration().baseUri()); | ||
return i; | ||
}).toCompletableFuture().get(); | ||
|
||
// Create the client and make a multipart/form-data request | ||
try (Client client = ClientBuilder.newClient()) { | ||
// Create the entity parts for the request | ||
final List<EntityPart> multipart = List.of( | ||
EntityPart.withName("name") | ||
.content("RESTEasy") | ||
.mediaType(MediaType.TEXT_PLAIN_TYPE) | ||
.build(), | ||
EntityPart.withName("entity") | ||
.content("entity-part") | ||
.mediaType(MediaType.TEXT_PLAIN_TYPE) | ||
.build(), | ||
EntityPart.withName("data") | ||
.content("test content".getBytes(StandardCharsets.UTF_8)) | ||
.mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE) | ||
.build()); | ||
try ( | ||
Response response = client.target(instance.configuration().baseUriBuilder().path("/api/upload")) | ||
.request(MediaType.APPLICATION_JSON_TYPE) | ||
.post(Entity.entity(new GenericEntity<>(multipart) { | ||
}, MediaType.MULTIPART_FORM_DATA_TYPE))) { | ||
printResponse(response); | ||
} | ||
} | ||
} | ||
|
||
private static void printResponse(final Response response) { | ||
System.out.println(response.getStatusInfo()); | ||
System.out.println(response.readEntity(String.class)); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
standalone-multipart/src/main/java/dev/resteasy/examples/multipart/RestActivator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 dev.resteasy.examples.multipart; | ||
|
||
import jakarta.enterprise.inject.Vetoed; | ||
import jakarta.servlet.annotation.MultipartConfig; | ||
import jakarta.ws.rs.ApplicationPath; | ||
import jakarta.ws.rs.core.Application; | ||
|
||
/** | ||
* Activates the REST application. | ||
* | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
@ApplicationPath("/api") | ||
// Currently required to enable multipart/form-data in Undertow see https://issues.redhat.com/browse/RESTEASY-3376 | ||
@MultipartConfig | ||
// See https://issues.redhat.com/browse/RESTEASY-3376 | ||
@Vetoed | ||
public class RestActivator extends Application { | ||
} |
Oops, something went wrong.