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 #ifndef PEEKPROCESSOR_H
21 #define PEEKPROCESSOR_H
22 
23 #include <string>
24 #include <thrift/TProcessor.h>
25 #include <thrift/transport/TTransport.h>
26 #include <thrift/transport/TTransportUtils.h>
27 #include <thrift/transport/TBufferTransports.h>
28 #include <memory>
29 
30 namespace apache {
31 namespace thrift {
32 namespace processor {
33 
34 /*
35  * Class for peeking at the raw data that is being processed by another processor
36  * and gives the derived class a chance to change behavior accordingly
37  *
38  */
39 class PeekProcessor : public apache::thrift::TProcessor {
40 
41 public:
42   PeekProcessor();
43   ~PeekProcessor() override;
44 
45   // Input here: actualProcessor  - the underlying processor
46   //             protocolFactory  - the protocol factory used to wrap the memory buffer
47   //             transportFactory - this TPipedTransportFactory is used to wrap the source transport
48   //                                via a call to getPipedTransport
49   void initialize(
50       std::shared_ptr<apache::thrift::TProcessor> actualProcessor,
51       std::shared_ptr<apache::thrift::protocol::TProtocolFactory> protocolFactory,
52       std::shared_ptr<apache::thrift::transport::TPipedTransportFactory> transportFactory);
53 
54   std::shared_ptr<apache::thrift::transport::TTransport> getPipedTransport(
55       std::shared_ptr<apache::thrift::transport::TTransport> in);
56 
57   void setTargetTransport(std::shared_ptr<apache::thrift::transport::TTransport> targetTransport);
58 
59   bool process(std::shared_ptr<apache::thrift::protocol::TProtocol> in,
60                        std::shared_ptr<apache::thrift::protocol::TProtocol> out,
61                        void* connectionContext) override;
62 
63   // The following three functions can be overloaded by child classes to
64   // achieve desired peeking behavior
65   virtual void peekName(const std::string& fname);
66   virtual void peekBuffer(uint8_t* buffer, uint32_t size);
67   virtual void peek(std::shared_ptr<apache::thrift::protocol::TProtocol> in,
68                     apache::thrift::protocol::TType ftype,
69                     int16_t fid);
70   virtual void peekEnd();
71 
72 private:
73   std::shared_ptr<apache::thrift::TProcessor> actualProcessor_;
74   std::shared_ptr<apache::thrift::protocol::TProtocol> pipedProtocol_;
75   std::shared_ptr<apache::thrift::transport::TPipedTransportFactory> transportFactory_;
76   std::shared_ptr<apache::thrift::transport::TMemoryBuffer> memoryBuffer_;
77   std::shared_ptr<apache::thrift::transport::TTransport> targetTransport_;
78 };
79 }
80 }
81 } // apache::thrift::processor
82 
83 #endif
84