@Override public boolean shouldProcessParam(String paramName, Channel channel) { final String messageTypePropertyValue = getProfiledOrDefaultProperty(channel.name()); final List<String> patterns = extractPatterns(messageTypePropertyValue); return anyPatternMatchesParamName(patterns, paramName); }
@Override public int generatedRequestIdLength() { return parseIntOrZero(getProfiledOrDefaultProperty(GENERATE_REQUEST_ID)); }
private String getProfiledOrDefaultProperty(String propertyName) { if (profileName != null) { final String profiledProperty = propertyChain.getProperty(TRACEE_CONFIG_PREFIX + profileName + "." + propertyName); if (profiledProperty != null) return profiledProperty; } return propertyChain.getProperty(TRACEE_CONFIG_PREFIX + DEFAULT_PROFILE_PREFIX + propertyName); }
void httpRequestInitialized(HttpServletRequest request) { final TraceeFilterConfiguration configuration = backend.getConfiguration(); if (configuration.shouldProcessContext(IncomingRequest)) { mergeIncomingContextToBackend(request); } if (configuration.shouldGenerateRequestId() && !backend.containsKey(TraceeConstants.REQUEST_ID_KEY)) { backend.put(TraceeConstants.REQUEST_ID_KEY, Utilities.createRandomAlphanumeric(configuration.generatedRequestIdLength())); } if (configuration.shouldGenerateSessionId() && !backend.containsKey(TraceeConstants.SESSION_ID_KEY)) { final HttpSession session = request.getSession(false); if (session != null) { backend.put(TraceeConstants.SESSION_ID_KEY, anonymizedSessionKey(session.getId(), configuration.generatedSessionIdLength())); } } }
private void writeContextToResponse(HttpServletResponse response, TraceeFilterConfiguration configuration) { if (configuration.shouldProcessContext(OutgoingResponse) && !backend.isEmpty()) { final Map<String, String> filteredContext = backend.getConfiguration(profile).filterDeniedParams(backend, OutgoingResponse); response.setHeader(HTTP_HEADER_NAME, transportSerialization.render(filteredContext)); } }
@Override public final void sessionCreated(HttpSessionEvent httpSessionEvent) { if (backend.getConfiguration().shouldGenerateSessionId()) { final String sessionId = httpSessionEvent.getSession().getId(); backend.put(TraceeConstants.SESSION_ID_KEY, anonymizedSessionKey(sessionId, backend.getConfiguration().generatedSessionIdLength())); } }
private void mergeIncomingContextToBackend(HttpServletRequest request) { final Enumeration headers = request.getHeaders(HTTP_HEADER_NAME); if (headers == null) { throw new IllegalStateException("Could not read headers with name '" + HTTP_HEADER_NAME + "'. The access seem to be forbidden by the container."); } final Map<String, String> parsed = new HashMap<String, String>(); while (headers.hasMoreElements()) { parsed.putAll(transportSerialization.parse((String) headers.nextElement())); } final Map<String, String> filtered = backend.getConfiguration().filterDeniedParams(parsed, IncomingRequest); backend.putAll(filtered); }
private PropertyChain loadPropertyChain() { try { final Properties traceeDefaultFileProperties = new TraceePropertiesFileLoader().loadTraceeProperties(TRACEE_DEFAULT_PROPERTIES_FILE); final Properties traceeFileProperties = new TraceePropertiesFileLoader().loadTraceeProperties(TRACEE_PROPERTIES_FILE); return PropertyChain.build(System.getProperties(), traceeFileProperties, traceeDefaultFileProperties); } catch (IOException ioe) { throw new IllegalStateException("Could not load TraceeProperties: " + ioe.getMessage(), ioe); } }
@Override public boolean shouldProcessContext(Channel channel) { final String messageTypePropertyValue = getProfiledOrDefaultProperty(channel.name()); return !Utilities.isNullOrEmptyString(messageTypePropertyValue); }
/** * Lazily initializes the configuration for this MDCLikeTraceeBackend. */ @Override public final TraceeFilterConfiguration getConfiguration() { if (lazyPropertyChain == null) { lazyPropertyChain = loadPropertyChain(); } return new PropertiesBasedTraceeFilterConfiguration(lazyPropertyChain); }
private boolean anyPatternMatchesParamName(Iterable<String> patterns, String paramName) { for (String pattern : patterns) { if (patternMatchesParamName(pattern, paramName)) return true; } return false; }
@Override public boolean shouldGenerateSessionId() { return generatedSessionIdLength() > 0; }
@Override public boolean shouldGenerateRequestId() { return generatedRequestIdLength() > 0; }
public static PropertyChain build(Properties ... properties) { return new PropertyChain(Arrays.asList(properties)); }
@Override public Map<String, String> filterDeniedParams(Map<String, String> unfiltered, Channel channel) { final HashMap<String, String> filtered = new HashMap<String, String>(unfiltered.size()); for (Map.Entry<String, String> entry : unfiltered.entrySet()) { if (shouldProcessParam(entry.getKey(), channel)) { filtered.put(entry.getKey(), entry.getValue()); } } return filtered; }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { final TraceeFilterConfiguration configuration = backend.getConfiguration(profileName); if (configuration.shouldProcessContext(IncomingRequest)) mergeIncomingContextToBackend(request, configuration); // create random RequestId if not already set if (!backend.containsKey(TraceeConstants.REQUEST_ID_KEY) && configuration.shouldGenerateRequestId()) { backend.put(TraceeConstants.REQUEST_ID_KEY, Utilities.createRandomAlphanumeric(configuration.generatedRequestIdLength())); } // create another random id to identify the http session if (!backend.containsKey(TraceeConstants.SESSION_ID_KEY) && configuration.shouldGenerateSessionId()) { final HttpSession session = request.getSession(false); if (session != null) { backend.put(TraceeConstants.SESSION_ID_KEY, Utilities.createAlphanumericHash(session.getId(), configuration.generatedSessionIdLength())); } } return true; }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception { final TraceeFilterConfiguration configuration = backend.getConfiguration(profileName); if (configuration.shouldProcessContext(OutgoingResponse) && !backend.isEmpty()) { final Map<String, String> filteredContext = configuration.filterDeniedParams(backend, OutgoingResponse); response.setHeader(outgoingHeaderName, httpJsonHeaderSerialization.render(filteredContext)); } backend.clear(); }
final void parseSoapHeaderToBackend(SOAPHeader soapHeader) { final Map<String, String> parsedContext = transportSerialization.parse(soapHeader); final Map<String, String> filteredContext = getTraceeBackend().getConfiguration().filterDeniedParams(parsedContext, OutgoingRequest); getTraceeBackend().putAll(filteredContext); }
@Override public int generatedSessionIdLength() { return parseIntOrZero(getProfiledOrDefaultProperty(GENERATE_SESSION_ID)); }
@Override public final TraceeFilterConfiguration getConfiguration(String profileName) { if (lazyPropertyChain == null) { lazyPropertyChain = loadPropertyChain(); } return new PropertiesBasedTraceeFilterConfiguration(lazyPropertyChain, profileName); }