Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 51 web apps In general an endpoint is accessible at ws or wss hostname port web app context path websocket path query string where websocket path is the value attribute of the @ ServerEndpoint annotation and query string is an optional query string When the websocket path is a URI as it is in the ClockServer endpoint the only request URI that will connect to the endpoint is the one that matches it exactly The Java WebSocket API also allows server endpoints to be mapped to level 1 URI templates URI templates are a fancy way of saying that one or more segments of the URI can be substituted with variables For example airlines service class is a URI template with a single variable called service class The Java WebSocket API allows incoming request URIs to match an endpoint using a URI template path mapping if and only if the request URI is a valid expansion of the URI template For example airlines coach airlines first airlines business are all valid expansions of the URI template airlines service class with variable service class equal to coach first and business respectively URI templates can be very useful in a WebSocket application because the template variable values are available within the endpoint that matches the request URI In any of the lifecycle methods of a server endpoint you can add as many String parameters annotated with the @ PathParam annotation to obtain the value of the variable path segments in the match Continuing this example suppose we had the server endpoint shown in Listing 1 Listing 1 A Booking notifier endpoint @ ServerEndpoint air1ines service class public class MyBookingNotifier @ OnOpen public void initializeUpdates Session session @ PathParam service class String sClass if first equals sClass open champagne else if business equals sC1ass heated nuts else dont bang your head on our aircraft This would yield different levels of service depending on which request URI a client connects with Accessing path information at runtime An endpoint has full access to all of its path information at runtime First it can always obtain the path under which the WebSocket implementation has published it Using the ServerEndpointConfig getPath call for the endpoint holds this information which you can easily access wherever you can get hold of the ServerEndpointConfig instance such as we see in Listing 2
You must have JavaScript enabled to view digital editions.