Class RouteRegistry

java.lang.Object
de.craftsblock.craftsnet.api.RouteRegistry

public class RouteRegistry extends Object
The RouteRegistry class manages the registration and unregistration of RequestHandler (routes) and SocketHandler (websockets). It stores and maps the registered routes and sockets based on their patterns, allowing for efficient handling of incoming requests.
Since:
1.0.0-SNAPSHOT
  • Constructor Details

    • RouteRegistry

      public RouteRegistry(CraftsNet craftsNet)
      Constructs a new instance of the RouteRegistry
      Parameters:
      craftsNet - The CraftsNet instance which instantiates this route registry
  • Method Details

    • register

      public void register(Handler handler)
      Registers an endpoint handler (RequestHandler and or SocketHandler) by inspecting its annotated methods and adding it to the registry.
      Parameters:
      handler - The Handler to be registered.
    • isRegistered

      public boolean isRegistered(Handler handler)
      Checks if the given Handler is registered. This class is a wrapper for isRegistered(Class).
      Parameters:
      handler - The Handler to check.
      Returns:
      true when the Handler was registered, false otherwise.
      Since:
      3.2.1-SNAPSHOT
    • isRegistered

      public boolean isRegistered(Class<? extends Handler> type)
      Checks if the given class representation of the Handler is registered.
      Parameters:
      type - The class representation of the Handler to check.
      Returns:
      true when the Handler was registered, false otherwise.
      Since:
      3.2.1-SNAPSHOT
    • share

      public void share(String path, File folder)
      Shares a folder for a specified path.
      Parameters:
      path - The path pattern to share.
      folder - The folder to be shared.
      Throws:
      IllegalArgumentException - If the provided "folder" is not a directory.
    • share

      public void share(String path, Path folder)
      Shares a folder for a specified path.
      Parameters:
      path - The path pattern to share.
      folder - The folder to be shared.
      Throws:
      IllegalArgumentException - If the provided "folder" is not a directory.
      Since:
      3.3.5-SNAPSHOT
    • share

      public void share(String path, File folder, boolean onlyGet)
      Shares a folder for a specified path.
      Parameters:
      path - The path pattern to share.
      folder - The folder to be shared.
      onlyGet - Set to true if only get requests should be received by this share, false otherwise.
      Throws:
      IllegalArgumentException - If the provided "folder" is not a directory.
    • share

      public void share(String path, Path folder, boolean onlyGet)
      Shares a folder for a specified path.
      Parameters:
      path - The path pattern to share.
      folder - The path of the folder to be shared.
      onlyGet - Set to true if only get requests should be received by this share, false otherwise.
      Throws:
      IllegalArgumentException - If the provided "folder" is not a directory.
      Since:
      3.3.5-SNAPSHOT
    • unregister

      public void unregister(Handler handler)
      Unregisters an endpoint handler (route or websocket) from the registry.
      Parameters:
      handler - The RequestHandler to be unregistered.
    • getShares

      public Map<Pattern,File> getShares()
      Returns an immutable copy of the shared folders and patterns.
      Returns:
      An immutable copy of the shared folders and patterns.
    • getShare

      public RouteRegistry.ShareMapping getShare(String url)
      Gets the mapping for the share associated with the given URL.
      Parameters:
      url - The URL for which to retrieve the associated mapping.
      Returns:
      The mapping of the share or null if no match is found.
    • getShareFolder

      public Path getShareFolder(String url)
      Gets the shared folder associated with the given URL.
      Parameters:
      url - The URL for which to retrieve the associated shared folder.
      Returns:
      The shared folder or null if no match is found.
    • getSharePattern

      public Pattern getSharePattern(String url)
      Gets the pattern associated with the shared folder that matches the given URL.
      Parameters:
      url - The URL for which to retrieve the associated pattern.
      Returns:
      The pattern or null if no match is found.
    • isShare

      public boolean isShare(String url)
      Checks if a URL corresponds to a shared folder by verifying if both a shared folder and its pattern exist for the URL.
      Parameters:
      url - The URL to check.
      Returns:
      True if the URL corresponds to a shared folder, false otherwise.
    • canShareAccept

      public boolean canShareAccept(String url, HttpMethod method)
      Checks if a URL corresponding share is able to accept a specific http method.
      Parameters:
      url - The URL to the corresponding share.
      method - The http method to check.
      Returns:
      True if the share can accept the specific http method, false otherwise.
    • getRoutes

      @NotNull public @NotNull Map<Pattern,ConcurrentLinkedQueue<RouteRegistry.EndpointMapping>> getRoutes()
      Gets an immutable copy of the registered routes in the registry.
      Returns:
      A ConcurrentHashMap containing the registered routes.
    • getRoute

      @Nullable public @Nullable EnumMap<ProcessPriority.Priority,List<RouteRegistry.EndpointMapping>> getRoute(Request request)
      Gets the route mappings associated with a specific request information.
      Parameters:
      request - The http request for which a routes should be found.
      Returns:
      A list of RouteMapping objects associated with the URL and HTTP method, or null if no mappings are found.
    • hasRouteMappings

      public boolean hasRouteMappings(Request request)
      Checks if there are any registered route mappings for a given Request.
      Parameters:
      request - The Request to check.
      Returns:
      true if a route mapping exists, otherwise false.
      Since:
      3.3.3-SNAPSHOT
    • getSockets

      @NotNull public @NotNull Map<Pattern,ConcurrentLinkedQueue<RouteRegistry.EndpointMapping>> getSockets()
      Gets an immutable copy of the registered socket handlers in the registry.
      Returns:
      A ConcurrentHashMap containing the registered socket handlers.
    • getSocket

      @Nullable public @Nullable EnumMap<ProcessPriority.Priority,List<RouteRegistry.EndpointMapping>> getSocket(WebSocketClient client)
      Gets the socket mapping associated with a specific URL and domain.
      Parameters:
      client - The client for which the socket mapping is sought.
      Returns:
      A list of SocketMapping objects associated with the URL, or null if no mapping is found.
    • hasSocketMappings

      public boolean hasSocketMappings(WebSocketClient client)
      Checks if there are any registered websocket mappings for a given WebSocketClient.
      Parameters:
      client - The WebSocketClient to check.
      Returns:
      true if a websocket mapping exists, otherwise false.
      Since:
      3.3.3-SNAPSHOT
    • getEndpoints

      @NotNull public @NotNull Map<Pattern,ConcurrentLinkedQueue<RouteRegistry.EndpointMapping>> getEndpoints(Class<? extends Server> server)
      Gets an immutable copy of the registered endpoints for the specific server type in the registry.
      Parameters:
      server - The Server from which the endpoints should be loaded.
      Returns:
      A ConcurrentHashMap containing the registered endpoints.
      Since:
      3.3.3-SNAPSHOT
    • hasWebsockets

      public boolean hasWebsockets()
      Checks if the registry has any registered socket handlers.
      Returns:
      true if the registry has registered socket handlers, false otherwise.
    • hasRoutes

      public boolean hasRoutes()
      Checks if the registry has any registered route handlers or shares.
      Returns:
      true if the registry has registered route handlers or shares, false otherwise.
    • hasShares

      public boolean hasShares()
      Checks if the registry has any registered shares.
      Returns:
      true if the registry has registered shares, false otherwise.
    • getServerMappings

      Retrieves all registered server mappings.
      Returns:
      A map which contains all registered endpoint mappings sorted per server.
      Since:
      3.2.1-SNAPSHOT
    • getCraftsNet

      public CraftsNet getCraftsNet()
      Retrieves the instance of CraftsNet bound to the registry.
      Returns:
      The instance of CraftsNet bound to the registry.
      Since:
      3.2.1-SNAPSHOT