title: Making path parameters optional in WebSocket endpoints in Java EE
body: <p>Given a WebSocket endpoint as follows.</p>
  
  <pre><code>@ServerEndpoint(value = "/Push/CartPush/{token}")
  public final class CartPush {
      // ...
  }
  </code></pre>
  
  <p>The endpoint is able to accept a path parameter <code>{token}</code>. The path parameter is however, optional <strong>which is determined dynamically at run-time in Java Script.</strong> Skipping this parameter in JavaScript like the following results in <code>404</code>.</p>
  
  <pre class="lang-javascript prettyprint-override"><code>var ws = new WebSocket("wss://localhost:8443/ContextPath/Push/CartPush");
  </code></pre>
  
  <blockquote>
    <p>WebSocket connection to
    <code>'wss://localhost:8443/ContextPath/Push/CartPush'</code> failed: Error during
    WebSocket handshake: Unexpected response code: 404</p>
  </blockquote>
  
  <p>It makes the token value compulsory as follows.</p>
  
  <pre class="lang-javascript prettyprint-override"><code>var token = "token value";
  var ws = new WebSocket("wss://localhost:8443/ContextPath/Push/CartPush" + "/" + token);
  </code></pre>
  
  <p>In order to exclude all unneeded HTTP methods except <code>GET</code> and <code>POST</code>, I use the following restrictions or constraints along with Servlet security constraints using appropriate URL patterns and role mappings in <code>web.xml</code>.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;security-constraint&gt;
      &lt;web-resource-collection&gt;
          &lt;web-resource-name&gt;Disable unneeded HTTP methods by 403&lt;/web-resource-name&gt;
  
          &lt;url-pattern&gt;/Public/*&lt;/url-pattern&gt;
          &lt;url-pattern&gt;/Push/*&lt;/url-pattern&gt;
          &lt;url-pattern&gt;/javax.faces.resource/*&lt;/url-pattern&gt;
  
          &lt;http-method&gt;GET&lt;/http-method&gt;
          &lt;http-method&gt;POST&lt;/http-method&gt;
      &lt;/web-resource-collection&gt;
  &lt;/security-constraint&gt;
  
  &lt;deny-uncovered-http-methods/&gt; &lt;!-- Requires Servlet 3.1 --&gt;
  </code></pre>
  
  <p>How to make the given path parameter optional?</p>
  
  <p><sub>The server in use is WildFly 10.0.0 final / Java EE 7.</sub></p>