View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.memcache;
17  
18  import io.netty.channel.ChannelHandler;
19  import io.netty.channel.ChannelPipeline;
20  import io.netty.handler.codec.MessageAggregator;
21  import io.netty.handler.codec.memcache.binary.BinaryMemcacheRequestDecoder;
22  import io.netty.handler.codec.memcache.binary.BinaryMemcacheResponseEncoder;
23  
24  /**
25   * A {@link ChannelHandler} that aggregates an {@link MemcacheMessage}
26   * and its following {@link MemcacheContent}s into a single {@link MemcacheMessage} with
27   * no following {@link MemcacheContent}s.  It is useful when you don't want to take
28   * care of memcache messages where the content comes along in chunks. Insert this
29   * handler after a AbstractMemcacheObjectDecoder in the {@link ChannelPipeline}.
30   * <p/>
31   * For example, here for the binary protocol:
32   * <p/>
33   * <pre>
34   * {@link ChannelPipeline} p = ...;
35   * ...
36   * p.addLast("decoder", new {@link BinaryMemcacheRequestDecoder}());
37   * p.addLast("aggregator", <b>new {@link io.netty.handler.codec.memcache.binary.BinaryMemcacheObjectAggregator}(1048576)
38   * </b>);
39   * ...
40   * p.addLast("encoder", new {@link BinaryMemcacheResponseEncoder}());
41   * p.addLast("handler", new YourMemcacheRequestHandler());
42   * </pre>
43   */
44  public abstract class AbstractMemcacheObjectAggregator<H extends MemcacheMessage> extends
45          MessageAggregator<MemcacheObject, H, MemcacheContent, FullMemcacheMessage> {
46  
47      protected AbstractMemcacheObjectAggregator(int maxContentLength) {
48          super(maxContentLength);
49      }
50  
51      @Override
52      protected boolean isContentMessage(MemcacheObject msg) throws Exception {
53          return msg instanceof MemcacheContent;
54      }
55  
56      @Override
57      protected boolean isLastContentMessage(MemcacheContent msg) throws Exception {
58          return msg instanceof LastMemcacheContent;
59      }
60  
61      @Override
62      protected boolean isAggregated(MemcacheObject msg) throws Exception {
63          return msg instanceof FullMemcacheMessage;
64      }
65  
66      @Override
67      protected boolean hasContentLength(H start) throws Exception {
68          return false;
69      }
70  
71      @Override
72      protected long contentLength(H start) throws Exception {
73          throw new UnsupportedOperationException();
74      }
75  
76      @Override
77      protected Object newContinueResponse(H start) throws Exception {
78          return null;
79      }
80  }