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)); } }
/** * Generate invocation id if it doesn't exist in TraceeBackend and configuration asks for one * * @param backend Currently used TraceeBackend */ public static void generateInvocationIdIfNecessary(final TraceeBackend backend) { if (backend != null && !backend.containsKey(TraceeConstants.INVOCATION_ID_KEY) && backend.getConfiguration().shouldGenerateInvocationId()) { backend.put(TraceeConstants.INVOCATION_ID_KEY, Utilities.createRandomAlphanumeric(backend.getConfiguration().generatedInvocationIdLength())); } }
void cleanUp() { if (backend.getConfiguration().shouldProcessContext(AsyncProcess)) { backend.clear(); } }
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); } }
/** * Generate session id hash if it doesn't exist in TraceeBackend and configuration asks for one * * @param backend Currently used TraceeBackend * @param sessionId Current http sessionId */ public static void generateSessionIdIfNecessary(final TraceeBackend backend, final String sessionId) { if (backend != null && !backend.containsKey(TraceeConstants.SESSION_ID_KEY) && backend.getConfiguration().shouldGenerateSessionId()) { backend.put(TraceeConstants.SESSION_ID_KEY, Utilities.createAlphanumericHash(sessionId, backend.getConfiguration().generatedSessionIdLength())); } } }
void cleanUp() { if (backend.getConfiguration().shouldProcessContext(AsyncProcess)) { backend.clear(); } }
private void handleHttpMessage(final Message message, final TraceeFilterConfiguration filterConfiguration) { final Map<String, List<String>> requestHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS)); if (requestHeaders != null && !requestHeaders.isEmpty()) { final List<String> traceeHeader = requestHeaders.get(TraceeConstants.TPIC_HEADER); if (traceeHeader != null && !traceeHeader.isEmpty()) { final Map<String, String> parsedContext = httpJsonSerializer.parse(traceeHeader); backend.putAll(filterConfiguration.filterDeniedParams(parsedContext, channel)); } } }
@Override public final void process(HttpResponse response, HttpContext context) { final TraceeFilterConfiguration filterConfiguration = backend.getConfiguration(profile); final Header[] responseHeaders = response.getHeaders(TraceeConstants.TPIC_HEADER); if (responseHeaders != null && responseHeaders.length > 0 && filterConfiguration.shouldProcessContext(IncomingResponse)) { final List<String> stringTraceeHeaders = new ArrayList<>(); for (Header header : responseHeaders) { stringTraceeHeaders.add(header.getValue()); } backend.putAll(filterConfiguration.filterDeniedParams(transportSerialization.parse(stringTraceeHeaders), IncomingResponse)); } } }
private void httpRequestInitialized(HttpServletRequest request) { final TraceeFilterConfiguration configuration = backend.getConfiguration(); if (configuration.shouldProcessContext(IncomingRequest)) { mergeIncomingContextToBackend(request); } Utilities.generateRequestIdIfNecessary(backend); final HttpSession session = request.getSession(false); if (session != null) { Utilities.generateSessionIdIfNecessary(backend, session.getId()); } }
private void handleSoapMessage(final SoapMessage message, final TraceeFilterConfiguration filterConfiguration) { final Header soapHeader = message.getHeader(TraceeConstants.SOAP_HEADER_QNAME); if (soapHeader != null) { final Map<String, String> parsedContext = httpSoapSerializer.parseTpicHeader((Element) soapHeader.getObject()); backend.putAll(filterConfiguration.filterDeniedParams(parsedContext, channel)); } } }
@Override public final void process(HttpResponse response, HttpContext context) { final TraceeFilterConfiguration filterConfiguration = backend.getConfiguration(profile); final Iterator<Header> headerIterator = response.headerIterator(TraceeConstants.TPIC_HEADER); if (headerIterator != null && headerIterator.hasNext() && filterConfiguration.shouldProcessContext(IncomingResponse)) { final List<String> stringTraceeHeaders = new ArrayList<>(); while (headerIterator.hasNext()) { stringTraceeHeaders.add(headerIterator.next().getValue()); } backend.putAll(filterConfiguration.filterDeniedParams(transportSerialization.parse(stringTraceeHeaders), IncomingResponse)); } } }
@Override public void handleMessage(final Message message) { if (shouldHandleMessage(message)) { final TraceeFilterConfiguration filterConfiguration = backend.getConfiguration(profile); LOGGER.debug("Interceptor handles message!"); if (filterConfiguration.shouldProcessContext(channel)) { if (Boolean.TRUE.equals(message.getExchange().get(Message.REST_MESSAGE))) { handleHttpMessage(message, filterConfiguration); } else { try { handleSoapMessage((SoapMessage) message, filterConfiguration); } catch (NoClassDefFoundError e) { LOGGER.error("Should handle SOAP-message but it seems that cxf soap dependency is not on the classpath. Unable to parse Tracee-Headers: {}", e.getMessage(), e); } } } } }
private void writeHeaderIfUncommitted(HttpServletResponse response) { if (!response.isCommitted() && !backend.isEmpty()) { final TraceeFilterConfiguration configuration = backend.getConfiguration(profileName); if (configuration.shouldProcessContext(OutgoingResponse)) { final Map<String, String> filteredContext = configuration.filterDeniedParams(backend.copyToMap(), OutgoingResponse); response.setHeader(outgoingHeaderName, httpHeaderSerialization.render(filteredContext)); } } }
private void postResponse(HttpMethod httpMethod) { if (!httpMethod.isRequestSent()) return; final Header[] responseHeaders = httpMethod.getResponseHeaders(TraceeConstants.TPIC_HEADER); final TraceeFilterConfiguration filterConfiguration = backend.getConfiguration(profile); if (responseHeaders != null && responseHeaders.length > 0 && filterConfiguration.shouldProcessContext(IncomingResponse)) { final List<String> stringTraceeHeaders = new ArrayList<>(); for (Header header : responseHeaders) { stringTraceeHeaders.add(header.getValue()); } backend.putAll(filterConfiguration.filterDeniedParams(transportSerialization.parse(stringTraceeHeaders), IncomingResponse)); } }
@Override public void jobToBeExecuted(JobExecutionContext context) { final TraceeFilterConfiguration configuration = backend.getConfiguration(profile); if (configuration.shouldProcessContext(AsyncProcess)) { @SuppressWarnings("unchecked") final Map<String, String> traceeContext = (Map<String, String>) context.getMergedJobDataMap().get(TraceeConstants.TPIC_HEADER); if (traceeContext != null && !traceeContext.isEmpty()) { final Map<String, String> filteredContext = configuration.filterDeniedParams(traceeContext, AsyncProcess); backend.putAll(filteredContext); } } Utilities.generateInvocationIdIfNecessary(backend); }
private void writeContextToResponse(final HttpServletResponse response, final TraceeFilterConfiguration configuration) { if (!backend.isEmpty() && configuration.shouldProcessContext(OutgoingResponse)) { final Map<String, String> filteredContext = backend.getConfiguration(profile).filterDeniedParams(backend.copyToMap(), OutgoingResponse); response.setHeader(HTTP_HEADER_NAME, transportSerialization.render(filteredContext)); } }
public void injectContext(JobDataMap jobDataMap) { final TraceeFilterConfiguration configuration = backend.getConfiguration(profile); if (!backend.isEmpty() && configuration.shouldProcessContext(AsyncDispatch)) { jobDataMap.put(TraceeConstants.TPIC_HEADER, backend.getConfiguration(profile).filterDeniedParams(backend.copyToMap(), AsyncDispatch)); } } }
@SuppressWarnings("unchecked") public void beforeProcessing(Message message) throws JMSException { if (backend.getConfiguration().shouldProcessContext(AsyncProcess)) { final Object encodedTraceeContext = message.getObjectProperty(TraceeConstants.JMS_HEADER_NAME); if (encodedTraceeContext != null) { final Map<String, String> contextFromMessage = (Map<String, String>) encodedTraceeContext; final Map<String, String> filteredContext = backend.getConfiguration().filterDeniedParams(contextFromMessage, AsyncProcess); backend.putAll(filteredContext); } } }
/** * Writes the current TraceeContext to the given javaee message. * This method is idempotent. */ protected void writeTraceeContextToMessage(Message message) throws JMSException { if (!backend.isEmpty() && backend.getConfiguration().shouldProcessContext(AsyncDispatch)) { final Map<String, String> filteredContext = backend.getConfiguration().filterDeniedParams(backend, AsyncDispatch); message.setObjectProperty(TraceeConstants.JMS_HEADER_NAME, filteredContext); } }
private void preRequest(final HttpRequest request) { final TraceeFilterConfiguration filterConfiguration = backend.getConfiguration(profile); if (!backend.isEmpty() && filterConfiguration.shouldProcessContext(OutgoingRequest)) { final Map<String, String> filteredParams = filterConfiguration.filterDeniedParams(backend.copyToMap(), OutgoingRequest); request.getHeaders().add(TraceeConstants.TPIC_HEADER, transportSerialization.render(filteredParams)); } }