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.binary;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.handler.codec.memcache.AbstractMemcacheObjectAggregator;
20  import io.netty.handler.codec.memcache.FullMemcacheMessage;
21  import io.netty.handler.codec.memcache.MemcacheContent;
22  import io.netty.handler.codec.memcache.MemcacheObject;
23  
24  /**
25   * An object aggregator for the memcache binary protocol.
26   *
27   * It aggregates {@link BinaryMemcacheMessage}s and {@link MemcacheContent} into {@link FullBinaryMemcacheRequest}s
28   * or {@link FullBinaryMemcacheResponse}s.
29   */
30  public class BinaryMemcacheObjectAggregator extends AbstractMemcacheObjectAggregator<BinaryMemcacheMessage> {
31  
32      public BinaryMemcacheObjectAggregator(int maxContentLength) {
33          super(maxContentLength);
34      }
35  
36      @Override
37      protected boolean isStartMessage(MemcacheObject msg) throws Exception {
38          return msg instanceof BinaryMemcacheMessage;
39      }
40  
41      @Override
42      protected FullMemcacheMessage beginAggregation(BinaryMemcacheMessage start, ByteBuf content) throws Exception {
43          if (start instanceof BinaryMemcacheRequest) {
44              return toFullRequest((BinaryMemcacheRequest) start, content);
45          }
46  
47          if (start instanceof BinaryMemcacheResponse) {
48              return toFullResponse((BinaryMemcacheResponse) start, content);
49          }
50  
51          // Should not reach here.
52          throw new Error();
53      }
54  
55      private static FullBinaryMemcacheRequest toFullRequest(BinaryMemcacheRequest request, ByteBuf content) {
56          FullBinaryMemcacheRequest fullRequest =
57                  new DefaultFullBinaryMemcacheRequest(request.key(), request.extras(), content);
58  
59          fullRequest.setMagic(request.magic());
60          fullRequest.setOpcode(request.opcode());
61          fullRequest.setKeyLength(request.keyLength());
62          fullRequest.setExtrasLength(request.extrasLength());
63          fullRequest.setDataType(request.dataType());
64          fullRequest.setTotalBodyLength(request.totalBodyLength());
65          fullRequest.setOpaque(request.opaque());
66          fullRequest.setCas(request.cas());
67          fullRequest.setReserved(request.reserved());
68  
69          return fullRequest;
70      }
71  
72      private static FullBinaryMemcacheResponse toFullResponse(BinaryMemcacheResponse response, ByteBuf content) {
73          FullBinaryMemcacheResponse fullResponse =
74                  new DefaultFullBinaryMemcacheResponse(response.key(), response.extras(), content);
75  
76          fullResponse.setMagic(response.magic());
77          fullResponse.setOpcode(response.opcode());
78          fullResponse.setKeyLength(response.keyLength());
79          fullResponse.setExtrasLength(response.extrasLength());
80          fullResponse.setDataType(response.dataType());
81          fullResponse.setTotalBodyLength(response.totalBodyLength());
82          fullResponse.setOpaque(response.opaque());
83          fullResponse.setCas(response.cas());
84          fullResponse.setStatus(response.status());
85  
86          return fullResponse;
87      }
88  }