summaryrefslogtreecommitdiff
path: root/src/main/java/foo/zaaarf/routecompass/Route.java
blob: af66aed1abf23f172e697df37f1941609cc19ed9 (plain) (blame)
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
package foo.zaaarf.routecompass;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Representation of a REST route.
 */
public class Route {
   /**
    * The path of the endpoint.
    */
   public final String path;

   /**
    * The supported {@link RequestMethod}s, flattened to a string.
    */
   public final String method;

   /**
    * The {@link MediaType} produced by the endpoint.
    * May be null if not specified.
    */
   public final String produces;

   /**
    * The {@link MediaType} consumed by the endpoint.
    * May be null if not specified.
    */
   public final String consumes;

   /**
    * Whether the endpoint is deprecated.
    */
   public final boolean deprecated;

   /**
    * A {@link DTO} representing the response body.
    */
   public final DTO returnType;

   /**
    * A {@link DTO} representing the request body.
    */
   public final DTO inputType;

   /**
    * An array of {@link Param}s, representing parameters accepted by the endpoint.
    */
   public final Param[] params;

   /**
    * The one and only constructor.
    * @param path the path of the endpoint
    * @param methods the {@link RequestMethod}s accepted by the endpoint
    * @param consumes the {@link MediaType} consumed by the endpoint, may be null
    * @param produces the {@link MediaType} produced by the endpoint, may be null
    * @param deprecated whether the endpoint is deprecated
    * @param returnType the DTO for the response type, may be null
    * @param inputType the DTO for the request type, may be null
    * @param params {@link Param}s of the endpoint, may be null
    */
   public Route(String path, RequestMethod[] methods, MediaType consumes, MediaType produces,
                boolean deprecated, DTO returnType, DTO inputType, Param... params) {
      this.path = path;

      StringBuilder methodStringBuilder = new StringBuilder("[");
      for(RequestMethod m : methods)
         methodStringBuilder
            .append(m.name())
            .append("|");
      methodStringBuilder
         .deleteCharAt(methodStringBuilder.length() - 1)
         .append("]");
      this.method = methodStringBuilder.toString();

      if(produces != null) this.produces = produces.toString();
      else this.produces = null;

      if(consumes != null) this.consumes = consumes.toString();
      else this.consumes = null;

      this.deprecated = deprecated;

      this.returnType = returnType;
      this.inputType = inputType;

      if(params != null) this.params = params;
      else this.params = new Param[0]; //just in case
   }

   /**
    * Representation of a parameter of a REST route.
    */
   public static class Param {
      /**
       * The fully-qualified name of the expected type of the parameter.
       */
      public final String typeFQN;

      /**
       * The name of the parameter.
       */
      public final String name;

      /**
       * The default value of the parameter.
       * May be null, in which case the parameter is required.
       */
      public final String defaultValue;

      /**
       * The one and only constructor.
       * @param typeFQN the FQN of the expected type of the parameter
       * @param name the name of the parameter
       * @param defaultValue the default value of the parameter, may be null if the parameter is required
       */
      public Param(String typeFQN, String name, String defaultValue) {
         this.typeFQN = typeFQN;
         this.name = name;
         this.defaultValue = defaultValue;
      }
   }

   /**
    * Representation of a DTO type.
    */
   public static class DTO {

      /**
       * Fully-qualified name of the type.
       */
      public final String FQN;

      /**
       * An array of {@link Param} representing the type's fields.
       */
      public final Route.Param[] fields;

      /**
       * The one and only constructor.
       * @param FQN the fully-qualified name
       * @param fields the {@link Param}s representing the fields
       */
      public DTO(String FQN, Route.Param ... fields) {
         this.FQN = FQN;
         if(fields == null) this.fields = new Route.Param[0];
         else this.fields = fields;
      }
   }
}