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.util.LinkedList;
16  import java.util.Queue;
17  
18  import org.eclipse.jetty.osgi.equinoxtools.console.WebConsoleWriterOutputStream.OnFlushListener;
19  
20  /**
21   * Processing of the messages to be received and sent to the chat servlets.
22   * Made to be extended for filtering of the messages and commands.
23   */
24  public class EquinoxChattingSupport
25  {
26      
27      private WebConsoleSession _consoleSession;
28      
29      public EquinoxChattingSupport(WebConsoleSession consoleSession)
30      {
31          _consoleSession = consoleSession;
32      }
33      
34      /**
35       * Split the output into multiple lines.
36       * Format them for the json messages sent to the chat.
37       * Empties the console output from what is already displayed in the chat.
38       * @return The lines to add to the message queue of each client.
39       */
40      protected Queue<String> processConsoleOutput(boolean escape, OnFlushListener onflush)
41      {
42          Queue<String> result = new LinkedList<String>();
43          String toDisplay = _consoleSession.getOutputAsWriter().getBuffer().toString();
44          //the last listener to be called is in charge of clearing the console.
45          boolean clearConsole = _consoleSession.getOnFlushListeners().indexOf(onflush) == _consoleSession.getOnFlushListeners().size();
46          if (clearConsole)
47          {
48              _consoleSession.clearOutput();
49          }
50          boolean lastLineIsComplete = toDisplay.endsWith("\n") || toDisplay.endsWith("\r");
51          String[] lines = toDisplay.split("\n");
52          String lastLine = lastLineIsComplete ? null : lines[lines.length-1];
53          if (clearConsole)
54          {
55              _consoleSession.getOutputAsWriter().append(lastLine);
56          }
57          for (int lnNb = 0; lnNb < (lastLineIsComplete ? lines.length : lines.length-1); lnNb++) 
58          {
59              String line = lines[lnNb];
60              while (line.trim().startsWith("null"))
61              {//hum..
62                  line = line.trim().substring("null".length()).trim();
63              }
64              if (line.startsWith("osgi>"))
65              {
66                  result.add("osgi>");
67                  result.add(escape ? jsonEscapeString(line.substring("osgi>".length())) : line.substring("osgi>".length()));
68              }
69              else
70              {
71                  result.add("&#10;");
72                  result.add(escape ? jsonEscapeString(line) : line);
73              }
74          }
75          return result;
76      }
77      
78      /**
79       * http://www.ietf.org/rfc/rfc4627.txt
80       * @param str
81       * @return The same string escaped according to the JSON RFC.
82       */
83      public static String jsonEscapeString(String str)
84      {
85          StringBuilder sb = new StringBuilder();
86          char[] asChars = str.toCharArray();
87          for (char ch : asChars)
88          {
89              switch (ch)
90              {
91                  //the reserved characters
92                  case '"':
93                      sb.append("\\\"");
94                      break;
95                  case '\\':
96                      sb.append("\\\\");
97                      break;
98                  case '\b':
99                      sb.append("\\b");
100                     break;
101                 case '\f':
102                     sb.append("\\f");
103                     break;
104                 case '\n':
105                     sb.append("\\n");
106                     break;
107                 case '\r':
108                     sb.append("\\r");
109                     break;
110                 case '\t':
111                     sb.append("\\t");
112                     break;
113                 case '/':
114                     sb.append("\\/");
115                     break;
116                 default:
117                     //The non reserved characters
118                     if (ch >= '\u0000' && ch <= '\u001F')
119                     {
120                         //escape as a unicode number when out of range.
121                         String ss = Integer.toHexString(ch);
122                         sb.append("\\u");
123                         for (int i = 0; i < 4 - ss.length(); i++)
124                         {
125                             //padding
126                             sb.append('0');
127                         }
128                         sb.append(ss.toUpperCase());
129                     }
130                     else
131                     {
132                         sb.append(ch);
133                     }
134             }
135         }
136         return sb.toString();
137     }
138     
139     public void broadcast(OnFlushListener source)
140     {
141         for (OnFlushListener onflush : _consoleSession.getOnFlushListeners())
142         {
143             if (onflush != source)
144             {
145                 onflush.onFlush();
146             }
147         }
148     }
149 
150 }