Skip to content

Commit

Permalink
Reduced package size. Improved fallback listeners and xml support.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Sep 27, 2018
1 parent e359c50 commit dc76b6c
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 131 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ bin
.cache-tests
/proteus/
/pom.xml.versionsBackup
.factorypath
105 changes: 25 additions & 80 deletions conf/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,54 @@
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>%date{ISO8601} %highlight(%-5level) [%boldCyan(%logger)] [%boldYellow(%method %F) ] - %boldWhite(%message) %n %red(%ex)
<pattern>%date{ISO8601} %highlight(%-5level) [%boldCyan(%logger)]
[%boldYellow(%method %F) ] - %boldWhite(%message) %n %red(%ex)
</pattern>
</encoder>
</appender>

<logger name="org.apache.http" level="ERROR" />


<logger name="javax.mail" level="ERROR" />
<logger name="com.squareup.okhttp" level="WARN" />
<logger name="javax.activation" level="ERROR" />

<logger name="javax.activation" level="ERROR" />

<logger name="io.netty" level="ERROR" />
<logger name="io.netty.handler" level="ERROR" />

<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" />
<logger name="org.apache.activemq" level="ERROR" />
<logger name="org.apache.camel" level="ERROR" />
<logger name="org.apache.activemq" level="ERROR" />
<logger name="sun.net" level="ERROR" />
<logger name="org.springsource" level="ERROR" />

<logger name="com.sun.jersey" level="ERROR" />



<logger name="org.xnio.nio" level="ERROR" />
<logger name="io.undertow" level="ERROR" />


<logger name="org.reflections" level="OFF" />
<logger name="org.elasticsearch" level="ERROR" />
<logger name="org.ajave.ebean" level="ERROR" />

<logger name="org.avaje.ebean.SQL" level="ERROR" />
<logger name="org.avaje.ebean.SUM" level="ERROR" />
<logger name="org.avaje.ebean.TXN" level="ERROR" />
<logger name="com.avaje.ebeaninternal" level="ERROR" />
<logger name="org.avaje.ebean.cache.BEAN" level="ERROR" />


<logger name="org.reflections" level="OFF" />

<logger name="io.swagger.converter" level="ERROR" />
<logger name="io.swagger.jackson" level="ERROR" />


<logger name="com.avaje.ebean.config.PropertyMapLoader" level="ERROR" />
<logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="ERROR" />
<logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="ERROR" />

<logger name="com.gargoylesoftware.htmlunit.javascript" level="OFF" />
<logger name="io.swagger.jackson" level="ERROR" />

<logger name="swagger" level="ERROR" />
<logger name="com.zaxxer.hikari" level="ERROR" />
<logger name="com.zaxxer.hikari.pool" level="ERROR" />

<logger name="javax.management" level="ERROR" />
<logger name="javax.management.mbeanserver" level="OFF" />
<logger name="java.jmx" level="ERROR" />
<logger name="java.jmx" level="ERROR" />

<logger name="net.sf.ehcache" level="ERROR" />

<logger name="org.apache.http" level="ERROR" />
<logger name="com.amazonaws" level="ERROR" />
<logger name="org.apache.http.wire" level="ERROR" />

<logger name="com.wordnik" level="OFF" />

<logger name="javax.mail" level="ERROR" />
<logger name="com.squareup.okhttp" level="WARN" />
<logger name="sun.net" level="ERROR" />

<logger name="org.apache.commons.logging.impl.Jdk14Logger"
level="ERROR" />

<logger name="org.hibernate.validator" level="ERROR" />
<logger name="org.xnio.nio" level="ERROR" />
<logger name="java.util.logging.Logger" level="ERROR" />
<logger name="com.google.inject.internal" level="ERROR" />
<logger name="com.google.inject" level="ERROR" />


<logger name="org.apache.camel" level="ERROR" />
<logger name="org.apache.activemq" level="ERROR" />
<logger name="sun.net" level="ERROR" />
<logger name="org.springsource" level="ERROR" />
<logger name="com.sun.jersey" level="ERROR" />
<logger name="akka.event.EventStream" level="ERROR" />
<logger name="akka.event.slf4j" level="ERROR" />
<logger name="com.googlecode.mp4parser" level="ERROR" />
<logger name="com.googlecode.mp4parser.util" level="ERROR" />

<logger name="org.elasticsearch.plugins" level="ERROR" />

<logger name="org.apache.commons.logging.impl.Jdk14Logger" level="ERROR" />
<logger name="org.zeroturnaround.exec.ProcessExecutor" level="ERROR" />
<logger name="org.zeroturnaround.exec" level="ERROR" />

<!-- <logger name="com.datastax.driver.core.QueryLogger.NORMAL" level="trace" />
-->

<logger name="org.apache.kafka.clients.NetworkClient" level="ERROR" />
<logger name="org.apache.kafka.clients.producer" level="ERROR" />


<logger name="java.util.logging.Logger" level="ERROR" />
<logger name="com.google.inject.internal" level="ERROR" />
<logger name="com.google.inject" level="ERROR" />
<logger name="com.datastax" level="ERROR" />

<root level="debug">
<appender-ref ref="STDOUT" />
</root>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
55 changes: 36 additions & 19 deletions src/main/java/io/sinistral/proteus/modules/ApplicationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.google.common.util.concurrent.Service;
Expand Down Expand Up @@ -58,34 +60,47 @@ protected void configure()
{

this.binder().requestInjection(this);


this.bindMappers();

RoutingHandler router = new RoutingHandler();

try
{
String className = config.getString("application.defaultResponseListener");
log.info("Installing DefaultResponseListener " + className);
Class<? extends DefaultResponseListener> clazz = (Class<? extends DefaultResponseListener>) Class.forName(className);
this.bind(DefaultResponseListener.class).to(clazz).in(Singleton.class);
} catch (Exception e)
{
this.binder().addError(e);
log.error(e.getMessage(), e);
}

try
{
String className = config.getString("application.fallbackHandler");
log.info("Installing FallbackListener " + className);

Class<? extends HttpHandler> clazz = (Class<? extends HttpHandler>) Class.forName(className);
router.setFallbackHandler(clazz.newInstance());

HttpHandler fallbackHandler = clazz.newInstance();

this.binder().requestInjection(fallbackHandler);

router.setFallbackHandler(fallbackHandler);

} catch (Exception e)
{
this.binder().addError(e);
log.error(e.getMessage(), e);
}

this.bind(RoutingHandler.class).toInstance(router);

this.bind(ApplicationModule.class).toInstance(this);

try
{
String className = config.getString("application.defaultResponseListener");
log.info("Installing DefaultResponseListener " + className);
Class<? extends DefaultResponseListener> clazz = (Class<? extends DefaultResponseListener>) Class.forName(className);
this.bind(DefaultResponseListener.class).to(clazz).in(Singleton.class);
} catch (Exception e)
{
log.error(e.getMessage(), e);
}


this.bind(new TypeLiteral<Set<Class<?>>>()
{
Expand All @@ -103,8 +118,7 @@ protected void configure()
{
}).annotatedWith(Names.named("registeredHandlerWrappers")).toInstance(registeredHandlerWrappers);


this.bindMappers();


}

Expand All @@ -113,12 +127,15 @@ protected void configure()
*/
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();

JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);

XmlMapper xmlMapper = new XmlMapper(xmlModule);
xmlMapper.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION);

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

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,32 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)
return false;
}

if (exchange.getStatusCode() >= 400) {
final int statusCode = exchange.getStatusCode();

if (statusCode >= 400) {

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

final String path = exchange.getRelativePath();

Throwable throwable = exchange.getAttachment(DefaultResponseListener.EXCEPTION);

if( throwable == null )
{
throwable = new Exception("An unknown error occured");
final String reason = StatusCodes.getReason(statusCode);

throwable = new Exception(reason);
}

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


errorMap.put("exceptionClass", throwable.getClass().getName());

errorMap.put("message", throwable.getMessage());

log.error(throwable.getMessage(),throwable);
errorMap.put("path", path);

errorMap.put("code", Integer.toString(statusCode));

log.error(throwable.getMessage() + " at " + path,throwable);

if( throwable.getStackTrace() != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,82 @@
*/
package io.sinistral.proteus.server.handlers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;

import io.sinistral.proteus.server.predicates.ServerPredicates;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import io.undertow.util.StatusCodes;

/**
* @author jbauer
*
*/
public class ServerFallbackHandler implements HttpHandler
{

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception
{
private class Message {

@SuppressWarnings("unused")
public final Integer statusCode;
@SuppressWarnings("unused")
public final String reason;

/**
* @param statusCode
* @param reason
*/
public Message(Integer statusCode, String reason)
{
this.statusCode = statusCode;
this.reason = reason;
}
}

@Inject
protected XmlMapper xmlMapper;

@Inject
protected ObjectMapper objectMapper;

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception
{
final int statusCode = 404;

exchange.setStatusCode(statusCode);

final String responseBody;

final String reason = StatusCodes.getReason(statusCode);

if (ServerPredicates.ACCEPT_JSON_PREDICATE.resolve(exchange))
{
responseBody = objectMapper.writeValueAsString(new Message(statusCode,reason));
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON);
}
else if (ServerPredicates.ACCEPT_XML_PREDICATE.resolve(exchange))
{
responseBody = xmlMapper.writeValueAsString(new Message(statusCode,reason));
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_XML);
}
else if (ServerPredicates.ACCEPT_HTML_PREDICATE.resolve(exchange))
{
responseBody = "<html><head><title>Error</title></head><body>" + statusCode + " - " + reason + "</body></html>";
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, javax.ws.rs.core.MediaType.TEXT_HTML);
}
else
{
exchange.setStatusCode(404);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Page Not Found!!");

}
responseBody = statusCode + " - " + reason;
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, javax.ws.rs.core.MediaType.TEXT_PLAIN);
}

exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + responseBody.length());
exchange.getResponseSender().send(responseBody);



}
}
Loading

0 comments on commit dc76b6c

Please sign in to comment.