|
| 1 | +package datadog.trace.instrumentation.springweb; |
| 2 | + |
| 3 | +import static datadog.trace.api.appsec.api.security.model.Endpoint.Operation.HTTP_REQUEST; |
| 4 | +import static datadog.trace.api.appsec.api.security.model.Endpoint.Type.REST; |
| 5 | + |
| 6 | +import datadog.trace.api.appsec.api.security.model.Endpoint; |
| 7 | +import datadog.trace.api.appsec.api.security.model.Endpoint.Method; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.LinkedList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.NoSuchElementException; |
| 15 | +import java.util.Queue; |
| 16 | +import java.util.Set; |
| 17 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 18 | +import org.springframework.web.method.HandlerMethod; |
| 19 | +import org.springframework.web.servlet.mvc.condition.MediaTypeExpression; |
| 20 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 21 | + |
| 22 | +public class RequestMappingInfoInterator implements Iterator<Endpoint> { |
| 23 | + |
| 24 | + private final Iterator<Map.Entry<RequestMappingInfo, HandlerMethod>> delegate; |
| 25 | + private final Queue<Endpoint> queue = new LinkedList<>(); |
| 26 | + |
| 27 | + public RequestMappingInfoInterator(final Map<RequestMappingInfo, HandlerMethod> mappings) { |
| 28 | + delegate = mappings.entrySet().iterator(); |
| 29 | + fetchNext(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public boolean hasNext() { |
| 34 | + return !queue.isEmpty(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Endpoint next() { |
| 39 | + Endpoint result = queue.poll(); |
| 40 | + if (result == null) { |
| 41 | + throw new NoSuchElementException(); |
| 42 | + } |
| 43 | + if (queue.isEmpty()) { |
| 44 | + fetchNext(); |
| 45 | + } |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + private void fetchNext() { |
| 50 | + if (!delegate.hasNext()) { |
| 51 | + return; |
| 52 | + } |
| 53 | + final Map.Entry<RequestMappingInfo, HandlerMethod> nextEntry = delegate.next(); |
| 54 | + final RequestMappingInfo nextInfo = nextEntry.getKey(); |
| 55 | + final HandlerMethod nextHandler = nextEntry.getValue(); |
| 56 | + for (final String path : nextInfo.getPatternsCondition().getPatterns()) { |
| 57 | + final List<Method> methods = new LinkedList<>(); |
| 58 | + if (nextInfo.getMethodsCondition().getMethods().isEmpty()) { |
| 59 | + methods.add(Method.ALL); |
| 60 | + } else { |
| 61 | + for (final RequestMethod method : nextInfo.getMethodsCondition().getMethods()) { |
| 62 | + methods.add(Method.parseMethod(method.name())); |
| 63 | + } |
| 64 | + } |
| 65 | + for (final Method method : methods) { |
| 66 | + final Endpoint endpoint = new Endpoint(); |
| 67 | + endpoint.setType(REST); |
| 68 | + endpoint.setOperation(HTTP_REQUEST); |
| 69 | + endpoint.setPath(path); |
| 70 | + endpoint.setMethod(method); |
| 71 | + endpoint.setRequestBodyType( |
| 72 | + parseMediaTypes(nextInfo.getConsumesCondition().getExpressions())); |
| 73 | + endpoint.setResponseBodyType( |
| 74 | + parseMediaTypes(nextInfo.getProducesCondition().getExpressions())); |
| 75 | + final Map<String, Object> metadata = new HashMap<>(); |
| 76 | + metadata.put("handler", nextHandler.toString()); |
| 77 | + endpoint.setMetadata(metadata); |
| 78 | + queue.add(endpoint); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private List<String> parseMediaTypes(final Set<MediaTypeExpression> expressions) { |
| 84 | + if (expressions.isEmpty()) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + final List<String> result = new ArrayList<>(expressions.size()); |
| 88 | + for (final MediaTypeExpression expression : expressions) { |
| 89 | + result.add(expression.toString()); |
| 90 | + } |
| 91 | + return result; |
| 92 | + } |
| 93 | +} |
0 commit comments