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

org.apache.cxf.ws.security.tokenstore.MemoryTokenStore Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * 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.cxf.ws.security.tokenstore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.ws.security.tokenstore.SecurityToken.State;

/**
 * 
 */
public class MemoryTokenStore implements TokenStore {
    boolean autoRemove = true;
    
    Map tokens = new ConcurrentHashMap();
    
    /** {@inheritDoc}*/
    public void add(SecurityToken token) {
        if (token != null && !StringUtils.isEmpty(token.getId())) {
            tokens.put(token.getId(), token);
        }
    }

    /** {@inheritDoc}*/
    public void update(SecurityToken token) {
        if (autoRemove 
            && (token.getState() == State.EXPIRED
                || token.getState() == State.CANCELLED)) {
            remove(token);
        } else {
            add(token);
        }
    }
    public void remove(SecurityToken token) {
        if (token != null && !StringUtils.isEmpty(token.getId())) {
            tokens.remove(token.getId());
        }
    }

    public Collection getCancelledTokens() {
        return getTokens(SecurityToken.State.CANCELLED);
    }
    public Collection getExpiredTokens() {
        return getTokens(SecurityToken.State.EXPIRED);
    }
    public Collection getRenewedTokens() {
        return getTokens(SecurityToken.State.RENEWED);
    }
    public Collection getTokenIdentifiers() {
        processTokenExpiry();        
        return tokens.keySet();
    }

    public Collection getValidTokens() {
        Collection toks = getTokens(SecurityToken.State.ISSUED);
        toks.addAll(getTokens(SecurityToken.State.RENEWED));
        toks.addAll(getTokens(SecurityToken.State.UNKNOWN));
        return toks;
    }

    public SecurityToken getToken(String id) {
        processTokenExpiry();
        
        SecurityToken token = tokens.get(id);
        if (token == null) {
            for (SecurityToken t : tokens.values()) {
                if (id.equals(t.getWsuId())) {
                    return t;
                }
            }
        }
        return token;
    }
    
    public SecurityToken getTokenByAssociatedHash(int hashCode) {
        processTokenExpiry();
        
        for (String id : tokens.keySet()) {
            SecurityToken securityToken = tokens.get(id);
            if (hashCode == securityToken.getAssociatedHash()) {
                return securityToken;
            }
        }
        return null;
    }

    
    protected Collection getTokens(SecurityToken.State state) {
        processTokenExpiry();
        List t = new ArrayList();
        for (SecurityToken token : tokens.values()) {
            if (token.getState() == state) {
                t.add(token);
            }
        }
        return t;
    }

    protected void processTokenExpiry() {
        for (SecurityToken token : tokens.values()) {
            if (token.getState() == State.EXPIRED
                || token.getState() == State.CANCELLED) {
                if (autoRemove) {
                    remove(token);
                }
            } else if (token.getExpires() != null) {
                Date current = new Date();
                if (token.getExpires().before(current)) {
                    token.setState(SecurityToken.State.EXPIRED);
                    if (autoRemove) {
                        remove(token);
                    }
                }
            }            
        }
    }


    public void removeCancelledTokens() {
        for (SecurityToken token : tokens.values()) {
            if (token.getState() == State.CANCELLED) {
                remove(token);
            }
        }
    }

    public void removeExpiredTokens() {
        processTokenExpiry();
        for (SecurityToken token : tokens.values()) {
            if (token.getState() == State.EXPIRED) {
                remove(token);
            }
        }
    }

    public void setAutoRemoveTokens(boolean auto) {
        autoRemove = auto;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy