Example #1
0
 @Override
 public void doFilter(
     @Nonnull ServletRequest request,
     @Nonnull ServletResponse response,
     @Nonnull FilterChain chain)
     throws IOException, ServletException {
   final StopWatch stopWatch = new StopWatch();
   request.setAttribute(CURRENT_REQUEST_STOP_WATCH_ATTRIBUTE_NAME, stopWatch);
   final ScopeMapping globalMapping = _patternToMapping.get(null);
   final ScopeMapping specificMapping =
       request instanceof HttpServletRequest
           ? getMappingFor(((HttpServletRequest) request).getRequestURI())
           : null;
   try {
     chain.doFilter(request, response);
   } finally {
     request.removeAttribute(CURRENT_REQUEST_STOP_WATCH_ATTRIBUTE_NAME);
     final Duration duration = stopWatch.getCurrentDuration();
     final ServletHealthInterceptor interceptor = _interceptor;
     if (interceptor == null
         || interceptor.isRecordAllowed(request, globalMapping, specificMapping)) {
       globalMapping.record(null, duration);
       if (specificMapping != null) {
         final String targetName =
             interceptor != null
                 ? interceptor.getSpecificTargetName(request, specificMapping)
                 : null;
         specificMapping.record(targetName, duration);
       }
     }
   }
 }
Example #2
0
  public void setMapping(@Nullable String mappingAsString) {
    final Map<Pattern, ScopeMapping> patternToMapping = new LinkedHashMap<>();
    final ServletHealthInterceptor interceptor = _interceptor;

    if (mappingAsString != null) {
      for (String parts : mappingAsString.split("[,\\n\\r]")) {
        final String trimmedPart = parts.trim();
        if (!trimmedPart.isEmpty()) {
          final int lastArrow = trimmedPart.lastIndexOf('>');
          if (lastArrow > 0 && lastArrow + 1 < trimmedPart.length()) {
            try {
              final Pattern pattern = Pattern.compile(trimmedPart.substring(0, lastArrow).trim());
              final String name = trimmedPart.substring(lastArrow + 1).trim();
              if (name.isEmpty()) {
                throw new IllegalArgumentException("Illegal formatted mapping: " + mappingAsString);
              }
              final Collection<String> possibleSpecificNames =
                  interceptor != null ? interceptor.getPossibleNames(name) : null;
              patternToMapping.put(pattern, new ScopeMapping(name, possibleSpecificNames));
            } catch (PatternSyntaxException e) {
              throw new IllegalArgumentException(
                  "Illegal formatted mapping: " + mappingAsString, e);
            }
          } else {
            throw new IllegalArgumentException("Illegal formatted mapping: " + mappingAsString);
          }
        }
      }
    }

    patternToMapping.put(null, new ScopeMapping(null, Collections.<String>emptyList()));

    _nameToMapping = asNameToMapping(patternToMapping.values());
    _patternToMapping = unmodifiableMap(patternToMapping);
    synchronized (_pathToMappingCache) {
      _pathToMappingCache.clear();
    }
  }