Skip to content

Commit

Permalink
Override param arg name with annotation value.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Apr 7, 2017
1 parent 3f0ef60 commit 6d30450
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 40 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/wurrly/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.google.common.util.concurrent.ServiceManager.Listener;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.j256.simplemagic.ContentType;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.typesafe.config.Config;
import com.wurrly.controllers.Users;
import com.wurrly.modules.ConfigModule;
Expand All @@ -35,6 +38,7 @@
import io.undertow.server.RoutingHandler;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.StatusCodes;
/**
* @author jbauer
*/
Expand Down Expand Up @@ -166,7 +170,20 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception

router.handleRequest(exchange);

} catch (Exception e)
}
catch (IllegalArgumentException e)
{
if (exchange.isResponseChannelAvailable())
{
log.error(e.getMessage(),e);

exchange.setStatusCode(StatusCodes.BAD_REQUEST);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, ContentType.JSON.getMimeType());
exchange.getResponseSender().send(JsonStream.serialize(Any.wrap(e)));

}
}
catch (Exception e)
{
if (exchange.isResponseChannelAvailable())
{
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/wurrly/controllers/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,33 @@ public Any userType(
@QueryParam("optionalQueryString") Optional<String> optionalQueryString,
@QueryParam("optionalLong") Optional<Long> optionalLong,
@QueryParam("longValue") Long longValue,
@QueryParam("dateValue") @ApiParam(format="date") Optional<Date> dateValue,
@QueryParam("dateValue") @ApiParam(defaultValue="2014-04-23T04:30:45.123+01:00", format="date") Optional<Date> optionalDate,
@QueryParam("numbers") List<Integer> numbers,

@QueryParam("queryFromString") UUID queryFromString,
@ApiParam(defaultValue="01234567-9ABC-DEF0-1234-56789ABCDEF0", format="uuid") @QueryParam("queryFromString") UUID queryFromString,
@ApiParam(defaultValue="01234567-9ABC-DEF0-1234-56789ABCDEF0", format="uuid") @QueryParam("optionalQueryFromString") Optional<UUID> optionalQueryFromString,
@ApiParam(defaultValue="01234567-9ABC-DEF0-1234-56789ABCDEF0") @HeaderParam("headerFromString") Optional<UUID> headerFromString,
@HeaderParam("headerString") String headerString,
@ApiParam(defaultValue="123") @HeaderParam("headerString") String headerString,
@HeaderParam("optionalHeaderString") Optional<String> optionalHeaderString,
@QueryParam("queryEnum") User.UserType queryEnum,
@QueryParam("optionalQueryEnum") Optional<User.UserType> optionalQueryEnum

)
{
//
log.debug("optionalQueryFromString: " + optionalQueryFromString);
log.debug("queryFromString: " + queryFromString);
log.debug("optionalQueryString: " + optionalQueryString);
log.debug("headerFromString: " + headerFromString);
log.debug("headerString: " + headerString);
log.debug("optionalHeaderString: " + optionalHeaderString);
log.debug("queryEnum: " + queryEnum);
log.debug("optionalQueryEnum: " + optionalQueryEnum);
log.debug("userId: " + userId);
log.debug("numbers: " + numbers);


//
log.debug("optionalQueryFromString: " + optionalQueryFromString);
log.debug("queryFromString: " + queryFromString);
log.debug("optionalQueryString: " + optionalQueryString);
log.debug("headerFromString: " + headerFromString);
log.debug("headerString: " + headerString);
log.debug("optionalHeaderString: " + optionalHeaderString);
log.debug("queryEnum: " + queryEnum);
log.debug("optionalQueryEnum: " + optionalQueryEnum);
log.debug("userId: " + userId);
log.debug("numbers: " + numbers);
log.debug("optionalDate: " + optionalDate);

return Any.wrap(new User(232343L));

}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/wurrly/server/Extractors.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
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;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
Expand All @@ -33,6 +34,8 @@
*/
public class Extractors
{
private static Logger log = LoggerFactory.getLogger(Extractors.class.getCanonicalName());

public static class Optional
{

Expand Down Expand Up @@ -68,7 +71,7 @@ public static <T> java.util.Optional<T> typed(final HttpServerExchange exchange
}

public static java.util.Optional<Date> date(final HttpServerExchange exchange,final String name) {


return string(exchange, name).map( ZonedDateTime::parse ).map(ZonedDateTime::toInstant).map(Date::from);

Expand Down
40 changes: 36 additions & 4 deletions src/main/java/com/wurrly/server/handlers/HandlerGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
import java.util.stream.Stream;

import javax.lang.model.element.Modifier;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
Expand Down Expand Up @@ -151,14 +155,43 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter,
{
if (handler.parameterTypes[i] instanceof StatementParameterType)
{
String pName = parameter.getName();

if( parameter.isAnnotationPresent(QueryParam.class))
{
QueryParam qp = parameter.getAnnotation(QueryParam.class);
pName = qp.value();
}
else if( parameter.isAnnotationPresent(HeaderParam.class))
{
HeaderParam hp = parameter.getAnnotation(HeaderParam.class);
pName = hp.value();
}
else if( parameter.isAnnotationPresent(PathParam.class))
{
PathParam pp = parameter.getAnnotation(PathParam.class);
pName = pp.value();
}
else if( parameter.isAnnotationPresent(CookieParam.class))
{
CookieParam cp = parameter.getAnnotation(CookieParam.class);
pName = cp.value();
}
else if( parameter.isAnnotationPresent(FormParam.class))
{
FormParam fp = parameter.getAnnotation(FormParam.class);
pName = fp.value();
}


StatementParameterType pType = (StatementParameterType) handler.parameterTypes[i];
switch (pType)
{
case LITERAL:
args[i] = parameter.getName();
break;
case STRING:
args[i] = parameter.getName();
args[i] = pName;
break;
case TYPE:
args[i] = parameter.getParameterizedType();
Expand All @@ -183,8 +216,7 @@ else if (handler.parameterTypes[i] instanceof Class)
}
}

log.info("type: " + parameter.getParameterizedType().getTypeName() + " : " + handler);
builder.addStatement(handler.statement, args);
builder.addStatement(handler.statement, args);
}

public static void addStatement(MethodSpec.Builder builder, Parameter parameter) throws Exception
Expand Down Expand Up @@ -407,7 +439,7 @@ public Class<? extends Supplier<RoutingHandler>> compileClass()
{
this.generateRoutes();

log.debug("CLASS:\n" + this.sourceString);
log.info("CLASS:\n" + this.sourceString);
return CompilerUtils.CACHED_COMPILER.loadFromJava(packageName + "." + className, this.sourceString);

} catch (Exception e)
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/com/wurrly/services/SwaggerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception
canonicalPath = swaggerThemesPath + canonicalPath.split(swaggerBasePath+"/themes")[1];

exchange.setRelativePath(canonicalPath);
log.info("path: " + canonicalPath);


super.handleRequest(exchange);

}
Expand All @@ -298,11 +297,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception

try
{
Resource swaggerUIResource = resourceManager.getResource("./swagger/index.html");

log.info("swaggerUIResource: " + swaggerUIResource);
log.info("swaggerUIResource: " + swaggerUIResource.getPath());
log.info("swaggerUIResource: " + swaggerUIResource.getFilePath());


pathTemplate = this.swaggerBasePath + "/*";

Expand All @@ -315,8 +310,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception
String canonicalPath = CanonicalPathUtils.canonicalize((exchange.getRelativePath()));

canonicalPath = swaggerResourcePath + canonicalPath.split(swaggerBasePath)[1];

log.info("path: " + canonicalPath);

exchange.setRelativePath(canonicalPath);

super.handleRequest(exchange);
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/swagger/swagger-ui-bundle.js
100644 → 100755

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/resources/swagger/swagger-ui-bundle.js.map
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified src/main/resources/swagger/swagger-ui-standalone-preset.js
100644 → 100755
Empty file.
Empty file modified src/main/resources/swagger/swagger-ui.css
100644 → 100755
Empty file.
Empty file modified src/main/resources/swagger/swagger-ui.css.map
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions src/main/resources/swagger/swagger-ui.js
100644 → 100755

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/resources/swagger/swagger-ui.js.map
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d30450

Please sign in to comment.