Repository: mina Updated Branches: refs/heads/2.0 b0ba8a09e -> 05a5c2f2e
http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/logging/MdcInjectionFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/logging/MdcInjectionFilter.java b/mina-core/src/main/java/org/apache/mina/filter/logging/MdcInjectionFilter.java index 6378feb..a5141bf 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/logging/MdcInjectionFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/logging/MdcInjectionFilter.java @@ -71,9 +71,30 @@ import org.slf4j.MDC; */ public class MdcInjectionFilter extends CommonEventFilter { - + /** + * This enum lists all the possible keys this filter will process + */ public enum MdcKey { - handlerClass, remoteAddress, localAddress, remoteIp, remotePort, localIp, localPort + /** Tha class handling the requests */ + handlerClass, + + /** The remote peer address */ + remoteAddress, + + /** The local address */ + localAddress, + + /** The remote peer IP address */ + remoteIp, + + /** The remote peer port */ + remotePort, + + /** The local IP address */ + localIp, + + /** The local port */ + localPort } /** key used for storing the context map in the IoSession */ @@ -107,14 +128,20 @@ public class MdcInjectionFilter extends CommonEventFilter { * @see #setProperty(org.apache.mina.core.session.IoSession, String, String) */ public MdcInjectionFilter(MdcKey... keys) { - Set<MdcKey> keySet = new HashSet<MdcKey>(Arrays.asList(keys)); + Set<MdcKey> keySet = new HashSet<>(Arrays.asList(keys)); this.mdcKeys = EnumSet.copyOf(keySet); } + /** + * Create a new MdcInjectionFilter instance + */ public MdcInjectionFilter() { this.mdcKeys = EnumSet.allOf(MdcKey.class); } + /** + * {@inheritDoc} + */ @Override protected void filter(IoFilterEvent event) throws Exception { // since this method can potentially call into itself @@ -157,8 +184,9 @@ public class MdcInjectionFilter extends CommonEventFilter { @SuppressWarnings("unchecked") private static Map<String, String> getContext(final IoSession session) { Map<String, String> context = (Map<String, String>) session.getAttribute(CONTEXT_KEY); + if (context == null) { - context = new ConcurrentHashMap<String, String>(); + context = new ConcurrentHashMap<>(); session.setAttribute(CONTEXT_KEY, context); } return context; @@ -174,12 +202,15 @@ public class MdcInjectionFilter extends CommonEventFilter { if (mdcKeys.contains(MdcKey.handlerClass)) { context.put(MdcKey.handlerClass.name(), session.getHandler().getClass().getName()); } + if (mdcKeys.contains(MdcKey.remoteAddress)) { context.put(MdcKey.remoteAddress.name(), session.getRemoteAddress().toString()); } + if (mdcKeys.contains(MdcKey.localAddress)) { context.put(MdcKey.localAddress.name(), session.getLocalAddress().toString()); } + if (session.getTransportMetadata().getAddressType() == InetSocketAddress.class) { InetSocketAddress remoteAddress = (InetSocketAddress) session.getRemoteAddress(); InetSocketAddress localAddress = (InetSocketAddress) session.getLocalAddress(); @@ -187,18 +218,28 @@ public class MdcInjectionFilter extends CommonEventFilter { if (mdcKeys.contains(MdcKey.remoteIp)) { context.put(MdcKey.remoteIp.name(), remoteAddress.getAddress().getHostAddress()); } + if (mdcKeys.contains(MdcKey.remotePort)) { context.put(MdcKey.remotePort.name(), String.valueOf(remoteAddress.getPort())); } + if (mdcKeys.contains(MdcKey.localIp)) { context.put(MdcKey.localIp.name(), localAddress.getAddress().getHostAddress()); } + if (mdcKeys.contains(MdcKey.localPort)) { context.put(MdcKey.localPort.name(), String.valueOf(localAddress.getPort())); } } } + /** + * Get the property associated with a given key + * + * @param session The {@IoSession} + * @param key The key we are looking at + * @return The associated property + */ public static String getProperty(IoSession session, String key) { if (key == null) { throw new IllegalArgumentException("key should not be null"); @@ -206,6 +247,7 @@ public class MdcInjectionFilter extends CommonEventFilter { Map<String, String> context = getContext(session); String answer = context.get(key); + if (answer != null) { return answer; } @@ -224,18 +266,27 @@ public class MdcInjectionFilter extends CommonEventFilter { if (key == null) { throw new IllegalArgumentException("key should not be null"); } + if (value == null) { removeProperty(session, key); } + Map<String, String> context = getContext(session); context.put(key, value); MDC.put(key, value); } + /** + * Remove a property from the context for the given session + * This property will be removed from the MDC for all subsequent events + * @param session The session for which you want to remove a property + * @param key The name of the property (should not be null) + */ public static void removeProperty(IoSession session, String key) { if (key == null) { throw new IllegalArgumentException("key should not be null"); } + Map<String, String> context = getContext(session); context.remove(key); MDC.remove(key); http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/stream/AbstractStreamWriteFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/stream/AbstractStreamWriteFilter.java b/mina-core/src/main/java/org/apache/mina/filter/stream/AbstractStreamWriteFilter.java index 0d15640..80a0887 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/stream/AbstractStreamWriteFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/stream/AbstractStreamWriteFilter.java @@ -32,7 +32,10 @@ import org.apache.mina.core.write.DefaultWriteRequest; import org.apache.mina.core.write.WriteRequest; /** - * TODO Add documentation + * Filter implementation which makes it possible to write Stream + * objects directly using {@link IoSession#write(Object)}. + * + * @param <T> The type of Stream * * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ @@ -45,32 +48,42 @@ public abstract class AbstractStreamWriteFilter<T> extends IoFilterAdapter { /** * The attribute name used when binding the streaming object to the session. */ - protected final AttributeKey CURRENT_STREAM = new AttributeKey(getClass(), "stream"); + protected static final AttributeKey CURRENT_STREAM = new AttributeKey(AbstractStreamWriteFilter.class, "stream"); - protected final AttributeKey WRITE_REQUEST_QUEUE = new AttributeKey(getClass(), "queue"); + protected static final AttributeKey WRITE_REQUEST_QUEUE = new AttributeKey(AbstractStreamWriteFilter.class, "queue"); - protected final AttributeKey CURRENT_WRITE_REQUEST = new AttributeKey(getClass(), "writeRequest"); + protected static final AttributeKey CURRENT_WRITE_REQUEST = new AttributeKey(AbstractStreamWriteFilter.class, "writeRequest"); private int writeBufferSize = DEFAULT_STREAM_BUFFER_SIZE; + /** + * {@inheritDoc} + */ @Override public void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { Class<? extends IoFilterAdapter> clazz = getClass(); + if (parent.contains(clazz)) { throw new IllegalStateException("Only one " + clazz.getName() + " is permitted."); } } + /** + * {@inheritDoc} + */ @Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { // If we're already processing a stream we need to queue the WriteRequest. if (session.getAttribute(CURRENT_STREAM) != null) { Queue<WriteRequest> queue = getWriteRequestQueue(session); + if (queue == null) { - queue = new ConcurrentLinkedQueue<WriteRequest>(); + queue = new ConcurrentLinkedQueue<>(); session.setAttribute(WRITE_REQUEST_QUEUE, queue); } + queue.add(writeRequest); + return; } @@ -81,6 +94,7 @@ public abstract class AbstractStreamWriteFilter<T> extends IoFilterAdapter { T stream = getMessageClass().cast(message); IoBuffer buffer = getNextBuffer(stream); + if (buffer == null) { // End of stream reached. writeRequest.getFuture().setWritten(); @@ -97,7 +111,7 @@ public abstract class AbstractStreamWriteFilter<T> extends IoFilterAdapter { } } - abstract protected Class<T> getMessageClass(); + protected abstract Class<T> getMessageClass(); @SuppressWarnings("unchecked") private Queue<WriteRequest> getWriteRequestQueue(IoSession session) { @@ -109,6 +123,9 @@ public abstract class AbstractStreamWriteFilter<T> extends IoFilterAdapter { return (Queue<WriteRequest>) session.removeAttribute(WRITE_REQUEST_QUEUE); } + /** + * {@inheritDoc} + */ @Override public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { T stream = getMessageClass().cast(session.getAttribute(CURRENT_STREAM)); @@ -125,8 +142,10 @@ public abstract class AbstractStreamWriteFilter<T> extends IoFilterAdapter { // Write queued WriteRequests. Queue<WriteRequest> queue = removeWriteRequestQueue(session); + if (queue != null) { WriteRequest wr = queue.poll(); + while (wr != null) { filterWrite(nextFilter, session, wr); wr = queue.poll(); @@ -160,8 +179,9 @@ public abstract class AbstractStreamWriteFilter<T> extends IoFilterAdapter { if (writeBufferSize < 1) { throw new IllegalArgumentException("writeBufferSize must be at least 1"); } + this.writeBufferSize = writeBufferSize; } - abstract protected IoBuffer getNextBuffer(T message) throws IOException; + protected abstract IoBuffer getNextBuffer(T message) throws IOException; } http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/stream/FileRegionWriteFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/stream/FileRegionWriteFilter.java b/mina-core/src/main/java/org/apache/mina/filter/stream/FileRegionWriteFilter.java index 8bf0c96..b48e021 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/stream/FileRegionWriteFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/stream/FileRegionWriteFilter.java @@ -54,12 +54,17 @@ import org.apache.mina.core.session.IoSession; * @org.apache.xbean.XBean */ public class FileRegionWriteFilter extends AbstractStreamWriteFilter<FileRegion> { - + /** + * {@inheritDoc} + */ @Override protected Class<FileRegion> getMessageClass() { return FileRegion.class; } + /** + * {@inheritDoc} + */ @Override protected IoBuffer getNextBuffer(FileRegion fileRegion) throws IOException { // If there are no more bytes to read, return null @@ -77,7 +82,7 @@ public class FileRegionWriteFilter extends AbstractStreamWriteFilter<FileRegion> // return the buffer buffer.flip(); + return buffer; } - } http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/stream/StreamWriteFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/stream/StreamWriteFilter.java b/mina-core/src/main/java/org/apache/mina/filter/stream/StreamWriteFilter.java index 64140ab..953ce14 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/stream/StreamWriteFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/stream/StreamWriteFilter.java @@ -49,13 +49,16 @@ import org.apache.mina.core.session.IoSession; * @org.apache.xbean.XBean */ public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> { - + /** + * {@inheritDoc} + */ @Override protected IoBuffer getNextBuffer(InputStream is) throws IOException { byte[] bytes = new byte[getWriteBufferSize()]; int off = 0; int n = 0; + while (off < bytes.length && (n = is.read(bytes, off, bytes.length - off)) != -1) { off += n; } @@ -64,14 +67,14 @@ public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> { return null; } - IoBuffer buffer = IoBuffer.wrap(bytes, 0, off); - - return buffer; + return IoBuffer.wrap(bytes, 0, off); } + /** + * {@inheritDoc} + */ @Override protected Class<InputStream> getMessageClass() { return InputStream.class; } - } http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/util/CommonEventFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/util/CommonEventFilter.java b/mina-core/src/main/java/org/apache/mina/filter/util/CommonEventFilter.java index 1b88ceb..24ae5e3 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/util/CommonEventFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/util/CommonEventFilter.java @@ -33,53 +33,75 @@ import org.apache.mina.core.write.WriteRequest; * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ public abstract class CommonEventFilter extends IoFilterAdapter { - - public CommonEventFilter() { - // Do nothing - } - protected abstract void filter(IoFilterEvent event) throws Exception; + /** + * {@inheritDoc} + */ @Override public final void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CREATED, session, null)); } + /** + * {@inheritDoc} + */ @Override public final void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED, session, null)); } + /** + * {@inheritDoc} + */ @Override public final void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CLOSED, session, null)); } + /** + * {@inheritDoc} + */ @Override public final void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_IDLE, session, status)); } + /** + * {@inheritDoc} + */ @Override public final void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.EXCEPTION_CAUGHT, session, cause)); } + /** + * {@inheritDoc} + */ @Override public final void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_RECEIVED, session, message)); } + /** + * {@inheritDoc} + */ @Override public final void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_SENT, session, writeRequest)); } + /** + * {@inheritDoc} + */ @Override public final void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.WRITE, session, writeRequest)); } + /** + * {@inheritDoc} + */ @Override public final void filterClose(NextFilter nextFilter, IoSession session) throws Exception { filter(new IoFilterEvent(nextFilter, IoEventType.CLOSE, session, null)); http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/util/NoopFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/util/NoopFilter.java b/mina-core/src/main/java/org/apache/mina/filter/util/NoopFilter.java index 00477c2..ec052d6 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/util/NoopFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/util/NoopFilter.java @@ -32,10 +32,4 @@ import org.apache.mina.core.filterchain.IoFilterAdapter; * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ public class NoopFilter extends IoFilterAdapter { - /** - * Default Constructor. - */ - public NoopFilter() { - super(); - } } http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/util/ReferenceCountingFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/util/ReferenceCountingFilter.java b/mina-core/src/main/java/org/apache/mina/filter/util/ReferenceCountingFilter.java index 1aaf3c2..a504384 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/util/ReferenceCountingFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/util/ReferenceCountingFilter.java @@ -38,18 +38,19 @@ public class ReferenceCountingFilter extends IoFilterAdapter { private int count = 0; + /** + * Creates a new ReferenceCountingFilter instance + * + * @param filter the filter we are counting references on + */ public ReferenceCountingFilter(IoFilter filter) { this.filter = filter; } - public void init() throws Exception { - // no-op, will init on-demand in pre-add if count == 0 - } - - public void destroy() throws Exception { - //no-op, will destroy on-demand in post-remove if count == 0 - } - + /** + * {@inheritDoc} + */ + @Override public synchronized void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { if (0 == count) { filter.init(); @@ -60,6 +61,10 @@ public class ReferenceCountingFilter extends IoFilterAdapter { filter.onPreAdd(parent, name, nextFilter); } + /** + * {@inheritDoc} + */ + @Override public synchronized void onPostRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { filter.onPostRemove(parent, name, nextFilter); @@ -70,46 +75,90 @@ public class ReferenceCountingFilter extends IoFilterAdapter { } } + /** + * {@inheritDoc} + */ + @Override public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception { filter.exceptionCaught(nextFilter, session, cause); } + /** + * {@inheritDoc} + */ + @Override public void filterClose(NextFilter nextFilter, IoSession session) throws Exception { filter.filterClose(nextFilter, session); } + /** + * {@inheritDoc} + */ + @Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { filter.filterWrite(nextFilter, session, writeRequest); } + /** + * {@inheritDoc} + */ + @Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { filter.messageReceived(nextFilter, session, message); } + /** + * {@inheritDoc} + */ + @Override public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { filter.messageSent(nextFilter, session, writeRequest); } + /** + * {@inheritDoc} + */ + @Override public void onPostAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { filter.onPostAdd(parent, name, nextFilter); } + /** + * {@inheritDoc} + */ + @Override public void onPreRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { filter.onPreRemove(parent, name, nextFilter); } + /** + * {@inheritDoc} + */ + @Override public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception { filter.sessionClosed(nextFilter, session); } + /** + * {@inheritDoc} + */ + @Override public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception { filter.sessionCreated(nextFilter, session); } + /** + * {@inheritDoc} + */ + @Override public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception { filter.sessionIdle(nextFilter, session, status); } + /** + * {@inheritDoc} + */ + @Override public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception { filter.sessionOpened(nextFilter, session); } http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/util/SessionAttributeInitializingFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/util/SessionAttributeInitializingFilter.java b/mina-core/src/main/java/org/apache/mina/filter/util/SessionAttributeInitializingFilter.java index 4dc9087..d8e9fc6 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/util/SessionAttributeInitializingFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/util/SessionAttributeInitializingFilter.java @@ -38,7 +38,7 @@ import org.apache.mina.core.session.IoSession; * @org.apache.xbean.XBean */ public class SessionAttributeInitializingFilter extends IoFilterAdapter { - private final Map<String, Object> attributes = new ConcurrentHashMap<String, Object>(); + private final Map<String, Object> attributes = new ConcurrentHashMap<>(); /** * Creates a new instance with no default attributes. You can set @@ -130,12 +130,11 @@ public class SessionAttributeInitializingFilter extends IoFilterAdapter { * @param attributes The attributes Map to set */ public void setAttributes(Map<String, ? extends Object> attributes) { - if (attributes == null) { - attributes = new ConcurrentHashMap<String, Object>(); - } - this.attributes.clear(); - this.attributes.putAll(attributes); + + if (attributes != null) { + this.attributes.putAll(attributes); + } } /** http://git-wip-us.apache.org/repos/asf/mina/blob/a9c468fb/mina-core/src/main/java/org/apache/mina/filter/util/WriteRequestFilter.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/util/WriteRequestFilter.java b/mina-core/src/main/java/org/apache/mina/filter/util/WriteRequestFilter.java index 787751b..8e78197 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/util/WriteRequestFilter.java +++ b/mina-core/src/main/java/org/apache/mina/filter/util/WriteRequestFilter.java @@ -19,6 +19,8 @@ */ package org.apache.mina.filter.util; +import java.lang.annotation.Inherited; + import org.apache.mina.core.filterchain.IoFilter; import org.apache.mina.core.filterchain.IoFilterAdapter; import org.apache.mina.core.session.IoEventType; @@ -34,9 +36,13 @@ import org.apache.mina.core.write.WriteRequestWrapper; * */ public abstract class WriteRequestFilter extends IoFilterAdapter { + /** + * {@inheritDoc} + */ @Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { Object filteredMessage = doFilterWrite(nextFilter, session, writeRequest); + if (filteredMessage != null && filteredMessage != writeRequest.getMessage()) { nextFilter.filterWrite(session, new FilteredWriteRequest(filteredMessage, writeRequest)); } else { @@ -44,10 +50,14 @@ public abstract class WriteRequestFilter extends IoFilterAdapter { } } + /** + * {@inheritDoc} + */ @Override public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { if (writeRequest instanceof FilteredWriteRequest) { FilteredWriteRequest req = (FilteredWriteRequest) writeRequest; + if (req.getParent() == this) { nextFilter.messageSent(session, req.getParentRequest()); return;