624d983499af15016fe3c79c7cbe25ddb25b7a43
[shibboleth/cpp-sp.git] / shibsp / handler / Handler.h
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file shibsp/handler/Handler.h
19  * 
20  * Pluggable runtime functionality that implement protocols and services.
21  */
22
23 #ifndef __shibsp_handler_h__
24 #define __shibsp_handler_h__
25
26 #include <shibsp/SPRequest.h>
27 #include <shibsp/util/PropertySet.h>
28
29 #ifndef SHIBSP_LITE
30 namespace opensaml {
31     namespace saml2md {
32         class SAML_API SPSSODescriptor;
33     };
34 };
35 #endif
36
37 namespace xmltooling {
38     class XMLTOOL_API HTTPRequest;
39     class XMLTOOL_API HTTPResponse;
40 };
41
42 namespace shibsp {
43
44     /**
45      * Pluggable runtime functionality that implement protocols and services
46      */
47     class SHIBSP_API Handler : public virtual PropertySet
48     {
49         MAKE_NONCOPYABLE(Handler);
50     protected:
51         Handler();
52
53         /**
54          * Log using handler's specific logging object.
55          *
56          * @param level logging level
57          * @param msg   message to log
58          */
59         virtual void log(SPRequest::SPLogLevel level, const std::string& msg) const;
60
61         /**
62          * Implements various mechanisms to preserve RelayState,
63          * such as cookies or StorageService-backed keys.
64          *
65          * <p>If a supported mechanism can be identified, the input parameter will be
66          * replaced with a suitable state key.
67          *
68          * @param application   the associated Application
69          * @param response      outgoing HTTP response
70          * @param relayState    RelayState token to supply with message
71          */
72         virtual void preserveRelayState(
73             const Application& application, xmltooling::HTTPResponse& response, std::string& relayState
74             ) const;
75
76         /**
77          * Implements various mechanisms to recover RelayState,
78          * such as cookies or StorageService-backed keys.
79          *
80          * <p>If a supported mechanism can be identified, the input parameter will be
81          * replaced with the recovered state information.
82          *
83          * @param application   the associated Application
84          * @param request       incoming HTTP request
85          * @param response      outgoing HTTP response
86          * @param relayState    RelayState token supplied with message
87          * @param clear         true iff the token state should be cleared
88          */
89         virtual void recoverRelayState(
90             const Application& application,
91             const xmltooling::HTTPRequest& request,
92             xmltooling::HTTPResponse& response,
93             std::string& relayState,
94             bool clear=true
95             ) const;
96
97     public:
98         virtual ~Handler();
99
100         /**
101          * Returns an identifier for the protocol family associated with the handler, if any.
102          *
103          * @return  a protocol identifier, or nullptr
104          */
105         virtual const XMLCh* getProtocolFamily() const;
106
107         /**
108          * Executes handler functionality as an incoming request.
109          * 
110          * <p>Handlers can be run either directly by incoming web requests
111          * or indirectly/implicitly during other SP processing.
112          * 
113          * @param request   SP request context
114          * @param isHandler true iff executing in the context of a direct handler invocation
115          * @return  a pair containing a "request completed" indicator and a server-specific response code
116          */
117         virtual std::pair<bool,long> run(SPRequest& request, bool isHandler=true) const=0;
118
119 #ifndef SHIBSP_LITE
120         /**
121          * Generates and/or modifies metadata reflecting the Handler.
122          *
123          * <p>The default implementation does nothing.
124          *
125          * @param role          metadata role to decorate
126          * @param handlerURL    base location of handler's endpoint
127          */
128         virtual void generateMetadata(opensaml::saml2md::SPSSODescriptor& role, const char* handlerURL) const {
129         }
130
131         /**
132          * Returns the "type" of the Handler plugin.
133          *
134          * @return  a Handler type
135          */
136         virtual const char* getType() const;
137 #endif
138     };
139     
140     /** Registers Handler implementations. */
141     void SHIBSP_API registerHandlers();
142
143     /** Handler for metadata generation. */
144     #define METADATA_GENERATOR_HANDLER "MetadataGenerator"
145
146     /** Handler for status information. */
147     #define STATUS_HANDLER "Status"
148
149     /** Handler for session diagnostic information. */
150     #define SESSION_HANDLER "Session"
151 };
152
153 #endif /* __shibsp_handler_h__ */