Skip to content

Commit

Permalink
created a ClientRequestFilter to use with correlation id
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Cerbo committed Nov 14, 2016
1 parent ef06d73 commit f06b32f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<User> getAll()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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()));
}
}

}

0 comments on commit f06b32f

Please sign in to comment.