/** * /!\ Work only if application run on embedded Tomcat server * * @return * @throws Exception */ public static String getBaseUrlForEmbeddedTomcat() throws Exception { // get embedded tomcat EmbeddedWebApplicationContext appContext = (EmbeddedWebApplicationContext) new ApplicationContextProvider().getApplicationContext(); Tomcat tomcat = ((TomcatEmbeddedServletContainer) appContext.getEmbeddedServletContainer()).getTomcat(); Connector connector = tomcat.getConnector(); // compose address String scheme = connector.getScheme(); String hostName = tomcat.getHost().getName(); int port = connector.getPort(); String contextPath = appContext.getServletContext().getContextPath(); return scheme + "://" + hostName + ":" + port + contextPath; }
private Integer getServicePort(EmbeddedWebApplicationContext webContext) { Integer port = null; if (webContext != null) { final EmbeddedServletContainer container = webContext.getEmbeddedServletContainer(); if (container instanceof TomcatEmbeddedServletContainer) { // Work around an issue with the tomcat container, which uses the local port // as the port (-1) instead of the registered port port = ((TomcatEmbeddedServletContainer) container).getTomcat().getConnector().getPort(); } else { port = container.getPort(); } } return port; }
private Manager getManagerFromContext(EmbeddedWebApplicationContext applicationContext) { EmbeddedServletContainer embeddedServletContainer = applicationContext.getEmbeddedServletContainer(); if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) { return getManagerFromContainer((TomcatEmbeddedServletContainer) embeddedServletContainer); } return null; }
private Manager getManagerFromContainer(TomcatEmbeddedServletContainer servletContainer) { for (Container container : servletContainer.getTomcat().getHost().findChildren()) { if (container instanceof Context) { return ((Context) container).getManager(); } } return null; }
@Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { //tomcat.enableNaming(); System.out.println("Enterring TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer()..."); return super.getTomcatEmbeddedServletContainer(tomcat); } }; }
@Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { return super.getTomcatEmbeddedServletContainer(tomcat); } }; }
private Manager getManager(EmbeddedWebApplicationContext applicationContext) { EmbeddedServletContainer embeddedServletContainer = applicationContext .getEmbeddedServletContainer(); if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) { return getManager((TomcatEmbeddedServletContainer) embeddedServletContainer); } return null; }
private Manager getManager(TomcatEmbeddedServletContainer servletContainer) { for (Container container : servletContainer.getTomcat().getHost() .findChildren()) { if (container instanceof Context) { return ((Context) container).getManager(); } } return null; }
@Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { tomcat.enableNaming(); return super.getTomcatEmbeddedServletContainer(tomcat); } @Override /** * Configure Tomcat connection pooling. In the future other configuration parameters * could be added from https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html */ protected void postProcessContext(Context context) { ContextResource r = new ContextResource(); r.setName(env.getProperty("sta.datasource.name", "jdbc/sensorThings")); r.setAuth(env.getProperty("sta.datasource.auth", "Container")); r.setType(env.getProperty("sta.datasource.type", "javax.sql.DataSource")); r.setProperty("factory", env.getProperty("sta.datasource.factory","org.apache.tomcat.jdbc.pool.DataSourceFactory")); r.setProperty("driverClassName", env.getProperty("sta.datasource.driverClassName", "org.postgresql.Driver")); r.setProperty("url", env.getProperty("sta.datasource.url", "jdbc:postgresql://localhost:5432/sensorthings")); r.setProperty("username", env.getProperty("sta.datasource.username", "sensorthings")); r.setProperty("password", env.getProperty("sta.datasource.password", "ChangeMe!")); r.setProperty("maxTotal", env.getProperty("sta.datasource.maxTotal", "20")); r.setProperty("maxIdle", env.getProperty("sta.datasource.maxIdle", "10")); r.setProperty("maxWaitMillis", env.getProperty("sta.datasource.maxWaitMillis", "-1")); r.setProperty("defaultAutoCommit", env.getProperty("sta.datasource.defaultAutoCommit", "false")); r.setProperty("testOnBorrow", env.getProperty("sta.datasource.testOnBorrow", "true")); r.setProperty("validationQuery", env.getProperty("sta.datasource.validationQuery", "SELECT 1;")); r.setProperty("validationInterval", env.getProperty("sta.datasource.validationInterval", "5000")); context.getNamingResources().addResource(r); } }; }
@Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { Server server = tomcat.getServer(); Service service = new StandardService(); service.setName("ssl-redirect-service"); Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setPort(sslRedirectPort); service.addConnector(connector); server.addService(service); Engine engine = new StandardEngine(); service.setContainer(engine); Host host = new StandardHost(); host.setName("ssl-redirect-host"); engine.addChild(host); engine.setDefaultHost(host.getName()); Context context = new StandardContext(); context.addLifecycleListener(new Tomcat.FixContextListener()); context.setName("ssl-redirect-context"); context.setPath(""); host.addChild(context); Wrapper wrapper = context.createWrapper(); wrapper.setServlet(new HttpServlet() { @Override public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletServerHttpRequest r = new ServletServerHttpRequest(req); UriComponentsBuilder b = UriComponentsBuilder.fromHttpRequest(r); b.scheme("https"); b.port(null); res.sendRedirect(b.toUriString()); } }); wrapper.setName("ssl-redirect-servlet"); context.addChild(wrapper); context.addServletMapping("/", wrapper.getName()); return super.getTomcatEmbeddedServletContainer(tomcat); } }; }