|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.docs.cloudfoundry; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Collections; |
| 21 | + |
| 22 | +import javax.servlet.GenericServlet; |
| 23 | +import javax.servlet.Servlet; |
| 24 | +import javax.servlet.ServletContainerInitializer; |
| 25 | +import javax.servlet.ServletContext; |
| 26 | +import javax.servlet.ServletException; |
| 27 | +import javax.servlet.ServletRequest; |
| 28 | +import javax.servlet.ServletResponse; |
| 29 | + |
| 30 | +import org.apache.catalina.Host; |
| 31 | +import org.apache.catalina.core.StandardContext; |
| 32 | +import org.apache.catalina.startup.Tomcat; |
| 33 | + |
| 34 | +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; |
| 35 | +import org.springframework.boot.web.servlet.ServletContextInitializer; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | + |
| 38 | +/** |
| 39 | + * Example configuration for custom context path in Cloud Foundry. |
| 40 | + * |
| 41 | + * @author Johnny Lim |
| 42 | + */ |
| 43 | +public class CloudFoundryCustomContextPathExample { |
| 44 | + |
| 45 | + // tag::configuration[] |
| 46 | + @Bean |
| 47 | + public TomcatServletWebServerFactory servletWebServerFactory() { |
| 48 | + return new TomcatServletWebServerFactory() { |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void prepareContext(Host host, ServletContextInitializer[] initializers) { |
| 52 | + super.prepareContext(host, initializers); |
| 53 | + StandardContext child = new StandardContext(); |
| 54 | + child.addLifecycleListener(new Tomcat.FixContextListener()); |
| 55 | + child.setPath("/cloudfoundryapplication"); |
| 56 | + ServletContainerInitializer initializer = getServletContextInitializer(getContextPath()); |
| 57 | + child.addServletContainerInitializer(initializer, Collections.emptySet()); |
| 58 | + child.setCrossContext(true); |
| 59 | + host.addChild(child); |
| 60 | + } |
| 61 | + |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + private ServletContainerInitializer getServletContextInitializer(String contextPath) { |
| 66 | + return (c, context) -> { |
| 67 | + Servlet servlet = new GenericServlet() { |
| 68 | + |
| 69 | + @Override |
| 70 | + public void service(ServletRequest req, ServletResponse res) |
| 71 | + throws ServletException, IOException { |
| 72 | + ServletContext context = req.getServletContext().getContext(contextPath); |
| 73 | + context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res); |
| 74 | + } |
| 75 | + |
| 76 | + }; |
| 77 | + context.addServlet("cloudfoundry", servlet).addMapping("/*"); |
| 78 | + }; |
| 79 | + } |
| 80 | + // end::configuration[] |
| 81 | + |
| 82 | +} |
0 commit comments