Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created an example with Jakarta REST 3.1 SeBootstrap & Multipart #5398

Merged
merged 1 commit into from Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions examples/multipart-webapp/README.MD
@@ -1,4 +1,4 @@
[//]: # " Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved. "
[//]: # " Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved. "
[//]: # " "
[//]: # " This program and the accompanying materials are made available under the "
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
Expand All @@ -10,20 +10,23 @@ Multipart Web app Example
=========================

This example demonstrates how to develop RESTful web service with
demonstrating JAX-RS Integration with MIME MultiPart Message Formats and
an Jakarta EE 9 compliant Web container.
demonstrating Jakarta-REST Integration with MIME MultiPart Message Formats and
a Jakarta EE 9 compliant Web container.

Please see also comparable example demonstrating usage of Jakarta REST 3.1 API for
MIME MultiPart Message Formats (JERSEY_ROOT/examples/rest31-sebootstrap-multipart)

Contents
--------

The mapping of the URI path space is presented in the following table:

URI path | Description | Sample request using curl
---------------------------- | ---------------------------------------------- | -----------------------------------------------------------------------------------------------
**_/form/part_** | POST message returning entire string | `curl -X POST -F "part=part1" http://localhost:8080/multipart-webapp/form/part`
**_/form/part-file-name_** | POST message returning part filename string. | Be sure to execute this curl from project directory where pom.xml resides
| | `curl -X POST -F "part=@pom.xml" http://localhost:8080/multipart-webapp/form/part-file-name`
**_/form/xml-jaxb-part_** | POST message returning xml jaxb part string. | No curl sample available, please check test sources.
URI path | Description | Sample request using curl
----------------------------|----------------------------------------------| -----------------------------------------------------------------------------------------------
**_/form/part_** | POST message returning entire string | `curl -X POST -F "part=part1" http://localhost:8080/multipart-webapp/form/part`
**_/form/part-file-name_** | POST message returning part filename string. | Be sure to execute this curl from project directory where pom.xml resides
| | | `curl -X POST -F "part=@pom.xml" http://localhost:8080/multipart-webapp/form/part-file-name`
**_/form/xml-jaxb-part_** | POST message returning xml jaxb part string. | No curl sample available, please check test sources.

Running the Example
-------------------
Expand All @@ -32,4 +35,4 @@ You can run the example using Jetty as follows:

> `mvn clean package jetty:run`

Following steps are using [cURL](http://curl.haxx.se/) command line tool:
The sample requests are using [cURL](http://curl.haxx.se/) command line tool.
1 change: 1 addition & 0 deletions examples/pom.xml
Expand Up @@ -105,6 +105,7 @@
<module>oauth-client-twitter</module>
<!--<module>oauth2-client-google-webapp</module>-->
<module>reload</module>
<module>rest31-sebootstrap-multipart</module>
<module>rx-client-webapp</module>
<module>server-async</module>
<module>server-async-managed</module>
Expand Down
37 changes: 37 additions & 0 deletions examples/rest31-sebootstrap-multipart/README.MD
@@ -0,0 +1,37 @@
[//]: # " Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. "
[//]: # " "
[//]: # " This program and the accompanying materials are made available under the "
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. "
[//]: # " "
[//]: # " SPDX-License-Identifier: BSD-3-Clause "

Multipart Web app Example
=========================

This example demonstrates how to develop RESTful web service with
demonstrating Jakarta REST Integration with MIME MultiPart Message Formats and
Jakarta REST EE 10 SeBootstrap functionality.

Feel free to compare with pre-Jakarta REST 3.1 Jersey Multipart example (JERSEY_ROOT/examples/multipart-webapp).

Contents
--------

The mapping of the URI path space is presented in the following table:

URI path | Description | Sample request using curl
----------------------------|----------------------------------------------| -----------------------------------------------------------------------------------------------
**_/form/part_** | POST message returning entire string | `curl -X POST -F "part=part1" http://localhost:8080/multipart-webapp/form/part`
**_/form/part-file-name_** | POST message returning part filename string. | Be sure to execute this curl from project directory where pom.xml resides
| | | `curl -X POST -F "part=@pom.xml" http://localhost:8080/multipart-webapp/form/part-file-name`
**_/form/xml-jaxb-part_** | POST message returning xml jaxb part string. | No curl sample available, please check test sources.

Running the Example
-------------------

You can run the example using Jetty as follows:

> `mvn clean package exec:java`

The sample requests are using [cURL](http://curl.haxx.se/) command line tool.
107 changes: 107 additions & 0 deletions examples/rest31-sebootstrap-multipart/pom.xml
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v. 1.0, which is available at
http://www.eclipse.org/org/documents/edl-v10.php.

SPDX-License-Identifier: BSD-3-Clause

-->

<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>org.glassfish.jersey.examples</groupId>
<artifactId>project</artifactId>
<version>3.1.99-SNAPSHOT</version>
</parent>

<artifactId>rest31-sebootstrap-multipart</artifactId>
<name>jersey-examples-multipart-webapp</name>
<packaging>jar</packaging>

<description>Jersey SeBootstrap Multipart example.</description>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-osgi</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>org.glassfish.jersey.examples.multipart.webapp.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>pre-release</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.multipart.webapp;

import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Bean {

public String value;

public Bean() {
}

public Bean(String str) {
value = str;
}

}
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.multipart.webapp;

import jakarta.ws.rs.SeBootstrap;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
final SeBootstrap.Configuration.Builder bootstrapConfigurationBuilder = SeBootstrap.Configuration.builder();
bootstrapConfigurationBuilder.property(SeBootstrap.Configuration.PORT, 8080);

SeBootstrap.start(new MyApplication(), bootstrapConfigurationBuilder.build())
.whenComplete((instance1, throwable) -> {
try {
System.out.println("Press enter to exit");
System.in.read();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).thenAccept((i) -> i.stop())

.toCompletableFuture().get();

System.out.println("Exiting...");
}
}
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.multipart.webapp;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.EntityPart;
import jakarta.ws.rs.core.MediaType;

import java.io.IOException;

@Path("/form")
public class MultiPartResource {

@GET
@Path("test")
public String test() {
return "Test successful";
}

@POST
@Path("part")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String post(@FormParam("part") String s) {
return s;
}

@POST
@Path("part-file-name")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String post(@FormParam("part")EntityPart entityPart) throws IOException {
return entityPart.getContent(String.class) + ":" + entityPart.getFileName().get();
}

@POST
@Path("xml-jaxb-part")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String post(
@FormParam("string") EntityPart stringEntityPart,
@FormParam("bean") EntityPart beanEntityPart) throws IOException {
return stringEntityPart.getContent(String.class) + ":" + stringEntityPart.getFileName().get() + ","
+ beanEntityPart.getContent(Bean.class).value + ":" + beanEntityPart.getFileName().get();
}
}
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.multipart.webapp;

import jakarta.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/multipart-webapp")
public class MyApplication extends ResourceConfig {

public MyApplication() {
super(MultiPartResource.class);
}
}