libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4AsyncUtil.cc
Go to the documentation of this file.
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
26
28
34void D4AsyncUtil::writeD4AsyncRequired(XMLWriter &xml, long expectedDelay, long responseLifetime,
35 string *stylesheet_ref) {
36
37 // ------ AsynchronousResponse Element and Attributes - BEGIN
38
39 /*
40 int xmlTextWriterWriteAttributeNS (xmlTextWriterPtr writer,
41 const xmlChar * prefix,
42 const xmlChar * name,
43 const xmlChar * namespaceURI,
44 const xmlChar * content)
45 */
46
47 if (stylesheet_ref) {
48 string href = "href='" + *stylesheet_ref + "'";
49 if (xmlTextWriterStartPI(xml.get_writer(), (const xmlChar *)"xml-stylesheet") < 0)
50 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
51 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)"type='text/xsl'") < 0)
52 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
53 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)" ") < 0)
54 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
55 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)href.c_str()) < 0)
56 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
57 if (xmlTextWriterEndPI(xml.get_writer()) < 0)
58 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
59 }
60
61 DapXmlNamspaces dapns;
62 if (xmlTextWriterStartElementNS(xml.get_writer(), (const xmlChar *)"dap", (const xmlChar *)"AsynchronousResponse",
63 (const xmlChar *)dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
64 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
65 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"status", (const xmlChar *)"required") < 0)
66 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
67
68 // ------ expectedDelay Element and Attributes
69 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"dap:expectedDelay") < 0)
70 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
71 ostringstream oss;
72 oss << expectedDelay;
73 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"seconds", (const xmlChar *)oss.str().c_str()) <
74 0)
75 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
76 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
77 throw InternalErr(__FILE__, __LINE__, "Could not end expectedDelay element");
78 // ------ expectedDelay Element and Attributes - END
79
80 // ------ responseLifetime Element and Attributes
81 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"dap:responseLifetime") < 0)
82 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
83 ostringstream oss2;
84 oss2 << responseLifetime;
85 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"seconds", (const xmlChar *)oss2.str().c_str()) <
86 0)
87 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'");
88 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
89 throw InternalErr(__FILE__, __LINE__, "Could not end responseLifetime element");
90 // ------ responseLifetime Element and Attributes - END
91
92 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
93 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
94 // ------ AsynchronousResponse Element and Attributes - END
95}
96
102void D4AsyncUtil::writeD4AsyncAccepted(XMLWriter &xml, long expectedDelay, long responseLifetime,
103 string asyncResourceUrl, string *stylesheet_ref) {
104
105 // ------ AsynchronousResponse Element and Attributes - BEGIN
106 DapXmlNamspaces dapns;
107
108 if (stylesheet_ref) {
109 string href = "href='" + *stylesheet_ref + "'";
110 if (xmlTextWriterStartPI(xml.get_writer(), (const xmlChar *)"xml-stylesheet") < 0)
111 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
112 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)"type='text/xsl'") < 0)
113 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
114 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)" ") < 0)
115 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
116 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)href.c_str()) < 0)
117 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
118 if (xmlTextWriterEndPI(xml.get_writer()) < 0)
119 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
120 }
121
122 if (xmlTextWriterStartElementNS(xml.get_writer(), (const xmlChar *)"dap", (const xmlChar *)"AsynchronousResponse",
123 (const xmlChar *)dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
124 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
125 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"status", (const xmlChar *)"accepted") < 0)
126 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
127
128 // ------ expectedDelay Element and Attributes
129 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"dap:expectedDelay") < 0)
130 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
131 ostringstream oss;
132 oss << expectedDelay;
133 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"seconds", (const xmlChar *)oss.str().c_str()) <
134 0)
135 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'");
136 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
137 throw InternalErr(__FILE__, __LINE__, "Could not end expectedDelay element");
138 // ------ expectedDelay Element and Attributes - END
139
140 // ------ responseLifetime Element and Attributes
141 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"dap:responseLifetime") < 0)
142 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
143 ostringstream oss2;
144 oss2 << responseLifetime;
145 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"seconds", (const xmlChar *)oss2.str().c_str()) <
146 0)
147 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'");
148 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
149 throw InternalErr(__FILE__, __LINE__, "Could not end responseLifetime element");
150 // ------ responseLifetime Element and Attributes - END
151
152 // ------ link Element and Attributes
153 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"dap:link") < 0)
154 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element");
155
156 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"href",
157 (const xmlChar *)asyncResourceUrl.c_str()) < 0)
158 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'href'");
159 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
160 throw InternalErr(__FILE__, __LINE__, "Could not end link element");
161 // ------ link Element and Attributes - END
162
163 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
164 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
165 // ------ AsynchronousResponse Element and Attributes - END
166}
167
173void D4AsyncUtil::writeD4AsyncPending(XMLWriter &xml, string *stylesheet_ref) {
174
175 // ------ AsynchronousResponse Element and Attributes - BEGIN
176 DapXmlNamspaces dapns;
177
178 if (stylesheet_ref) {
179 string href = "href='" + *stylesheet_ref + "'";
180 if (xmlTextWriterStartPI(xml.get_writer(), (const xmlChar *)"xml-stylesheet") < 0)
181 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
182 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)"type='text/xsl'") < 0)
183 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
184 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)" ") < 0)
185 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
186 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)href.c_str()) < 0)
187 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
188 if (xmlTextWriterEndPI(xml.get_writer()) < 0)
189 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
190 }
191
192 if (xmlTextWriterStartElementNS(xml.get_writer(), (const xmlChar *)"dap", (const xmlChar *)"AsynchronousResponse",
193 (const xmlChar *)dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
194 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
195 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"status", (const xmlChar *)"pending") < 0)
196 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
197
198 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
199 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
200 // ------ AsynchronousResponse Element and Attributes - END
201}
202
208void D4AsyncUtil::writeD4AsyncResponseGone(XMLWriter &xml, string *stylesheet_ref) {
209
210 // ------ AsynchronousResponse Element and Attributes - BEGIN
211 DapXmlNamspaces dapns;
212
213 if (stylesheet_ref) {
214 string href = "href='" + *stylesheet_ref + "'";
215 if (xmlTextWriterStartPI(xml.get_writer(), (const xmlChar *)"xml-stylesheet") < 0)
216 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
217 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)"type='text/xsl'") < 0)
218 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
219 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)" ") < 0)
220 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
221 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)href.c_str()) < 0)
222 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
223 if (xmlTextWriterEndPI(xml.get_writer()) < 0)
224 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
225 }
226
227 if (xmlTextWriterStartElementNS(xml.get_writer(), (const xmlChar *)"dap", (const xmlChar *)"AsynchronousResponse",
228 (const xmlChar *)dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
229 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
230 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"status", (const xmlChar *)"gone") < 0)
231 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
232
233 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
234 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
235 // ------ AsynchronousResponse Element and Attributes - END
236}
237
244 string *stylesheet_ref) {
245
246 // ------ AsynchronousResponse Element and Attributes - BEGIN
247 DapXmlNamspaces dapns;
248
249 if (stylesheet_ref) {
250 string href = "href='" + *stylesheet_ref + "'";
251 if (xmlTextWriterStartPI(xml.get_writer(), (const xmlChar *)"xml-stylesheet") < 0)
252 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction.");
253 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)"type='text/xsl'") < 0)
254 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
255 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)" ") < 0)
256 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
257 if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar *)href.c_str()) < 0)
258 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content.");
259 if (xmlTextWriterEndPI(xml.get_writer()) < 0)
260 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction.");
261 }
262
263 if (xmlTextWriterStartElementNS(xml.get_writer(), (const xmlChar *)"dap", (const xmlChar *)"AsynchronousResponse",
264 (const xmlChar *)dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0)
265 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element");
266 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"status", (const xmlChar *)"rejected") < 0)
267 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'");
268
269 // ------ reason Element and Attributes
270 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"dap:reason") < 0)
271 throw InternalErr(__FILE__, __LINE__, "Could not write reason element");
272 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"code",
273 (const xmlChar *)getRejectReasonCodeString(code).c_str()) < 0)
274 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'code'");
275 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
276 throw InternalErr(__FILE__, __LINE__, "Could not end reason element");
277 // ------ reason Element and Attributes - END
278
279 // ------ description Element and Attributes
280 if (xmlTextWriterWriteElement(xml.get_writer(), (const xmlChar *)"dap:description",
281 (const xmlChar *)description.c_str()) < 0)
282 throw InternalErr(__FILE__, __LINE__, "Could not write description element");
283
284 // ------ description Element and Attributes - END
285
286 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
287 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element");
288 // ------ AsynchronousResponse Element and Attributes - END
289}
290
292
293 string codeStr;
294 switch (code) {
295 case TIME:
296 codeStr = "time";
297 break;
298
299 case UNAVAILABLE:
300 codeStr = "unavailable";
301 break;
302
303 case PRIVILEGES:
304 codeStr = "privileges";
305 break;
306
307 case OTHER:
308 codeStr = "other";
309 break;
310
311 default:
312 throw InternalErr(__FILE__, __LINE__,
313 "D4AsyncUtil::getRejectReasonCodeString() - Unrecognized reject_reason_code.");
314 }
315
316 return codeStr;
317}
318
319// Unused paramters generate warnings, so I removed/commented them below. jhrg 3/12/14
320void D4AsyncUtil::writeD2AsyncRequired(XMLWriter & /*xml*/, long /*expectedDelay*/, long /*responseLifetime*/) {
321 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
322}
323
324void D4AsyncUtil::writeD2AsyncAccepted(XMLWriter &, long, long, string /*asyncResourceUrl*/) {
325 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
326}
327
329 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
330}
331
333 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
334}
335
337 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async.");
338}
339
340} /* namespace libdap */
string getRejectReasonCodeString(RejectReasonCode code)
void writeD2AsyncPending(XMLWriter &xml)
void writeD4AsyncResponseRejected(XMLWriter &xml, RejectReasonCode code, string description, string *stylesheet_ref=0)
Write the DAP4 ResponseRejected response. Write the DAP4 AsyncRequired in XML form.
static const string STYLESHEET_REFERENCE_KEY
Definition D4AsyncUtil.h:28
void writeD2AsyncResponseRejected(XMLWriter &xml, RejectReasonCode code, string description)
void writeD2AsyncAccepted(XMLWriter &xml, long expectedDelay, long responseLifetime, string asyncResourceUrl)
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 writeD2AsyncResponseGone(XMLWriter &xml)
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.
static std::string getDapNamespaceString(DAPVersion version)
A class for software fault reporting.
Definition InternalErr.h:61
xmlTextWriterPtr get_writer() const
Definition XMLWriter.h:55
top level DAP object to house generic methods
Definition AISConnect.cc:30
RejectReasonCode
Definition D4AsyncUtil.h:15
@ UNAVAILABLE
Definition D4AsyncUtil.h:15
@ PRIVILEGES
Definition D4AsyncUtil.h:15