libdap Updated for version 3.20.10
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4AsyncUtil.cc
1/*
2 * D4AsyncUtil.cc
3 *
4 * Created on: Feb 18, 2014
5 * Author: ndp
6 */
7
8#include "config.h"
9
10#include <sstream>
11
12#include "XMLWriter.h"
13
14#include "Error.h"
15#include "InternalErr.h"
16#include "util.h"
17
18#include "D4AsyncUtil.h"
19#include "DapXmlNamespaces.h"
20
21namespace libdap {
22
23const string D4AsyncUtil::STYLESHEET_REFERENCE_KEY = "DAP.Async.StyleSheet.Ref";
24
25D4AsyncUtil::D4AsyncUtil() {}
26
27D4AsyncUtil::~D4AsyncUtil() {}
28
34void D4AsyncUtil::writeD4AsyncRequired(XMLWriter &xml, long expectedDelay, long responseLifetime, string *stylesheet_ref) {
35
36 // ------ AsynchronousResponse Element and Attributes - BEGIN
37
38 /*
39 int xmlTextWriterWriteAttributeNS (xmlTextWriterPtr writer,
40 const xmlChar * prefix,
41 const xmlChar * name,
42 const xmlChar * namespaceURI,
43 const xmlChar * content)
44 */
45
46 if(stylesheet_ref){
47 string href = "href='" + *stylesheet_ref +"'";
48 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0)
49 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
50 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0)
51 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
52 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0)
53 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
54 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0)
55 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
56 if(xmlTextWriterEndPI(xml.get_writer()) < 0)
57 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
58 }
59
60 DapXmlNamspaces dapns;
61 if (xmlTextWriterStartElementNS(xml.get_writer(),
62 (const xmlChar*)"dap",
63 (const xmlChar*) "AsynchronousResponse",
64 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
65 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
66 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "required") < 0)
67 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
68
69
70 // ------ expectedDelay Element and Attributes
71 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:expectedDelay") < 0)
72 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
73 ostringstream oss;
74 oss << expectedDelay;
75 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss.str().c_str()) < 0)
76 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
77 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
78 throw InternalErr(__FILE__, __LINE__, "Could not end expectedDelay element");
79 // ------ expectedDelay Element and Attributes - END
80
81
82 // ------ responseLifetime Element and Attributes
83 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:responseLifetime") < 0)
84 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
85 ostringstream oss2;
86 oss2 << responseLifetime;
87 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss2.str().c_str()) < 0)
88 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'");
89 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
90 throw InternalErr(__FILE__, __LINE__, "Could not end responseLifetime element");
91 // ------ responseLifetime Element and Attributes - END
92
93
94 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
95 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
96 // ------ AsynchronousResponse Element and Attributes - END
97}
98
99
105void D4AsyncUtil::writeD4AsyncAccepted(XMLWriter &xml, long expectedDelay, long responseLifetime, string asyncResourceUrl, string *stylesheet_ref) {
106
107 // ------ AsynchronousResponse Element and Attributes - BEGIN
108 DapXmlNamspaces dapns;
109
110 if(stylesheet_ref){
111 string href = "href='" + *stylesheet_ref +"'";
112 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0)
113 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
114 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0)
115 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
116 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0)
117 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
118 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0)
119 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
120 if(xmlTextWriterEndPI(xml.get_writer()) < 0)
121 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
122 }
123
124 if (xmlTextWriterStartElementNS(xml.get_writer(),
125 (const xmlChar*)"dap",
126 (const xmlChar*) "AsynchronousResponse",
127 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
128 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
129 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "accepted") < 0)
130 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
131
132
133 // ------ expectedDelay Element and Attributes
134 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:expectedDelay") < 0)
135 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
136 ostringstream oss;
137 oss << expectedDelay;
138 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss.str().c_str()) < 0)
139 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'");
140 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
141 throw InternalErr(__FILE__, __LINE__, "Could not end expectedDelay element");
142 // ------ expectedDelay Element and Attributes - END
143
144
145 // ------ responseLifetime Element and Attributes
146 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:responseLifetime") < 0)
147 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
148 ostringstream oss2;
149 oss2 << responseLifetime;
150 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss2.str().c_str()) < 0)
151 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'");
152 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
153 throw InternalErr(__FILE__, __LINE__, "Could not end responseLifetime element");
154 // ------ responseLifetime Element and Attributes - END
155
156
157 // ------ link Element and Attributes
158 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:link") < 0)
159 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
160
161 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "href", (const xmlChar*) asyncResourceUrl.c_str()) < 0)
162 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'href'");
163 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
164 throw InternalErr(__FILE__, __LINE__, "Could not end link element");
165 // ------ link Element and Attributes - END
166
167
168 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
169 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
170 // ------ AsynchronousResponse Element and Attributes - END
171}
172
178void D4AsyncUtil::writeD4AsyncPending(XMLWriter &xml, string *stylesheet_ref) {
179
180 // ------ AsynchronousResponse Element and Attributes - BEGIN
181 DapXmlNamspaces dapns;
182
183
184 if(stylesheet_ref){
185 string href = "href='" + *stylesheet_ref +"'";
186 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0)
187 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
188 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0)
189 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
190 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0)
191 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
192 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0)
193 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
194 if(xmlTextWriterEndPI(xml.get_writer()) < 0)
195 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
196 }
197
198 if (xmlTextWriterStartElementNS(xml.get_writer(),
199 (const xmlChar*)"dap",
200 (const xmlChar*) "AsynchronousResponse",
201 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
202 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
203 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "pending") < 0)
204 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
205
206 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
207 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
208 // ------ AsynchronousResponse Element and Attributes - END
209}
210
211
217void D4AsyncUtil::writeD4AsyncResponseGone(XMLWriter &xml, string *stylesheet_ref) {
218
219 // ------ AsynchronousResponse Element and Attributes - BEGIN
220 DapXmlNamspaces dapns;
221
222
223 if(stylesheet_ref){
224 string href = "href='" + *stylesheet_ref +"'";
225 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0)
226 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
227 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0)
228 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
229 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0)
230 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
231 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0)
232 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
233 if(xmlTextWriterEndPI(xml.get_writer()) < 0)
234 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
235 }
236
237 if (xmlTextWriterStartElementNS(xml.get_writer(),
238 (const xmlChar*)"dap",
239 (const xmlChar*) "AsynchronousResponse",
240 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
241 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
242 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "gone") < 0)
243 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
244
245 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
246 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
247 // ------ AsynchronousResponse Element and Attributes - END
248}
249
250
256void D4AsyncUtil::writeD4AsyncResponseRejected(XMLWriter &xml, RejectReasonCode code, string description, string *stylesheet_ref) {
257
258 // ------ AsynchronousResponse Element and Attributes - BEGIN
259 DapXmlNamspaces dapns;
260
261
262 if(stylesheet_ref){
263 string href = "href='" + *stylesheet_ref +"'";
264 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0)
265 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
266 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0)
267 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
268 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0)
269 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
270 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0)
271 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
272 if(xmlTextWriterEndPI(xml.get_writer()) < 0)
273 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
274 }
275
276 if (xmlTextWriterStartElementNS(xml.get_writer(),
277 (const xmlChar*)"dap",
278 (const xmlChar*) "AsynchronousResponse",
279 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
280 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
281 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "rejected") < 0)
282 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
283
284 // ------ reason Element and Attributes
285 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:reason") < 0)
286 throw InternalErr(__FILE__, __LINE__, "Could not write reason element");
287 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "code", (const xmlChar*) getRejectReasonCodeString(code).c_str()) < 0)
288 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'code'");
289 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
290 throw InternalErr(__FILE__, __LINE__, "Could not end reason element");
291 // ------ reason Element and Attributes - END
292
293
294 // ------ description Element and Attributes
295 if (xmlTextWriterWriteElement(xml.get_writer(), (const xmlChar*) "dap:description", (const xmlChar*) description.c_str()) < 0)
296 throw InternalErr(__FILE__, __LINE__, "Could not write description element");
297
298 // ------ description Element and Attributes - END
299
300 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
301 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
302 // ------ AsynchronousResponse Element and Attributes - END
303
304
305}
306
307string D4AsyncUtil::getRejectReasonCodeString(RejectReasonCode code){
308
309 string codeStr;
310 switch(code){
311 case TIME:
312 codeStr = "time";
313 break;
314
315 case UNAVAILABLE:
316 codeStr = "unavailable";
317 break;
318
319 case PRIVILEGES:
320 codeStr = "privileges";
321 break;
322
323 case OTHER:
324 codeStr = "other";
325 break;
326
327 default:
328 throw InternalErr(__FILE__, __LINE__, "D4AsyncUtil::getRejectReasonCodeString() - Unrecognized reject_reason_code.");
329 }
330
331 return codeStr;
332}
333
334// Unused paramters generate warnings, so I removed/commented them below. jhrg 3/12/14
335void D4AsyncUtil::writeD2AsyncRequired(XMLWriter &/*xml*/, long /*expectedDelay*/, long /*responseLifetime*/) {
336 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
337}
338
339void D4AsyncUtil::writeD2AsyncAccepted(XMLWriter &, long , long , string /*asyncResourceUrl*/) {
340 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
341}
342
343
344void D4AsyncUtil::writeD2AsyncPending(XMLWriter &) {
345 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
346}
347
348void D4AsyncUtil::writeD2AsyncResponseGone(XMLWriter &) {
349 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
350}
351
352void D4AsyncUtil::writeD2AsyncResponseRejected(XMLWriter &, RejectReasonCode /*code*/, string /*description*/) {
353 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
354}
355
356} /* namespace libdap */
void writeD4AsyncResponseRejected(XMLWriter &xml, RejectReasonCode code, string description, string *stylesheet_ref=0)
Write the DAP4 ResponseRejected response. Write the DAP4 AsyncRequired in XML form.
void writeD4AsyncPending(XMLWriter &xml, string *stylesheet_ref=0)
Write the DAP4 AsyncPending response. Write the DAP4 AsyncPending in XML form.
void writeD4AsyncRequired(XMLWriter &xml, long expectedDelay, long responseLifetime, string *stylesheet_ref=0)
Write the DAP4 AsyncRequired response. Print the AsyncRequired in XML form.
void writeD2AsyncRequired(XMLWriter &xml, long expectedDelay, long responseLifetime)
Write the DAP2 AsyncRequired response . Write the DAP2 AsyncRequired in XML form.
void writeD4AsyncAccepted(XMLWriter &xml, long expectedDelay, long responseLifetime, string asyncResourceUrl, string *stylesheet_ref=0)
Write the DAP4 AsyncAccepted response. Write the AsyncAccepted in XML form.
void writeD4AsyncResponseGone(XMLWriter &xml, string *stylesheet_ref=0)
Write the DAP4 AsyncResponseGone response. Write the DAP4 AsyncRequired in XML form.
A class for software fault reporting.
Definition InternalErr.h:65
top level DAP object to house generic methods