Skip to content

A web-based Groovy console written as a pure Java Servlet

License

Notifications You must be signed in to change notification settings

sniffertine/groovy-web-console

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

groovy-web-console

A web-based Groovy console written as a pure Java Servlet or Filter

General

As a developer, it would be very convenient if you could access your running Java application and write some debug code for runtime analysis. This is especially useful for debugging purposes.

Using this project enables you to open a web (HTML) based console view that lets you immediately access your runtime environment using the groovy langauge.

Table of Contents

Demo

  1. Checkout the code to your local machine
    git clone https://github.com/sniffertine/groovy-web-console.git
    cd groovy-web-console
  2. Build the project and run the demo webapp
    ./mvnw clean install org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run
  3. Open your webbrowser here: http://localhost:8080

Setup

The setup is very simple, as the code is availabe via the maven central repository. Just add the dependency to your project pom.xml

<dependency>
    <groupId>ch.baurs</groupId>
    <artifactId>groovy-web-console</artifactId>
    <version>1.0.0</version>
</dependency>

Usage

There are three possible usage scenarios.

1. Java Servlet

Configure the GroovyConsoleServlet in your web.xml

<servlet>
    <servlet-name>groovyConsoleServlet</servlet-name>
    <servlet-class>ch.baurs.groovyconsole.GroovyConsoleServlet</servlet-class>
    <init-param>
        <param-name>mappingPath</param-name>
        <param-value>/groovyConsoleServlet</param-value>
    </init-param>
    <init-param>
        <param-name>theme</param-name>
        <param-value>twilight</param-value>
    </init-param>
    <init-param>
        <param-name>prompt</param-name>
        <param-value>console via servlet $></param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>groovyConsoleServlet</servlet-name>
    <url-pattern>/groovyConsoleServlet/*</url-pattern>
</servlet-mapping>

2. Java Servlet Filter

Configure the GroovyConsoleServlet in your web.xml

<filter>
    <filter-name>groovyConsoleFilter</filter-name>
    <filter-class>ch.baurs.groovyconsole.GroovyConsoleFilter</filter-class>
    <init-param>
        <param-name>mappingPath</param-name>
        <param-value>/groovyConsoleFilter</param-value>
    </init-param>
    <init-param>
        <param-name>theme</param-name>
        <param-value>twilight</param-value>
    </init-param>
    <init-param>
        <param-name>prompt</param-name>
        <param-value>console via filter $></param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>groovyConsoleFilter</filter-name>
    <url-pattern>/groovyConsoleFilter/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

3. Your own code

Configure the Application directly in your onw Java code. Here, I use an empty Servlet to demonstrate.

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>ch.baurs.groovyconsole.testwebapp.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myServlet/*</url-pattern>
</servlet-mapping>
package my.web.project.servlet;

public class MyServlet extends HttpServlet {

    private Application application;

    @Override
    public void init(ServletConfig config) throws ServletException {

        Configuration configuration = Configuration.builder()
                .mappingPath("/myServlet")
                .prompt("console directly configured $>")
                .theme("mdn-like")
                .build();

        application = new Application(configuration);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        application.handle(req, resp);
    }
}

Configuration

The Groovy Console Application has a number of configuration parameters available for customization. You can configure most of the parameters in two ways:

A) Through direct configuration using the Configuration.builder() before initializing the ch.baurs.groovyconsole.Application

B) As init-param when using the built-in GroovyConsoleFilter or GroovyConsoleServlet.

config param available as init-param (web.xml) description
contextPath no (taken from ServletContext) The contextPath of the web application the groovy-web-console is part of.
💡 see ServletContext#getContextPath

default value: <empty string>
mappingPath yes The path below contextPath were the groovy-web-console is mapped to.
This value should correspond to the <url-pattern> in your <servlet-mapping> section of the web.xml file.
💡 see http://tutorials.jenkov.com/java-servlets/web-xml.html#configuration

default value: /groovyWebConsole
theme yes The codemirror theme to be used for console text formatting.
💡 see https://codemirror.net/demo/theme.html#vibrant-ink

default value: vibrant-ink
prompt yes The prompt text of the console.

default value: gc$
sessionInactiveInterval yes The number of seconds of inactivity before the session times out.
💡 see https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpSession.html#setMaxInactiveInterval-int-.

default value: 86400 ( = 24 hours)
characterEncoding yes The charset to be used.

default value: UTF-8
inlineAssets yes Defines if assets (js, css) should be inlined in the html.

default value: false
ipAuth yes Enables access restriction by remote IP address. The value can be a regular expression.
💡 see https://javaee.github.io/javaee-spec/javadocs/javax/servlet/ServletRequest.html#getRemoteAddr--.

default value: null
envAuth yes Enables access restriction by environment variable. The value must be in the form key=value
This requires System.getProperty("key") to be equal to value.

default value: null