5d8b0b58363ad96b98e4b60625dec8c8ae28e1a1
[gssweb.git] / json_gssapi / src / GSSRequest.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  *
4  * For license details, see the LICENSE file in the root of this project.
5  *
6  */
7
8 #include <cstddef>
9 #include <stdexcept>
10
11 #include "commands/GSSAcquireCred.h"
12 #include "commands/GSSInitSecContext.h"
13 #include "commands/GSSImportName.h"
14 #include "commands/GSSDisplayName.h"
15 #include "GSSRequest.h"
16
17 using std::bad_alloc;
18
19 GSSRequest::GSSRequest ( string jsonString )
20 {
21   /* Local variables */
22   /* Error checking */
23   /* Setup */
24   /* Main processing */
25   response = JSONObject();
26   cmd = NULL;
27   requestString = jsonString;
28   
29   /* Cleanup */
30   /* Return */
31 }
32
33 void GSSRequest::execute()
34 {
35   /* variables */
36   /* Error checking */
37   /* Setup */
38   parseJSON();
39   getCommand();
40   
41   /* Main processing */
42   if (NULL != cmd)
43     cmd->execute();
44   
45   /* Cleanup */
46   /* Return */
47 }
48
49
50
51 void GSSRequest::parseJSON()
52 {
53   /* variables */
54   json_error_t jsonErr;
55   
56   try {
57     JSONObject cookies;
58     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
59     cookies = request.get("cookies");
60     response.set("cookies", cookies );
61     response.set("method", request.get("method").string());
62   }
63   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
64   catch ( bad_alloc& e ) 
65   {
66     // Top-level response
67     response.set("error_message", "Could not parse the input JSON.");
68     response.set("original_message", requestString.c_str());
69   }
70 }
71
72
73 void GSSRequest::getCommand()
74 {
75   string method;
76   JSONObject arguments = request.get("arguments");
77   
78   /* Error checking */
79   /* Setup */
80   if (request.get("method").isNull() )
81     method = "";
82   else
83     method = string( request.get("method").string() );
84   
85   if ( "gss_import_name" == method ) 
86   {
87     cmd = new GSSImportName ( &arguments );
88   } 
89   else if ( "gss_init_sec_context" == method ) 
90   {
91     cmd = new GSSInitSecContext ( &arguments );
92   } 
93   else if ( "gss_acquire_cred" == method ) 
94   {
95     cmd = new GSSAcquireCred ( &arguments );
96   } 
97   else if ( "gss_display_name" == method )
98   {
99     cmd = new GSSDisplayName ( &arguments );
100   }
101   else 
102   {
103     string error_message = string("Unrecognized command: ") + method;
104     response.set("error_message", error_message.c_str() );
105     response.set("original_message", requestString.c_str());
106     cmd = NULL;
107   }
108 }
109
110 string GSSRequest::getResponse()
111 {
112   /* Variables */
113   JSONObject *return_values;
114   string gssResponse;
115   
116   /* Main processing */
117   // Put the return values into the response, assuming that the command
118   // was actually executed.
119   if (NULL != cmd)
120   {
121     return_values = cmd->toJSON();
122     response.set("return_values", *return_values);
123   }
124   
125   // Convert the response into a string to return.
126   gssResponse = string( response.dump() );
127   
128   /* Return */
129   return(gssResponse);
130 }