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