libdap
Updated for version 3.17.0
|
00001 /* 00002 * D4AsyncUtil.cc 00003 * 00004 * Created on: Feb 18, 2014 00005 * Author: ndp 00006 */ 00007 00008 #include "config.h" 00009 00010 #include <sstream> 00011 00012 #include "XMLWriter.h" 00013 00014 #include "Error.h" 00015 #include "InternalErr.h" 00016 #include "util.h" 00017 00018 #include "D4AsyncUtil.h" 00019 #include "DapXmlNamespaces.h" 00020 00021 namespace libdap { 00022 00023 const string D4AsyncUtil::STYLESHEET_REFERENCE_KEY = "DAP.Async.StyleSheet.Ref"; 00024 00025 D4AsyncUtil::D4AsyncUtil() {} 00026 00027 D4AsyncUtil::~D4AsyncUtil() {} 00028 00034 void D4AsyncUtil::writeD4AsyncRequired(XMLWriter &xml, long expectedDelay, long responseLifetime, string *stylesheet_ref) { 00035 00036 // ------ AsynchronousResponse Element and Attributes - BEGIN 00037 00038 /* 00039 int xmlTextWriterWriteAttributeNS (xmlTextWriterPtr writer, 00040 const xmlChar * prefix, 00041 const xmlChar * name, 00042 const xmlChar * namespaceURI, 00043 const xmlChar * content) 00044 */ 00045 00046 if(stylesheet_ref){ 00047 string href = "href='" + *stylesheet_ref +"'"; 00048 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0) 00049 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction."); 00050 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0) 00051 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00052 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0) 00053 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00054 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0) 00055 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00056 if(xmlTextWriterEndPI(xml.get_writer()) < 0) 00057 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction."); 00058 } 00059 00060 DapXmlNamspaces dapns; 00061 if (xmlTextWriterStartElementNS(xml.get_writer(), 00062 (const xmlChar*)"dap", 00063 (const xmlChar*) "AsynchronousResponse", 00064 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0) 00065 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element"); 00066 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "required") < 0) 00067 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'"); 00068 00069 00070 // ------ expectedDelay Element and Attributes 00071 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:expectedDelay") < 0) 00072 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element"); 00073 ostringstream oss; 00074 oss << expectedDelay; 00075 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss.str().c_str()) < 0) 00076 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'"); 00077 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00078 throw InternalErr(__FILE__, __LINE__, "Could not end expectedDelay element"); 00079 // ------ expectedDelay Element and Attributes - END 00080 00081 00082 // ------ responseLifetime Element and Attributes 00083 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:responseLifetime") < 0) 00084 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element"); 00085 ostringstream oss2; 00086 oss2 << responseLifetime; 00087 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss2.str().c_str()) < 0) 00088 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'"); 00089 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00090 throw InternalErr(__FILE__, __LINE__, "Could not end responseLifetime element"); 00091 // ------ responseLifetime Element and Attributes - END 00092 00093 00094 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00095 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element"); 00096 // ------ AsynchronousResponse Element and Attributes - END 00097 } 00098 00099 00105 void D4AsyncUtil::writeD4AsyncAccepted(XMLWriter &xml, long expectedDelay, long responseLifetime, string asyncResourceUrl, string *stylesheet_ref) { 00106 00107 // ------ AsynchronousResponse Element and Attributes - BEGIN 00108 DapXmlNamspaces dapns; 00109 00110 if(stylesheet_ref){ 00111 string href = "href='" + *stylesheet_ref +"'"; 00112 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0) 00113 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction."); 00114 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0) 00115 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00116 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0) 00117 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00118 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0) 00119 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00120 if(xmlTextWriterEndPI(xml.get_writer()) < 0) 00121 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction."); 00122 } 00123 00124 if (xmlTextWriterStartElementNS(xml.get_writer(), 00125 (const xmlChar*)"dap", 00126 (const xmlChar*) "AsynchronousResponse", 00127 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0) 00128 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element"); 00129 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "accepted") < 0) 00130 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'"); 00131 00132 00133 // ------ expectedDelay Element and Attributes 00134 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:expectedDelay") < 0) 00135 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element"); 00136 ostringstream oss; 00137 oss << expectedDelay; 00138 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss.str().c_str()) < 0) 00139 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'"); 00140 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00141 throw InternalErr(__FILE__, __LINE__, "Could not end expectedDelay element"); 00142 // ------ expectedDelay Element and Attributes - END 00143 00144 00145 // ------ responseLifetime Element and Attributes 00146 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:responseLifetime") < 0) 00147 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element"); 00148 ostringstream oss2; 00149 oss2 << responseLifetime; 00150 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "seconds", (const xmlChar*) oss2.str().c_str()) < 0) 00151 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'seconds'"); 00152 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00153 throw InternalErr(__FILE__, __LINE__, "Could not end responseLifetime element"); 00154 // ------ responseLifetime Element and Attributes - END 00155 00156 00157 // ------ link Element and Attributes 00158 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:link") < 0) 00159 throw InternalErr(__FILE__, __LINE__, "Could not write expectedDelay element"); 00160 00161 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "href", (const xmlChar*) asyncResourceUrl.c_str()) < 0) 00162 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'href'"); 00163 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00164 throw InternalErr(__FILE__, __LINE__, "Could not end link element"); 00165 // ------ link Element and Attributes - END 00166 00167 00168 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00169 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element"); 00170 // ------ AsynchronousResponse Element and Attributes - END 00171 } 00172 00178 void D4AsyncUtil::writeD4AsyncPending(XMLWriter &xml, string *stylesheet_ref) { 00179 00180 // ------ AsynchronousResponse Element and Attributes - BEGIN 00181 DapXmlNamspaces dapns; 00182 00183 00184 if(stylesheet_ref){ 00185 string href = "href='" + *stylesheet_ref +"'"; 00186 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0) 00187 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction."); 00188 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0) 00189 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00190 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0) 00191 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00192 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0) 00193 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00194 if(xmlTextWriterEndPI(xml.get_writer()) < 0) 00195 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction."); 00196 } 00197 00198 if (xmlTextWriterStartElementNS(xml.get_writer(), 00199 (const xmlChar*)"dap", 00200 (const xmlChar*) "AsynchronousResponse", 00201 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0) 00202 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element"); 00203 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "pending") < 0) 00204 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'"); 00205 00206 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00207 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element"); 00208 // ------ AsynchronousResponse Element and Attributes - END 00209 } 00210 00211 00217 void D4AsyncUtil::writeD4AsyncResponseGone(XMLWriter &xml, string *stylesheet_ref) { 00218 00219 // ------ AsynchronousResponse Element and Attributes - BEGIN 00220 DapXmlNamspaces dapns; 00221 00222 00223 if(stylesheet_ref){ 00224 string href = "href='" + *stylesheet_ref +"'"; 00225 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0) 00226 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction."); 00227 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0) 00228 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00229 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0) 00230 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00231 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0) 00232 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00233 if(xmlTextWriterEndPI(xml.get_writer()) < 0) 00234 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction."); 00235 } 00236 00237 if (xmlTextWriterStartElementNS(xml.get_writer(), 00238 (const xmlChar*)"dap", 00239 (const xmlChar*) "AsynchronousResponse", 00240 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0) 00241 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element"); 00242 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "gone") < 0) 00243 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'"); 00244 00245 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00246 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element"); 00247 // ------ AsynchronousResponse Element and Attributes - END 00248 } 00249 00250 00256 void D4AsyncUtil::writeD4AsyncResponseRejected(XMLWriter &xml, RejectReasonCode code, string description, string *stylesheet_ref) { 00257 00258 // ------ AsynchronousResponse Element and Attributes - BEGIN 00259 DapXmlNamspaces dapns; 00260 00261 00262 if(stylesheet_ref){ 00263 string href = "href='" + *stylesheet_ref +"'"; 00264 if(xmlTextWriterStartPI(xml.get_writer(), (const xmlChar*) "xml-stylesheet") < 0) 00265 throw InternalErr(__FILE__, __LINE__, "Could not start XML Processing Instruction."); 00266 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) "type='text/xsl'") < 0) 00267 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00268 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) " ") < 0) 00269 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00270 if(xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) href.c_str()) < 0) 00271 throw InternalErr(__FILE__, __LINE__, "Could not write Processing Instruction content."); 00272 if(xmlTextWriterEndPI(xml.get_writer()) < 0) 00273 throw InternalErr(__FILE__, __LINE__, "Could not Close XML Processing Instruction."); 00274 } 00275 00276 if (xmlTextWriterStartElementNS(xml.get_writer(), 00277 (const xmlChar*)"dap", 00278 (const xmlChar*) "AsynchronousResponse", 00279 (const xmlChar*) dapns.getDapNamespaceString(DAP_4_0).c_str()) < 0) 00280 throw InternalErr(__FILE__, __LINE__, "Could not write AsynchronousResponse element"); 00281 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "status", (const xmlChar *) "rejected") < 0) 00282 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'status'"); 00283 00284 // ------ reason Element and Attributes 00285 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "dap:reason") < 0) 00286 throw InternalErr(__FILE__, __LINE__, "Could not write reason element"); 00287 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "code", (const xmlChar*) getRejectReasonCodeString(code).c_str()) < 0) 00288 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for 'code'"); 00289 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00290 throw InternalErr(__FILE__, __LINE__, "Could not end reason element"); 00291 // ------ reason Element and Attributes - END 00292 00293 00294 // ------ description Element and Attributes 00295 if (xmlTextWriterWriteElement(xml.get_writer(), (const xmlChar*) "dap:description", (const xmlChar*) description.c_str()) < 0) 00296 throw InternalErr(__FILE__, __LINE__, "Could not write description element"); 00297 00298 // ------ description Element and Attributes - END 00299 00300 if (xmlTextWriterEndElement(xml.get_writer()) < 0) 00301 throw InternalErr(__FILE__, __LINE__, "Could not end AsynchronousResponse element"); 00302 // ------ AsynchronousResponse Element and Attributes - END 00303 00304 00305 } 00306 00307 string D4AsyncUtil::getRejectReasonCodeString(RejectReasonCode code){ 00308 00309 string codeStr; 00310 switch(code){ 00311 case TIME: 00312 codeStr = "time"; 00313 break; 00314 00315 case UNAVAILABLE: 00316 codeStr = "unavailable"; 00317 break; 00318 00319 case PRIVILEGES: 00320 codeStr = "privileges"; 00321 break; 00322 00323 case OTHER: 00324 codeStr = "other"; 00325 break; 00326 00327 default: 00328 throw InternalErr(__FILE__, __LINE__, "D4AsyncUtil::getRejectReasonCodeString() - Unrecognized reject_reason_code."); 00329 break; 00330 00331 } 00332 return codeStr; 00333 } 00334 00335 // Unused paramters generate warnings, so I removed/commented them below. jhrg 3/12/14 00336 void D4AsyncUtil::writeD2AsyncRequired(XMLWriter &/*xml*/, long /*expectedDelay*/, long /*responseLifetime*/) { 00337 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async."); 00338 } 00339 00340 void D4AsyncUtil::writeD2AsyncAccepted(XMLWriter &, long , long , string /*asyncResourceUrl*/) { 00341 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async."); 00342 } 00343 00344 00345 void D4AsyncUtil::writeD2AsyncPending(XMLWriter &) { 00346 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async."); 00347 } 00348 00349 void D4AsyncUtil::writeD2AsyncResponseGone(XMLWriter &) { 00350 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async."); 00351 } 00352 00353 void D4AsyncUtil::writeD2AsyncResponseRejected(XMLWriter &, RejectReasonCode /*code*/, string /*description*/) { 00354 throw InternalErr(__FILE__, __LINE__, "DAP2 Doesn't handle Async."); 00355 } 00356 00357 } /* namespace libdap */