View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2011 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.osgi.equinoxtools.console;
14  
15  import java.io.IOException;
16  import java.util.Queue;
17  import java.util.Set;
18  import java.util.concurrent.CopyOnWriteArraySet;
19  
20  import javax.servlet.ServletException;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.eclipse.jetty.osgi.equinoxtools.WebEquinoxToolsActivator;
25  import org.eclipse.jetty.osgi.equinoxtools.console.WebConsoleWriterOutputStream.OnFlushListener;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.websocket.WebSocket;
28  import org.eclipse.jetty.websocket.WebSocketServlet;
29  import org.eclipse.osgi.framework.console.ConsoleSession;
30  
31  /**
32   * Websocket version of the Chat with equinox.
33   * Ported from jetty's example 'WebSocketChatServlet'
34   */
35  public class EquinoxConsoleWebSocketServlet extends WebSocketServlet implements OnFlushListener
36  {
37      private final Set<ChatWebSocket> _members = new CopyOnWriteArraySet<ChatWebSocket>();
38      private static final long serialVersionUID = 1L;
39      private WebConsoleSession _consoleSession;
40      private EquinoxChattingSupport _support;
41  
42      public EquinoxConsoleWebSocketServlet()
43      {
44          
45      }
46      public EquinoxConsoleWebSocketServlet(WebConsoleSession consoleSession, EquinoxChattingSupport support)
47      {
48          _consoleSession = consoleSession;
49          _support = support;
50      }
51      @Override
52      public void init() throws ServletException
53      {
54          if (_consoleSession == null)
55          {
56              _consoleSession = new WebConsoleSession();
57              WebEquinoxToolsActivator.getContext().registerService(ConsoleSession.class.getName(), _consoleSession, null);
58          }
59          if (_support == null)
60          {
61              _support = new EquinoxChattingSupport(_consoleSession);
62          }
63          super.init();
64          _consoleSession.addOnFlushListener(this);
65      }
66      
67      @Override
68      public void destroy()
69      {
70          _consoleSession.removeOnFlushListener(this);
71      }
72  
73      
74      protected void doGet(HttpServletRequest request, HttpServletResponse response) 
75          throws javax.servlet.ServletException ,IOException 
76      {
77          //getServletContext().getNamedDispatcher("default").forward(request,response);
78          response.sendRedirect(request.getContextPath() + request.getServletPath()
79                  + (request.getPathInfo() != null ? request.getPathInfo() : "") +  "/index.html");
80      };
81      
82      public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
83      {
84          return new ChatWebSocket();
85      }
86      
87      /* ------------------------------------------------------------ */
88      /* ------------------------------------------------------------ */
89      class ChatWebSocket implements WebSocket.OnTextMessage
90      {
91          Connection _connection;
92          String _username;
93          
94          public void onOpen(Connection connection)
95          {
96              // Log.info(this+" onConnect");
97              _connection=connection;
98              _members.add(this);
99          }
100         
101         public void onMessage(byte frame, byte[] data,int offset, int length)
102         {
103             // Log.info(this+" onMessage: "+TypeUtil.toHexString(data,offset,length));
104         }
105 
106         public void onMessage(String data)
107         {
108             Log.info("onMessage: {}",data);
109             if (data.indexOf("disconnect")>=0)
110                 _connection.disconnect();
111             else
112             {
113                 if (!data.endsWith(":has joined!"))
114                 {
115                     if (_username != null)
116                     {
117                         if (data.startsWith(_username + ":"))
118                         {
119                             data = data.substring(_username.length()+1);
120                         }
121                         else
122                         {
123                             //we should not be here?
124                         }
125                     }
126                     _consoleSession.processCommand(data, false);
127                 }
128                 else
129                 {
130                     _username = data.substring(0, data.length()-":has joined!".length());
131                 }
132                 // Log.info(this+" onMessage: "+data);
133                 onFlush();
134             }
135         }
136 
137         public void onClose(int code, String message)
138         {
139             // Log.info(this+" onDisconnect");
140             _members.remove(this);
141         }
142 
143     }
144     
145     
146     /**
147      * Called right after the flush method on the output stream has been executed.
148      */
149     public void onFlush()
150     {
151         Queue<String> pendingConsoleOutputMessages = _support.processConsoleOutput(false, this);
152         for (ChatWebSocket member : _members)
153         {
154             try
155             {
156                 for (String line : pendingConsoleOutputMessages)
157                 {
158                     member._connection.sendMessage(line);
159                 }
160             }
161             catch(IOException e)
162             {
163                 Log.warn(e);
164             }
165         }
166     }    
167 
168 }