diff options
author | zaaarf <me@zaaarf.foo> | 2024-01-24 16:33:26 +0100 |
---|---|---|
committer | zaaarf <me@zaaarf.foo> | 2024-01-24 16:33:26 +0100 |
commit | 304d2a9ad6d9c65b5dc7e101a6e1e40911ebcaea (patch) | |
tree | 4cce22beb54bdd1814087dddd8b0614ef947ce53 /src/main/java/foo/zaaarf/routecartographer/Route.java | |
parent | 324d7e24e5f02f0328a2ab35501238aea5674755 (diff) |
Diffstat (limited to 'src/main/java/foo/zaaarf/routecartographer/Route.java')
-rw-r--r-- | src/main/java/foo/zaaarf/routecartographer/Route.java | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/main/java/foo/zaaarf/routecartographer/Route.java b/src/main/java/foo/zaaarf/routecartographer/Route.java new file mode 100644 index 0000000..650cc17 --- /dev/null +++ b/src/main/java/foo/zaaarf/routecartographer/Route.java @@ -0,0 +1,143 @@ +package foo.zaaarf.routecartographer; + +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.Arrays; + +/** + * Representation of a REST route. + */ +public class Route { + /** + * The paths of the endpoint. + */ + public final String[] paths; + + /** + * The supported {@link RequestMethod}s, as strings. + */ + public final String[] methods; + + /** + * The media types produced by the endpoint. + * May be null if not specified. + */ + public final String[] produces; + + /** + * The media types 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 paths the paths of the endpoint + * @param methods the {@link RequestMethod}s accepted by the endpoint + * @param consumes the media types consumed by the endpoint, may be null + * @param produces the media types 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[] paths, RequestMethod[] methods, String[] consumes, String[] produces, + boolean deprecated, DTO returnType, DTO inputType, Param... params) { + this.paths = paths; + this.methods = Arrays.stream(methods).map(Enum::name).toArray(String[]::new); + + if(produces != null) this.produces = produces; + else this.produces = new String[0]; + + if(consumes != null) this.consumes = consumes; + else this.consumes = new String[0]; + + 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; + } + } +} |