private void logUsedEncoder(CoderWrapper<Encoder> encoder, Session session) { if (LOGGER.isLoggable(Level.FINEST)) { if (session instanceof TyrusSession) { ((TyrusSession) session).getDebugContext() .appendLogMessage(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Encoding with: ", encoder); } } }
private DebugContext createDebugContext(UpgradeRequest upgradeRequest) { String thresholdHeader = upgradeRequest.getHeader(UpgradeRequest.TRACING_THRESHOLD); DebugContext.TracingThreshold threshold = tracingThreshold; Exception thresholdHeaderParsingError = null; if (thresholdHeader != null) { try { threshold = DebugContext.TracingThreshold.valueOf(thresholdHeader); } catch (Exception e) { thresholdHeaderParsingError = e; } } DebugContext debugContext; if (tracingType == DebugContext.TracingType.ALL || tracingType == DebugContext.TracingType.ON_DEMAND && upgradeRequest.getHeader(UpgradeRequest.ENABLE_TRACING_HEADER) != null) { debugContext = new DebugContext(threshold); } else { debugContext = new DebugContext(); } if (thresholdHeaderParsingError != null) { debugContext.appendTraceMessageWithThrowable(LOGGER, Level.WARNING, DebugContext.Type.MESSAGE_IN, thresholdHeaderParsingError, "An error occurred while parsing ", UpgradeRequest.TRACING_THRESHOLD, " header:", thresholdHeaderParsingError.getMessage()); } return debugContext; }
/** * Append a message to the log and to the list of trace messages that are sent in handshake response. * The logging will be postponed until the message can be provided with a session ID. Randomly generated session ID * is used if a session has not been created. * * @param logger logger to be used to log the message. * @param loggingLevel message level. * @param type type of the message. * @param t throwable that has been thrown. * @param messageParts message parts that will be stringified and concatenated to create a log message. */ public void appendTraceMessageWithThrowable(Logger logger, Level loggingLevel, Type type, Throwable t, Object... messageParts) { if (this.tracingLevel.intValue() <= loggingLevel.intValue()) { String message = stringifyMessageParts(messageParts); appendTracingHeader(message); } appendLogMessageWithThrowable(logger, loggingLevel, type, t, messageParts); }
private void logUpgradeRequest(UpgradeRequest upgradeRequest) { if (LOGGER.isLoggable(Level.FINE)) { debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_OUT, "Sending handshake " + "request:\n" + Utils.stringifyUpgradeRequest(upgradeRequest)); } else { if (logUpgradeMessages) { debugContext.appendStandardOutputMessage(DebugContext.Type.MESSAGE_OUT, "Sending handshake " + "request:\n" + Utils.stringifyUpgradeRequest(upgradeRequest)); } } }
/** * Append a message to the log, the logging will be postponed until the message can be provided with a session ID. * Randomly generated session ID is used if a session has not been created. * * @param logger logger to be used to log the message. * @param loggingLevel message level. * @param type type of the message. * @param t throwable that has been thrown. * @param messageParts message parts that will be stringified and concatenated to create a log message. */ public void appendLogMessageWithThrowable(Logger logger, Level loggingLevel, Type type, Throwable t, Object... messageParts) { if (logger.isLoggable(loggingLevel)) { String message = stringifyMessageParts(messageParts); if (sessionId == null) { logRecords.add(new LogRecord(logger, loggingLevel, type, message, t, false)); } else { if (t != null) { logger.log(loggingLevel, formatLogMessage(message, type, System.nanoTime()), t); } else { logger.log(loggingLevel, formatLogMessage(message, type, System.nanoTime())); } } } }
debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Received handshake request:\n" + Utils.stringifyUpgradeRequest(request)); if (protocolHandler == null) { handleUnsupportedVersion(request, response); debugContext.appendTraceMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Upgrade request contains unsupported version of Websocket protocol"); debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_OUT, "Sending handshake response:\n" + Utils.stringifyUpgradeResponse(response)); response.getHeaders().putAll(debugContext.getTracingHeaders()); debugContext.flush(); return HANDSHAKE_FAILED_UPGRADE_INFO; .appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.OTHER, "Connection ID: ", connectionId); debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_OUT, "Sending handshake response:\n" + Utils.stringifyUpgradeResponse(response) + "\n"); response.getHeaders().putAll(debugContext.getTracingHeaders()); return new SuccessfulUpgradeInfo(endpointWrapper, protocolHandler, incomingBufferSize, request, response, extensionContext, debugContext); response.getHeaders().putAll(debugContext.getTracingHeaders()); debugContext.flush(); return NOT_APPLICABLE_UPGRADE_INFO;
TyrusEndpointWrapper getEndpointWrapper(UpgradeRequest request, DebugContext debugContext) throws HandshakeException { if (endpointWrappers.isEmpty()) { return null; } final String requestPath = request.getRequestUri(); for (Match m : Match.getAllMatches(requestPath, endpointWrappers, debugContext)) { final TyrusEndpointWrapper endpointWrapper = m.getEndpointWrapper(); for (Map.Entry<String, String> parameter : m.getParameters().entrySet()) { request.getParameterMap().put(parameter.getKey(), Arrays.asList(parameter.getValue())); } if (endpointWrapper.upgrade(request)) { debugContext.appendTraceMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Endpoint selected as a match to the handshake URI: ", endpointWrapper.getEndpointPath()); debugContext.appendLogMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, "Target endpoint: ", endpointWrapper); return endpointWrapper; } } return null; }
debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Session opening refused: ", refuseDetail); session.close(new CloseReason(CloseReason.CloseCodes.TRY_AGAIN_LATER, refuseDetail)); } catch (IOException e) { debugContext.appendLogMessageWithThrowable(LOGGER, Level.WARNING, DebugContext.Type.MESSAGE_IN, e, e.getMessage()); if (!collector.isEmpty()) { Throwable t = collector.composeComprehensiveException(); debugContext.appendLogMessageWithThrowable(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, t, t.getMessage()); session.close(CloseReasons.UNEXPECTED_CONDITION.getCloseReason()); } catch (IOException e) { debugContext.appendLogMessageWithThrowable(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_IN, e, e.getMessage()); onError.invoke(toCall, session, t); } catch (Exception e) { debugContext.appendLogMessageWithThrowable(LOGGER, Level.WARNING, DebugContext.Type.MESSAGE_IN, t, t.getMessage());
@Override public int compare(Match m1, Match m2) { debugContext .appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, "Choosing better match from ", m1, " and ", m2); boolean m1exact = m1.isExact(); if (m2exact) { // both exact matches, no-one wins debugContext .appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, "Both ", m1, " and ", m2, " are exact matches"); return 0; } else { // m2not exact, m1 is, m1 wins debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m1, " is an exact match"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m2, " is an exact match"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m2, " is a better match, because ", m1, " has more variables"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m1, " is a better match, because ", m2, " has more variables"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m1, " is a better match, because it has longer exact path"); return -1; } else if (m2Index > m1Index) {
/** * Set a session ID that will be used as a common identifier for logged messages related to the same upgrade * request. Setting the session ID will cause the pending messages to be written into the log. * * @param sessionId session ID. */ public void setSessionId(String sessionId) { this.sessionId = sessionId; flush(); }
/** * Append a message to the log, the logging will be postponed until the message can be provided with a session ID. * Randomly generated session ID is used if a session has not been created. * * @param logger logger to be used to log the message. * @param loggingLevel message level. * @param type type of the message. * @param messageParts message parts that will be concatenated to create a log message. */ public void appendLogMessage(Logger logger, Level loggingLevel, Type type, Object... messageParts) { appendLogMessageWithThrowable(logger, loggingLevel, type, null, messageParts); }
/** * Write pending messages to the log. */ public void flush() { if (sessionId == null) { // something went wrong before the session could have been initialized, just give all the messages some id. sessionId = UUID.randomUUID().toString(); } for (LogRecord logRecord : logRecords) { if (logRecord.printToSout) { System.out.println(formatLogMessage(logRecord.message, logRecord.type, logRecord.timestamp)); } else if (logRecord.logger.isLoggable(logRecord.loggingLevel)) { if (logRecord.t != null) { logRecord.logger.log(logRecord.loggingLevel, formatLogMessage(logRecord.message, logRecord.type, logRecord.timestamp), logRecord.t); } else { logRecord.logger.log(logRecord.loggingLevel, formatLogMessage(logRecord.message, logRecord.type, logRecord.timestamp)); } } } logRecords.clear(); }
/** * Append a message to the log and to the list of trace messages that are sent in handshake response. * The logging will be postponed until the message can be provided with a session ID. Randomly generated session ID * is used if a session has not been created. * * @param logger logger to be used to log the message. * @param loggingLevel message level. * @param type type of the message. * @param messageParts message parts that will be stringified and concatenated to create a log message. */ public void appendTraceMessage(Logger logger, Level loggingLevel, Type type, Object... messageParts) { appendTraceMessageWithThrowable(logger, loggingLevel, type, null, messageParts); }
debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Received handshake request:\n" + Utils.stringifyUpgradeRequest(request)); if (protocolHandler == null) { handleUnsupportedVersion(request, response); debugContext.appendTraceMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Upgrade request contains unsupported version of Websocket protocol"); debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_OUT, "Sending handshake response:\n" + Utils.stringifyUpgradeResponse(response)); response.getHeaders().putAll(debugContext.getTracingHeaders()); debugContext.flush(); return HANDSHAKE_FAILED_UPGRADE_INFO; .appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.OTHER, "Connection ID: ", connectionId); debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_OUT, "Sending handshake response:\n" + Utils.stringifyUpgradeResponse(response) + "\n"); response.getHeaders().putAll(debugContext.getTracingHeaders()); return new SuccessfulUpgradeInfo(endpointWrapper, protocolHandler, incomingBufferSize, request, response, extensionContext, debugContext); response.getHeaders().putAll(debugContext.getTracingHeaders()); debugContext.flush(); return NOT_APPLICABLE_UPGRADE_INFO;
private void logUpgradeRequest(UpgradeRequest upgradeRequest) { if (LOGGER.isLoggable(Level.FINE)) { debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_OUT, "Sending handshake " + "request:\n" + Utils.stringifyUpgradeRequest(upgradeRequest)); } else { if (logUpgradeMessages) { debugContext.appendStandardOutputMessage(DebugContext.Type.MESSAGE_OUT, "Sending handshake " + "request:\n" + Utils.stringifyUpgradeRequest(upgradeRequest)); } } }
TyrusEndpointWrapper getEndpointWrapper(UpgradeRequest request, DebugContext debugContext) throws HandshakeException { if (endpointWrappers.isEmpty()) { return null; } final String requestPath = request.getRequestUri(); for (Match m : Match.getAllMatches(requestPath, endpointWrappers, debugContext)) { final TyrusEndpointWrapper endpointWrapper = m.getEndpointWrapper(); for (Map.Entry<String, String> parameter : m.getParameters().entrySet()) { request.getParameterMap().put(parameter.getKey(), Arrays.asList(parameter.getValue())); } if (endpointWrapper.upgrade(request)) { debugContext.appendTraceMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Endpoint selected as a match to the handshake URI: ", endpointWrapper.getEndpointPath()); debugContext.appendLogMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, "Target endpoint: ", endpointWrapper); return endpointWrapper; } } return null; }
debugContext.appendLogMessage(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, "Session opening refused: ", refuseDetail); session.close(new CloseReason(CloseReason.CloseCodes.TRY_AGAIN_LATER, refuseDetail)); } catch (IOException e) { debugContext.appendLogMessageWithThrowable(LOGGER, Level.WARNING, DebugContext.Type.MESSAGE_IN, e, e.getMessage()); if (!collector.isEmpty()) { Throwable t = collector.composeComprehensiveException(); debugContext.appendLogMessageWithThrowable(LOGGER, Level.FINE, DebugContext.Type.MESSAGE_IN, t, t.getMessage()); session.close(CloseReasons.UNEXPECTED_CONDITION.getCloseReason()); } catch (IOException e) { debugContext.appendLogMessageWithThrowable(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_IN, e, e.getMessage()); onError.invoke(toCall, session, t); } catch (Exception e) { debugContext.appendLogMessageWithThrowable(LOGGER, Level.WARNING, DebugContext.Type.MESSAGE_IN, t, t.getMessage());
/** * Append a message to the log, the logging will be postponed until the message can be provided with a session ID. * Randomly generated session ID is used if a session has not been created. * * @param logger logger to be used to log the message. * @param loggingLevel message level. * @param type type of the message. * @param t throwable that has been thrown. * @param messageParts message parts that will be stringified and concatenated to create a log message. */ public void appendLogMessageWithThrowable(Logger logger, Level loggingLevel, Type type, Throwable t, Object... messageParts) { if (logger.isLoggable(loggingLevel)) { String message = stringifyMessageParts(messageParts); if (sessionId == null) { logRecords.add(new LogRecord(logger, loggingLevel, type, message, t, false)); } else { if (t != null) { logger.log(loggingLevel, formatLogMessage(message, type, System.nanoTime()), t); } else { logger.log(loggingLevel, formatLogMessage(message, type, System.nanoTime())); } } } }
@Override public int compare(Match m1, Match m2) { debugContext .appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, "Choosing better match from ", m1, " and ", m2); boolean m1exact = m1.isExact(); if (m2exact) { // both exact matches, no-one wins debugContext .appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, "Both ", m1, " and ", m2, " are exact matches"); return 0; } else { // m2not exact, m1 is, m1 wins debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m1, " is an exact match"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m2, " is an exact match"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m2, " is a better match, because ", m1, " has more variables"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m1, " is a better match, because ", m2, " has more variables"); debugContext.appendTraceMessage(LOGGER, Level.FINER, DebugContext.Type.MESSAGE_IN, m1, " is a better match, because it has longer exact path"); return -1; } else if (m2Index > m1Index) {
/** * Set a session ID that will be used as a common identifier for logged messages related to the same upgrade * request. Setting the session ID will cause the pending messages to be written into the log. * * @param sessionId session ID. */ public void setSessionId(String sessionId) { this.sessionId = sessionId; flush(); }