@Override protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable { // MDCにメソッド呼び出し情報をセットする。 String beforeClassName = MDC.get(MDC_CLASS_NAME); String beforeMethodName = MDC.get(MDC_METHOD_NAME); MDC.put(MDC_CLASS_NAME, getClassForLogging(invocation.getThis()).getName()); MDC.put(MDC_METHOD_NAME, invocation.getMethod().getName()); try { return super.invokeUnderTrace(invocation, logger); } finally { // MDCのメソッド呼び出し情報を元に戻す。 if (beforeClassName == null) { MDC.remove(MDC_CLASS_NAME); } else { MDC.put(MDC_CLASS_NAME, beforeClassName); } if (beforeMethodName == null) { MDC.remove(MDC_METHOD_NAME); } else { MDC.put(MDC_METHOD_NAME, beforeMethodName); } } }
@AfterClass protected void tearDownClass() { MDC.remove("id"); MDC.remove("test"); LOG.info("===[ {} ]==============================[END ]===", clasz.getSimpleName()); MDC.remove("test-class"); }
/** @see wicket.RequestCycle#onEndRequest() */ @SuppressWarnings("nls") @Override protected void onEndRequest() { J2DBGlobals.setServiceProvider(null); WebClientSession webClientSession = (WebClientSession) getSession(); WebClient webClient = webClientSession.getWebClient(); if (webClient != null) { try { webClient.onEndRequest(webClientSession); } finally { MDC.remove("clientid"); MDC.remove("solution"); } } }
@Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { assert request != null; assert response != null; // Log the request URI+URL muck String uri = request.getRequestURI(); if (request.getQueryString() != null) { uri = String.format("%s?%s", uri, request.getQueryString()); } if (log.isDebugEnabled()) { log.debug( "Processing: {} {} ({}) [{}]", $(request.getMethod(), uri, request.getRequestURL(), request.getHeader(HUDSON_HEADER))); } MDC.put(getClass().getName(), uri); try { super.service(request, response); } finally { MDC.remove(getClass().getName()); } // Include the version details of the api + model response.addHeader(HUDSON_HEADER, String.format("api=%s", getApiVersion())); }
/** * Wrapper around <code>MDC.put</code> and <code>MDC.remove</code>. <code>value</code> is allowed * to e null. */ private static void setMdc(String key, String value) { if (value != null) { MDC.put(key, value); } else { MDC.remove(key); } }
/** * Setup the cell diagnostic context of the calling thread. Threads created from the calling * thread automatically inherit this information. The old diagnostic context is captured and * returned. */ public static CDC reset(String cellName, String domainName) { CDC cdc = new CDC(); setMdc(MDC_CELL, cellName); setMdc(MDC_DOMAIN, domainName); MDC.remove(MDC_SESSION); NDC.clear(); return cdc; }
// TODO MISSING JAVADOC @AroundInvoke public Object call(final InvocationContext invocationContext) throws Exception { final String id = MDC.get(TRANSACTION_ID); if (id != null) { return invocationContext.proceed(); } MDC.put(TRANSACTION_ID, Long.toHexString(RANDOM.nextLong())); try { return invocationContext.proceed(); } finally { MDC.remove(TRANSACTION_ID); } }
@Override public <R, E extends Throwable> R doRetryable( final IRetryableTask<R, E> task, final IRetryStrategy strategy) throws RetryerException, InterruptedException { checkArgument(task != null, "task can't be null"); checkArgument(strategy != null, "strategy can't be null"); int tryNo = 0; final List<Throwable> reasons = Lists.newArrayList(); MDC.put("task", task.toString()); try { while (true) { try { log.debug("try #{}...", tryNo); if (Thread.interrupted()) { throw new InterruptedException("Interrupted on " + tryNo + " try"); } return task.execute(tryNo); } catch (InterruptedException e) { log.debug("try #{} interrupted: {}", tryNo, e.getMessage()); throw e; } catch (final Throwable reason) { log.debug("try #{} failed: {}", tryNo, reason.getMessage()); reasons.add(reason); // cleanup if (task.isFatalReason(tryNo, reason)) { throw new RetryerException(reasons); } final RetryInfo retryInfo = strategy.shouldRetry(tryNo, reason); if (retryInfo.shouldFail()) { throw new RetryerException(reasons); } else { final long delay = retryInfo.delay(); if (delay > 0) { log.debug("retry after {} ms", delay); Thread.sleep(delay); } else { log.debug("retry now"); } } } finally { tryNo++; } } } finally { MDC.remove("task"); } }
@Before public void before() throws Exception { lc = new LoggerContext(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); URL xmlConfigFile = getClass().getResource("/logback-gelf-with-dynamic-originhost.xml"); configurator.doConfigure(xmlConfigFile); GelfTestSender.getMessages().clear(); MDC.remove("mdcField1"); }
void clearMDC() { MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY); MDC.remove(ClassicConstants.REQUEST_REQUEST_URI); MDC.remove(ClassicConstants.REQUEST_QUERY_STRING); // removing possibly inexistent item is OK MDC.remove(ClassicConstants.REQUEST_REQUEST_URL); MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY); MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR); }
@Override public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { String sessionId = ((HttpServletRequest) servletRequest).getSession().getId(); String url = ((HttpServletRequest) servletRequest).getRequestURI(); String httpMethod = ((HttpServletRequest) servletRequest).getMethod(); String ip = servletRequest.getRemoteAddr(); try { MDC.put(SESSION_ID, sessionId); MDC.put(URL, url); MDC.put(HTTP_METHOD, httpMethod); MDC.put(IP, ip); Logger.info("one request started before filter"); filterChain.doFilter(servletRequest, servletResponse); Logger.info("one request finished after filter"); } finally { MDC.remove(SESSION_ID); MDC.remove(URL); MDC.remove(HTTP_METHOD); MDC.remove(IP); } }
public void commit(boolean ignoreTimeout) { TransactionID txId = currentTransactionIdThreadLocal.get(); if (txId == null) { throw new TransactionException("no transaction started"); } TransactionContext currentTx = contextMap.get(txId); try { currentTx.commit(ignoreTimeout); } finally { contextMap.remove(txId); currentTransactionIdThreadLocal.remove(); MDC.remove(MDC_KEY); } }
public void rollback() { TransactionID txId = currentTransactionIdThreadLocal.get(); if (txId == null) { throw new TransactionException("no transaction started"); } TransactionContext currentTx = contextMap.get(txId); try { currentTx.rollback(); } finally { contextMap.remove(txId); currentTransactionIdThreadLocal.remove(); MDC.remove(MDC_KEY); } }
/** * {@inheritDoc} * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, * javax.servlet.FilterChain) */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest httpRequest = (HttpServletRequest) request; String transaction = httpRequest.getHeader(headerName); if (transaction == null || transaction.length() == 0) { transaction = ANONYMOUS; } MDC.put(mdcKey, transaction); try { chain.doFilter(request, response); } finally { MDC.remove(mdcKey); } }
/* * (non-Javadoc) * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, * javax.servlet.FilterChain) */ @Override public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { try { final SecurityContext context = SecurityContextHolder.getContext(); String uid = null; if (context != null) { final Authentication authentication = context.getAuthentication(); if (authentication != null) { uid = context.getAuthentication().getName(); } } MDC.put(identifiert, uid == null ? NOT_KNOWN : uid); chain.doFilter(request, response); } finally { MDC.remove(identifiert); } }
/** * {@inheritDoc} * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, * javax.servlet.FilterChain) */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest httpRequest = (HttpServletRequest) request; String username = ANONYMOUS; Principal principal = httpRequest.getUserPrincipal(); if (principal != null && principal.getName().length() > 0) { username = principal.getName(); } MDC.put(mdcKey, username); try { chain.doFilter(request, response); } finally { MDC.remove(mdcKey); } }
/** Clears information put on the MDC by this {@link MDCUnitOfWork} */ public void clear() { if (this.originalBreadcrumbId != null) { MDC.put(MDC_BREADCRUMB_ID, originalBreadcrumbId); } else { MDC.remove(MDC_BREADCRUMB_ID); } if (this.originalExchangeId != null) { MDC.put(MDC_EXCHANGE_ID, originalExchangeId); } else { MDC.remove(MDC_EXCHANGE_ID); } if (this.originalMessageId != null) { MDC.put(MDC_MESSAGE_ID, originalMessageId); } else { MDC.remove(MDC_MESSAGE_ID); } if (this.originalCorrelationId != null) { MDC.put(MDC_CORRELATION_ID, originalCorrelationId); } else { MDC.remove(MDC_CORRELATION_ID); } if (this.originalRouteId != null) { MDC.put(MDC_ROUTE_ID, originalRouteId); } else { MDC.remove(MDC_ROUTE_ID); } if (this.originalCamelContextId != null) { MDC.put(MDC_CAMEL_CONTEXT_ID, originalCamelContextId); } else { MDC.remove(MDC_CAMEL_CONTEXT_ID); } if (this.originalTransactionKey != null) { MDC.put(MDC_TRANSACTION_KEY, originalTransactionKey); } else { MDC.remove(MDC_TRANSACTION_KEY); } }
@AfterMethod protected void tearDownMDC() { MDC.remove("id"); MDC.remove("test"); }
@Override public void requestDestroyed(ServletRequestEvent sre) { // application, host and instance don't have to be removed, because they stay the same for all // threads MDC.remove("requestId"); }
protected void tearDown() throws Exception { logger.info("Completed test case {}", getName()); MDC.remove("testcase"); MDC.remove("testclass"); super.tearDown(); }
protected void cleanupMDC() { MDC.remove(QueryBase.MDC_QUERY); MDC.remove(QueryBase.MDC_PARAMETERS); }
@Override public void endTransactedBy(Object key) { MDC.remove(MDC_TRANSACTION_KEY); super.endTransactedBy(key); }
/** * Clears the diagnostic context entries added by <code>setMessageContext</code>. For this to * work, the NDC has to be in the same state as when <code>setMessageContext</code> returned. * * @see setMessageContext */ public static void clearMessageContext() { MDC.remove(MDC_SESSION); NDC.pop(); }
@GET @Path("/location/{deviceId}") /* * @PreAuthorize( "hasRole('mobileAppUser')") */ @Produces(MediaType.APPLICATION_JSON) public Response getClientsByMAC(@PathParam("deviceId") String deviceId) { MDC.put(MDCKeys.DEVICE_ID, deviceId); MobileAppStats.getInstance().incrementLocationRequestsCount(); LOGGER.trace("Request to retrieve client current location for device ID '{}'", deviceId); WirelessClient macClientLocation = mobileServerCacheService.getWirelessClientByUniqueID(deviceId); if (macClientLocation == null) { // Forbidden better than File Not Found - it doesn't leak // information LOGGER.trace("Unable to determine client MAC address from device ID '{}'", deviceId); MDC.remove(MDCKeys.DEVICE_ID); return Response.status(Response.Status.UNAUTHORIZED).build(); } JSONObject clientObject = new JSONObject(); try { clientObject.accumulate("deviceId", macClientLocation.getUniqueID()); clientObject.accumulate( "lastLocationUpdateTime", macClientLocation.getLastLocationUpdateTime()); clientObject.accumulate("venueId", macClientLocation.getVenueUdId()); clientObject.accumulate("floorId", macClientLocation.getFloorId()); try { if (!macClientLocation.getZoneId().isEmpty()) { clientObject.accumulate("zoneId", macClientLocation.getZoneId()); clientObject.accumulate("zoneName", macClientLocation.getZoneName()); clientObject.accumulate("zonePoints", macClientLocation.getZonePoints()); } } catch (Exception ex) { LOGGER.error( "Error during zone object creation for device ID '{}'", deviceId, ex.getLocalizedMessage()); } JSONObject locationObject = new JSONObject(); locationObject.accumulate("x", macClientLocation.getX()); locationObject.accumulate("y", macClientLocation.getY()); clientObject.accumulate("mapCoordinate", locationObject); JSONObject geoCoordinateObject = new JSONObject(); geoCoordinateObject.accumulate("latitude", macClientLocation.getLatitude()); geoCoordinateObject.accumulate("longitude", macClientLocation.getLongitude()); clientObject.accumulate("geoCoordinate", geoCoordinateObject); LOGGER.trace( "Completed setting current location for device ID '{}' with MAC Address '{}'", deviceId, macClientLocation.getMacAddress()); } catch (Exception e) { LOGGER.error( "Error during location object creation for device ID '{}'", deviceId, e.getLocalizedMessage()); } LOGGER.trace("Returning JSON object with device ID '{}' : {}", deviceId, clientObject); MDC.remove(MDCKeys.DEVICE_ID); if (macClientLocation.getFloorId() == null || macClientLocation.getFloorId().length() <= 0 || macClientLocation.getVenueUdId() == null || macClientLocation.getVenueUdId().length() <= 0) { return Response.status(Response.Status.NOT_FOUND).entity(clientObject).build(); } return Response.status(Response.Status.OK).entity(clientObject).build(); }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { String nonce = requestContext.getHeaderString(HEADER_NONCE); String identity = requestContext.getHeaderString(HEADER_IDENTITY); try { if (StringUtils.isNotEmpty(identity)) { MDC.put(MDC_IDENTITY, identity); } String timestampStr = requestContext.getHeaderString(HEADER_TIMESTAMP); String signature = requestContext.getHeaderString(HEADER_SIGNATURE); if (!ValidationUtils.notEmpty(nonce, identity, timestampStr, signature)) { logUnauthorizedAccess( "Unauthorized request (missing any of nonce, identify, timestamp, signature)", requestContext); throw new AccessUnauthorizedException(); } URI requestUri = requestContext.getUriInfo().getRequestUri(); StringBuilder path = new StringBuilder(requestUri.getPath()); if (requestUri.getRawQuery() != null) { path.append('?').append(requestUri.getRawQuery()); } RESTRequestSigner restRequestSigner = new RESTRequestSigner( requestContext.getMethod(), path.toString(), nonce, timestampStr, identity); ByteArrayOutputStream content = new ByteArrayOutputStream(); InputStream is = requestContext.getEntityStream(); IOUtils.copy( is, contentMaxSize != null ? new BoundedOutputStream(content, contentMaxSize, true) : content); byte[] contentData = content.toByteArray(); restRequestSigner.setContent(contentData); try { Date timestamp = TimeUtils.parseISOUTCDateTime(timestampStr); if (timestamp.after(new Date(System.currentTimeMillis() + expiry))) { String message = "Unauthorized request (expired timestamp): " + timestampStr; logUnauthorizedAccess(message, requestContext); throw new AccessUnauthorizedException(message); } if (replayAttackValidator.checkNonceReplay(nonce)) { String message = "Unauthorized request (duplicated nonce): " + nonce; logUnauthorizedAccess(message, requestContext); throw new AccessUnauthorizedException(); } requestContext.setEntityStream(new ByteArrayInputStream(contentData)); Principal principal = findUserPrincipal(identity); if (principal == null) { logUnauthorizedAccess( "Unauthorized request (principal not found): " + identity, requestContext); throw new AccessUnauthorizedException(); } if (!verifySignature( principal, restRequestSigner.getDataToSign(), signature, requestContext)) { logUnauthorizedAccess( "Unauthorized request (invalid signature): " + restRequestSigner.toString(), requestContext); throw new AccessUnauthorizedException(); } updateAuthenticatedContext(requestContext, principal); } catch (ParseException e) { logBadRequest("Invalid timestamp: " + timestampStr, e, requestContext); throw new WebApplicationException(BAD_REQUEST); } } finally { try { MDC.remove(MDC_IDENTITY); } catch (IllegalArgumentException e) { // } } }
private void logLocationEntry(LogLocationEntry lle) throws JsonProcessingException { MDC.put(LOGFILENAME, lle.getUserId()); userLogger.info(om.writeValueAsString(lle)); MDC.remove(LOGFILENAME); }
@AfterClass public static void tearDown() { HTTPSCalls.close(); MDC.remove("logFileName"); }
/** Clears all cells related MDC entries and the NDC. */ public static void clear() { MDC.remove(MDC_DOMAIN); MDC.remove(MDC_CELL); MDC.remove(MDC_SESSION); NDC.clear(); }
@Override public RouteContext popRouteContext() { MDC.remove(MDC_ROUTE_ID); return super.popRouteContext(); }
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { logger.info("message receives in session handler..."); ChannelBuffer buffer = (ChannelBuffer) e.getMessage(); Packet packet = Packet.parseFrom(buffer.readBytes(buffer.readableBytes()).array()); ClientIdentity clientIdentity = null; try { switch (packet.getType()) { case SUBSCRIPTION: Sub sub = Sub.parseFrom(packet.getBody()); if (StringUtils.isNotEmpty(sub.getDestination()) && StringUtils.isNotEmpty(sub.getClientId())) { clientIdentity = new ClientIdentity( sub.getDestination(), Short.valueOf(sub.getClientId()), sub.getFilter()); MDC.put("destination", clientIdentity.getDestination()); embededServer.subscribe(clientIdentity); // 尝试启动,如果已经启动,忽略 if (!embededServer.isStart(clientIdentity.getDestination())) { ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(clientIdentity.getDestination()); if (!runningMonitor.isStart()) { runningMonitor.start(); } } ctx.setAttachment(clientIdentity); // 设置状态数据 NettyUtils.ack(ctx.getChannel(), null); } else { NettyUtils.error( 401, MessageFormatter.format("destination or clientId is null", sub.toString()) .getMessage(), ctx.getChannel(), null); } break; case UNSUBSCRIPTION: Unsub unsub = Unsub.parseFrom(packet.getBody()); if (StringUtils.isNotEmpty(unsub.getDestination()) && StringUtils.isNotEmpty(unsub.getClientId())) { clientIdentity = new ClientIdentity( unsub.getDestination(), Short.valueOf(unsub.getClientId()), unsub.getFilter()); MDC.put("destination", clientIdentity.getDestination()); embededServer.unsubscribe(clientIdentity); stopCanalInstanceIfNecessary(clientIdentity); // 尝试关闭 NettyUtils.ack(ctx.getChannel(), null); } else { NettyUtils.error( 401, MessageFormatter.format("destination or clientId is null", unsub.toString()) .getMessage(), ctx.getChannel(), null); } break; case GET: Get get = CanalPacket.Get.parseFrom(packet.getBody()); if (StringUtils.isNotEmpty(get.getDestination()) && StringUtils.isNotEmpty(get.getClientId())) { clientIdentity = new ClientIdentity(get.getDestination(), Short.valueOf(get.getClientId())); MDC.put("destination", clientIdentity.getDestination()); Message message = embededServer.getWithoutAck(clientIdentity, get.getFetchSize()); Packet.Builder packetBuilder = CanalPacket.Packet.newBuilder(); packetBuilder.setType(PacketType.MESSAGES); Messages.Builder messageBuilder = CanalPacket.Messages.newBuilder(); messageBuilder.setBatchId(message.getId()); if (message.getId() != -1 && !CollectionUtils.isEmpty(message.getEntries())) { for (Entry entry : message.getEntries()) { messageBuilder.addMessages(entry.toByteString()); } } packetBuilder.setBody(messageBuilder.build().toByteString()); NettyUtils.write(ctx.getChannel(), packetBuilder.build().toByteArray(), null); // 输出数据 } else { NettyUtils.error( 401, MessageFormatter.format("destination or clientId is null", get.toString()) .getMessage(), ctx.getChannel(), null); } break; case CLIENTACK: ClientAck ack = CanalPacket.ClientAck.parseFrom(packet.getBody()); MDC.put("destination", ack.getDestination()); if (StringUtils.isNotEmpty(ack.getDestination()) && StringUtils.isNotEmpty(ack.getClientId())) { if (ack.getBatchId() == 0L) { NettyUtils.error( 402, MessageFormatter.format("batchId should assign value", ack.toString()) .getMessage(), ctx.getChannel(), null); } else if (ack.getBatchId() == -1L) { // -1代表上一次get没有数据,直接忽略之 // donothing } else { clientIdentity = new ClientIdentity(ack.getDestination(), Short.valueOf(ack.getClientId())); embededServer.ack(clientIdentity, ack.getBatchId()); } } else { NettyUtils.error( 401, MessageFormatter.format("destination or clientId is null", ack.toString()) .getMessage(), ctx.getChannel(), null); } break; case CLIENTROLLBACK: ClientRollback rollback = CanalPacket.ClientRollback.parseFrom(packet.getBody()); MDC.put("destination", rollback.getDestination()); if (StringUtils.isNotEmpty(rollback.getDestination()) && StringUtils.isNotEmpty(rollback.getClientId())) { clientIdentity = new ClientIdentity( rollback.getDestination(), Short.valueOf(rollback.getClientId())); if (rollback.getBatchId() == 0L) { embededServer.rollback(clientIdentity); // 回滚所有批次 } else { embededServer.rollback(clientIdentity, rollback.getBatchId()); // 只回滚单个批次 } } else { NettyUtils.error( 401, MessageFormatter.format("destination or clientId is null", rollback.toString()) .getMessage(), ctx.getChannel(), null); } break; default: NettyUtils.error( 400, MessageFormatter.format("packet type={} is NOT supported!", packet.getType()) .getMessage(), ctx.getChannel(), null); break; } } catch (Throwable exception) { NettyUtils.error( 400, MessageFormatter.format( "something goes wrong with channel:{}, exception={}", ctx.getChannel(), ExceptionUtils.getStackTrace(exception)) .getMessage(), ctx.getChannel(), null); } finally { MDC.remove("destination"); } }