RestMethod.java

  1. /*
  2.  * The coLAB project
  3.  * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
  4.  *
  5.  * Licensed under the MIT License
  6.  */
  7. package ch.colabproject.colab.generator.plugin.rest;

  8. import java.io.File;
  9. import java.io.InputStream;
  10. import java.lang.reflect.Type;
  11. import java.text.MessageFormat;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Optional;
  15. import org.glassfish.jersey.media.multipart.FormDataBodyPart;
  16. import org.glassfish.jersey.media.multipart.FormDataContentDisposition;

  17. /**
  18.  * Represent a rest method
  19.  *
  20.  * @author maxence
  21.  */
  22. public class RestMethod {

  23.     /**
  24.      * HTTP method. One of GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
  25.      */
  26.     private String httpMethod;

  27.     /**
  28.      * Method name
  29.      */
  30.     private String name;

  31.     /**
  32.      * full path. class path + method path
  33.      */
  34.     private String fullPath;

  35.     /**
  36.      * List of path parameters
  37.      */
  38.     private List<Param> pathParameters = new ArrayList<>();

  39.     /**
  40.      * List of querystring parameters
  41.      */
  42.     private List<Param> queryParameters = new ArrayList<>();

  43.     /**
  44.      * List of form parameters
  45.      */
  46.     private List<Param> formParameters = new ArrayList<>();

  47.     /**
  48.      * body param
  49.      */
  50.     private Param bodyParam;

  51.     /**
  52.      * return type
  53.      */
  54.     private Type returnType;

  55.     /**
  56.      * Is return type generic ?
  57.      */
  58.     private boolean returnTypeGeneric;

  59.     /**
  60.      * is this class only for admin ?
  61.      */
  62.     private boolean adminResource;

  63.     /**
  64.      * Is the method deprecated?
  65.      */
  66.     private boolean deprecated;

  67.     /**
  68.      * does this class restricted to authenticated users ?
  69.      */
  70.     private boolean authenticationRequired;

  71.     /**
  72.      * MIME Type the method produces.
  73.      */
  74.     private List<String> produces;

  75.     /**
  76.      * Effective MIME Type the method consumes.
  77.      */
  78.     private List<String> consumes;

  79.     /**
  80.      * Get the value of authenticationRequired
  81.      *
  82.      * @return the value of authenticationRequired
  83.      */
  84.     public boolean isAuthenticationRequired() {
  85.         return authenticationRequired;
  86.     }

  87.     /**
  88.      * Set the value of authenticationRequired
  89.      *
  90.      * @param authenticationRequired new value of authenticationRequired
  91.      */
  92.     public void setAuthenticationRequired(boolean authenticationRequired) {
  93.         this.authenticationRequired = authenticationRequired;
  94.     }

  95.     /**
  96.      * Get the value of adminResource
  97.      *
  98.      * @return the value of adminResource
  99.      */
  100.     public boolean isAdminResource() {
  101.         return adminResource;
  102.     }

  103.     /**
  104.      * Set the value of adminResource
  105.      *
  106.      * @param adminResource new value of adminResource
  107.      */
  108.     public void setAdminResource(boolean adminResource) {
  109.         this.adminResource = adminResource;
  110.     }

  111.     /**
  112.      * Get the value of deprecated
  113.      *
  114.      * @return the value of deprecated
  115.      */
  116.     public boolean isDeprecated() {
  117.         return deprecated;
  118.     }

  119.     /**
  120.      * Set the value of deprecated
  121.      *
  122.      * @param deprecated new value of deprecated
  123.      */
  124.     public void setDeprecated(boolean deprecated) {
  125.         this.deprecated = deprecated;
  126.     }

  127.     /**
  128.      * Get the value of returnTypeGeneric
  129.      *
  130.      * @return the value of returnTypeGeneric
  131.      */
  132.     public boolean isReturnTypeGeneric() {
  133.         return returnTypeGeneric;
  134.     }

  135.     /**
  136.      * Set the value of returnTypeGeneric
  137.      *
  138.      * @param returnTypeGeneric new value of returnTypeGeneric
  139.      */
  140.     public void setReturnTypeGeneric(boolean returnTypeGeneric) {
  141.         this.returnTypeGeneric = returnTypeGeneric;
  142.     }

  143.     /**
  144.      * Get the value of returnType
  145.      *
  146.      * @return the value of returnType
  147.      */
  148.     public Type getReturnType() {
  149.         return returnType;
  150.     }

  151.     /**
  152.      * Set the value of returnType
  153.      *
  154.      * @param returnType new value of returnType
  155.      */
  156.     public void setReturnType(Type returnType) {
  157.         this.returnType = returnType;
  158.     }

  159.     /**
  160.      * Get the value of bodyParam
  161.      *
  162.      * @return the value of bodyParam
  163.      */
  164.     public Param getBodyParam() {
  165.         return bodyParam;
  166.     }

  167.     /**
  168.      * Set the value of bodyParam
  169.      *
  170.      * @param bodyParam new value of bodyParam
  171.      */
  172.     public void setBodyParam(Param bodyParam) {
  173.         this.bodyParam = bodyParam;
  174.     }

  175.     /**
  176.      * Get the value of queryParameters
  177.      *
  178.      * @return the value of queryParameters
  179.      */
  180.     public List<Param> getQueryParameters() {
  181.         return queryParameters;
  182.     }

  183.     /**
  184.      * Set the value of queryParameters
  185.      *
  186.      * @param queryParameters new value of queryParameters
  187.      */
  188.     public void setQueryParameters(List<Param> queryParameters) {
  189.         this.queryParameters = queryParameters;
  190.     }

  191.     /**
  192.      * Get the value of formParameters
  193.      *
  194.      * @return the value of formParameters
  195.      */
  196.     public List<Param> getFormParameters() {
  197.         return formParameters;
  198.     }

  199.     /**
  200.      * Set the value of formParameters
  201.      *
  202.      * @param formParameters new value of formParameters
  203.      */
  204.     public void setFormParameters(List<Param> formParameters) {
  205.         this.formParameters = formParameters;
  206.     }

  207.     /**
  208.      * Get the value of pathParameters
  209.      *
  210.      * @return the value of pathParameters
  211.      */
  212.     public List<Param> getPathParameters() {
  213.         return pathParameters;
  214.     }

  215.     /**
  216.      * Set the value of pathParameters
  217.      *
  218.      * @param pathParameters new value of pathParameters
  219.      */
  220.     public void setPathParameters(List<Param> pathParameters) {
  221.         this.pathParameters = pathParameters;
  222.     }

  223.     /**
  224.      * Get the value of name
  225.      *
  226.      * @return the value of name
  227.      */
  228.     public String getName() {
  229.         return name;
  230.     }

  231.     /**
  232.      * Set the value of name
  233.      *
  234.      * @param name new value of name
  235.      */
  236.     public void setName(String name) {
  237.         this.name = name;
  238.     }

  239.     /**
  240.      * Get the value of fullPath
  241.      *
  242.      * @return the value of fullPath
  243.      */
  244.     public String getFullPath() {
  245.         return fullPath;
  246.     }

  247.     /**
  248.      * Set the value of fullPath
  249.      *
  250.      * @param fullPath new value of fullPath
  251.      */
  252.     public void setFullPath(String fullPath) {
  253.         this.fullPath = fullPath;
  254.     }

  255.     /**
  256.      * Get the value of httpMethod
  257.      *
  258.      * @return the value of httpMethod
  259.      */
  260.     public String getHttpMethod() {
  261.         return httpMethod;
  262.     }

  263.     /**
  264.      * Set the value of httpMethod
  265.      *
  266.      * @param httpMethod new value of httpMethod
  267.      */
  268.     public void setHttpMethod(String httpMethod) {
  269.         this.httpMethod = httpMethod;
  270.     }

  271.     /**
  272.      * Get the value of produces
  273.      *
  274.      * @return the value of produces
  275.      */
  276.     public List<String> getProduces() {
  277.         return produces;
  278.     }

  279.     /**
  280.      * Set the value of produces
  281.      *
  282.      * @param produces new value of produces
  283.      */
  284.     public void setProduces(List<String> produces) {
  285.         this.produces = produces;
  286.     }

  287.     /**
  288.      * Get the value of consumes
  289.      *
  290.      * @return the value of consumes
  291.      */
  292.     public List<String> getConsumes() {
  293.         return consumes;
  294.     }

  295.     /**
  296.      * Set the value of consumes
  297.      *
  298.      * @param consumes new value of consumes
  299.      */
  300.     public void setConsumes(List<String> consumes) {
  301.         this.consumes = consumes;
  302.     }

  303.     /**
  304.      * Register a path parameter
  305.      *
  306.      * @param name           name of the parameter
  307.      * @param annotationName PathParam name
  308.      * @param javadoc        some documentation
  309.      * @param type           type of the parameter
  310.      */
  311.     public void addPathParameter(String name, String annotationName, String javadoc, Type type) {
  312.         Param param = new Param();
  313.         param.setName(name);
  314.         param.setInAnnotationName(annotationName);
  315.         param.setJavadoc(javadoc);
  316.         param.setType(type);

  317.         this.pathParameters.add(param);
  318.     }

  319.     /**
  320.      * Register a query parameter
  321.      *
  322.      * @param name           name of the parameter
  323.      * @param annotationName QueryParam name
  324.      * @param javadoc        some documentation
  325.      * @param type           type of the parameter
  326.      */
  327.     public void addQueryParameter(String name, String annotationName, String javadoc, Type type) {
  328.         Param param = new Param();
  329.         param.setName(name);
  330.         param.setInAnnotationName(annotationName);
  331.         param.setJavadoc(javadoc);
  332.         param.setType(type);

  333.         this.queryParameters.add(param);
  334.     }

  335.     /**
  336.      * Register a query parameter
  337.      *
  338.      * @param name           name of the parameter
  339.      * @param annotationName FormData name
  340.      * @param javadoc        some documentation
  341.      * @param type           type of the parameter
  342.      */
  343.     public void addFormParameter(String name, String annotationName, String javadoc, Type type) {
  344.         Optional<Param> findFirst = this.formParameters.stream()
  345.             .filter(p -> p.getInAnnotationName().equals(annotationName)).findFirst();
  346.         if (findFirst.isEmpty()) {
  347. //            Param param = findFirst.get();
  348. //            // hack: Files are injected with two disctinct parameter: one InputStream for bytes, one
  349. //            // FormDataBodyPart for metadata
  350. //            if (param.getType() instanceof Class
  351. //                && type instanceof Class
  352. //                && ((InputStream.class.isAssignableFrom((Class) type)
  353. //                && ContentDisposition.class.isAssignableFrom((Class) param.getType()))
  354. //                || (InputStream.class.isAssignableFrom((Class) type)
  355. //                && ContentDisposition.class.isAssignableFrom((Class) param.getType())))) {
  356. //                // Both ContentDisposition and InputStream exists: only keep InputStream
  357. //                param.setType(File.class);
  358. //            } else {
  359. //                Logger.warn("Duplicate Form Data");
  360. //            }
  361. //
  362. //        } else {
  363.             Param param = new Param();
  364.             param.setName(name);
  365.             param.setInAnnotationName(annotationName);
  366.             param.setJavadoc(javadoc);
  367.             param.setType(type);

  368.             // hack: MultiPartFile
  369.             if (FormDataBodyPart.class.isAssignableFrom((Class) type)
  370.                 || FormDataContentDisposition.class.isAssignableFrom((Class) type)
  371.                 || InputStream.class.isAssignableFrom((Class) type)) {
  372.                 param.setType(File.class);
  373.             }

  374.             this.formParameters.add(param);
  375.         }
  376.     }

  377.     /**
  378.      * Get all parameters. PathParams + queryParams + fromParam + body
  379.      *
  380.      * @return list of all non-null parameters
  381.      */
  382.     public List<Param> getAllParameters() {
  383.         List<Param> list = new ArrayList<>();
  384.         list.addAll(this.pathParameters);
  385.         list.addAll(this.queryParameters);
  386.         list.addAll(this.formParameters);

  387.         if (bodyParam != null) {
  388.             list.add(bodyParam);
  389.         }
  390.         return list;
  391.     }

  392.     /**
  393.      * Get URL parameters. PathParams + queryParams
  394.      *
  395.      * @return list of all non-null parameters
  396.      */
  397.     public List<Param> getUrlParameters() {
  398.         List<Param> list = new ArrayList<>();
  399.         list.addAll(this.pathParameters);
  400.         list.addAll(this.queryParameters);

  401.         return list;
  402.     }

  403.     @Override
  404.     public String toString() {
  405.         return MessageFormat.format("{0} {1}", this.getHttpMethod(), this.getFullPath());
  406.     }
  407. }