OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
DapRequestHandler.cc
Go to the documentation of this file.
1 // DapRequestHandler.cc
2 
3 #include "config.h"
4 
5 #include "DapRequestHandler.h"
6 #include "DapResponseNames.h"
7 
8 #include <BESResponseHandler.h>
9 #include <BESResponseNames.h>
10 #include <BESVersionInfo.h>
11 #include <BESTextInfo.h>
12 #include <BESDapNames.h>
13 #include <BESDataDDSResponse.h>
14 #include <BESDDSResponse.h>
15 #include <BESDASResponse.h>
16 #include <BESConstraintFuncs.h>
17 #include <BESServiceRegistry.h>
18 #include <BESUtil.h>
19 #include <BESDapError.h>
20 #include <BESInternalFatalError.h>
21 #include <BESDebug.h>
22 
23 #include <BaseTypeFactory.h>
24 #include <Ancillary.h>
25 #include <Connect.h>
26 #include <Response.h>
27 #include <InternalErr.h>
28 #include <mime_util.h>
29 
30 using namespace libdap ;
31 
33  : BESRequestHandler( name )
34 {
40 }
41 
43 {
44 }
45 
46 bool
48 {
50  BESDASResponse *bdas = dynamic_cast < BESDASResponse * >(response) ;
51  if( !bdas )
52  throw BESInternalError( "cast error", __FILE__, __LINE__ ) ;
53  try {
54  bdas->set_container( dhi.container->get_symbolic_name() ) ;
55  DAS *das = bdas->get_das();
56  string accessed = dhi.container->access();
57  das->parse( accessed ) ;
58  bdas->clear_container( ) ;
59  }
60  catch( BESError &e ) {
61  throw e ;
62  }
63  catch(InternalErr & e) {
64  BESDapError ex( e.get_error_message(), true, e.get_error_code(),
65  __FILE__, __LINE__ ) ;
66  throw ex;
67  }
68  catch(Error & e) {
69  BESDapError ex( e.get_error_message(), false, e.get_error_code(),
70  __FILE__, __LINE__ ) ;
71  throw ex;
72  }
73  catch(...) {
74  string s = "unknown exception caught building DAS";
75  BESInternalFatalError ex(s, __FILE__, __LINE__);
76  throw ex;
77  }
78 
79  return true;
80 }
81 
82 bool
84 {
86  BESDDSResponse *bdds = dynamic_cast < BESDDSResponse * >(response);
87  if( !bdds )
88  throw BESInternalError( "cast error", __FILE__, __LINE__ ) ;
89  try {
90  bdds->set_container( dhi.container->get_symbolic_name() ) ;
91  DDS *dds = bdds->get_dds();
92  string accessed = dhi.container->access() ;
93  BaseTypeFactory factory;
94  dds->set_factory( &factory ) ;
95  dds->filename( accessed );
96  dds->set_dataset_name( name_path( accessed ) ) ;
97  dds->parse( accessed ) ;
98  dds->set_factory( 0 ) ;
99 
100  DAS *das = new DAS ;
101  BESDASResponse bdas( das ) ;
102  bdas.set_container( dhi.container->get_symbolic_name() ) ;
103  Ancillary::read_ancillary_das( *das, accessed ) ;
104 
105  dds->transfer_attributes( das ) ;
106 
107  bdds->set_constraint( dhi ) ;
108 
109  bdds->clear_container( ) ;
110  }
111  catch( BESError &e ) {
112  throw e ;
113  }
114  catch(InternalErr & e) {
115  BESDapError ex( e.get_error_message(), true, e.get_error_code(),
116  __FILE__, __LINE__ ) ;
117  throw ex;
118  }
119  catch(Error & e) {
120  BESDapError ex( e.get_error_message(), false, e.get_error_code(),
121  __FILE__, __LINE__ ) ;
122  throw ex;
123  }
124  catch(...) {
125  string s = "unknown exception caught building DDS";
126  BESInternalFatalError ex(s, __FILE__, __LINE__);
127  throw ex;
128  }
129 
130  return true;
131 }
132 
133 bool
135 {
137  BESDataDDSResponse *bdds = dynamic_cast < BESDataDDSResponse * >(response);
138  if( !bdds )
139  throw BESInternalError( "cast error", __FILE__, __LINE__ ) ;
140 
141  try {
142  bdds->set_container( dhi.container->get_symbolic_name() ) ;
143  DataDDS *dds = bdds->get_dds() ;
144  string accessed = dhi.container->access() ;
145  BaseTypeFactory factory;
146  dds->set_factory( &factory ) ;
147  dds->filename( accessed ) ;
148  dds->set_dataset_name( name_path( accessed ) ) ;
149  Connect *url = new Connect( accessed ) ;
150  Response *r = new Response( fopen( accessed.c_str(), "r" ), 0 ) ;
151 
152  if( !r->get_stream() )
153  throw Error(string("The input source: ")
154  + accessed
155  + string(" could not be opened"));
156 
157  url->read_data_no_mime( *dds, r ) ;
158  dds->set_factory( 0 ) ;
159 
160  // mark everything as read.
161  DDS::Vars_iter i = dds->var_begin() ;
162  DDS::Vars_iter e = dds->var_end() ;
163  for( ; i != e; i++ )
164  {
165  BaseType *b = (*i) ;
166  b->set_read_p( true ) ;
167  }
168 
169  DAS *das = new DAS ;
170  BESDASResponse bdas( das ) ;
171  bdas.set_container( dhi.container->get_symbolic_name() ) ;
172  Ancillary::read_ancillary_das( *das, accessed ) ;
173  dds->transfer_attributes( das ) ;
174 
175  bdds->set_constraint( dhi ) ;
176 
177  bdds->clear_container( ) ;
178  }
179  catch( BESError &e ) {
180  throw e ;
181  }
182  catch(InternalErr & e) {
183  BESDapError ex( e.get_error_message(), true, e.get_error_code(),
184  __FILE__, __LINE__ ) ;
185  throw ex;
186  }
187  catch(Error & e) {
188  BESDapError ex( e.get_error_message(), false, e.get_error_code(),
189  __FILE__, __LINE__ ) ;
190  throw ex;
191  }
192  catch(...) {
193  string s = "unknown exception caught building DAS";
194  BESInternalFatalError ex(s, __FILE__, __LINE__);
195  throw ex;
196  }
197 
198  return true;
199 }
200 
201 bool
203 {
204  bool ret = true ;
205  BESVersionInfo *info = dynamic_cast<BESVersionInfo *>(dhi.response_handler->get_response_object() ) ;
207  return ret ;
208 }
209 
210 bool
212 {
213  bool ret = true ;
214  BESInfo *info = dynamic_cast<BESInfo *>(dhi.response_handler->get_response_object());
215 
216  // This is an example. If you had a help file you could load it like
217  // this and if your handler handled the following responses.
218  map<string,string> attrs ;
219  attrs["name"] = PACKAGE_NAME ;
220  attrs["version"] = PACKAGE_VERSION ;
221  list<string> services ;
222  BESServiceRegistry::TheRegistry()->services_handled( "dapreader", services );
223  if( services.size() > 0 )
224  {
225  string handles = BESUtil::implode( services, ',' ) ;
226  attrs["handles"] = handles ;
227  }
228  info->begin_tag( "module", &attrs ) ;
229  //info->add_data_from_file( "Dap.Help", "Dap Help" ) ;
230  info->end_tag( "module" ) ;
231 
232  return ret ;
233 }
234 
235 void
236 DapRequestHandler::dump( ostream &strm ) const
237 {
238  strm << BESIndent::LMarg << "DapRequestHandler::dump - ("
239  << (void *)this << ")" << endl ;
241  BESRequestHandler::dump( strm ) ;
243 }
244