RequestMappingHelper.java 7.82 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
package com.apigcc.springmvc;

import com.apidoc.core.common.URI;
import com.apidoc.core.common.helper.AnnotationHelper;
import com.apidoc.core.common.helper.ClassDeclarationHelper;
import com.apidoc.core.common.helper.ExpressionHelper;
import com.apidoc.core.schema.Method;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.Expression;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Optional;

/**
 * The type Request mapping helper.
 *
 * @author fengyuchenglun
 * @version 1.0.0
 */
public class RequestMappingHelper {


    /**
     * 常量-GetMapping
     */
    public static final String ANNOTATION_GET_MAPPING = "GetMapping";
    /**
     * 常量-PostMapping
     */
    public static final String ANNOTATION_POST_MAPPING = "PostMapping";
    /**
     * 常量-PutMapping
     */
    public static final String ANNOTATION_PUT_MAPPING = "PutMapping";
    /**
     * 常量-PatchMapping
     */
    public static final String ANNOTATION_PATCH_MAPPING = "PatchMapping";
    /**
     * 常量-DeleteMapping
     */
    public static final String ANNOTATION_DELETE_MAPPING = "DeleteMapping";
    /**
     * 常量-RequestMapping
     */
    public static final String ANNOTATION_REQUEST_MAPPING = "RequestMapping";

    /**
     * 常量-ResponseBody
     */
    public static final String ANNOTATION_RESPONSE_BODY = "ResponseBody";

    /**
     * The constant ANNOTATION_REQUEST_MAPPINGS.
     */
    public static final List<String> ANNOTATION_REQUEST_MAPPINGS = Lists.newArrayList(
            ANNOTATION_GET_MAPPING,
            ANNOTATION_POST_MAPPING,
            ANNOTATION_PUT_MAPPING,
            ANNOTATION_PATCH_MAPPING,
            ANNOTATION_DELETE_MAPPING,
            ANNOTATION_REQUEST_MAPPING);

    /**
     * Is rest boolean.
     *
     * @param n the n
     * @return the boolean
     */
    public static boolean isRest(MethodDeclaration n) {
        if (n.isAnnotationPresent(ANNOTATION_RESPONSE_BODY)) {
            return true;
        }
        Optional<Node> parentOptional = n.getParentNode();
        if (parentOptional.isPresent()) {
            Node parentNode = parentOptional.get();
            if (parentNode instanceof ClassOrInterfaceDeclaration) {
                if (AnnotationHelper.isAnnotationPresent(((ClassOrInterfaceDeclaration) parentNode), SpringMVCContext.getInstance().getRestControllers())) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * Pick method method.
     *
     * @param methodDeclaration methodDeclaration
     * @return the method
     */
    public static Method pickMethod(MethodDeclaration methodDeclaration) {
        if (methodDeclaration.isAnnotationPresent(ANNOTATION_GET_MAPPING)) {
            return Method.GET;
        }
        if (methodDeclaration.isAnnotationPresent(ANNOTATION_POST_MAPPING)) {
            return Method.POST;
        }
        if (methodDeclaration.isAnnotationPresent(ANNOTATION_PUT_MAPPING)) {
            return Method.PUT;
        }
        if (methodDeclaration.isAnnotationPresent(ANNOTATION_PATCH_MAPPING)) {
            return Method.PATCH;
        }
        if (methodDeclaration.isAnnotationPresent(ANNOTATION_DELETE_MAPPING)) {
            return Method.DELETE;
        }
        if (methodDeclaration.isAnnotationPresent(ANNOTATION_REQUEST_MAPPING)) {
            AnnotationExpr annotationExpr = methodDeclaration.getAnnotationByName(ANNOTATION_REQUEST_MAPPING).get();
            Optional<Expression> expressionOptional = AnnotationHelper.getAttribute(annotationExpr, "method");
            if (expressionOptional.isPresent()) {
                Expression expression = expressionOptional.get();
                if (expression.isArrayInitializerExpr()) {
                    NodeList<Expression> values = expression.asArrayInitializerExpr().getValues();
                    if (values != null && values.size() > 0) {
                        return Method.valueOf(values.get(0).toString().replaceAll("RequestMethod.", ""));
                    }
                }
                return Method.of(expression.toString().replaceAll("RequestMethod.", ""));
            }
        }
        return Method.GET;
    }


    /**
     * 获取uri数据
     *
     * @param n the n
     * @return uri
     */
    public static URI pickUriToParent(ClassOrInterfaceDeclaration n) {
        URI parentUri = null;
        Optional<ClassOrInterfaceDeclaration> parentOptional = ClassDeclarationHelper.getParent(n);
        if (parentOptional.isPresent()) {
            parentUri = pickUriToParent(parentOptional.get());
        }
        URI uri = new URI(pickUri(n.getAnnotations()));
        if (parentUri != null) {
            parentUri.add(uri);
            return parentUri;
        }
        return uri;
    }

    /**
     * 获取uri数据,有多个时,暂时只取第一个
     *
     * @param nodeList the node list
     * @return string
     */
    public static String pickUri(NodeList<AnnotationExpr> nodeList) {
        for (AnnotationExpr annotationExpr : nodeList) {
            if (ANNOTATION_REQUEST_MAPPINGS.contains(annotationExpr.getNameAsString())) {
                Optional<Expression> expressionOptional = AnnotationHelper.getAnyAttribute(annotationExpr, "value", "path");
                if (expressionOptional.isPresent()) {
                    Expression expression = expressionOptional.get();
                    return ExpressionHelper.getStringValue(expression);
                }
            }
        }
        return "";
    }

    /**
     * 获取headers
     *
     * @param nodeList the node list
     * @return list
     */
    public static List<String> pickHeaders(NodeList<AnnotationExpr> nodeList) {
        for (AnnotationExpr annotationExpr : nodeList) {
            if (ANNOTATION_REQUEST_MAPPINGS.contains(annotationExpr.getNameAsString())) {
                Optional<Expression> expressionOptional = AnnotationHelper.getAttribute(annotationExpr, "headers");
                if (expressionOptional.isPresent()) {
                    Expression expression = expressionOptional.get();
                    return ExpressionHelper.getStringValues(expression);
                }
            }
        }
        return Lists.newArrayList();
    }

    /**
     * 获取headers
     *
     * @param nodeList the node list
     * @return list
     */
    public static List<String> pickConsumers(NodeList<AnnotationExpr> nodeList) {
        for (AnnotationExpr annotationExpr : nodeList) {
            if (ANNOTATION_REQUEST_MAPPINGS.contains(annotationExpr.getNameAsString())) {
                Optional<Expression> expressionOptional = AnnotationHelper.getAttribute(annotationExpr, "consumes");
                if (expressionOptional.isPresent()) {
                    Expression expression = expressionOptional.get();
                    return ExpressionHelper.getStringValues(expression);
                }
            }
        }
        return Lists.newArrayList();
    }

    /**
     * 获取headers
     *
     * @param nodeList the node list
     * @return list
     */
    public static List<String> pickProduces(NodeList<AnnotationExpr> nodeList) {
        for (AnnotationExpr annotationExpr : nodeList) {
            if (ANNOTATION_REQUEST_MAPPINGS.contains(annotationExpr.getNameAsString())) {
                Optional<Expression> expressionOptional = AnnotationHelper.getAttribute(annotationExpr, "produces");
                if (expressionOptional.isPresent()) {
                    Expression expression = expressionOptional.get();
                    return ExpressionHelper.getStringValues(expression);
                }
            }
        }
        return Lists.newArrayList();
    }

}