Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Integration with Jetty can be done in a number of ways.

Integrating with Jetty in

...

Ceptors Distribution

PortalProtect Ceptor contains a built-in Jetty server, which you can use for your own webapps.

...

Code Block
languagexml
titlepp_launch_demoapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- PortalProtectCeptor Launcher configuration -->
<portalprotectlauncher<ceptorlauncher port="21311"
	launcherclasspath="classes/launcher;lib/PortalProtectAgentCeptorAgent.jar;extlib/logback-core-1.1.5.jar;extlib/logback-classic-1.1.5.jar;extlib/slf4j-api-1.7.16.jar" jvmstartdelay="10">
    <!-- PortalProtectCeptor DemoApp -->
    <jvm name="demoapp" vmargs="-Xmx1024M -Xnoclassgc" systemclasspath="">
        <config servers="nios://localhost:21233?validateservercert=false" />
        <classloader name="demoapp" extraclasspath="extlib_extras/jsp">
            <service name="webserver1" launcherclass="dk.itp.managed.service.GenericWarLauncher">
                <property name="registerppsecurityhandler" value="true"/>
                <webserver webapp="${portalprotect.home}/samples/demoapp/PortalProtectDemoApp.war"
                    contextpath="/"
                    bindaddress="0.0.0.0" httpport="8080" sslport="0" minthreads="2" maxthreads="500"
                    maxpostsize="67108864" maxidletime="10000"
                    responseheadersize="32768"
                    outputbuffersize="32768"
                    />
            </service>
        </classloader>
    </jvm>
</portalprotectlauncher>ceptorlauncher>

In the above example, the property 

...

Code Block
package dk.portalprotect.launcher.demoapp;

import java.io.File;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.security.auth.Subject;
import javax.servlet.ServletRequest;

import dk.itp.security.passticket.Agent;
import dk.itp.security.passticket.PTException;
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.security.DefaultIdentityService;
import org.eclipse.jetty.security.IdentityService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.webapp.WebAppContext;


/**
 * Bootstrapper, used to start the demoapp using an embedded jetty webserver
 * 
 * @author Kim Rasmussen
 * @version $Revision$
 * 
 * <pre>
 * PortalProtect - Security infrastructure
 * Copyright(c) 2010, IT Practice A/S, All rights reserved.
 * 
 * This source code is confidential.
 * </pre>
 */
public class StartJetty {
	public static class PPPrincipal implements Principal {
		String sessionid;
		
		public PPPrincipal(String sessionid) {
			this.sessionid = sessionid;
		}
		
		public String getName() {
			return sessionid;
		}
		
	}
	
	public static class PPUserIdentity implements UserIdentity {
		Subject subject;
		PPPrincipal principal;
		
		public PPUserIdentity(Subject subject, PPPrincipal principal) {
			this.subject = subject;
			this.principal = principal;
		}

		public Subject getSubject() {
			return subject;
		}

		public Principal getUserPrincipal() {
			return principal;
		}

		public boolean isUserInRole(String role, Scope scope) {
			try {
				boolean member = Agent.getInstance().isMemberOfGroup(principal.sessionid, role);
				if (!member) {
					System.err.println("Warning: User: "+ principal.sessionid + " was not a member of role: " + role);
				}
				return member;
			} catch (PTException e) {
				e.printStackTrace();
				return false;
			}
		}
		
	}
	
	public static class PPLoginService implements LoginService {
		IdentityService identService = new DefaultIdentityService();
		
		public UserIdentity login(String user, Object credentials, ServletRequest request) {
			if (Agent.getInstance().isValid(user)) {
				PPPrincipal principal = new PPPrincipal(user);
				Subject subject = new Subject();
				subject.getPrincipals().add(principal);
				
				return new PPUserIdentity(subject, principal);
				
			}
			return null;
		}
		
		public boolean validate(UserIdentity useridentity) {
			if (useridentity instanceof PPUserIdentity) {
				return Agent.getInstance().isValid(((PPUserIdentity)useridentity).principal.sessionid);
			}
			return false;
		}
		
		public void setIdentityService(IdentityService identityservice) {
			identService = identityservice;
		}
		
		public String getName() {
			return "PortalProtect Security Realm";
		}
		
		public IdentityService getIdentityService() {
			return identService;
		}

		public void logout(UserIdentity useridentity) {
			if (useridentity instanceof PPUserIdentity) {
				try {
					Agent.getInstance().logoff(((PPUserIdentity)useridentity).principal.sessionid);
				} catch (PTException e) {
				}
			}
		}		
	}
	
    private static List<ContainerInitializer> jspInitializers()
    {
        JettyJasperInitializer sci = new JettyJasperInitializer();
        ContainerInitializer initializer = new ContainerInitializer(sci, null);
        List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
        initializers.add(initializer);
        return initializers;
    }
    
	public static void main(String[] arguments) {
		try {
			System.setProperty("org.apache.jasper.compiler.disablejsr199","false");
			
			boolean develop = false;
			int port = 8080;
			for(int i = 0; i < arguments.length; i++) {
				if (arguments[i].equals("-develop"))
					develop = true;
				else if (arguments[i].equals("-port") && i+1 < arguments.length) {
					port = Integer.parseInt(arguments[++i]);
				}
			}
			
			HttpConfiguration httpConfig = new HttpConfiguration();
			httpConfig.setSendServerVersion(false);
			httpConfig.setSendXPoweredBy(false);
			httpConfig.setSendDateHeader(false);
			httpConfig.setResponseHeaderSize(32768);
			httpConfig.setOutputBufferSize(32768);

			final Server srv = new Server();
			ServerConnector con = new ServerConnector(srv, new HttpConnectionFactory(httpConfig));

			con.setPort(port);
			srv.setConnectors(new Connector[] {con});
			
			WebAppContext webapp = new WebAppContext();
			webapp.setContextPath("/");
			webapp.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
			webapp.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
			
			if (develop) {
				webapp.setWar("webapp");
			} else { 
				if (new File("dist/PortalProtectDemoApp.war").exists())
					webapp.setWar("dist/PortalProtectDemoApp.war");
				else
					webapp.setWar("PortalProtectDemoApp.war");
			}
			
			webapp.getSecurityHandler().setLoginService(new PPLoginService());
			webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(60*60);
			srv.setHandler(webapp);
			srv.start();
			
	        Handler handler = srv.getHandler(); 
	        if(handler instanceof WebAppContext) { 
	            webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(60*60); // sets to 30 sec 
	        } 
	        
			Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
				public void run() {
					try {
						srv.stop();
					} catch(Throwable t) {
						System.err.println(new Date() + ": Problem shutting down.");
						t.printStackTrace();
					}
				}
			}));

			System.out.println("Jetty now ready on port " + port + " - connect to the demoapp using the PP dispatcher.");
			while(true)
				Thread.sleep(60000);
		} catch (Throwable e) {
			System.err.println("Fatal error starting webserver");
			e.printStackTrace();
		}		
	}	
}


...