Skip to content

Commit

Permalink
Final 3.7 release. Last before modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jan 9, 2019
1 parent aeca144 commit 921bf84
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 46 deletions.
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.sinistral</groupId>
<artifactId>proteus-core</artifactId>
<version>0.3.7-SNAPSHOT</version>
<version>0.3.7</version>
<name>proteus core</name>
<description>Proteus is an extremely light, fast, and flexible Java REST API framework built atop Undertow.</description>
<url>http://github.com/noboomu/proteus</url>
Expand Down Expand Up @@ -192,17 +192,13 @@
<scope>test</scope>
</dependency>

<!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<version>4.12</version> <scope>test</scope> </dependency> -->

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.server.endpoints.EndpointInfo;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
Expand All @@ -34,10 +33,8 @@
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.net.URI;
import java.net.URL;
import java.util.*;
Expand Down Expand Up @@ -354,12 +351,30 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
List<String> typeLevelSecurityDefinitions = new ArrayList<>();

if (Optional.ofNullable(clazz.getAnnotation(Path.class)).isPresent()) {
SecurityRequirement securityRequirementAnnotation = clazz.getAnnotation(SecurityRequirement.class);

Annotation[] annotations = clazz.getAnnotations();

Annotation securityRequirementAnnotation = Arrays.stream(annotations).filter(a -> a.getClass().getName().contains("SecurityRequirement" +
"")).findFirst().orElse(null);

if (securityRequirementAnnotation != null) {
String securityRequirement = securityRequirementAnnotation.name();

typeLevelSecurityDefinitions.add(securityRequirement);
if (securityRequirementAnnotation != null) {

try {
Field nameField = securityRequirementAnnotation.getClass().getField("name");

if (nameField != null) {
Object securityRequirement = nameField.get(securityRequirementAnnotation);
typeLevelSecurityDefinitions.add(securityRequirement.toString());
}

} catch (Exception e) {
log.warn("No name field on security requirement");
}

}

}
}

Expand Down Expand Up @@ -754,12 +769,31 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
*/

if (Optional.ofNullable(m.getAnnotation(Path.class)).isPresent()) {
SecurityRequirement securityRequirementAnnotation = m.getAnnotation(SecurityRequirement.class);

Annotation[] annotations = clazz.getAnnotations();

Annotation securityRequirementAnnotation = Arrays.stream(annotations).filter( a -> a.getClass().getName().contains("SecurityRequirement") ).findFirst().orElse(null);

if (securityRequirementAnnotation != null) {
String securityRequirement = securityRequirementAnnotation.name();

securityDefinitions.add(securityRequirement);
if (securityRequirementAnnotation != null) {

try
{
Field nameField = securityRequirementAnnotation.getClass().getField("name");

if(nameField != null) {
Object securityRequirement = nameField.get(securityRequirementAnnotation);
securityDefinitions.add(securityRequirement.toString());
}

} catch( Exception e )
{
log.warn("No name field on security requirement");
}

}

}

}
Expand Down
71 changes: 43 additions & 28 deletions src/test/java/io/sinistral/proteus/test/controllers/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import io.swagger.v3.oas.models.servers.Server;
import io.undertow.server.HttpServerExchange;

/**
Expand Down Expand Up @@ -108,15 +110,15 @@ public void exchangeJsonSerializeToBytes(HttpServerExchange exchange)


@GET
@Path("/exchange/user/json")
@Path("exchange/user/json")
@Operation(description = "User serialization endpoint" )
public void exchangeUserJson(HttpServerExchange exchange)
{
response( new User(123L) ).applicationJson().send(exchange);
}

@GET
@Path("/exchange/user/xml")
@Path("exchange/user/xml")
@Produces((MediaType.APPLICATION_XML))
@Operation(description = "User serialization endpoint" )
public void exchangeUserXml(HttpServerExchange exchange)
Expand All @@ -125,7 +127,7 @@ public void exchangeUserXml(HttpServerExchange exchange)
}

@GET
@Path("/response/user/json")
@Path("response/user/json")
@Operation(description = "User serialization endpoint" )
public ServerResponse<User> responseUserJson(ServerRequest request)
{
Expand All @@ -135,7 +137,7 @@ public ServerResponse<User> responseUserJson(ServerRequest request)
}

@GET
@Path("/response/user/xml")
@Path("response/user/xml")
@Produces((MediaType.APPLICATION_XML))
@Operation(description = "User serialization endpoint" )
public ServerResponse<User> responseUserXml(ServerRequest request)
Expand All @@ -147,7 +149,7 @@ public ServerResponse<User> responseUserXml(ServerRequest request)


@GET
@Path("/exchange/plaintext")
@Path("exchange/plaintext")
@Produces((MediaType.TEXT_PLAIN))
@Operation(description = "Plaintext endpoint" )
public void exchangePlaintext(HttpServerExchange exchange)
Expand All @@ -157,7 +159,7 @@ public void exchangePlaintext(HttpServerExchange exchange)
}

@GET
@Path("/exchange/plaintext2")
@Path("exchange/plaintext2")
@Produces((MediaType.TEXT_PLAIN))
@Operation(description = "Plaintext endpoint 2" )
public void exchangePlaintext2(HttpServerExchange exchange)
Expand All @@ -167,7 +169,7 @@ public void exchangePlaintext2(HttpServerExchange exchange)
}

@GET
@Path("/response/plaintext")
@Path("response/plaintext")
@Produces((MediaType.TEXT_PLAIN))
@Operation(description = "Plaintext endpoint" )
public ServerResponse<ByteBuffer> responsePlaintext(ServerRequest request)
Expand All @@ -177,7 +179,7 @@ public ServerResponse<ByteBuffer> responsePlaintext(ServerRequest request)
}

@GET
@Path("/response/future/map")
@Path("response/future/map")
@Operation(description = "Future map endpoint" )
public CompletableFuture<ServerResponse<Map<String,String>>> responseFutureMap( ServerRequest request )
{
Expand All @@ -186,7 +188,7 @@ public CompletableFuture<ServerResponse<Map<String,String>>> responseFutureMap(
}

@GET
@Path("/response/map")
@Path("response/map")
@Operation(description = "Map endpoint" )
public ServerResponse<Map<String,String>> futureMap( ServerRequest request )
{
Expand All @@ -195,7 +197,7 @@ public ServerResponse<Map<String,String>> futureMap( ServerRequest request )
}

@POST
@Path("/response/file/path")
@Path("response/file/path")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Operation(description = "Upload file path endpoint" )
Expand All @@ -205,7 +207,7 @@ public ServerResponse<ByteBuffer> responseUploadFilePath(ServerRequest request,
}

@POST
@Path("/response/file/path/optional")
@Path("response/file/path/optional")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Operation(description = "Upload optional file path endpoint" )
Expand All @@ -222,7 +224,7 @@ public ServerResponse<ByteBuffer> responseUploadOptionalFilePath(ServerRequest r
}

@POST
@Path("/response/json/echo")
@Path("response/json/echo")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Operation(description = "Echo json endpoint" )
Expand All @@ -232,7 +234,7 @@ public ServerResponse<User> responseEchoJson(ServerRequest request, @FormParam("
}

@POST
@Path("/response/json/beanparam")
@Path("response/json/beanparam")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_JSON)
@Operation(description = "Echo json inner class endpoint" )
Expand All @@ -243,7 +245,7 @@ public ServerResponse<User> responseInnerClassTest(ServerRequest request, @BeanP


@GET
@Path("/generic/set")
@Path("generic/set")
@Produces((MediaType.APPLICATION_JSON))
@Operation(description = "Generic set endpoint" )
public ServerResponse<Set<Long>> genericSet( ServerRequest request, @QueryParam("ids") Set<Long> ids ) throws Exception
Expand All @@ -253,7 +255,7 @@ public ServerResponse<Set<Long>> genericSet( ServerRequest request, @QueryParam


@POST
@Path("/generic/set/bean")
@Path("generic/set/bean")
@Produces((MediaType.APPLICATION_JSON))
@Consumes(MediaType.APPLICATION_JSON)
@Operation(description = "Generic bean set endpoint" )
Expand All @@ -264,7 +266,7 @@ public ServerResponse<Set<Long>> genericBeanSet( ServerRequest request, @BeanPa


@POST
@Path("/generic/list/bean")
@Path("generic/list/bean")
@Produces((MediaType.APPLICATION_JSON))
@Consumes(MediaType.APPLICATION_JSON)

Expand All @@ -275,7 +277,7 @@ public ServerResponse<List<Long>> genericBeanList( ServerRequest request, @Bean
}

@GET
@Path("/optional/set")
@Path("optional/set")
@Produces((MediaType.APPLICATION_JSON))
@Operation(description = "Generic optional set endpoint" )
public ServerResponse<Set<Long>> genericOptionalSet( ServerRequest request, @QueryParam("ids") Optional<Set<Long>> ids ) throws Exception
Expand All @@ -285,7 +287,7 @@ public ServerResponse<Set<Long>> genericOptionalSet( ServerRequest request, @Qu


@GET
@Path("/redirect/permanent")
@Path("redirect/permanent")
@Operation(description = "Permanent redirect endpoint" )
@Produces(MediaType.WILDCARD)
public ServerResponse<Void> testPermanentRedirect()
Expand All @@ -294,7 +296,7 @@ public ServerResponse<Void> testPermanentRedirect()
}

@GET
@Path("/redirect")
@Path("redirect")
@Operation(description = "Redirect endpoint" )
@Produces(MediaType.WILDCARD)
public ServerResponse<Void> testRedirect()
Expand All @@ -303,7 +305,7 @@ public ServerResponse<Void> testRedirect()
}

@POST
@Path("/response/parse/ids")
@Path("response/parse/ids")
@Blocking
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Expand All @@ -317,7 +319,7 @@ public ServerResponse<List<Long>> listConversion( ServerRequest request, @BeanPa
}

@GET
@Path("/response/parse/timestamp")
@Path("response/parse/timestamp")
@Blocking
@Produces(MediaType.TEXT_PLAIN)
@Operation(description = "Convert timestamp")
Expand All @@ -330,7 +332,7 @@ public ServerResponse<ByteBuffer> timestampConversion( ServerRequest request, @Q
}

@GET
@Path("/response/parse/instant")
@Path("response/parse/instant")
@Blocking
@Produces(MediaType.TEXT_PLAIN)
@Operation(description = "Convert instant")
Expand All @@ -343,7 +345,7 @@ public ServerResponse<ByteBuffer> instantConversion( ServerRequest request, @Que
}

@POST
@Path("/response/bytebuffer")
@Path("response/bytebuffer")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes("*/*")
@Operation(description = "Upload file path endpoint")
Expand All @@ -356,7 +358,7 @@ public ServerResponse<ByteBuffer> responseUploadByteBuffer(ServerRequest request
}

@POST
@Path("/response/file")
@Path("response/file")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes("*/*")
@Operation(description = "Upload file path endpoint")
Expand All @@ -372,7 +374,7 @@ public ServerResponse<ByteBuffer> responseUploadFile(ServerRequest request, @For
}

@GET
@Path("/response/debug")
@Path("response/debug")
@Operation(description = "Debug endpoint")
public ServerResponse<Map<String,String>> debugEndpoint(ServerRequest request)
{
Expand All @@ -389,7 +391,7 @@ public ServerResponse<Map<String,String>> debugEndpoint(ServerRequest request)


@GET
@Path("/response/debug/blocking")
@Path("response/debug/blocking")
@Blocking
@Operation(description="Debug blocking endpoint")
public ServerResponse<Map<String,String>> debugBlockingEndpoint(ServerRequest request)
Expand All @@ -406,7 +408,7 @@ public ServerResponse<Map<String,String>> debugBlockingEndpoint(ServerRequest re
}

@GET
@Path("/response/future/user")
@Path("response/future/user")
@Operation(description="Future user endpoint")
@Produces((MediaType.APPLICATION_JSON))
public CompletableFuture<ServerResponse<User>> responseFutureUser()
Expand All @@ -415,7 +417,7 @@ public CompletableFuture<ServerResponse<User>> responseFutureUser()
}

@GET
@Path("/response/parameters/complex/{pathLong}")
@Path("response/parameters/complex/{pathLong}")
@Operation(description = "Complex parameters" )
@Produces((MediaType.APPLICATION_JSON))
public ServerResponse<Map<String,Object>> complexParameters(
Expand Down Expand Up @@ -454,4 +456,17 @@ public ServerResponse<Map<String,Object>> complexParameters(
responseMap.put("queryIntegerList", queryIntegerList);
return response(responseMap).applicationJson();
}

@GET
@SecurityRequirement(name = "testRequirement")
@Path("secure/resource")
@Operation(description="Secure resource")
@Produces(MediaType.APPLICATION_JSON)
public ServerResponse<Map<String,Object>> responseSecureContext()
{
Map<String,Object> responseMap = new HashMap<>();
responseMap.put("secure",true);

return response(responseMap);
}
}
Loading

0 comments on commit 921bf84

Please sign in to comment.