RequestMappingMethodInterceptor.java 2.7 KB
Newer Older
1
/*
wu-sheng's avatar
wu-sheng 已提交
2 3 4 5 6 7
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
8 9 10 11 12 13 14 15 16 17 18
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

19
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
20

21
import org.apache.skywalking.apm.plugin.spring.mvc.commons.ParsePathUtil;
22
import org.springframework.core.annotation.AnnotationUtils;
23 24
import org.springframework.web.bind.annotation.RequestMapping;

25 26
import java.lang.reflect.Method;

27
/**
28 29
 * The <code>RequestMappingMethodInterceptor</code> only use the first mapping value. it will interceptor with
 * <code>@RequestMapping</code>
30
 */
31
public class RequestMappingMethodInterceptor extends AbstractMethodInterceptor {
32 33
    @Override
    public String getRequestURL(Method method) {
34
        return ParsePathUtil.recursiveParseMethodAnnotation(method, m -> {
35 36 37
            String requestURL = null;
            RequestMapping methodRequestMapping = AnnotationUtils.getAnnotation(m, RequestMapping.class);
            if (methodRequestMapping != null) {
38
                requestURL = methodRequestMapping.value().length > 0 ? methodRequestMapping.value()[0] : "";
39 40 41
            }
            return requestURL;
        });
42 43 44 45
    }

    @Override
    public String getAcceptedMethodTypes(Method method) {
46
        return ParsePathUtil.recursiveParseMethodAnnotation(method, m -> {
47 48 49 50 51
            RequestMapping methodRequestMapping = AnnotationUtils.getAnnotation(m, RequestMapping.class);
            if (methodRequestMapping == null || methodRequestMapping.method().length == 0) {
                return null;
            }
            StringBuilder methodTypes = new StringBuilder();
52
            methodTypes.append("{");
53
            for (int i = 0; i < methodRequestMapping.method().length; i++) {
54
                methodTypes.append(methodRequestMapping.method()[i].toString());
55
                if (methodRequestMapping.method().length > (i + 1)) {
56
                    methodTypes.append(",");
57 58
                }
            }
59
            methodTypes.append("}");
60 61
            return methodTypes.toString();
        });
62 63
    }
}