Skip to content

Commit

Permalink
Do a refactoring to move in other projects some classes to have a bet…
Browse files Browse the repository at this point in the history
…ter code organizzation and to have less code in the archetype. This will help in future migrations
  • Loading branch information
Davide Cerbo committed Nov 14, 2016
1 parent c4804b6 commit 6d6d1fb
Show file tree
Hide file tree
Showing 29 changed files with 800 additions and 54 deletions.
47 changes: 47 additions & 0 deletions nut-archetype/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.nutcore</groupId>
<artifactId>nut-archetype</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-archetype</packaging>

<name>nut-archetype</name>
<description>A maven archetype to create a microservice component using OrientDB, Resteasy, Undertow, Metrics and Swagger</description>
<url>https://github.com/jesty/orientdb-microservices</url>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url>https://github.com/jesty/orientdb-microservices</url>
<connection>scm:git:git://github.com/jesty/orientdb-microservices.git</connection>
<developerConnection>scm:git:[email protected]:jesty/orientdb-microservices.git</developerConnection>
</scm>

<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>2.4</version>
</extension>
</extensions>

<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,33 @@
<undertow.version>1.3.5.Final</undertow.version>
<orientdb.version>2.2.12</orientdb.version>
<weld.version>2.4.0.Final</weld.version>
<logback.version>1.1.7</logback.version>
<slf4j.version>1.7.21</slf4j.version>
<metrics.version>3.0.1</metrics.version>
<nut.version>1.0-SNAPSHOT</nut.version>
<orientdb-javaee.version>1.0</orientdb-javaee.version>
</properties>

<dependencies>
<dependency>
<groupId>com.nutcore</groupId>
<artifactId>orientdb-javaee</artifactId>
<version>1.0</version>
<version>${orientdb-javaee.version}</version>
</dependency>
<dependency>
<groupId>com.nutcore</groupId>
<artifactId>nut-corelationid</artifactId>
<version>${nut.version}</version>
</dependency>
<dependency>
<groupId>com.nutcore</groupId>
<artifactId>nut-metrics</artifactId>
<version>${nut.version}</version>
</dependency>
<dependency>
<groupId>com.nutcore</groupId>
<artifactId>nut-utils</artifactId>
<version>${nut.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
Expand Down Expand Up @@ -140,7 +160,7 @@
</execution>
</executions>
<configuration>
<mainClass>${groupId}.Main</mainClass>
<mainClass>${package}.Main</mainClass>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#set( $symbol_escape = '\' )
package ${package};

import com.nutcore.nut.correlationid.CorrelationIdFilter;
import com.nutcore.nut.metrics.MetricsListener;
import com.nutcore.orientdb.javaee.orientdb.OrientDBFilter;
import com.nutcore.orientdb.javaee.orientdb.OrientDBJacksonProvider;
import com.nutcore.orientdb.javaee.orientdb.OrientDBServletContextListener;
Expand All @@ -12,10 +14,14 @@
import io.undertow.servlet.api.FilterInfo;
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.weld.environment.servlet.Listener;

import javax.servlet.DispatcherType;
import java.util.Arrays;

import static com.nutcore.nut.correlationid.CorrelationIdFilter.CORRELATION_ID_FILTER;
import static com.nutcore.nut.metrics.MetricsListener.METRICS_LISTENER;

public class Main
{
private static final String ORIENTDB_FILTER = "orientdb";
Expand Down Expand Up @@ -43,18 +49,28 @@ private static void startWebServer(String[] args)
deployment.setInjectorFactoryClass("org.jboss.resteasy.cdi.CdiInjectorFactory");
deployment.setApplicationClass(MyApplication.class.getName());

Undertow.Builder builder = Undertow.builder().addHttpListener(port, host);
Undertow.Builder builder = Undertow
.builder()
.addHttpListener(port, host);

UndertowJaxrsServer server = new UndertowJaxrsServer().start(builder);
DeploymentInfo deploymentInfo = server
.undertowDeployment(deployment)
.setClassLoader(Main.class.getClassLoader())
.addListener(Servlets.listener(OrientDBServletContextListener.class))
.addListeners(Servlets.listener(org.jboss.weld.environment.servlet.Listener.class))
.setContextPath("/api")
.setDeploymentName("${artifactId}")
.addFilter(Servlets.filter(METRICS_LISTENER, MetricsListener.class))
.addFilterUrlMapping(METRICS_LISTENER, "/*", DispatcherType.REQUEST)
.addListeners(Servlets.listener(Listener.class))
.addServlets(
Servlets.servlet(com.codahale.metrics.servlets.MetricsServlet.class)
.addMapping("/metrics"))
.addFilter(new FilterInfo(ORIENTDB_FILTER, OrientDBFilter.class))
.addFilterUrlMapping(ORIENTDB_FILTER, "/*", DispatcherType.REQUEST);
.addFilterUrlMapping(ORIENTDB_FILTER, "/*", DispatcherType.REQUEST)
.addFilter(new FilterInfo(CORRELATION_ID_FILTER, CorrelationIdFilter.class))
.addFilterUrlMapping(CORRELATION_ID_FILTER, "/*", DispatcherType.REQUEST)
.setContextPath("/api")
.setDeploymentName("${artifactId}");

server.deploy(deploymentInfo);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
import com.nutcore.nut.httputils.CORSFilter;

public class MyApplication extends Application
{
Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions nut-corelationid/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nut</artifactId>
<groupId>com.nutcore</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>nut-corelationid</artifactId>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nutcore.nut.correlationid;

/**
* Created by davidecerbo on 07/11/2016.
*/
public class CorrelationId
{
private final String id;
private final String source;
private final Long creationTime;

public CorrelationId(String id, String source, Long creationTime)
{
this.id = id;
this.source = source;
this.creationTime = creationTime;
}

public String getId()
{
return id;
}

public String getSource()
{
return source;
}

public Long getCreationTime()
{
return creationTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.nutcore.nut.correlationid;

import org.slf4j.MDC;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* Created by davidecerbo on 07/11/2016.
*/
public class CorrelationIdFilter implements Filter
{

public static final String CORRELATION_ID_FILTER = "correlationIDFilter";

public static final int CORRELLATION_ID_MAX_LENGTH = 36;

public static final String CORRELATION_ID_HEADER = "X-Correlation-Id";
public static final String CORRELATION_ID_SOURCE_HEADER = "X-Correlation-Id-source";

public static final String CORRELATION_ID_TIME_HEADER = "X-Correlation-Id-time";

private static final String CORELLATION_ID_LOG_KEY = "correlationId";
private static final String CORELLATION_ID_SOURCE_LOG_KEY = "correlationIdSource";
private static final String CORELLATION_ID_TIME_LOG_KEY = "correlationIdTime";


@Override
public void init(FilterConfig filterConfig) throws ServletException
{

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String correlationId = httpServletRequest.getHeader(CORRELATION_ID_HEADER);
String source = httpServletRequest.getHeader(CORRELATION_ID_SOURCE_HEADER);
Long creationDate = getCreationTime(httpServletRequest);
if (correlationId == null || correlationId.isEmpty())
{
correlationId = generateCorrelationID();
source = getFullURL(httpServletRequest);
creationDate = System.currentTimeMillis();
}
CorrelationId correlationIdObj = new CorrelationId(correlationId, source, creationDate);
setCorellationId((HttpServletResponse) servletResponse, correlationIdObj);

filterChain.doFilter(servletRequest, servletResponse);
}

protected String generateCorrelationID()
{
return UUID.randomUUID().toString();
}

private void setCorellationId(HttpServletResponse servletResponse, CorrelationId correlationIdObj)
{
correlationIdObj = truncIfTooLong(correlationIdObj);
setInHeader(servletResponse, correlationIdObj);
setInLog(correlationIdObj);
setInThreadLocal(correlationIdObj);
}

private void setInThreadLocal(CorrelationId correlationIdObj)
{
CorrelationIdUtil.setId(correlationIdObj);
}

private void setInHeader(HttpServletResponse servletResponse, CorrelationId correlationIdObj)
{
servletResponse.setHeader(CORRELATION_ID_HEADER, correlationIdObj.getId());
servletResponse.setHeader(CORRELATION_ID_SOURCE_HEADER, correlationIdObj.getSource());
servletResponse.setHeader(CORRELATION_ID_TIME_HEADER, correlationIdObj.getCreationTime().toString());
}

private void setInLog(CorrelationId correlationIdObj)
{
Map<String, String> map = new HashMap<>();
map.put(CORELLATION_ID_LOG_KEY, correlationIdObj.getId());
map.put(CORELLATION_ID_SOURCE_LOG_KEY, correlationIdObj.getSource());
map.put(CORELLATION_ID_TIME_LOG_KEY, correlationIdObj.getCreationTime().toString());
MDC.setContextMap(map);
}

private String getFullURL(HttpServletRequest request) {
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();

if (queryString == null) {
return requestURL.toString();
} else {
return requestURL.append('?').append(queryString).toString();
}
}

private CorrelationId truncIfTooLong(CorrelationId correlationIdObj)
{
String correlationId = correlationIdObj.getId();
if (correlationId.length() > CORRELLATION_ID_MAX_LENGTH)
{
return new CorrelationId(correlationId.substring(0, CORRELLATION_ID_MAX_LENGTH), correlationIdObj.getSource(), correlationIdObj.getCreationTime());
}
return correlationIdObj;
}

private long getCreationTime(HttpServletRequest httpServletRequest)
{
long time = 0;
try
{
time = Long.parseLong(httpServletRequest.getHeader(CORRELATION_ID_TIME_HEADER));
} catch (NumberFormatException e)
{
//log
}
return time;
}

@Override
public void destroy()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.nutcore.nut.correlationid;

import javax.enterprise.inject.Produces;


/**
* Created by davidecerbo on 05/11/2016.
*/
public class CorrelationIdProducer
{
@Produces
public CorrelationId getCorrelationId() {
return CorrelationIdUtil.getId();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.nutcore.nut.correlationid;

/**
* Created by davidecerbo on 04/11/2016.
*/
public class CorrelationIdUtil
{

private static final ThreadLocal<CorrelationId> db = new ThreadLocal<>();

public static CorrelationId getId()
{
return db.get();
}

static void setId(CorrelationId oObjectDatabaseTx)
{
db.set(oObjectDatabaseTx);
}

}
Loading

0 comments on commit 6d6d1fb

Please sign in to comment.