Skip to content

Commit

Permalink
Add a standalone multipart example.
Browse files Browse the repository at this point in the history
Signed-off-by: James R. Perkins <[email protected]>
  • Loading branch information
jamezp committed Aug 23, 2023
1 parent 3f755f8 commit 9191f3b
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
<module>examples-jsapi</module>
<module>json-binding</module>
<module>microprofile-openapi</module>
<module>servlet-example</module>
<module>smime</module>
<module>standalone-multipart</module>
<module>tracing-example</module>
<module>servlet-example</module>
</modules>
</project>
172 changes: 172 additions & 0 deletions standalone-multipart/pom.xml
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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));
}

}
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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.io.IOException;
import java.io.InputStream;

import jakarta.json.Json;
import jakarta.json.JsonObjectBuilder;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.ServerErrorException;
import jakarta.ws.rs.core.EntityPart;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

/**
* A simple resource for creating a greeting.
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@Path("/")
public class UploadResource {

@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response upload(@FormParam("name") final String name, @FormParam("data") final InputStream data,
@FormParam("entity") final EntityPart entityPart) {
final JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("name", name);

// Read the data into a string
try (data) {
builder.add("data", new String(data.readAllBytes()));
} catch (IOException e) {
throw new ServerErrorException("Failed to read data " + data, Response.Status.BAD_REQUEST);
}
try {
builder.add(entityPart.getName(), entityPart.getContent(String.class));
} catch (IOException e) {
throw new ServerErrorException("Failed to read entity " + entityPart, Response.Status.BAD_REQUEST);
}
return Response.ok(builder.build()).build();
}
}
Loading

0 comments on commit 9191f3b

Please sign in to comment.