Commit 3aeace48 by Tuomas Riihimäki

Merge branch 'feature/userlog-and-testfix' into 'master'

userlog, testfix and breakglass cleanup

- Add user information to SLF4J MDC logging context, so user information can be attached to log rows. MDC is ignored if the logging implementation does not support MDC.
- Fix the code that caused JSON related test to fail. Also add another test to be sure.
- Clean up after jvm-breakglass introspection tool

There also seems to be an eclipse file in the repository and the file changed. Might not be relevant.

See merge request !127
2 parents d2ba7174 67bd2ad5
eclipse.preferences.version=1 eclipse.preferences.version=1
entitygen.DEFAULT_PACKAGE=model
org.eclipse.jpt.core.platform=eclipselink2_4 org.eclipse.jpt.core.platform=eclipselink2_4
org.eclipse.jpt.jpa.core.discoverAnnotatedClasses=true org.eclipse.jpt.jpa.core.discoverAnnotatedClasses=true
org.eclipse.jpt.jpa.core.metamodelSourceFolderName=target/generated-sources/annotations org.eclipse.jpt.jpa.core.metamodelSourceFolderName=target/generated-sources/annotations
...@@ -70,12 +70,13 @@ public class JsonUtils { ...@@ -70,12 +70,13 @@ public class JsonUtils {
* The value associate to the key * The value associate to the key
* @return JsonObject with the key:value pair added * @return JsonObject with the key:value pair added
*/ */
public static JsonObject assocJsonObject(JsonObject jsonObject, String key, JsonValue value) { public static JsonObject assocJsonObject(JsonObject jsonObject, String key,
JsonValue value) {
JsonObjectBuilder builder = Json.createObjectBuilder(); JsonObjectBuilder builder = Json.createObjectBuilder();
// Copy all non conflicting json entries // Copy all non conflicting json entries
for (Map.Entry<String, JsonValue> v : jsonObject.entrySet()) { for (Map.Entry<String, JsonValue> v : jsonObject.entrySet()) {
if (v.getKey().equals(key)) { if (v.getKey().equals(key) == false) {
builder = builder.add(v.getKey(), v.getValue()); builder = builder.add(v.getKey(), v.getValue());
} }
} }
...@@ -102,7 +103,7 @@ public class JsonUtils { ...@@ -102,7 +103,7 @@ public class JsonUtils {
// Recurse? // Recurse?
if (keys.size() > 1) { if (keys.size() > 1) {
String firstKey = keys.get(0); String firstKey = keys.get(0);
List<String> restKeys = keys.subList(0, keys.size()); List<String> restKeys = keys.subList(1, keys.size());
JsonObject subObj = jsonObject.getJsonObject(firstKey); JsonObject subObj = jsonObject.getJsonObject(firstKey);
return assocJsonObject(jsonObject, firstKey, return assocJsonObject(jsonObject, firstKey,
assocInJsonObject(subObj, restKeys, value)); assocInJsonObject(subObj, restKeys, value));
......
...@@ -64,7 +64,21 @@ public class JsonUtilsTest { ...@@ -64,7 +64,21 @@ public class JsonUtilsTest {
JsonObject actual = JsonUtils.alterSubObject(meta, path, newData); JsonObject actual = JsonUtils.alterSubObject(meta, path, newData);
JsonObject expected = jsonObject("{\"foo\":\"bar\",\"baz\":{\"quux\":\"plop\"}}"); JsonObject expected = jsonObject("{\"foo\":\"bar\",\"baz\":{\"quux\":\"plop\"}}");
Assert.assertEquals(expected.toString(), actual.toString()); Assert.assertEquals(actual.toString(), expected.toString());
} }
@Test
public final void testAlterSubSubObject() {
JsonObject meta = jsonObject("{\"bystander1\":\"canary1\", \"module1\":{\"bystander2\":\"canary2\",\"submodule1\":{\"replacee1\":\"replacedvalue1\",\"r2\":\"rv2\"}}}");
JsonObject newData = jsonObject("{\"quux\":\"plop\"}");
ArrayList<String> path = new ArrayList<String>();
path.add("module1");
path.add("submodule1");
JsonObject actual = JsonUtils.alterSubObject(meta, path, newData);
JsonObject expected = jsonObject("{\"bystander1\":\"canary1\", \"module1\":{\"bystander2\":\"canary2\",\"submodule1\":{\"quux\":\"plop\"}}}");
Assert.assertEquals(actual.toString(), expected.toString());
}
} }
...@@ -33,8 +33,10 @@ import javax.servlet.ServletResponse; ...@@ -33,8 +33,10 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpRequest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import fi.codecrew.moya.beans.RestBeanLocal; import fi.codecrew.moya.beans.RestBeanLocal;
import fi.codecrew.moya.beans.SessionMgmtBeanLocal; import fi.codecrew.moya.beans.SessionMgmtBeanLocal;
...@@ -88,6 +90,27 @@ public class HostnameFilter implements Filter { ...@@ -88,6 +90,27 @@ public class HostnameFilter implements Filter {
UNKNOWN, ANON, REST, USER UNKNOWN, ANON, REST, USER
} }
/**
* Add user information to SLF4J MDC context, so current user can be shown
* in logs.
*
* @param request
* @param authType
*/
void insertLoggingContext(HttpServletRequest request, AuthType authType) {
String userString = request.getUserPrincipal().getName();
MDC.put("user", userString);
MDC.put("authtype", authType.name());
}
/**
* Remove user info from SLF4J MDC context.
*/
void removeLoggingContext() {
MDC.remove("user");
}
/** /**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/ */
...@@ -131,6 +154,7 @@ public class HostnameFilter implements Filter { ...@@ -131,6 +154,7 @@ public class HostnameFilter implements Filter {
} }
// pass the request along the filter chain // pass the request along the filter chain
try { try {
insertLoggingContext((HttpServletRequest)request, authtype);
chain.doFilter(request, response); chain.doFilter(request, response);
} catch (Throwable t) { } catch (Throwable t) {
if (AuthType.REST == authtype) { if (AuthType.REST == authtype) {
...@@ -139,6 +163,7 @@ public class HostnameFilter implements Filter { ...@@ -139,6 +163,7 @@ public class HostnameFilter implements Filter {
throw t; throw t;
} finally { } finally {
BortalLocalContextHolder.cleanupThread(); BortalLocalContextHolder.cleanupThread();
removeLoggingContext();
} }
} }
......
...@@ -54,6 +54,9 @@ public class ReplServlet implements Servlet { ...@@ -54,6 +54,9 @@ public class ReplServlet implements Servlet {
public void destroy() { public void destroy() {
if (nreplServer != null) { if (nreplServer != null) {
nreplServer.stop(); nreplServer.stop();
nreplServer.unregisterMBean();
nreplServer.clear();
nreplServer = null;
} }
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!