Skip to content

Commit

Permalink
Support the version 4 uuid string on XTrackMDCPutFilter terasolunaorg…
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo committed Nov 14, 2018
1 parent 9956f1f commit ecc20f9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,39 @@
import javax.servlet.http.HttpServletResponse;

/**
* Set random value per request to MDC and HTTP Response Header and HTTP Request Attribute (request scope). <br>
* Set random value(track ID) per request to MDC and HTTP Response Header and HTTP Request Attribute (request scope). <br>
* <p>
* default attribute name is "X-Track". You can change this name by configure. The value of X-Track is retrieved from HTTP
* Request Header (same attribute name).<br>
* If the attibute is not set in HTTP Request Header, this filter creates random value as X-Track and use it.
*
* The value of track ID is retrieved from HTTP Request Header.<br>
* If the attribute is not set in HTTP Request Header, this filter creates random value as track ID and use it.
*
* <p>
* An attribute name of track ID is "X-Track" by default.
* Also you can change to an any name as follow:
*
* <pre>
* in web.xml
* <code>
* &lt;init-param&gt;
* &lt;param-name&gt;attributeName&lt;/param-name&gt;
* &lt;param-value&gt;XXXX&lt;/param-value&gt;
* &lt;param-value&gt;Tracking-Id&lt;/param-value&gt;
* &lt;/init-param&gt;
* </code>
* </pre>
*
*
* <p>
* A random value is the 32 length HEX string(trimmed the '-' character from version 4 uuid string) by default.
* Also you can change to use a version 4 uuid string(not trimmed the '-' character) as follow:
*
* <pre>
* in web.xml
* <code>
* &lt;init-param&gt;
* &lt;param-name&gt;useV4Uuid&lt;/param-name&gt;
* &lt;param-value&gt;true&lt;/param-value&gt;
* &lt;/init-param&gt;
* </code>
* </pre>
*
*/
public class XTrackMDCPutFilter extends AbstractMDCPutFilter {

Expand All @@ -51,6 +68,16 @@ public class XTrackMDCPutFilter extends AbstractMDCPutFilter {
*/
private String attributeName = "X-Track";

/**
* Whether use the version 4 uuid string when a track ID will create
*/
private boolean useV4Uuid;

/**
* Whether there was specified the {@code maxMDCValueLength} by user
*/
private boolean specifiedMaxMDCValueLength;

/**
* set attribute name to set MDC and HTTP Response Header<br>
* @param attributeName attribute name
Expand All @@ -59,6 +86,37 @@ public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}

/**
* set whether use the version 4 uuid string when a track ID will create.
* <p>
* Default is {@code false}.
* </p>
* @param useV4Uuid If use the version 4 uuid string, set to {@code true}
*/
public void setUseV4Uuid(boolean useV4Uuid) {
this.useV4Uuid = useV4Uuid;
}

/**
* {@inheritDoc}
*/
@Override
public void setMaxMDCValueLength(int maxMDCValueLength) {
super.setMaxMDCValueLength(maxMDCValueLength);
this.specifiedMaxMDCValueLength = true;
}

/**
* If the {@code useV4Uuid} is {@code true} and the {@code maxMDCValueLength } not present,
* set the {@code maxMDCValueLength} to {@code 36}(length of version 4 uuid string).
*/
@Override
protected void initFilterBean() {
if (!specifiedMaxMDCValueLength && useV4Uuid) {
setMaxMDCValueLength(36);
}
}

/**
* <p>
* get attribute name to set MDC and HTTP Response Header. (default: X-Track)
Expand All @@ -75,13 +133,14 @@ protected String getMDCKey(HttpServletRequest request,
/**
* Create track ID (X-Track)<br>
* <p>
* returns 32-length random HEX string.
* returns 32-length random HEX string or version 4 uuid string.
* </p>
* @return X-Track
*/
protected String createXTrack() {
String uuid = UUID.randomUUID().toString();
String xTrack = UUID_REPLACE_PATTERN.matcher(uuid).replaceAll("");
String xTrack = useV4Uuid ? uuid
: UUID_REPLACE_PATTERN.matcher(uuid).replaceAll("");
return xTrack;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,38 @@ public void testGetMDCValue_default_attributeName_set_in_http_request_too_long_l
assertThat((String) request.getAttribute("X-Track"), is(
"12345678901234567890123456789012"));
}

@Test
public void testGetMDCValue_useV4Uuid() throws ServletException {
mockFilterConfig.addInitParameter("useV4Uuid", "true");
xTrackMDCPutFilter.init(mockFilterConfig);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();

String xTrack = xTrackMDCPutFilter.getMDCValue(request, response);
assertThat(xTrack, is(notNullValue()));
assertThat(xTrack.matches(
"^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"),
is(true));
assertThat(response.getHeader("X-Track"), is(xTrack));
assertThat((String) request.getAttribute("X-Track"), is(xTrack));
}

@Test
public void testGetMDCValue_useV4Uuid_when_specified_maxMDCValueLength() throws ServletException {
mockFilterConfig.addInitParameter("useV4Uuid", "true");
mockFilterConfig.addInitParameter("maxMDCValueLength", "39");
xTrackMDCPutFilter.init(mockFilterConfig);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();

request.addHeader("X-Track",
"1234567890123456789012345678901234567890");

String xTrack = xTrackMDCPutFilter.getMDCValue(request, response);
assertThat(xTrack, is("123456789012345678901234567890123456789"));
assertThat(response.getHeader("X-Track"), is(xTrack));
assertThat((String) request.getAttribute("X-Track"), is(xTrack));
}

}

0 comments on commit ecc20f9

Please sign in to comment.