-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add microprofile multipart example * Update SE multipart example to add a unit test that uses WebClient. Add an example under microprofile that implements the same application using Jersey. * rethrow IOException using UncheckedIOException
- Loading branch information
1 parent
83b74b5
commit 0c9b513
Showing
18 changed files
with
943 additions
and
60 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Helidon SE MultiPart Example | ||
|
||
This example demonstrates how to use `MultiPartSupport` with both the `WebServer` | ||
and `WebClient` APIs. | ||
|
||
This project implements a simple file service web application that supports uploading | ||
and downloading files. The unit test uses the `WebClient` API to test the endpoints. | ||
|
||
## Build | ||
|
||
``` | ||
mvn package | ||
``` | ||
|
||
## Run | ||
|
||
First, start the server: | ||
|
||
``` | ||
java -jar target/helidon-examples-microprofile-multipart.jar | ||
``` | ||
|
||
Then open <http://localhost:8080/ui> in your browser. |
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
106 changes: 106 additions & 0 deletions
106
examples/media/multipart/src/main/java/io/helidon/examples/media/multipart/FileStorage.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,106 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* 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 io.helidon.examples.media.multipart; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
import io.helidon.webserver.BadRequestException; | ||
import io.helidon.webserver.NotFoundException; | ||
|
||
/** | ||
* Simple bean to managed a directory based storage. | ||
*/ | ||
public class FileStorage { | ||
|
||
private final Path storageDir; | ||
|
||
/** | ||
* Create a new instance. | ||
*/ | ||
public FileStorage() { | ||
try { | ||
storageDir = Files.createTempDirectory("fileupload"); | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
} | ||
|
||
/** | ||
* Get the storage directory. | ||
* | ||
* @return directory | ||
*/ | ||
public Path storageDir() { | ||
return storageDir; | ||
} | ||
|
||
/** | ||
* Get the names of the files in the storage directory. | ||
* | ||
* @return Stream of file names | ||
*/ | ||
public Stream<String> listFiles() { | ||
try { | ||
return Files.walk(storageDir) | ||
.filter(Files::isRegularFile) | ||
.map(storageDir::relativize) | ||
.map(java.nio.file.Path::toString); | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
} | ||
|
||
/** | ||
* Create a new file in the storage. | ||
* | ||
* @param fname file name | ||
* @return file | ||
* @throws BadRequestException if the resolved file is not contained in the storage directory | ||
*/ | ||
public Path create(String fname) { | ||
Path file = storageDir.resolve(fname); | ||
if (!file.getParent().equals(storageDir)) { | ||
throw new BadRequestException("Invalid file name"); | ||
} | ||
return file; | ||
} | ||
|
||
/** | ||
* Lookup an existing file in the storage. | ||
* | ||
* @param fname file name | ||
* @return file | ||
* @throws NotFoundException If the resolved file does not exist | ||
* @throws BadRequestException if the resolved file is not contained in the storage directory | ||
*/ | ||
public Path lookup(String fname) { | ||
Path file = storageDir.resolve(fname); | ||
if (!file.getParent().equals(storageDir)) { | ||
throw new BadRequestException("Invalid file name"); | ||
} | ||
if (!Files.exists(file)) { | ||
throw new NotFoundException("file not found"); | ||
} | ||
if (!Files.isRegularFile(file)) { | ||
throw new BadRequestException("Not a file"); | ||
} | ||
return file; | ||
} | ||
} |
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
Oops, something went wrong.