|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.nifi.web.content.viewer; |
| 18 | + |
| 19 | +import jakarta.servlet.DispatcherType; |
| 20 | +import jakarta.servlet.FilterRegistration; |
| 21 | +import jakarta.servlet.ServletContext; |
| 22 | +import jakarta.servlet.ServletContextEvent; |
| 23 | +import jakarta.servlet.ServletContextListener; |
| 24 | +import jakarta.servlet.ServletRegistration; |
| 25 | +import jakarta.servlet.annotation.WebListener; |
| 26 | +import org.apache.nifi.web.controller.StandardContentViewerController; |
| 27 | +import org.apache.nifi.web.servlet.filter.QueryStringToFragmentFilter; |
| 28 | +import org.eclipse.jetty.ee10.servlet.DefaultServlet; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.util.EnumSet; |
| 33 | + |
| 34 | +/** |
| 35 | + * Servlet Context Listener supporting registration of Filters |
| 36 | + */ |
| 37 | +@WebListener |
| 38 | +public class StandardServletContextListener implements ServletContextListener { |
| 39 | + private static final String ALL_URLS = "/*"; |
| 40 | + |
| 41 | + private static final String API_CONTENT_MAPPING = "/api/content"; |
| 42 | + |
| 43 | + private static final int LOAD_ON_STARTUP_ENABLED = 1; |
| 44 | + |
| 45 | + private static final String DIR_ALLOWED_PARAMETER = "dirAllowed"; |
| 46 | + |
| 47 | + private static final String BASE_RESOURCE_PARAMETER = "baseResource"; |
| 48 | + |
| 49 | + private static final String BASE_RESOURCE_DIRECTORY = "WEB-INF/classes/static"; |
| 50 | + |
| 51 | + private static final String FILTER_URL = ""; |
| 52 | + |
| 53 | + private static final Logger logger = LoggerFactory.getLogger(StandardServletContextListener.class); |
| 54 | + |
| 55 | + @Override |
| 56 | + public void contextInitialized(final ServletContextEvent sce) { |
| 57 | + final ServletContext servletContext = sce.getServletContext(); |
| 58 | + final FilterRegistration.Dynamic filter = servletContext.addFilter(QueryStringToFragmentFilter.class.getSimpleName(), QueryStringToFragmentFilter.class); |
| 59 | + filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, FILTER_URL); |
| 60 | + |
| 61 | + final ServletRegistration.Dynamic servlet = servletContext.addServlet(StandardContentViewerController.class.getSimpleName(), StandardContentViewerController.class); |
| 62 | + servlet.addMapping(API_CONTENT_MAPPING); |
| 63 | + servlet.setLoadOnStartup(LOAD_ON_STARTUP_ENABLED); |
| 64 | + |
| 65 | + final ServletRegistration.Dynamic defaultServlet = servletContext.addServlet(DefaultServlet.class.getSimpleName(), DefaultServlet.class); |
| 66 | + defaultServlet.addMapping(ALL_URLS); |
| 67 | + defaultServlet.setInitParameter(DIR_ALLOWED_PARAMETER, Boolean.FALSE.toString()); |
| 68 | + defaultServlet.setInitParameter(BASE_RESOURCE_PARAMETER, BASE_RESOURCE_DIRECTORY); |
| 69 | + defaultServlet.setLoadOnStartup(LOAD_ON_STARTUP_ENABLED); |
| 70 | + |
| 71 | + logger.info("Standard Content Viewer Initialized"); |
| 72 | + } |
| 73 | +} |
0 commit comments