java-helidon.server.libraries.se.partsUtils.mustache Maven / Gradle / Ivy
{{!
For v4 and later.
}}
package {{apiPackage}};
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import io.helidon.common.GenericType;
import io.helidon.http.media.multipart.MultiPart;
import io.helidon.http.media.multipart.ReadablePart;
import io.helidon.webserver.http.ServerRequest;
@io.helidon.common.Generated(value = "{{additionalProperties.generatorClass}}",
trigger = "utility",
version = "{{generatorVersion}}")
class PartsUtils {
private final Map parts = new LinkedHashMap<>();
static Map partsMap(ServerRequest request) {
Map parts = new LinkedHashMap<>();
request.content().as(MultiPart.class).forEachRemaining(part -> parts.put(part.name(), part));
return parts;
}
static PartsUtils create(ServerRequest request) {
return new PartsUtils(request);
}
private PartsUtils(ServerRequest request) {
request.content().as(MultiPart.class).forEachRemaining(part -> parts.put(part.name(), part));
}
T part(String partName, Class type, T defaultValue) {
ReadablePart part = parts.get(partName);
if (part == null) {
return defaultValue;
}
return part.as(type);
}
Optional partOpt(String partName, Class type, T defaultValue) {
return Optional.ofNullable(part(partName, type, defaultValue));
}
T part(String partName, GenericType type, T defaultValue) {
ReadablePart part = parts.get(partName);
if (part == null) {
return defaultValue;
}
return part.as(type);
}
Optional partOpt(String partName, GenericType type, T defaultValue) {
return Optional.ofNullable(part(partName, type, defaultValue));
}
InputStream inputStreamPart(String partName) {
ReadablePart part = parts.get(partName);
if (part == null) {
return null;
}
return part.inputStream();
}
Optional inputStreamPartOpt(String partName) {
return Optional.ofNullable(inputStreamPart(partName));
}
File filePart(String partName, Path directory) {
ReadablePart part = parts.get(partName);
if (part == null) {
return null;
}
try (InputStream inputStream = part.inputStream()) {
Path path = directory.resolve(part.fileName().orElse(partName));
inputStream.transferTo(Files.newOutputStream(path, StandardOpenOption.CREATE_NEW));
return path.toFile();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy