All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.undertow.server.servlet.HybridServletHandlerFactory Maven / Gradle / Ivy

There is a newer version: 8.1.3
Show newest version
/*
 * Copyright (c) 2008-2019 The Aspectran Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aspectran.undertow.server.servlet;

import com.aspectran.core.component.bean.aware.ActivityContextAware;
import com.aspectran.core.context.ActivityContext;
import com.aspectran.core.service.CoreService;
import com.aspectran.undertow.server.resource.StaticResourceHandler;
import com.aspectran.web.service.DefaultWebService;
import com.aspectran.web.service.WebService;
import com.aspectran.web.socket.jsr356.ServerEndpointExporter;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.ResponseCodeHandler;
import io.undertow.servlet.api.DeploymentManager;

import javax.servlet.ServletContext;
import javax.websocket.server.ServerContainer;
import java.util.Arrays;
import java.util.List;

import static com.aspectran.web.service.WebService.ROOT_WEB_SERVICE_ATTR_NAME;

/**
 * 

Created: 2019-08-04

*/ public class HybridServletHandlerFactory implements ActivityContextAware { private ActivityContext context; private TowServletContainer towServletContainer; private StaticResourceHandler staticResourceHandler; private List outerHandlerChainWrappers; @Override public void setActivityContext(ActivityContext context) { this.context = context; } public TowServletContainer getTowServletContainer() { return towServletContainer; } public void setTowServletContainer(TowServletContainer towServletContainer) { this.towServletContainer = towServletContainer; } public StaticResourceHandler getStaticResourceHandler() { return staticResourceHandler; } public void setStaticResourceHandler(StaticResourceHandler staticResourceHandler) { this.staticResourceHandler = staticResourceHandler; } public void setOuterHandlerChainWrappers(HandlerWrapper[] wrappers) { if (wrappers != null && wrappers.length > 0) { this.outerHandlerChainWrappers = Arrays.asList(wrappers); } else { this.outerHandlerChainWrappers = null; } } public HttpHandler createHandler() throws Exception { HttpHandler rootHandler; if (towServletContainer != null && towServletContainer.getDeploymentManagers() != null) { PathHandler pathHandler = new PathHandler(); for (DeploymentManager manager : towServletContainer.getDeploymentManagers()) { manager.deploy(); ServletContext servletContext = manager.getDeployment().getServletContext(); Object attr = servletContext.getAttribute(TowServletContext.DERIVED_WEB_SERVICE_ATTRIBUTE); servletContext.removeAttribute(TowServletContext.DERIVED_WEB_SERVICE_ATTRIBUTE); if ("true".equals(attr)) { CoreService rootService = context.getRootService(); WebService webService = DefaultWebService.create(servletContext, rootService); servletContext.setAttribute(ROOT_WEB_SERVICE_ATTR_NAME, webService); } // Required for any websocket support in undertow ServerContainer serverContainer = (ServerContainer)servletContext.getAttribute(ServerContainer.class.getName()); if (serverContainer != null) { ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter(context); serverEndpointExporter.initServletContext(servletContext); serverEndpointExporter.registerEndpoints(); } HttpHandler handler = manager.start(); String contextPath = manager.getDeployment().getDeploymentInfo().getContextPath(); pathHandler.addPrefixPath(contextPath, handler); } rootHandler = pathHandler; } else { rootHandler = ResponseCodeHandler.HANDLE_404; } if (staticResourceHandler != null && staticResourceHandler.hasPatterns()) { rootHandler = new HybridServletHandler(rootHandler, staticResourceHandler); } if (outerHandlerChainWrappers != null) { rootHandler = wrapHandlers(rootHandler, outerHandlerChainWrappers); } return rootHandler; } private static HttpHandler wrapHandlers(HttpHandler wrapee, List wrappers) { HttpHandler current = wrapee; for (HandlerWrapper wrapper : wrappers) { current = wrapper.wrap(current); } return current; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy