Skip to content

Commit

Permalink
Better generic parameter handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Aug 31, 2020
1 parent 94f0759 commit 4917d30
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 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

**Improve error handling.**


[94f075989e28408](https://github.com/noboomu/proteus/commit/94f075989e28408) Joshua Bauer *2020-06-24 20:28:31*

**Added protocol package and custom HttpHeaders implementation.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public void start()
return;
}

final Thread mainThread = Thread.currentThread();

final long startTime = System.currentTimeMillis();

log.info("Configuring modules: " + registeredModules);
Expand Down Expand Up @@ -188,11 +190,25 @@ public void healthy()
public void failure(Service service)
{
log.error("Service failure: " + service);

startupDuration = Duration.ofMillis(System.currentTimeMillis() - startTime);

for (ListenerInfo info : undertow.getListenerInfo()) {
log.debug("listener info: " + info);
SocketAddress address = info.getAddress();

if (address != null) {
ports.add(((java.net.InetSocketAddress) address).getPort());
}
}

printStatus();

running.set(true);
}

}, MoreExecutors.directExecutor());

final Thread mainThread = Thread.currentThread();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)

final Map<String, Object> errorMap = new HashMap<>();


final String path = exchange.getRelativePath();

if (throwable == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public enum TypeHandler
// StatementParameterType.LITERAL,
// io.sinistral.proteus.server.Extractors.class,
// StatementParameterType.LITERAL, StatementParameterType.RAW),

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),

Expand Down Expand Up @@ -304,7 +305,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)
boolean isSet = type.getTypeName().contains("java.util.Set");
boolean isMap = type.getTypeName().contains("java.util.Map");

if (!isOptional && !isArray && !isSet) {
if (!isOptional && !isArray && !isSet && !isMap) {
try {
Class<?> clazz = Class.forName(type.getTypeName());

Expand All @@ -314,6 +315,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)

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

}
}

Expand Down Expand Up @@ -341,7 +343,20 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)

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

if (isMap && !isOptional) {
try {


return ModelType;


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

Expand Down Expand Up @@ -369,9 +384,10 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)

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

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

if (type instanceof ParameterizedType) {
Expand Down Expand Up @@ -402,9 +418,23 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)

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

else if (isMap ) {
try {


return ModelType;

} catch (Exception e) {
HandlerGenerator.log.error(e.getMessage(), e);
throw e;
}
} else if (isSet && isOptional) {
}

else if (isSet ) {
try {

if (type instanceof ParameterizedType) {
Expand Down Expand Up @@ -435,6 +465,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam)

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

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ public ServerResponse<Set<Long>> genericBeanSet( ServerRequest request, @BeanPa
{
return response( ids ).applicationJson();
}

@POST
@Path("generic/map/bean")
@Produces((MediaType.APPLICATION_JSON))
@Consumes(MediaType.APPLICATION_JSON)
public ServerResponse<Map<String,Long>> genericBeanMap( ServerRequest request, @BeanParam Map<String,Long> ids ) throws Exception
{
return response( ids ).applicationJson();
}


@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,43 @@ public void genericBeanList()
e.printStackTrace();
}
}

@Test
public void genericBeanMap()
{
Map<String,Long> randomLongs = new java.util.HashMap<>();

Random random = new Random();

Long firstNumber = null;

for(int i = 0; i < 10; i++)
{
Long v = random.nextLong();

randomLongs.put(v.toString(),v);

if(firstNumber == null)
{
firstNumber = v;
}
}

ObjectMapper mapper = new ObjectMapper();

try
{


String body = mapper.writeValueAsString(randomLongs);

given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("tests/generic/map/bean").then().statusCode(200).body(containsString(firstNumber.toString()));

} catch (Exception e)
{
e.printStackTrace();
}
}

@Test
public void optionalGenericSet()
Expand Down

0 comments on commit 4917d30

Please sign in to comment.