Over a million developers have joined DZone.

Configuring a Jetty Servlet Proxy

Want to deploy some apps? See how to properly configure the Jetty servlet proxy.

· Integration Zone

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

Sample application can be downloaded from here

Lets say you deploy app1, app2, someotherapp to devhost:3000 and testhost:3000 then those apps can be accessed as follows

DEV instances
http://devhost:3000/app1
http://devhost:3000/app2
http://devhost:3000/someotherapp

TEST instances
http://testhost:3000/app1
http://testhost:3000/app2
http://testhost:3000/someotherapp

Many a times one application depends upon other applications with in the same server and hence, your application may contain code snippets as follows:

<link title="Stylesheet" href="/someotherapp/style.css" rel="stylesheet" media="screen" type="text/css" />

Further, those can be directly accessed as well

http://devhost:3000/someotherapp/style.css
http://testhost:3000/someotherapp/style.css

Further Jetty is the best choice for local development either embedded or using jetty-maven-plugin.

In this case it does not make sense to replicate the entire environment locally, instead those urls can be proxied in local environment such that /someotherapp/style.css would be proxied to http://devhost:3000/someotherapp/style.css

Here is how you can achieve it.

1) Map ProxyServlet.Transparent to /someotherapp/*
2) Set the Servlet init parameters as follows
ProxyTo ==> http://devhost:3000/someotherapp
Prefix ==> /someotherapp

The Above Two Steps Can be Done in Two Ways

Configuring Using Java

package com.nadeem.app;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;

import org.apache.wicket.util.time.Duration;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.ssl.SslSocketConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.webapp.WebAppContext;

public class Start {

    public static void main(String[] args) throws Exception {
        int timeout = (int) Duration.ONE_HOUR.getMilliseconds();

        SocketConnector connector = newConnector(timeout);
        Server server = newServer(timeout, connector);

        addSecureSupport(timeout, connector, server);

        addJmxSupport(server);

        try {
            System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
            server.start();
            System.in.read();
            System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
            server.stop();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    private static void addJmxSupport(Server server) throws Exception
    {
        // START JMX SERVER
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
        server.getContainer().addEventListener(mBeanContainer);
        mBeanContainer.start();
    }

    private static void addSecureSupport(int timeout, SocketConnector connector, Server server)
    {
        Resource keystore = Resource.newClassPathResource("/keystore");
        if (keystore != null && keystore.exists()) {
            // if a keystore for a SSL certificate is available, start a SSL
            // connector on port 8443.
            // By default, the quickstart comes with a Apache Wicket Quickstart
            // Certificate that expires about half way september 2021. Do not
            // use this certificate anywhere important as the passwords are
            // available in the source.

            connector.setConfidentialPort(8443);

            SslContextFactory factory = new SslContextFactory();
            factory.setKeyStoreResource(keystore);
            factory.setKeyStorePassword("wicket");
            factory.setTrustStoreResource(keystore);
            factory.setKeyManagerPassword("wicket");
            SslSocketConnector sslConnector = new SslSocketConnector(factory);
            sslConnector.setMaxIdleTime(timeout);
            sslConnector.setPort(8443);
            sslConnector.setAcceptors(4);
            server.addConnector(sslConnector);

            System.out.println("SSL access to the quickstart has been enabled on port 8443");
            System.out.println("You can access the application using SSL on https://localhost:8443");
            System.out.println();
        }
    }

    private static Server newServer(int timeout, SocketConnector connector) {
        Server server = new Server(); 
        server.setHandler(newHandlers(server));
        server.addConnector(connector);
        return server;
    }

    private static HandlerList newHandlers(Server server) {
        WebAppContext appContext = newWebAppContext(server);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] {appContext, proxyHandler(), new DefaultHandler()});
        return handlers;
    }

    private static SocketConnector newConnector(int timeout) {
        SocketConnector connector = new SocketConnector();

        // Set some timeout options to make debugging easier.
        connector.setMaxIdleTime(timeout);
        connector.setSoLingerTime(-1);
        connector.setPort(8080);

        return connector;

    }

    private static WebAppContext newWebAppContext(Server server) {
        WebAppContext bb = new WebAppContext();
        bb.setServer(server);
        bb.setContextPath("/webApp");
        bb.setWar("src/main/webapp");
        return bb;
    }

    private static ServletContextHandler proxyHandler() {
        ServletContextHandler contextHandler = new ServletContextHandler();
        contextHandler.setServletHandler(newServletHandler());

        return contextHandler;
    }

    private static ServletHandler newServletHandler() {


        ServletHandler handler = new ServletHandler();

        ServletHolder holder = handler.addServletWithMapping(org.eclipse.jetty.servlets.ProxyServlet.Transparent.class, "/proxy/*");
        holder.setInitParameter("ProxyTo", "http://localhost:8080/webApp");
        holder.setInitParameter("Prefix", "/proxy");        

        return handler;
    }
}

Configuring Using Maven Jetty Plugin

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <webAppConfig>
            <contextPath>/webApp</contextPath>
        </webAppConfig>
        <jettyConfig>src/main/etc/jetty.xml</jettyConfig>
        <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>8080</port>
                <maxIdleTime>3600000</maxIdleTime>
            </connector>                      
        </connectors>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>
</plugin>

src/main/etc/jetty.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">

<New id="ServletContextHandler" class="org.eclipse.jetty.servlet.ServletContextHandler">
 <Set name="servletHandler">
<New id="servletHandler" class="org.eclipse.jetty.servlet.ServletHandler">
<Call id="proxyHolder" name="addServletWithMapping">
<Arg>org.eclipse.jetty.servlets.ProxyServlet$Transparent</Arg>
<Arg>/proxy/*</Arg>
<Call name="setInitParameter">
<Arg>maxThreads</Arg>
<Arg>
<Property name="jetty.proxy.maxThreads" default="128" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>maxConnections</Arg>
<Arg>
<Property name="jetty.proxy.maxConnections" default="256" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>idleTimeout</Arg>
<Arg>
<Property name="jetty.proxy.idleTimeout" default="30000" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>timeout</Arg>
<Arg>
<Property name="jetty.proxy.timeout" default="60000" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>ProxyTo</Arg>
<Arg>http://localhost:8080/wicket</Arg>
</Call>
<Call name="setInitParameter">
<Arg>Prefix</Arg>
<Arg>/proxy</Arg>
</Call>
</Call>

</New>
</Set>
</New>

<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerList">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<Ref id="ServletContextHandler"></Ref>
</Item>
</Array>
</Set>
</New>
</Set>


</Configure>

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
jetty ,integration

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}