View Javadoc

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.session;
21  
22  import java.nio.ByteBuffer;
23  
24  import org.apache.mina.api.IoFuture;
25  import org.apache.mina.util.ByteBufferDumper;
26  
27  /**
28   * Default implementation for write requests.
29   * 
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public class DefaultWriteRequest implements WriteRequest {
33      /** The stored message */
34      private Object message;
35  
36      /** The original message (before being processed by the filter chain */
37      private Object originalMessage;
38  
39      /** the future to complete when this message is written */
40      private IoFuture<Void> future;
41  
42      /**
43       * Creates a new instance of a WriteRequest, storing the message as it was
44       * when the IoSession.write() has been called.
45       * 
46       * @param message The original message
47       */
48      public DefaultWriteRequest(Object originalMessage) {
49          this.message = originalMessage;
50          this.originalMessage = originalMessage;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public Object getMessage() {
58          return message;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void setMessage(Object message) {
66          this.message = message;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public IoFuture<Void> getFuture() {
74          return future;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void setFuture(final IoFuture<Void> future) {
82          this.future = future;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public Object getOriginalMessage() {
90          return originalMessage;
91      }
92  
93      /**
94       * @see Object#toString()
95       */
96      @Override
97      public String toString() {
98          final StringBuilder sb = new StringBuilder();
99  
100         sb.append("WriteRequest[");
101 
102         if (future != null) {
103             sb.append("Future : ");
104             sb.append(future);
105             sb.append(",");
106         } else {
107             sb.append("No future, ");
108         }
109 
110         if (originalMessage != null) {
111             // Dump the original message
112             sb.append("Original message : '");
113 
114             if (originalMessage instanceof ByteBuffer) {
115                 sb.append(ByteBufferDumper.dump((ByteBuffer) originalMessage));
116             } else {
117                 sb.append(originalMessage);
118             }
119 
120             sb.append("', ");
121         } else {
122             sb.append("No Orginal message,");
123         }
124 
125         if (message != null) {
126             // Dump the encoded message
127             // Just dump the first 16 bytes
128             sb.append("Encoded message : '");
129 
130             if (message instanceof ByteBuffer) {
131                 sb.append(ByteBufferDumper.dump((ByteBuffer) message, 16, false));
132             } else {
133                 sb.append(message);
134             }
135 
136             sb.append("'");
137         } else {
138             sb.append("No encoded message,");
139         }
140 
141         sb.append("]");
142 
143         return sb.toString();
144     }
145 }