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
entitygen.DEFAULT_PACKAGE=model
org.eclipse.jpt.core.platform=eclipselink2_4
org.eclipse.jpt.jpa.core.discoverAnnotatedClasses=true
org.eclipse.jpt.jpa.core.metamodelSourceFolderName=target/generated-sources/annotations
......@@ -70,12 +70,13 @@ public class JsonUtils {
* The value associate to the key
* @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();
// Copy all non conflicting json entries
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());
}
}
......@@ -102,7 +103,7 @@ public class JsonUtils {
// Recurse?
if (keys.size() > 1) {
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);
return assocJsonObject(jsonObject, firstKey,
assocInJsonObject(subObj, restKeys, value));
......
......@@ -64,7 +64,21 @@ public class JsonUtilsTest {
JsonObject actual = JsonUtils.alterSubObject(meta, path, newData);
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import fi.codecrew.moya.beans.RestBeanLocal;
import fi.codecrew.moya.beans.SessionMgmtBeanLocal;
......@@ -88,6 +90,27 @@ public class HostnameFilter implements Filter {
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)
*/
......@@ -131,6 +154,7 @@ public class HostnameFilter implements Filter {
}
// pass the request along the filter chain
try {
insertLoggingContext((HttpServletRequest)request, authtype);
chain.doFilter(request, response);
} catch (Throwable t) {
if (AuthType.REST == authtype) {
......@@ -139,6 +163,7 @@ public class HostnameFilter implements Filter {
throw t;
} finally {
BortalLocalContextHolder.cleanupThread();
removeLoggingContext();
}
}
......
......@@ -54,6 +54,9 @@ public class ReplServlet implements Servlet {
public void destroy() {
if (nreplServer != null) {
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!