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