-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do a refactoring to move in other projects some classes to have a bet…
…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
Showing
29 changed files
with
800 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
33 changes: 33 additions & 0 deletions
33
nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.