spring GlassFishRequestUpgradeStrategy 源码

  • 2022-08-08
  • 浏览 (439)

spring GlassFishRequestUpgradeStrategy 代码

文件路径:/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/GlassFishRequestUpgradeStrategy.java

/*
 * Copyright 2002-2018 the original author or authors.
 *
 * 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
 *
 *      https://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 org.springframework.web.socket.server.standard;

import java.io.IOException;
import java.lang.reflect.Constructor;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.glassfish.tyrus.core.TyrusUpgradeResponse;
import org.glassfish.tyrus.core.Utils;
import org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler;
import org.glassfish.tyrus.spi.WebSocketEngine.UpgradeInfo;
import org.glassfish.tyrus.spi.Writer;

import org.springframework.util.ReflectionUtils;
import org.springframework.web.socket.server.HandshakeFailureException;

/**
 * A WebSocket {@code RequestUpgradeStrategy} for Oracle's GlassFish 4.1 and higher.
 *
 * @author Rossen Stoyanchev
 * @author Juergen Hoeller
 * @author Michael Irwin
 * @since 4.0
 */
public class GlassFishRequestUpgradeStrategy extends AbstractTyrusRequestUpgradeStrategy {

	private static final Constructor<?> constructor;

	static {
		try {
			ClassLoader classLoader = GlassFishRequestUpgradeStrategy.class.getClassLoader();
			Class<?> type = classLoader.loadClass("org.glassfish.tyrus.servlet.TyrusServletWriter");
			constructor = type.getDeclaredConstructor(TyrusHttpUpgradeHandler.class);
			ReflectionUtils.makeAccessible(constructor);
		}
		catch (Exception ex) {
			throw new IllegalStateException("No compatible Tyrus version found", ex);
		}
	}


	@Override
	protected void handleSuccess(HttpServletRequest request, HttpServletResponse response,
			UpgradeInfo upgradeInfo, TyrusUpgradeResponse upgradeResponse) throws IOException, ServletException {

		TyrusHttpUpgradeHandler handler = request.upgrade(TyrusHttpUpgradeHandler.class);
		Writer servletWriter = newServletWriter(handler);
		handler.preInit(upgradeInfo, servletWriter, request.getUserPrincipal() != null);

		response.setStatus(upgradeResponse.getStatus());
		upgradeResponse.getHeaders().forEach((key, value) -> response.addHeader(key, Utils.getHeaderFromList(value)));
		response.flushBuffer();
	}

	private Writer newServletWriter(TyrusHttpUpgradeHandler handler) {
		try {
			return (Writer) constructor.newInstance(handler);
		}
		catch (Exception ex) {
			throw new HandshakeFailureException("Failed to instantiate TyrusServletWriter", ex);
		}
	}

}

相关信息

spring 源码目录

相关文章

spring AbstractStandardUpgradeStrategy 源码

spring AbstractTyrusRequestUpgradeStrategy 源码

spring ServerEndpointExporter 源码

spring ServerEndpointRegistration 源码

spring ServletServerContainerFactoryBean 源码

spring SpringConfigurator 源码

spring TomcatRequestUpgradeStrategy 源码

spring UndertowRequestUpgradeStrategy 源码

spring WebLogicRequestUpgradeStrategy 源码

spring WebSphereRequestUpgradeStrategy 源码

0  赞