use of io.undertow.jsp.HackInstanceManager in project keycloak by keycloak.
the class UndertowDeployerHelper method getDeploymentInfo.
public DeploymentInfo getDeploymentInfo(UndertowContainerConfiguration config, WebArchive archive, DeploymentInfo di) {
String archiveName = archive.getName();
String contextPath = getContextPath(archive);
String appContextUrl = "http://" + config.getBindAddress() + ":" + config.getBindHttpPort() + contextPath;
try {
if (di == null) {
di = new DeploymentInfo();
}
UndertowWarClassLoader classLoader = new UndertowWarClassLoader(Thread.currentThread().getContextClassLoader(), archive);
di.setClassLoader(classLoader);
di.setDeploymentName(archiveName);
di.setContextPath(contextPath);
ResourceManager undertowResourcesWrapper = getResourceManager(appContextUrl, archive);
di.setResourceManager(undertowResourcesWrapper);
if (archive.contains("/WEB-INF/web.xml")) {
Document webXml = loadXML(archive.get("/WEB-INF/web.xml").getAsset().openStream());
new SimpleWebXmlParser().parseWebXml(webXml, di);
}
di.addServlet(JspServletBuilder.createServlet("Default Jsp Servlet", "*.jsp"));
di.addWelcomePages("index.html", "index.jsp");
JspServletBuilder.setupDeployment(di, new HashMap<>(), new HashMap<>(), new HackInstanceManager());
addAnnotatedServlets(di, archive);
return di;
} catch (IOException | IllegalArgumentException ioe) {
throw new RuntimeException("Error deploying " + archive.getName(), ioe);
}
}
use of io.undertow.jsp.HackInstanceManager in project solon by noear.
the class PluginUndertowJsp method buildHandler.
@Override
protected HttpHandler buildHandler() throws Exception {
DeploymentInfo builder = initDeploymentInfo();
// 添加jsp处理
String fileRoot = getResourceRoot();
builder.setResourceManager(new JspResourceManager(JarClassLoader.global(), fileRoot)).addServlet(new ServletInfo("ACTServlet", UtHandlerJspHandler.class).addMapping("/")).addServlet(JspServletEx.createServlet("JSPServlet", "*.jsp"));
// 添加taglib支持
HashMap<String, TagLibraryInfo> tagLibraryMap = JspTldLocator.createTldInfos("WEB-INF");
JspServletBuilder.setupDeployment(builder, new HashMap<String, JspPropertyGroup>(), tagLibraryMap, new HackInstanceManager());
// 开始部署
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
return manager.start();
}
use of io.undertow.jsp.HackInstanceManager in project OpenUnison by TremoloSecurity.
the class OpenUnisonOnUndertow method main.
public static void main(String[] args) throws Exception {
OpenUnisonConfig config = null;
logger.info("Starting OpenUnison on Undertow " + OpenUnisonServletFilter.version);
if (args.length == 0) {
logger.error("One argument required, path to yaml or json config");
System.exit(1);
} else if (args[0].endsWith(".yaml")) {
logger.info("Parsing YAML : '" + args[0] + "'");
Yaml yaml = new Yaml();
Map<String, Object> map = (Map<String, Object>) yaml.load(new FileInputStream(args[0]));
JSONObject jsonObject = new JSONObject(map);
String json = jsonObject.toJSONString();
config = gson.fromJson(json, OpenUnisonConfig.class);
} else {
logger.info("Parsing JSON : '" + args[0] + "'");
config = gson.fromJson(new InputStreamReader(new FileInputStream(args[0])), OpenUnisonConfig.class);
}
final OpenUnisonConfig fconfig = config;
if (config.getContextRoot() == null) {
config.setContextRoot("/");
}
logger.info("Config Open Port : '" + config.getOpenPort() + "'");
logger.info("Disable HTTP2 : '" + config.isDisableHttp2() + "'");
logger.info("Allow unescaped characters : '" + config.isAllowUnEscapedChars() + "'");
logger.info("Config Open External Port : '" + config.getOpenExternalPort() + "'");
logger.info("Config Secure Port : '" + config.getSecurePort() + "'");
logger.info("Config Secure External Port : '" + config.getSecureExternalPort() + "'");
logger.info("Config Context Root : '" + config.getContextRoot() + "'");
logger.info("Force to Secure : '" + config.isForceToSecure() + "'");
logger.info("ActiveMQ Directory : '" + config.getActivemqDir() + "'");
logger.info("Quartz Directory : '" + config.getQuartzDir() + "'");
logger.info("Config TLS Client Auth Mode : '" + config.getClientAuth() + "'");
logger.info("Config TLS Allowed Client Subjects : '" + config.getAllowedClientNames() + "'");
logger.info("Config TLS Protocols : '" + config.getAllowedTlsProtocols() + "'");
logger.info("Config TLS Ciphers : '" + config.getCiphers() + "'");
logger.info("Config Path to Deployment : '" + config.getPathToDeployment() + "'");
logger.info("Config Path to Environment File : '" + config.getPathToEnvFile() + "'");
logger.info("Redirect to contex root : '" + config.isRedirectToContextRoot() + "'");
logger.info("Support socket shutdown : " + config.isSocketShutdownListener());
if (config.isSocketShutdownListener()) {
logger.info("Socket shutdown host : '" + config.getSocketShutdownHost() + "'");
logger.info("Socket shutdown port : '" + config.getSocketShutdownPort() + "'");
logger.info("Socket shutdown command : '" + config.getSocketShutdownCommand() + "'");
}
logger.info("Override Queue Configuration : '" + config.getQueueConfiguration() != null + "'");
logger.info("Creating unisonServiceProps");
File f = File.createTempFile("unisonService", "props");
logger.info("Temporary unisonServiceProps : '" + f.getAbsolutePath() + "'");
Properties unisonServiceProps = new Properties();
unisonServiceProps.put("com.tremolosecurity.openunison.forceToSSL", Boolean.toString(config.isForceToSecure()));
unisonServiceProps.put("com.tremolosecurity.openunison.openPort", Integer.toString(config.getOpenPort()));
unisonServiceProps.put("com.tremolosecurity.openunison.securePort", Integer.toString(config.getSecurePort()));
unisonServiceProps.put("com.tremolosecurity.openunison.externalOpenPort", Integer.toString(config.getOpenExternalPort()));
unisonServiceProps.put("com.tremolosecurity.openunison.externalSecurePort", Integer.toString(config.getSecureExternalPort()));
if (config.getActivemqDir() != null) {
unisonServiceProps.put("com.tremolosecurity.openunison.activemqdir", config.getActivemqDir());
}
if (config.getQuartzDir() != null) {
unisonServiceProps.put("com.tremolosecurity.openunison.quartzdir", config.getQuartzDir());
}
unisonServiceProps.store(new FileOutputStream(f), "OpenUnison Configuration");
System.getProperties().put("com.tremolosecurity.unison.unisonServicePropsPath", f.getAbsolutePath());
System.getProperties().put("com.tremolosecurity.unison.unisonXML", config.getPathToDeployment() + "/webapp/WEB-INF/unison.xml");
logger.info("Loading environment file : '" + config.getPathToEnvFile() + "'");
Properties env = new Properties();
env.load(new FileInputStream(config.getPathToEnvFile()));
for (Object name : env.keySet()) {
logger.info("Adding property : '" + name + "'");
System.setProperty((String) name, env.getProperty((String) name));
}
if (config.getQueueConfiguration() != null) {
QueueConfigType qc = new QueueConfigType();
qc.setConnectionFactory(config.getQueueConfiguration().getConnectionFactory());
qc.setEncryptionKeyName(config.getQueueConfiguration().getEncryptionKeyName());
qc.setIsUseInternalQueue(config.getQueueConfiguration().isUseInternalQueue());
qc.setKeepAliveMillis(config.getQueueConfiguration().getKeepAliveMillis());
qc.setMaxConsumers(((Long) config.getQueueConfiguration().getMaxConsumers()).intValue());
qc.setMaxProducers(((Long) config.getQueueConfiguration().getMaxProducers()).intValue());
qc.setMaxSessionsPerConnection(((Long) config.getQueueConfiguration().getMaxSessionsPerConnection()).intValue());
qc.setMultiTaskQueues(config.getQueueConfiguration().isMultiTaskQueues());
qc.setNumQueues(((Long) config.getQueueConfiguration().getNumQueues()).intValue());
qc.setSmtpQueueName(config.getQueueConfiguration().getSmtpQueueName());
qc.setTaskQueueName(config.getQueueConfiguration().getTaskQueueName());
for (QueueConfigParam param : config.getQueueConfiguration().getParams()) {
ParamType pt = new ParamType();
pt.setName(param.getName());
if (param.getSourceType().equalsIgnoreCase("static")) {
pt.setValue(param.getValue());
} else {
pt.setValue(System.getProperty(param.getValue()));
}
qc.getParam().add(pt);
}
GlobalEntries.getGlobalEntries().set("openunison.queueconfig", qc);
}
logger.info("Loading keystore for Undertow");
String unisonXML = config.getPathToDeployment() + "/webapp/WEB-INF/unison.xml";
logger.info("OpenUnison XML File : '" + unisonXML + "'");
String unisonXMLContent = OpenUnisonConfigLoader.generateOpenUnisonConfig(unisonXML);
JAXBContext jc = JAXBContext.newInstance("com.tremolosecurity.config.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Object obj = unmarshaller.unmarshal(new ByteArrayInputStream(unisonXMLContent.getBytes("UTF-8")));
JAXBElement<TremoloType> cfg = (JAXBElement<TremoloType>) obj;
TremoloType unisonConfiguration = cfg.getValue();
logger.info("Loading keystore : '" + unisonConfiguration.getKeyStorePath() + "'");
logger.info("Building Undertow");
Builder buildUndertow = Undertow.builder();
buildUndertow.setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, 60000);
logger.info("Check if enabling HTTP2 - " + config.isDisableHttp2());
if (!config.isDisableHttp2()) {
logger.info("Enabling HTTP2");
buildUndertow.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
}
if (config.getOpenPort() > 0) {
buildUndertow.addHttpListener(config.getOpenPort(), "0.0.0.0");
logger.info("Adding open port : '" + config.getOpenPort() + "'");
}
if (config.getSecurePort() > 0) {
setupTlsListener(config, unisonConfiguration, buildUndertow);
}
File pathToWebApp = new File(config.getPathToDeployment() + "/webapp");
logger.info("Path to webapp : '" + pathToWebApp.getAbsolutePath() + "'");
logger.info("Path directory? : '" + pathToWebApp.isDirectory() + "'");
logger.info("Path exists : '" + pathToWebApp.exists() + "'");
DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(OpenUnisonOnUndertow.class.getClassLoader()).setEagerFilterInit(true).setContextPath(config.getContextRoot()).setDeploymentName("openunison").addFilter(Servlets.filter("openunison", com.tremolosecurity.openunison.OpenUnisonServletFilter.class).addInitParam("mode", "appliance")).addFilterUrlMapping("openunison", "/*", DispatcherType.REQUEST).setResourceManager(new FileResourceManager(pathToWebApp, 1024, true, true)).addServlet(JspServletBuilder.createServlet("Default Jsp Servlet", "*.jsp")).addServlet(Servlets.servlet("identityProvider", com.tremolosecurity.idp.server.IDP.class).addMapping("/auth/idp/*"));
if (config.getWelcomePages() != null) {
servletBuilder.addWelcomePages(config.getWelcomePages());
}
if (config.getErrorPages() != null) {
logger.info("Adding error pages");
ArrayList<ErrorPage> errorPages = new ArrayList<ErrorPage>();
for (ErrorPageConfig ep : config.getErrorPages()) {
if (ep.getCode() == 0) {
logger.info("Adding default page: " + ep.getLocation());
errorPages.add(new ErrorPage(ep.getLocation()));
} else {
logger.info("Adding page for " + ep.getCode() + " : " + ep.getLocation());
errorPages.add(new ErrorPage(ep.getLocation(), ep.getCode()));
}
}
servletBuilder.addErrorPages(errorPages);
}
JspServletBuilder.setupDeployment(servletBuilder, new HashMap<String, JspPropertyGroup>(), new HashMap<String, TagLibraryInfo>(), new HackInstanceManager());
DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
PathHandler path = Handlers.path(Handlers.redirect(config.getContextRoot())).addPrefixPath(config.getContextRoot(), manager.start());
if (config.isForceToLowerCase()) {
buildUndertow.setHandler(new OpenUnisonPathHandler(path));
} else {
buildUndertow.setHandler(path);
}
if (!config.getContextRoot().equals("/")) {
if (!config.isRedirectToContextRoot()) {
logger.info("Not redirecting to context");
servletBuilder = Servlets.deployment().setClassLoader(OpenUnisonOnUndertow.class.getClassLoader()).setEagerFilterInit(true).setContextPath("/").setDeploymentName("root");
manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
path.addPrefixPath("/", manager.start());
} else {
logger.info("Redirecting to context");
path.addPrefixPath("/", new RedirectHandler(config.getContextRoot()));
}
}
if (config.isAllowUnEscapedChars()) {
buildUndertow.setServerOption(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, true);
}
undertow = buildUndertow.build();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
logger.info("Shutting down");
undertow.stop();
GlobalEntries.getGlobalEntries().getConfigManager().clearThreads();
if (myvd != null) {
try {
myvd.shutdown();
} catch (Exception e) {
logger.warn("Did not gracefullt stop directory service", e);
}
}
}
});
if (config.isSocketShutdownListener()) {
new Thread() {
public void run() {
logger.info("Starting shutdown socket listener");
try {
ServerSocket socket = new ServerSocket(fconfig.getSocketShutdownPort(), 0, InetAddress.getByName(fconfig.getSocketShutdownHost()));
while (true) {
logger.info("shutdown waiting for input");
Socket clientSocket = null;
try {
clientSocket = socket.accept();
} catch (Throwable t) {
logger.warn("Could not accept connection", t);
continue;
}
logger.info("request received");
// PrintWriter out =
// new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
logger.info("reading data");
String command = in.readLine();
logger.info("'" + command + "'");
if (command != null) {
command.trim();
}
logger.info("'" + command + "'");
if (fconfig.getSocketShutdownCommand().equalsIgnoreCase(command)) {
logger.info("Stopping threads");
GlobalEntries.getGlobalEntries().getConfigManager().clearThreads();
logger.info("Shutting down undertow");
undertow.stop();
if (myvd != null) {
try {
myvd.shutdown();
} catch (Exception e) {
logger.warn("Did not gracefullt stop directory service", e);
}
}
logger.info("Closing input stream");
try {
in.close();
} catch (Throwable t) {
}
/*try {
out.close();
} catch (Throwable t) {}*/
logger.info("Closing client socket");
try {
clientSocket.close();
} catch (Throwable t) {
}
logger.info("Closing server socket");
try {
socket.close();
} catch (Throwable t) {
}
logger.info("Sleeping for 10 seconds");
try {
Thread.sleep(10000);
logger.info("Exiting");
System.exit(0);
return;
} catch (Exception e) {
}
} else {
command = null;
logger.info("invalid command");
try {
in.close();
} catch (Throwable t) {
}
/*try {
out.close();
} catch (Throwable t) {}
*/
try {
clientSocket.close();
} catch (Throwable t) {
}
}
}
} catch (IOException e) {
logger.error("Could not start shutdown listener", e);
}
}
}.start();
}
undertow.start();
if (config.getLdapPort() != 0 || config.getLdapsPort() != 0) {
myvd = (MyVDWrapper) Class.forName("com.tremolosecurity.openunison.myvd.MyVDOnUndertow").newInstance();
myvd.startMyVD(config, unisonConfiguration);
}
}
Aggregations