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.io.PrintWriter;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  
24  /**
25   * Take the example ChatServlet and use it to  make an Equinox Console on the web.
26   */
27  public class EquinoxConsoleSyncServlet extends HttpServlet
28  {
29  
30      private static final long serialVersionUID = 1L;
31      
32      private WebConsoleSession _consoleSession;
33  
34      public EquinoxConsoleSyncServlet(WebConsoleSession consoleSession)
35      {
36      	_consoleSession = consoleSession;
37      }
38      
39      @Override
40      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
41      {
42      	String cmd = request.getParameter("cmd");
43      	String Action = request.getParameter("Action");
44      	if (Action != null && Action.toLowerCase().indexOf("clear") != -1)
45      	{
46              _consoleSession.clearOutput();
47      	}
48      	if (cmd != null)
49      	{
50      	    _consoleSession.processCommand(cmd, true);
51      	}
52      	
53          response.setContentType("text/html;charset=utf-8");
54      	PrintWriter p = response.getWriter();
55          p.println("<html><head><title>Equinox Console (Synchroneous)</title></head><body>");
56          p.println("<textarea rows=\"30\" cols=\"110\">");
57      	p.println(_consoleSession.getOutputAsWriter().toString());
58          p.println("</textarea>");
59          p.println("<form method=\"GET\" action=\""+response.encodeURL(getURI(request))+"\">");
60          p.println("osgi>&nbsp;<input type=\"text\" name=\"cmd\" value=\"\"/><br/>\n");
61          p.println("<input type=\"submit\" name=\"Action\" value=\"Submit or Refresh\"><br/>");
62          p.println("<input type=\"submit\" name=\"Action\" value=\"Clear and Submit\"><br/>");
63          p.println("</form>");
64          p.println("<br/>");
65      }
66      
67      
68      private String getURI(HttpServletRequest request)
69      {
70          String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
71          if (uri == null)
72              uri= request.getRequestURI();
73          return uri;
74      }
75  
76  }