From f06b32f020a83271d5329c5d8ebf7359b4b77b74 Mon Sep 17 00:00:00 2001 From: Davide Cerbo Date: Mon, 14 Nov 2016 13:57:43 +0100 Subject: [PATCH] created a ClientRequestFilter to use with correlation id --- .../src/main/java/resource/HelloResource.java | 20 ++++++++++++++ .../CorrelationIdClientRequestFilter.java | 27 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdClientRequestFilter.java diff --git a/nut-archetype/src/main/resources/archetype-resources/src/main/java/resource/HelloResource.java b/nut-archetype/src/main/resources/archetype-resources/src/main/java/resource/HelloResource.java index 0e47ea1..07c108a 100644 --- a/nut-archetype/src/main/resources/archetype-resources/src/main/java/resource/HelloResource.java +++ b/nut-archetype/src/main/resources/archetype-resources/src/main/java/resource/HelloResource.java @@ -5,6 +5,7 @@ import ${package}.domain.User; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; +import com.nutcore.nut.correlationid.CorrelationIdClientRequestFilter; import com.orientechnologies.orient.object.db.OObjectDatabaseTx; import io.swagger.annotations.Api; @@ -32,9 +33,28 @@ public User get(@PathParam("user") String username) user.setName(username); User result = db.save(user); db.commit(); + Client client = ClientBuilder + .newClient() + .register(new CorrelationIdClientRequestFilter()); + + logger.info("The correlation ID in this line must be equals to the next line"); + client.target("http://localhost:8080/api/") + .path("hello") + .path(username) + .path("correlationId") + .request(MediaType.APPLICATION_JSON) + .get(); + return result; } + @GET + @Path("/{user}/correlationId") + public void correlationIdTest(@PathParam("user") String username) + { + logger.info("The correlation ID in this line must be equals to the previous line"); + } + @GET public List getAll() { diff --git a/nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdClientRequestFilter.java b/nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdClientRequestFilter.java new file mode 100644 index 0000000..e91f577 --- /dev/null +++ b/nut-corelationid/src/main/java/com/nutcore/nut/correlationid/CorrelationIdClientRequestFilter.java @@ -0,0 +1,27 @@ +package com.nutcore.nut.correlationid; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.core.MultivaluedMap; +import java.io.IOException; +import java.util.Arrays; + +/** + * Created by davidecerbo on 14/11/2016. + */ +public class CorrelationIdClientRequestFilter implements ClientRequestFilter +{ + + @Override + public void filter(ClientRequestContext requestContext) throws IOException + { + CorrelationId correlationId = CorrelationIdUtil.getId(); + if(correlationId != null){ + MultivaluedMap headers = requestContext.getHeaders(); + headers.put(CorrelationIdFilter.CORRELATION_ID_HEADER, Arrays.asList(correlationId.getId())); + headers.put(CorrelationIdFilter.CORRELATION_ID_SOURCE_HEADER, Arrays.asList(correlationId.getSource())); + headers.put(CorrelationIdFilter.CORRELATION_ID_TIME_HEADER, Arrays.asList(correlationId.getCreationTime())); + } + } + +}