/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 org.apache.catalina.valves; import java.io.IOException; import javax.servlet.ServletException; import org.apache.catalina.Manager; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.session.ManagerBase; public class RewriteJvmRouteValve extends ValveBase { public RewriteJvmRouteValve() { } @Override public void invoke(Request request, Response response) throws IOException,ServletException { String localRoute = getLocalJvmRoute(request); String id = request.getRequestedSessionId(); if (localRoute!=null && id!=null) { rewriteJvmRoute(request,response,id,localRoute); } getNext().invoke(request, response); } /** * Copied from JvmRouteBinderValve */ protected Manager getManager(Request request) { Manager manager = request.getContext().getManager(); return manager; } /** * Copied from JvmRouteBinderValve * get jvmroute from engine * * @param request current request * @return return jvmRoute from ManagerBase or null */ protected String getLocalJvmRoute(Request request) { Manager manager = getManager(request); if(manager!=null && manager instanceof ManagerBase) return ((ManagerBase) manager).getJvmRoute(); else return null ; } protected void rewriteJvmRoute(Request request, Response response, String sessionId, String localRoute) { // get requested jvmRoute. String requestedJvmRoute = null; int index = sessionId.indexOf("."); if (index > 0) requestedJvmRoute = sessionId.substring(index + 1, sessionId.length()); if (requestedJvmRoute != null && !requestedJvmRoute.equals(localRoute)) { String id = sessionId.substring(0, index); String newSessionID = id + "." + localRoute; request.setRequestedSessionId(newSessionID); } } }