Skip to content

Commit

Permalink
Merge pull request #4 from noboomu/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
noboomu authored Jul 18, 2018
2 parents 5931f6e + 2f59835 commit 3630ca8
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ bin
.settings/org.scala-ide.sdt.core.prefs
.cache-tests
/proteus/
/pom.xml.versionsBackup
6 changes: 3 additions & 3 deletions conf/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<logger name="io.netty" level="ERROR" />
<logger name="io.netty.handler" level="ERROR" />

<logger name="io.sinistral.proteus.server.swagger" level="DEBUG" />
<logger name="io.sinistral.proteus.server.handlers" level="DEBUG" />
<logger name="io.sinistral.proteus.services" level="DEBUG" />
<logger name="io.sinistral.proteus.server.swagger" level="ERROR" />
<logger name="io.sinistral.proteus.server.handlers" level="ERROR" />
<logger name="io.sinistral.proteus.services" level="ERROR" />

io.sinistral.proteus.services
<logger name="com.relayrides" level="ERROR" />
Expand Down
21 changes: 8 additions & 13 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.2.3-SNAPSHOT</version>
<version>0.3.1-SNAPSHOT</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 @@ -33,9 +33,9 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jackson>2.8.8.1</version.jackson>
<version.maven-shade-plugin>3.0.0</version.maven-shade-plugin>
<version.undertow>1.4.13.Final</version.undertow>
<version.jackson>2.9.6</version.jackson>
</properties>

<build>
Expand Down Expand Up @@ -247,35 +247,35 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
<version>${version.jackson}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.10</version>
<version>${version.jackson}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.10</version>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<version>2.8.10</version>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.8.10</version>
<version>${version.jackson}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8.1</version>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
Expand Down Expand Up @@ -341,11 +341,6 @@
<artifactId>jansi</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>io.sinistral</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.9</version>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/sinistral/proteus/ProteusApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,24 @@ public void start()
return;
}

log.info("Configuring modules...");
log.info("Configuring modules: " + registeredModules);

Set<Module> modules = registeredModules.stream().map(mc -> injector.getInstance(mc)).collect(Collectors.toSet());

//boolean needsMappingModule = true;

// for(Module m : modules)
// {
// if(m.getClass().getSuperclass().equals(MappingModule.class))
// {
// needsMappingModule = false;
// }
// }
//
// if(needsMappingModule)
// {
// modules.add(injector.getInstance(MappingModule.class));
// }

injector = injector.createChildInjector(modules);

Expand Down
45 changes: 36 additions & 9 deletions src/main/java/io/sinistral/proteus/modules/ApplicationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.google.common.util.concurrent.Service;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.jsoniter.DecodingMode;
import com.jsoniter.JsonIterator;
import com.jsoniter.annotation.JsoniterAnnotationSupport;
import com.jsoniter.output.EncodingMode;
import com.jsoniter.output.JsonStream;
import com.typesafe.config.Config;

import io.sinistral.proteus.server.Extractors;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.server.endpoints.EndpointInfo;
import io.undertow.server.DefaultResponseListener;
import io.undertow.server.HandlerWrapper;
Expand Down Expand Up @@ -102,12 +103,38 @@ protected void configure()
{
}).annotatedWith(Names.named("registeredHandlerWrappers")).toInstance(registeredHandlerWrappers);

this.bind(XmlMapper.class).toInstance(new XmlMapper());

this.bindMappers();

JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
JsoniterAnnotationSupport.enable();
}

/**
* Override for customizing XmlMapper and ObjectMapper
*/
public void bindMappers()
{
this.bind(XmlMapper.class).toInstance(new XmlMapper());

// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
// JsoniterAnnotationSupport.enable();

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
objectMapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH,true);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);

objectMapper.registerModule(new AfterburnerModule());
objectMapper.registerModule(new Jdk8Module());

this.bind(ObjectMapper.class).toInstance(objectMapper);

this.requestStaticInjection(Extractors.class);

this.requestStaticInjection(ServerResponse.class);
}

}
80 changes: 52 additions & 28 deletions src/main/java/io/sinistral/proteus/server/Extractors.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
import java.util.Objects;
import java.util.function.Function;

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

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.spi.TypeLiteral;
import com.google.inject.Inject;

import io.sinistral.proteus.server.predicates.ServerPredicates;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.form.FormData.FormValue;
import io.undertow.server.handlers.form.FormDataParser;
import io.undertow.util.HttpString;
import io.undertow.util.Methods;
Expand All @@ -34,8 +37,24 @@
*/
public class Extractors
{

protected static final XmlMapper XML_MAPPER = new XmlMapper();
private static Logger log = LoggerFactory.getLogger(Extractors.class.getCanonicalName());

@Inject
public static XmlMapper XML_MAPPER;

@Inject
public static ObjectMapper OBJECT_MAPPER;

public static Function<byte[],JsonNode> parseJson = (bytes) -> {
try
{
return OBJECT_MAPPER.readTree(bytes);
} catch (Exception e)
{
log.error(e.getMessage(),e);
return null;
}
};

public static class Optional
{
Expand All @@ -45,12 +64,12 @@ public static <T> java.util.Optional<T> extractWithFunction(final HttpServerExch
return string(exchange, name).map(function);
}

public static java.util.Optional<JsonIterator> jsonIterator(final HttpServerExchange exchange)
public static java.util.Optional<JsonNode> jsonNode(final HttpServerExchange exchange)
{
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map(ByteBuffer::array).map(JsonIterator::parse);
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map(ByteBuffer::array).map(parseJson);
}

public static <T> java.util.Optional<T> model(final HttpServerExchange exchange, final TypeLiteral<T> type )
public static <T> java.util.Optional<T> model(final HttpServerExchange exchange, final TypeReference<T> type )
{
if( ServerPredicates.XML_PREDICATE.resolve(exchange) )
{
Expand All @@ -77,40 +96,43 @@ public static <T> java.util.Optional<T> model(final HttpServerExchange exchange
}


public static <T> java.util.Optional<T> jsonModel(final HttpServerExchange exchange, final TypeLiteral<T> type )
public static <T> java.util.Optional<T> jsonModel(final HttpServerExchange exchange, final TypeReference<T> type )
{
return jsonIterator(exchange).map(i -> {
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map(ByteBuffer::array).map( b -> {
try
{
return i.read(type);
return OBJECT_MAPPER.readValue(b, type);
} catch (Exception e)
{
//log.error(e.getMessage(),e);
return null;
}
});
}

public static <T> java.util.Optional<T> jsonModel(final HttpServerExchange exchange, final Class<T> type )
{
return jsonIterator(exchange).map(i -> {
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map(ByteBuffer::array).map( b -> {
try
{
return i.read(type);
return OBJECT_MAPPER.readValue(b, type);
} catch (Exception e)
{
//log.error(e.getMessage(),e);
return null;
}
});
}

public static <T> java.util.Optional<T> xmlModel(final HttpServerExchange exchange, final TypeLiteral<T> type )
public static <T> java.util.Optional<T> xmlModel(final HttpServerExchange exchange, final TypeReference<T> type )
{
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map(ByteBuffer::array).map( b -> {
try
{
return XML_MAPPER.readValue(b,XML_MAPPER.getTypeFactory().constructType(type.getType()));
} catch (Exception e)
{
//log.error(e.getMessage(),e);
return null;
}
});
Expand All @@ -124,6 +146,7 @@ public static <T> java.util.Optional<T> xmlModel(final HttpServerExchange excha
return XML_MAPPER.readValue(b,type);
} catch (Exception e)
{
//log.error(e.getMessage(),e);
return null;
}
});
Expand All @@ -150,9 +173,9 @@ public static java.util.Optional<ZonedDateTime> zonedDateTime(final HttpServerEx
}


public static java.util.Optional<Any> any(final HttpServerExchange exchange )
public static java.util.Optional<JsonNode> any(final HttpServerExchange exchange )
{
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map(t -> JsonIterator.deserialize(t.array()));
return java.util.Optional.ofNullable(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY)).map( b -> parseJson.apply(b.array()));
}

public static java.util.Optional<Integer> integerValue(final HttpServerExchange exchange, final String name)
Expand Down Expand Up @@ -267,11 +290,11 @@ public static OffsetDateTime offsetDateTime(final HttpServerExchange exchange,fi

}

public static <T> T jsonModel(final HttpServerExchange exchange, final TypeLiteral<T> type ) throws IllegalArgumentException
public static <T> T jsonModel(final HttpServerExchange exchange, final TypeReference<T> type ) throws IllegalArgumentException
{
try
{
return jsonIterator(exchange).read(type);
return OBJECT_MAPPER.readValue(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY).array(), type);
}
catch( Exception e )
{
Expand All @@ -283,7 +306,7 @@ public static <T> T jsonModel(final HttpServerExchange exchange, final Class<T>
{
try
{
return jsonIterator(exchange).read(type);
return OBJECT_MAPPER.readValue(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY).array(), type);
}
catch( Exception e )
{
Expand All @@ -306,7 +329,7 @@ public static <T> T xmlModel(final HttpServerExchange exchange, final Class<T>
}
}

public static <T> T xmlModel(final HttpServerExchange exchange, final TypeLiteral<T> type ) throws IllegalArgumentException
public static <T> T xmlModel(final HttpServerExchange exchange, final TypeReference<T> type ) throws IllegalArgumentException
{
try
{
Expand All @@ -319,20 +342,21 @@ public static <T> T xmlModel(final HttpServerExchange exchange, final TypeLiter
}
}

public static Any any(final HttpServerExchange exchange )
public static JsonNode any(final HttpServerExchange exchange )
{
try
{
return JsonIterator.parse( exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY).array() ).readAny();
} catch (IOException e)
return parseJson.apply( exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY).array() );
} catch (Exception e)
{
return Any.wrapNull();
log.warn(e.getMessage(),e);
return OBJECT_MAPPER.createObjectNode();
}
}

public static JsonIterator jsonIterator(final HttpServerExchange exchange )
public static JsonNode jsonNode(final HttpServerExchange exchange )
{
return JsonIterator.parse(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY).array());
return parseJson.apply(exchange.getAttachment(ServerRequest.BYTE_BUFFER_KEY).array());
}

public static Path filePath(final HttpServerExchange exchange, final String name) throws java.lang.IllegalArgumentException
Expand Down Expand Up @@ -413,7 +437,7 @@ public static Boolean booleanValue(final HttpServerExchange exchange, final Str
return Boolean.parseBoolean(string(exchange, name));
}

public static <T> T model(final HttpServerExchange exchange, final TypeLiteral<T> type ) throws IllegalArgumentException
public static <T> T model(final HttpServerExchange exchange, final TypeReference<T> type ) throws IllegalArgumentException
{
if( ServerPredicates.XML_PREDICATE.resolve(exchange) )
{
Expand Down
Loading

0 comments on commit 3630ca8

Please sign in to comment.