atscppapi  1.0.9
C++ wrapper for Apache Traffic Server API
 All Classes Files Functions Enumerations Enumerator Macros
TransformationPlugin.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 LinkedIn Corp. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the license at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  *
7  * Unless required by applicable law or agreed to in writing, software distributed under the
8  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied.
10  *
11  */
12 
13 /**
14  * @file TransformationPlugin.h
15  * @author Brian Geffon
16  * @author Manjesh Nilange
17  */
18 
19 #pragma once
20 #ifndef ATSCPPAPI_TRANSFORMATIONPLUGIN_H_
21 #define ATSCPPAPI_TRANSFORMATIONPLUGIN_H_
22 
23 #include <string>
24 #include <atscppapi/Transaction.h>
26 
27 namespace atscppapi {
28 
29 class TransformationPluginState;
30 
31 /**
32  * @brief The interface used when you wish to transform Request or Response body content.
33  *
34  * Transformations are deceptively simple, transformations are chained so the output
35  * of one TransformationPlugin becomes the input of another TransformationPlugin. As
36  * data arrives it will fire a consume() and when all the data has been sent
37  * you will receive a handleInputComplete(). Data can be sent to the next TransformationPlugin
38  * in the chain by calling produce() and when the transformation has no data left to send
39  * it will fire a setOutputCompete().
40  *
41  * Since a TransformationPlugin is a type of TransactionPlugin you can call registerHook() and
42  * establish any hook for a Transaction also; however, remember that you must implement
43  * the appropriate callback for any hooks you register.
44  *
45  * A simple example of how to use the TransformationPlugin interface follows, this is an example
46  * of a Response transformation, the avialable options are REQUEST_TRANSFORMATION and RESPONSE_TRANSFORMATION
47  * which are defined in Type.
48  *
49  * This example is a Null Transformation, meaning it will just spit out the content it receives without
50  * actually doing any work on it.
51  *
52  * \code
53  * class NullTransformationPlugin : public TransformationPlugin {
54  * public:
55  * NullTransformationPlugin(Transaction &transaction)
56  * : TransformationPlugin(transaction, RESPONSE_TRANSFORMATION) {
57  * registerHook(HOOK_SEND_RESPONSE_HEADERS);
58  * }
59  * void handleSendResponseHeaders(Transaction &transaction) {
60  * transaction.getClientResponse().getHeaders().set("X-Content-Transformed", "1");
61  * transaction.resume();
62  * }
63  * void consume(const string &data) {
64  * produce(data);
65  * }
66  * void handleInputComplete() {
67  * setOutputComplete();
68  * }
69  * };
70  * \endcode
71  *
72  * @see Plugin
73  * @see TransactionPlugin
74  * @see Type
75  * @see HookType
76  */
78 public:
79  /**
80  * The available types of Transformations.
81  */
82  enum Type {
83  REQUEST_TRANSFORMATION = 0, /**< Transform the Request body content */
84  RESPONSE_TRANSFORMATION /**< Transform the Response body content */
85  };
86 
87  /**
88  * A method that you must implement when writing a TransformationPlugin, this method will be
89  * fired whenever an upstream TransformationPlugin has produced output.
90  */
91  virtual void consume(const std::string &data) = 0;
92 
93  /**
94  * A method that you must implement when writing a TransformationPlugin, this method
95  * will be fired whenever the upstream TransformationPlugin has completed writing data.
96  */
97  virtual void handleInputComplete() = 0;
98 
99  virtual ~TransformationPlugin(); /**< Destructor for a TransformationPlugin */
100 protected:
101 
102  /**
103  * This method is how a TransformationPlugin will produce output for the downstream
104  * transformation plugin, if you need to produce binary data this can still be
105  * done with strings by a call to string::assign() or by constructing a string
106  * with string::string(char *, size_t).
107  */
108  size_t produce(const std::string &);
109 
110  /**
111  * This is the method that you must call when you're done producing output for
112  * the downstream TranformationPlugin.
113  */
114  size_t setOutputComplete();
115 
116  /** a TransformationPlugin must implement this interface, it cannot be constructed directly */
117  TransformationPlugin(Transaction &transaction, Type type);
118 private:
119  TransformationPluginState *state_; /** Internal state for a TransformationPlugin */
120 };
121 
122 } /* atscppapi */
123 
124 
125 #endif /* ATSCPPAPI_TRANSFORMATIONPLUGIN_H_ */