View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.osgi.equinoxtools.console;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  
30  /**
31   * Take the example ChatServlet and use it to  make an Equinox Console on the web.
32   */
33  public class EquinoxConsoleSyncServlet extends HttpServlet
34  {
35  
36      private static final long serialVersionUID = 1L;
37      
38      private WebConsoleSession _consoleSession;
39  
40      public EquinoxConsoleSyncServlet(WebConsoleSession consoleSession)
41      {
42      	_consoleSession = consoleSession;
43      }
44      
45      @Override
46      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
47      {
48      	String cmd = request.getParameter("cmd");
49      	String Action = request.getParameter("Action");
50      	if (Action != null && Action.toLowerCase().indexOf("clear") != -1)
51      	{
52              _consoleSession.clearOutput();
53      	}
54      	if (cmd != null)
55      	{
56      	    _consoleSession.processCommand(cmd, true);
57      	}
58      	
59          response.setContentType("text/html;charset=utf-8");
60      	PrintWriter p = response.getWriter();
61          p.println("<html><head><title>Equinox Console (Synchroneous)</title></head><body>");
62          p.println("<textarea rows=\"30\" cols=\"110\">");
63      	p.println(_consoleSession.getOutputAsWriter().toString());
64          p.println("</textarea>");
65          p.println("<form method=\"GET\" action=\""+response.encodeURL(getURI(request))+"\">");
66          p.println("osgi>&nbsp;<input type=\"text\" name=\"cmd\" value=\"\"/><br/>\n");
67          p.println("<input type=\"submit\" name=\"Action\" value=\"Submit or Refresh\"><br/>");
68          p.println("<input type=\"submit\" name=\"Action\" value=\"Clear and Submit\"><br/>");
69          p.println("</form>");
70          p.println("<br/>");
71      }
72      
73      
74      private String getURI(HttpServletRequest request)
75      {
76          String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
77          if (uri == null)
78              uri= request.getRequestURI();
79          return uri;
80      }
81  
82  }