Skip to content

Commit

Permalink
Added support for multiple file uploads.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Sep 16, 2020
1 parent 9653432 commit 2ebba11
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 90 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Replace whenComplete in async.**


[96534322bf6613a](https://github.com/noboomu/proteus/commit/96534322bf6613a) Joshua Bauer *2020-09-15 18:33:35*

**Ensure worker is used for async dispatch.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import java.util.Arrays;
import java.util.Date;
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author jbauer
Expand Down Expand Up @@ -344,6 +346,28 @@ public static Path filePath(final HttpServerExchange exchange, final String name
}
}

public static List<Path> pathList(final HttpServerExchange exchange, final String name) throws IllegalArgumentException
{
try
{
return exchange.getAttachment(FormDataParser.FORM_DATA).get(name).stream().map( i -> i.getFileItem().getFile()).collect(Collectors.toList());
} catch( Exception e )
{
throw new IllegalArgumentException("Missing parameter " + name, e);
}
}

public static List<File> fileList(final HttpServerExchange exchange, final String name) throws IllegalArgumentException
{
try
{
return exchange.getAttachment(FormDataParser.FORM_DATA).get(name).stream().map( i -> i.getFileItem().getFile().toFile()).collect(Collectors.toList());
} catch( Exception e )
{
throw new IllegalArgumentException("Missing parameter " + name, e);
}
}

public static File file(final HttpServerExchange exchange, final String name) throws IllegalArgumentException
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)
exchange.setStatusCode(StatusCodes.BAD_REQUEST);
}


statusCode = exchange.getStatusCode();

errorMap.put("exceptionClass", throwable.getClass().getName());
errorMap.put("message", throwable.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
* Enum that assists in code generation for different method parameter types
*/


public enum TypeHandler
{
public enum TypeHandler {

LongType("Long $L = $T.longValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING),
IntegerType("Integer $L = $T.integerValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING),
Expand Down Expand Up @@ -71,6 +69,10 @@ public enum TypeHandler
BeanListValueOfType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL),
BeanListFromStringType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL),

FileListType("$T $L = io.sinistral.proteus.server.Extractors.fileList(exchange,$S)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.STRING),

PathListType("$T $L = io.sinistral.proteus.server.Extractors.pathList(exchange,$S)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.STRING),

HeaderValueOfType("$T $L = $T.valueOf($T.string(exchange,$S))", false, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.TYPE, io.sinistral.proteus.server.Extractors.Header.class, StatementParameterType.STRING),
HeaderFromStringType("$T $L = $T.fromString($T.string(exchange,$S))", false, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.TYPE, io.sinistral.proteus.server.Extractors.Header.class, StatementParameterType.STRING),
HeaderStringType("$T $L = $T.string(exchange,$S)", false, String.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Header.class, StatementParameterType.STRING),
Expand Down Expand Up @@ -104,7 +106,6 @@ public enum TypeHandler
OptionalDoubleType("$T<Double> $L = $T.doubleValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalBigDecimalType("$T<BigDecimal> $L = $T.bigDecimalValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),


OptionalDateType("$T<$T> $L = $T.date(exchange,$S)", false, Optional.class, java.util.Date.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalInstantType("$T<$T> $L = $T.instant(exchange,$S)", false, Optional.class, java.time.Instant.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
OptionalZonedDateTimeType("$T<$T> $L = $T.zonedDateTime(exchange,$S)", false, Optional.class, java.time.ZonedDateTime.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING),
Expand Down Expand Up @@ -173,7 +174,6 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter,
for (int i = 0; i < handler.parameterTypes.length; i++) {
if (handler.parameterTypes[i] instanceof StatementParameterType) {


if (parameter.isAnnotationPresent(QueryParam.class)) {
QueryParam qp = parameter.getAnnotation(QueryParam.class);
pName = qp.value();
Expand Down Expand Up @@ -229,36 +229,29 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter,

Min min = parameter.isAnnotationPresent(Min.class) ? parameter.getAnnotationsByType(Min.class)[0] : null;


if(max != null || min != null)
{
if(max != null && min != null)
{
if (max != null || min != null) {
if (max != null && min != null) {
long maxValue = min.value();
long minValue = min.value();

builder.beginControlFlow("if( $L < $L )", pName, minValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message());
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message());
builder.endControlFlow();
builder.beginControlFlow("else if( $L > $L )", pName, maxValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message());
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message());
builder.endControlFlow();

}
else if(max != null)
{
} else if (max != null) {
long maxValue = max.value();

builder.beginControlFlow("if( $L > $L )", pName, maxValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message());
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message());
builder.endControlFlow();
}
else
{
} else {
long minValue = min.value();

builder.beginControlFlow("if( $L < $L )", pName, minValue);
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message());
builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message());
builder.endControlFlow();
}
}
Expand Down Expand Up @@ -326,9 +319,10 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)
if (HandlerGenerator.hasValueOfMethod(erasedType)) {
if (!isBeanParam) {
return QueryListValueOfType;

} else {

return BeanListValueOfType;

}
} else if (HandlerGenerator.hasFromStringMethod(erasedType)) {
if (!isBeanParam) {
Expand All @@ -337,22 +331,24 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)
} else {
return BeanListFromStringType;
}
} else if (erasedType.getTypeName().contains("java.nio.file.Path")) {
return PathListType;
} else if (erasedType.getTypeName().contains("java.io.File")) {
return FileListType;
} else {
return ModelType;
}

} catch (Exception e) {
HandlerGenerator.log.error(e.getMessage(), e);
throw e;
throw e;
}
}

if (isMap && !isOptional) {
try {


return ModelType;

return ModelType;

} catch (Exception e) {
HandlerGenerator.log.error(e.getMessage(), e);
Expand Down Expand Up @@ -387,7 +383,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)
throw e;

}
} else if (isArray ) {
} else if (isArray) {
try {

if (type instanceof ParameterizedType) {
Expand Down Expand Up @@ -420,21 +416,16 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)
HandlerGenerator.log.error(e.getMessage(), e);
throw e;
}
}

else if (isMap ) {
} else if (isMap) {
try {


return ModelType;
return ModelType;

} catch (Exception e) {
HandlerGenerator.log.error(e.getMessage(), e);
throw e;
}
}

else if (isSet ) {
} else if (isSet) {
try {

if (type instanceof ParameterizedType) {
Expand Down Expand Up @@ -481,9 +472,9 @@ else if (isSet ) {
return FloatType;
} else if (type.equals(Double.class)) {
return DoubleType;
}else if (type.equals(BigDecimal.class)) {
} else if (type.equals(BigDecimal.class)) {
return BigDecimalType;
} else if (type.equals(java.nio.ByteBuffer.class)) {
} else if (type.equals(java.nio.ByteBuffer.class)) {
return ByteBufferType;
} else if (type.equals(Boolean.class)) {
return BooleanType;
Expand Down Expand Up @@ -524,9 +515,9 @@ else if (isSet ) {
return OptionalFloatType;
} else if (type.getTypeName().contains("java.lang.Double")) {
return OptionalDoubleType;
} else if (type.getTypeName().contains("java.math.BigDecimal")) {
} else if (type.getTypeName().contains("java.math.BigDecimal")) {
return OptionalBigDecimalType;
}else if (type.getTypeName().contains("java.lang.Integer")) {
} else if (type.getTypeName().contains("java.lang.Integer")) {
return OptionalIntegerType;
} else if (type.getTypeName().contains("java.nio.file.Path")) {
return OptionalFilePathType;
Expand Down
Loading

0 comments on commit 2ebba11

Please sign in to comment.