A thought of new exception handling; add entrypoint for FireFox
[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 #include "GSSException.h"
17
18 using std::bad_alloc;
19
20 GSSRequest::GSSRequest ( string jsonString )
21 {
22   /* Local variables */
23   /* Error checking */
24   /* Setup */
25   /* Main processing */
26   response = JSONObject();
27   cmd = NULL;
28   requestString = jsonString;
29   
30   /* Cleanup */
31   /* Return */
32 }
33
34 void GSSRequest::execute()
35 {
36   try {
37     /* variables */
38     /* Error checking */
39     /* Setup */
40     parseJSON();
41     getCommand();
42     
43     /* Main processing */
44     if (NULL != cmd)
45       cmd->execute();
46     
47     /* Cleanup */
48     /* Return */
49   }
50   catch (GSSException e)
51   {
52     delete(cmd);
53     cmd = NULL;
54     JSONObject return_values;
55     return_values.set("major_status", e.getMajor());
56     return_values.set("minor_status", e.getMinor());
57 //     response.set("error_message", e.what());
58   }
59 }
60
61
62
63 void GSSRequest::parseJSON()
64 {
65   /* variables */
66   json_error_t jsonErr;
67   
68   try {
69     JSONObject cookies;
70     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
71     cookies = request.get("cookies");
72     response.set("cookies", cookies );
73     response.set("method", request.get("method").string());
74   }
75   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
76   catch ( bad_alloc& ) 
77   {
78     // Top-level response
79     response.set("error_message", "Could not parse the input JSON.");
80     response.set("original_message", requestString.c_str());
81   }
82 }
83
84
85 void GSSRequest::getCommand()
86 {
87   string method;
88   JSONObject arguments = request.get("arguments");
89   
90   /* Error checking */
91   /* Setup */
92   if (request.get("method").isNull() )
93     method = "";
94   else
95     method = string( request.get("method").string() );
96   
97   if ( "gss_import_name" == method ) 
98   {
99     cmd = new GSSImportName ( &arguments );
100   } 
101   else if ( "gss_init_sec_context" == method ) 
102   {
103     cmd = new GSSInitSecContext ( &arguments );
104   } 
105   else if ( "gss_acquire_cred" == method ) 
106   {
107     cmd = new GSSAcquireCred ( &arguments );
108   } 
109   else if ( "gss_display_name" == method )
110   {
111     cmd = new GSSDisplayName ( &arguments );
112   }
113   else 
114   {
115     string error_message = string("Unrecognized command: ") + method;
116     response.set("error_message", error_message.c_str() );
117     response.set("original_message", requestString.c_str());
118     cmd = NULL;
119   }
120 }
121
122 string GSSRequest::getResponse()
123 {
124   /* Variables */
125   JSONObject *return_values;
126   string gssResponse;
127   
128   /* Main processing */
129   // Put the return values into the response, assuming that the command
130   // was actually executed.
131   if (NULL != cmd)
132   {
133     return_values = cmd->toJSON();
134     response.set("return_values", *return_values);
135   }
136   
137   // Convert the response into a string to return.
138   gssResponse = string( response.dump() );
139   
140   /* Return */
141   return(gssResponse);
142 }
143
144
145
146 char *gss_request(char *json_string)
147 {
148   /* Variables */
149   char *retVal;
150   string output;
151   GSSRequest *req = new GSSRequest(string(json_string));
152   
153   /* Error checking */
154   // An empty json_string could be an error, but GSSRequest does
155   // a good job of handling it.
156   
157   /* Setup */
158   /* Main processing */
159   req->execute();
160   output = req->getResponse();
161   retVal = new char[ output.length() + 1 ];
162   output.copy(retVal, output.length(), 0);
163   retVal[output.length()] = 0;
164   
165   return(retVal);
166 }
167
168 void deallocate_reply(char *reply)
169 {
170   delete(reply);
171 }