bes  Updated for version 3.20.8
BESTransmitter.cc
1 // BESTransmitter.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <iostream>
36 #include <string>
37 #include <algorithm>
38 #include <unistd.h>
39 
40 #include "BESDataHandlerInterface.h"
41 #include "BESResponseObject.h"
42 #include "BESInternalError.h"
43 #include "BESContextManager.h"
44 
45 #include "TheBESKeys.h"
46 #include "BESInfo.h"
47 #include "BESUtil.h"
48 #include "BESDebug.h"
49 
50 #include "BESTransmitter.h"
51 
52 using std::endl;
53 using std::string;
54 using std::ostream;
55 
56 bool BESTransmitter::add_method(string method_name, p_transmitter trans_method)
57 {
58  BESTransmitter::_method_citer i;
59  i = _method_list.find(method_name);
60  if (i == _method_list.end()) {
61  _method_list[method_name] = trans_method;
62  return true;
63  }
64  return false;
65 }
66 
67 bool BESTransmitter::remove_method(string method_name)
68 {
69  BESTransmitter::_method_iter i;
70  i = _method_list.find(method_name);
71  if (i != _method_list.end()) {
72  _method_list.erase(i);
73  return true;
74  }
75  return false;
76 }
77 
78 p_transmitter BESTransmitter::find_method(string method_name)
79 {
80  BESTransmitter::_method_citer i;
81  i = _method_list.find(method_name);
82  if (i != _method_list.end()) {
83  p_transmitter p = (*i).second;
84  return p;
85  }
86  return 0;
87 }
88 
89 void BESTransmitter::send_response(const string &method_name, BESResponseObject *response, BESDataHandlerInterface &dhi)
90 {
91  p_transmitter p = find_method(method_name);
92  if (p) {
93  p(response, dhi);
94  }
95  else {
96  throw BESInternalError(string("Unable to transmit response, no transmitter for ") + method_name, __FILE__,
97  __LINE__);
98  }
99 }
100 
101 void BESTransmitter::send_text(BESInfo &info, BESDataHandlerInterface &dhi)
102 {
103  bool found = false;
104  string context = "transmit_protocol";
105  string protocol = BESContextManager::TheManager()->get_context(context, found);
106  if (protocol == "HTTP") {
107  if (info.is_buffered()) {
108  BESUtil::set_mime_text(dhi.get_output_stream());
109  }
110  }
111  info.print(dhi.get_output_stream());
112 }
113 
114 void BESTransmitter::send_html(BESInfo &info, BESDataHandlerInterface &dhi)
115 {
116  bool found = false;
117  string context = "transmit_protocol";
118  string protocol = BESContextManager::TheManager()->get_context(context, found);
119  if (protocol == "HTTP") {
120  if (info.is_buffered()) {
121  BESUtil::set_mime_html(dhi.get_output_stream());
122  }
123  }
124  info.print(dhi.get_output_stream());
125 }
126 
134 void BESTransmitter::dump(ostream &strm) const
135 {
136  strm << BESIndent::LMarg << "BESTransmitter::dump - (" << (void *) this << ")" << endl;
137  BESIndent::Indent();
138  if (_method_list.size()) {
139  strm << BESIndent::LMarg << "registered methods:" << endl;
140  BESIndent::Indent();
141  _method_citer i = _method_list.begin();
142  _method_citer ie = _method_list.end();
143  for (; i != ie; i++) {
144  strm << BESIndent::LMarg << (*i).first << ": " << (void *) (*i).second << endl;
145  }
146  BESIndent::UnIndent();
147  }
148  else {
149  strm << BESIndent::LMarg << "registered methods: none" << endl;
150  }
151  BESIndent::UnIndent();
152 }
153 
virtual std::string get_context(const std::string &name, bool &found)
retrieve the value of the specified context from the BES
Structure storing information used by the BES to handle the request.
informational response object
Definition: BESInfo.h:63
virtual bool is_buffered()
return whether the information is to be buffered or not.
Definition: BESInfo.h:111
virtual void print(std::ostream &strm)
print the information from this informational object to the specified stream
Definition: BESInfo.cc:261
exception thrown if internal error encountered
Abstract base class representing a specific set of information in response to a request to the BES.
virtual void dump(std::ostream &strm) const
dumps information about this object
static void set_mime_text(std::ostream &strm)
Generate an HTTP 1.0 response header for a text document.
Definition: BESUtil.cc:83
static void set_mime_html(std::ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition: BESUtil.cc:102