package org.jboss.resteasy.plugins.server.undertow;
import io.undertow.Undertow;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.resource.ResourceHandler;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ServletContainer;
import io.undertow.servlet.api.ServletInfo;
import org.jboss.resteasy.core.ResteasyDeploymentImpl;
import org.jboss.resteasy.plugins.server.embedded.EmbeddedJaxrsServer;
import org.jboss.resteasy.plugins.server.embedded.SecurityDomain;
import org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.util.EmbeddedServerHelper;
import org.jboss.resteasy.util.PortProvider;
import javax.servlet.ServletException;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import static io.undertow.servlet.Servlets.servlet;
public class implements EmbeddedJaxrsServer<UndertowJaxrsServer>
{
protected final PathHandler = new PathHandler();
protected final ServletContainer = ServletContainer.Factory.newInstance();
protected Undertow ;
protected DeploymentManager ;
protected Map<String, String> ;
protected Map<String, String> ;
private ResteasyDeployment ;
private int = PortProvider.getPort();
private String = "localhost";
private String ;
private EmbeddedServerHelper = new EmbeddedServerHelper();
@Override
public UndertowJaxrsServer () {
serverHelper.checkDeployment(deployment);
return deploy(deployment, serverHelper.checkContextPath(rootResourcePath),
deployment.getClass().getClassLoader());
}
@Override
public UndertowJaxrsServer ()
{
server = Undertow.builder()
.addHttpListener(port, hostname)
.setHandler(root)
.build();
server.start();
return this;
}
@Override
public void ()
{
server.stop();
if (deployment != null) {
deployment.stop();
}
}
public ResteasyDeployment () {
if (deployment == null) {
deployment = new ResteasyDeploymentImpl();
}
return deployment;
}
@Override
public UndertowJaxrsServer (ResteasyDeployment deployment) {
this.deployment = deployment;
return this;
}
@Override
public UndertowJaxrsServer (int port) {
this.port = port;
return this;
}
@Override
public UndertowJaxrsServer (String hostname) {
this.hostname = hostname;
return this;
}
@Override
public UndertowJaxrsServer (String rootResourcePath) {
this.rootResourcePath = rootResourcePath;
return this;
}
@Override
public UndertowJaxrsServer setSecurityDomain(SecurityDomain sc) {
return this;
}
public UndertowJaxrsServer (Application application) {
ResteasyDeployment resteasyDeployment = new ResteasyDeploymentImpl();
resteasyDeployment.setApplication(application);
return deploy(resteasyDeployment,
serverHelper.checkAppPath(application.getClass().getAnnotation(ApplicationPath.class)),
application.getClass().getClassLoader());
}
public UndertowJaxrsServer (Application application, String contextPath) {
ResteasyDeployment resteasyDeployment = new ResteasyDeploymentImpl();
resteasyDeployment.setApplication(application);
resteasyDeployment.start();
return deploy(resteasyDeployment,
serverHelper.checkContextPath(contextPath),
application.getClass().getClassLoader());
}
public UndertowJaxrsServer (Class<? extends Application> application) {
ResteasyDeployment resteasyDeployment = new ResteasyDeploymentImpl();
resteasyDeployment.setApplicationClass(application.getName());
return deploy(resteasyDeployment,
serverHelper.checkAppPath(application.getAnnotation(ApplicationPath.class)),
resteasyDeployment.getClass().getClassLoader());
}
public UndertowJaxrsServer (Class<? extends Application> application,
String contextPath) {
ResteasyDeployment resteasyDeployment = new ResteasyDeploymentImpl();
resteasyDeployment.setApplicationClass(application.getName());
return deploy(resteasyDeployment, serverHelper.checkContextPath(contextPath),
resteasyDeployment.getClass().getClassLoader());
}
private UndertowJaxrsServer (ResteasyDeployment resteasyDeployment,
String contextPath, ClassLoader clazzLoader) {
DeploymentInfo di = undertowDeployment(resteasyDeployment);
populateDeploymentInfo(di, clazzLoader, contextPath);
return deploy(di);
}
public DeploymentInfo (ResteasyDeployment resteasyDeployment, String mappingPrefix)
{
String mapping = serverHelper.checkContextPath(mappingPrefix);
if (!mapping.endsWith("/")) {
mapping += "/";
}
mapping = mapping + "*";
ServletInfo resteasyServlet = servlet("ResteasyServlet", HttpServlet30Dispatcher.class)
.setAsyncSupported(true)
.setLoadOnStartup(1)
.addMapping(mapping);
if (!mapping.equals("/*")) {
String prefix = mapping.substring(0, mapping.length() - 2);
resteasyServlet.addInitParam("resteasy.servlet.mapping.prefix", prefix);
}
return new DeploymentInfo()
.addServletContextAttribute(ResteasyDeployment.class.getName(), resteasyDeployment)
.addServlet(resteasyServlet);
}
public DeploymentInfo (ResteasyDeployment resteasyDeployment)
{
return undertowDeployment(resteasyDeployment, serverHelper.checkAppDeployment(resteasyDeployment));
}
public DeploymentInfo (Class<? extends Application> application)
{
ResteasyDeployment resteasyDeployment = new ResteasyDeploymentImpl();
resteasyDeployment.setApplicationClass(application.getName());
DeploymentInfo di = undertowDeployment (resteasyDeployment,
serverHelper.checkAppPath(application.getAnnotation(ApplicationPath.class)));
di.setClassLoader(application.getClassLoader());
return di;
}
public void addResourcePrefixPath(String path, ResourceHandler handler)
{
root.addPrefixPath(path, handler);
}
public UndertowJaxrsServer (ResteasyDeployment resteasyDeployment)
{
return deploy(resteasyDeployment, serverHelper.checkContextPath(rootResourcePath),
resteasyDeployment.getClass().getClassLoader());
}
public UndertowJaxrsServer (DeploymentInfo builder)
{
manager = container.addDeployment(builder);
manager.deploy();
try
{
root.addPrefixPath(builder.getContextPath(), manager.start());
}
catch (ServletException e)
{
throw new RuntimeException(e);
}
return this;
}
public UndertowJaxrsServer (Undertow.Builder builder)
{
server = builder.setHandler(root).build();
server.start();
return this;
}
public DeploymentManager () {
return manager;
}
public Map<String, String> () {
if (contextParams == null) {
contextParams = new HashMap<>();
}
return contextParams;
}
public UndertowJaxrsServer (Map<String, String> contextParams) {
this.contextParams = contextParams;
return this;
}
public Map<String, String> () {
if (initParams == null) {
initParams = new HashMap<>();
}
return initParams;
}
public UndertowJaxrsServer (Map<String, String> initParams) {
this.initParams = initParams;
return this;
}
public UndertowJaxrsServer (Class<? extends Application> application) {
return deployOldStyle(application, serverHelper.checkAppPath(application
.getAnnotation(ApplicationPath.class)));
}
public UndertowJaxrsServer (Class<? extends Application> application,
String ctxtPath) {
ResteasyDeployment resteasyDeployment = new ResteasyDeploymentImpl();
resteasyDeployment.setApplicationClass(application.getName());
String contextPath = serverHelper.checkContextPath(ctxtPath);
DeploymentInfo di = undertowDeployment(resteasyDeployment, "/");
populateDeploymentInfo(di, resteasyDeployment.getClass().getClassLoader(), contextPath);
return deploy(di);
}
private void (DeploymentInfo di, ClassLoader clazzLoader,
String contextPath) {
di.setClassLoader(clazzLoader);
di.setContextPath(contextPath);
di.setDeploymentName("Resteasy" + contextPath);
if (contextParams != null) {
for (Entry<String, String> e : contextParams.entrySet()) {
di.addInitParameter(e.getKey(), e.getValue());
}
}
if (initParams != null) {
ServletInfo servletInfo = di.getServlets().get("ResteasyServlet");
for (Entry<String, String> e : initParams.entrySet()) {
servletInfo.addInitParam(e.getKey(), e.getValue());
}
}
}
}