Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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 io.undertow.server.session;
 
 import java.util.Map;

Session config that stores the session ID in the current SSL session.

It allows for a fallback to be provided for non-ssl connections

Author(s):
Stuart Douglas
 
 public class SslSessionConfig implements SessionConfig {
 
     private final SessionConfig fallbackSessionConfig;
     private final Map<KeyStringsessions = new HashMap<KeyString>();
     private final Map<StringKeyreverse = new HashMap<StringKey>();
 
     public SslSessionConfig(final SessionConfig fallbackSessionConfigSessionManager sessionManager) {
         this. = fallbackSessionConfig;
         sessionManager.registerSessionListener(new SessionListener() {
             @Override
             public void sessionCreated(Session sessionHttpServerExchange exchange) {
             }
 
             @Override
             public void sessionDestroyed(Session sessionHttpServerExchange exchangeSessionDestroyedReason reason) {
                 synchronized (SslSessionConfig.this) {
                     Key sid = .remove(session.getId());
                     if (sid != null) {
                         .remove(sid);
                     }
                 }
             }
 
             @Override
             public void attributeAdded(Session sessionString nameObject value) {
             }
 
             @Override
             public void attributeUpdated(Session sessionString nameObject newValueObject oldValue) {
             }
 
             @Override
             public void attributeRemoved(Session sessionString nameObject oldValue) {
             }
 
             @Override
             public void sessionIdChanged(Session sessionString oldSessionId) {
                 synchronized (SslSessionConfig.this) {
                     Key sid = .remove(session.getId());
                     if (sid != null) {
                         .remove(sid);
                     }
                 }
             }
         });
     }
 
     public SslSessionConfig(SessionManager sessionManager) {
         this(nullsessionManager);
     }
 
     @Override
     public void setSessionId(final HttpServerExchange exchangefinal String sessionId) {
         SSLSessionInfo sslSession = exchange.getConnection().getSslSessionInfo();
         if (sslSession == null) {
             if ( != null) {
                 .setSessionId(exchangesessionId);
             }
         } else {
             Key key = new Key(sslSession.getSessionId());
             synchronized (this) {
                 .put(keysessionId);
                 .put(sessionIdkey);
             }
         }
     }
    @Override
    public void clearSession(final HttpServerExchange exchangefinal String sessionId) {
        SSLSessionInfo sslSession = exchange.getConnection().getSslSessionInfo();
        if (sslSession == null) {
            if ( != null) {
                .clearSession(exchangesessionId);
            }
        } else {
            synchronized (this) {
                Key sid = .remove(sessionId);
                if (sid != null) {
                    .remove(sid);
                }
            }
        }
    }
    @Override
    public String findSessionId(final HttpServerExchange exchange) {
        SSLSessionInfo sslSession = exchange.getConnection().getSslSessionInfo();
        if (sslSession == null) {
            if ( != null) {
                return .findSessionId(exchange);
            }
        } else {
            synchronized (this) {
                return .get(new Key(sslSession.getSessionId()));
            }
        }
        return null;
    }
    @Override
        return findSessionId(exchange) != null ? . : .;
    }
    @Override
    public String rewriteUrl(final String originalUrlfinal String sessionId) {
        return originalUrl;
    }
    private static final class Key {
        private final byte[] id;
        private Key(byte[] id) {
            this. = id;
        }
        @Override
        public boolean equals(Object o) {
            if (this == oreturn true;
            if (o == null || getClass() != o.getClass()) return false;
            Key key = (Keyo;
            if (!Arrays.equals(key.id)) return false;
            return true;
        }
        @Override
        public int hashCode() {
            return  != null ? Arrays.hashCode() : 0;
        }
    }
New to GrepCode? Check out our FAQ X