Groovy Web Console

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

License

License

Categories

Categories

Groovy Languages
GroupId

GroupId

ch.baurs
ArtifactId

ArtifactId

groovy-web-console
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Groovy Web Console
A web-based Groovy console written as a pure Java Servlet / Filter

Download groovy-web-console

How to add to project

<!-- https://jarcasting.com/artifacts/ch.baurs/groovy-web-console/ -->
<dependency>
    <groupId>ch.baurs</groupId>
    <artifactId>groovy-web-console</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/ch.baurs/groovy-web-console/
implementation 'ch.baurs:groovy-web-console:1.0.0'
// https://jarcasting.com/artifacts/ch.baurs/groovy-web-console/
implementation ("ch.baurs:groovy-web-console:1.0.0")
'ch.baurs:groovy-web-console:jar:1.0.0'
<dependency org="ch.baurs" name="groovy-web-console" rev="1.0.0">
  <artifact name="groovy-web-console" type="jar" />
</dependency>
@Grapes(
@Grab(group='ch.baurs', module='groovy-web-console', version='1.0.0')
)
libraryDependencies += "ch.baurs" % "groovy-web-console" % "1.0.0"
[ch.baurs/groovy-web-console "1.0.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.codehaus.groovy : groovy jar 2.5.0
org.baswell : routes jar 1.3
org.json : json jar 20180130
org.slf4j : slf4j-api Optional jar [1.6.6,2.0)

provided (2)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0
org.projectlombok : lombok jar 1.18.0

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.0.0
0.9.0