1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.mina.filter.util; 21 22 import java.util.Map; 23 import java.util.Set; 24 import java.util.concurrent.ConcurrentHashMap; 25 26 import org.apache.mina.core.filterchain.IoFilter; 27 import org.apache.mina.core.filterchain.IoFilterAdapter; 28 import org.apache.mina.core.session.IoSession; 29 30 /** 31 * An {@link IoFilter} that sets initial attributes when a new 32 * {@link IoSession} is created. By default, the attribute map is empty when 33 * an {@link IoSession} is newly created. Inserting this filter will make 34 * the pre-configured attributes available after this filter executes the 35 * <tt>sessionCreated</tt> event. 36 * 37 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 38 * @org.apache.xbean.XBean 39 */ 40 public class SessionAttributeInitializingFilter extends IoFilterAdapter { 41 private final Map<String, Object> attributes = new ConcurrentHashMap<>(); 42 43 /** 44 * Creates a new instance with no default attributes. You can set 45 * the additional attributes by calling methods such as 46 * {@link #setAttribute(String, Object)} and {@link #setAttributes(Map)}. 47 */ 48 public SessionAttributeInitializingFilter() { 49 // Do nothing 50 } 51 52 /** 53 * Creates a new instance with the specified default attributes. You can 54 * set the additional attributes by calling methods such as 55 * {@link #setAttribute(String, Object)} and {@link #setAttributes(Map)}. 56 * 57 * @param attributes The Attribute's Map to set 58 */ 59 public SessionAttributeInitializingFilter(Map<String, ? extends Object> attributes) { 60 setAttributes(attributes); 61 } 62 63 /** 64 * Returns the value of user-defined attribute. 65 * 66 * @param key the key of the attribute 67 * @return <tt>null</tt> if there is no attribute with the specified key 68 */ 69 public Object getAttribute(String key) { 70 return attributes.get(key); 71 } 72 73 /** 74 * Sets a user-defined attribute. 75 * 76 * @param key the key of the attribute 77 * @param value the value of the attribute 78 * @return The old value of the attribute. <tt>null</tt> if it is new. 79 */ 80 public Object setAttribute(String key, Object value) { 81 if (value == null) { 82 return removeAttribute(key); 83 } 84 85 return attributes.put(key, value); 86 } 87 88 /** 89 * Sets a user defined attribute without a value. This is useful when 90 * you just want to put a 'mark' attribute. Its value is set to 91 * {@link Boolean#TRUE}. 92 * 93 * @param key the key of the attribute 94 * @return The old value of the attribute. <tt>null</tt> if it is new. 95 */ 96 public Object setAttribute(String key) { 97 return attributes.put(key, Boolean.TRUE); 98 } 99 100 /** 101 * Removes a user-defined attribute with the specified key. 102 * 103 * @param key The attribut's key we want to removee 104 * @return The old value of the attribute. <tt>null</tt> if not found. 105 */ 106 public Object removeAttribute(String key) { 107 return attributes.remove(key); 108 } 109 110 /** 111 * @return <tt>true</tt> if this session contains the attribute with 112 * the specified <tt>key</tt>. 113 */ 114 boolean containsAttribute(String key) { 115 return attributes.containsKey(key); 116 } 117 118 /** 119 * @return the set of keys of all user-defined attributes. 120 */ 121 public Set<String> getAttributeKeys() { 122 return attributes.keySet(); 123 } 124 125 /** 126 * Sets the attribute map. The specified attributes are copied into the 127 * underlying map, so modifying the specified attributes parameter after 128 * the call won't change the internal state. 129 * 130 * @param attributes The attributes Map to set 131 */ 132 public void setAttributes(Map<String, ? extends Object> attributes) { 133 this.attributes.clear(); 134 135 if (attributes != null) { 136 this.attributes.putAll(attributes); 137 } 138 } 139 140 /** 141 * Puts all pre-configured attributes into the actual session attribute 142 * map and forward the event to the next filter. 143 */ 144 @Override 145 public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception { 146 for (Map.Entry<String, Object> e : attributes.entrySet()) { 147 session.setAttribute(e.getKey(), e.getValue()); 148 } 149 150 nextFilter.sessionCreated(session); 151 } 152 }