001/*
002 *  $Id: TomsEchoCreator.java 997 2015-08-21 00:06:40Z tgutwin $
003 *  $HeadURL:  $
004 *  $Revision: 997 $
005 *  $LastChangedDate: 2015-08-20 17:06:40 -0700 (Thu, 20 Aug 2015) $
006 *  $LastChangedBy: tgutwin $
007 *  Copyright (c) 2017 Tom B. Gutwin P.Eng. North Vancouver BC Canada
008 *
009 *  This program is free software; you can redistribute it and/or
010 *  modify it under the terms of the GNU General Public License
011 *  as published by the Free Software Foundation; either version 3
012 *  of the License, or any later version.
013 *
014 *  This program is distributed in the hope that it will be useful,
015 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
016 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 *  GNU General Public License for more details.
018 *
019 *  You should have received a copy of the GNU General Public License
020 *  along with this program; If not, see <http://www.gnu.org/licenses/>.
021 */
022
023package ca.bc.webarts.servlet;
024
025import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
026import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
027import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
028
029import javax.servlet.ServletContext;
030import javax.servlet.http.HttpSession;
031
032/**
033  * TomsEchoCreator basic WebSocket Creator Implementation.
034  **/
035public class TomsEchoCreator implements WebSocketCreator
036{
037  private ServletContext servletContext = null;
038  private TomsBinaryEventSocket binaryEcho = null;
039  private TomsEventSocket textEcho = null;
040  public static boolean debugOut = true;
041
042
043    /**
044    TomsEchoCreator default constructor.
045   **/
046  public TomsEchoCreator()
047  {
048    if (debugOut) System.out.println("TomsEchoCreator()");
049    // Create the reusable sockets
050    this.binaryEcho = new TomsBinaryEventSocket();
051    this.textEcho = new TomsEventSocket();
052  }
053
054
055  public TomsEchoCreator(ServletContext servletContext)
056  {
057    this.servletContext = servletContext;
058  }
059
060
061
062  /**
063    TomsEchoCreator
064   **/
065  @Override
066  public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
067  {
068    String subProtocol = null;
069    if (debugOut) System.out.print("TomsEchoCreator.createWebSocket ");
070    for (String subP : req.getSubProtocols())
071    {
072      if ("binary".equals(subP))
073      {
074        if (debugOut) System.out.println(": BINARY");
075        resp.setAcceptedSubProtocol(subP);
076        subProtocol = subP;
077        return binaryEcho;
078      }
079      if ("text".equals(subP))
080      {
081        if (debugOut) System.out.println(": TEXT");
082        resp.setAcceptedSubProtocol(subP);
083        subProtocol = subP;
084        return textEcho;
085      }
086    }
087
088    try
089    {
090      String connectMessageId = getHeaderOrParameter(req, "connectId");
091      String clientId = getHeaderOrParameter(req, "GDSClientId");
092      String clientType = getHeaderOrParameter(req, "GDSClientType");
093
094      String sessionId = null;
095      HttpSession session = req.getSession();
096      if (session != null)
097      {
098        //ServletGraniteContext.createThreadInstance(gravity.getGraniteConfig(), gravity.getServicesConfig(), this.servletContext, session, clientType);
099        sessionId = session.getId();
100      }
101      else if (req.getCookies() != null)
102      {
103        for (int i = 0; i < req.getCookies().size(); i++)
104        {
105          if ("JSESSIONID".equals(req.getCookies().get(i).getName()))
106          {
107            sessionId = req.getCookies().get(i).getValue();
108            break;
109          }
110        }
111            //ServletGraniteContext.createThreadInstance(gravity.getGraniteConfig(), gravity.getServicesConfig(),       this.servletContext, sessionId, clientType);
112      }
113      else
114      {
115          //ServletGraniteContext.createThreadInstance(gravity.getGraniteConfig(), gravity.getServicesConfig(), this.servletContext, (String)null, clientType);
116      }
117    }
118    finally
119    {
120      //GraniteContext.release();
121    }
122
123
124    // No valid subprotocol in request, return a default text socket
125    if (debugOut) System.out.println(": ??? protocols=" +req.getSubProtocols());
126    if (debugOut) System.out.println(": ??? protocols=" +req.getSubProtocols());
127    resp.setAcceptedSubProtocol("text");
128    return textEcho;
129  }
130
131
132  private static String getHeaderOrParameter(ServletUpgradeRequest servletUpgradeRequest, String key)
133  {
134    String value = servletUpgradeRequest.getHeader(key);
135    if (value == null)
136      value = servletUpgradeRequest.getHttpServletRequest().getParameter(key);
137    return value;
138  }
139}