bes  Updated for version 3.19.1
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 <iostream>
34 #include <string>
35 #include <algorithm>
36 #include <unistd.h>
37 
38 #include "BESDataHandlerInterface.h"
39 #include "BESResponseObject.h"
40 #include "BESInternalError.h"
41 #include "BESContextManager.h"
42 
43 #include "TheBESKeys.h"
44 #include "BESInfo.h"
45 #include "BESUtil.h"
46 #include "BESDebug.h"
47 
48 #include "BESTransmitter.h"
49 
50 bool BESTransmitter::add_method(string method_name, p_transmitter trans_method)
51 {
52  BESTransmitter::_method_citer i;
53  i = _method_list.find(method_name);
54  if (i == _method_list.end()) {
55  _method_list[method_name] = trans_method;
56  return true;
57  }
58  return false;
59 }
60 
61 bool BESTransmitter::remove_method(string method_name)
62 {
63  BESTransmitter::_method_iter i;
64  i = _method_list.find(method_name);
65  if (i != _method_list.end()) {
66  _method_list.erase(i);
67  return true;
68  }
69  return false;
70 }
71 
72 p_transmitter BESTransmitter::find_method(string method_name)
73 {
74  BESTransmitter::_method_citer i;
75  i = _method_list.find(method_name);
76  if (i != _method_list.end()) {
77  p_transmitter p = (*i).second;
78  return p;
79  }
80  return 0;
81 }
82 
83 void BESTransmitter::send_response(const string &method_name, BESResponseObject *response, BESDataHandlerInterface &dhi)
84 {
85  p_transmitter p = find_method(method_name);
86  if (p) {
87  p(response, dhi);
88  }
89  else {
90  throw BESInternalError(string("Unable to transmit response, no transmitter for ") + method_name, __FILE__,
91  __LINE__);
92  }
93 }
94 
95 void BESTransmitter::send_text(BESInfo &info, BESDataHandlerInterface &dhi)
96 {
97  bool found = false;
98  string context = "transmit_protocol";
99  string protocol = BESContextManager::TheManager()->get_context(context, found);
100  if (protocol == "HTTP") {
101  if (info.is_buffered()) {
102  BESUtil::set_mime_text(dhi.get_output_stream());
103  }
104  }
105  info.print(dhi.get_output_stream());
106 }
107 
108 void BESTransmitter::send_html(BESInfo &info, BESDataHandlerInterface &dhi)
109 {
110  bool found = false;
111  string context = "transmit_protocol";
112  string protocol = BESContextManager::TheManager()->get_context(context, found);
113  if (protocol == "HTTP") {
114  if (info.is_buffered()) {
115  BESUtil::set_mime_html(dhi.get_output_stream());
116  }
117  }
118  info.print(dhi.get_output_stream());
119 }
120 
128 void BESTransmitter::dump(ostream &strm) const
129 {
130  strm << BESIndent::LMarg << "BESTransmitter::dump - (" << (void *) this << ")" << endl;
131  BESIndent::Indent();
132  if (_method_list.size()) {
133  strm << BESIndent::LMarg << "registered methods:" << endl;
134  BESIndent::Indent();
135  _method_citer i = _method_list.begin();
136  _method_citer ie = _method_list.end();
137  for (; i != ie; i++) {
138  strm << BESIndent::LMarg << (*i).first << ": " << (void *) (*i).second << endl;
139  }
140  BESIndent::UnIndent();
141  }
142  else {
143  strm << BESIndent::LMarg << "registered methods: none" << endl;
144  }
145  BESIndent::UnIndent();
146 }
147 
exception thrown if inernal error encountered
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual string get_context(const string &name, bool &found)
retrieve the value of the specified context from the BES
virtual bool is_buffered()
return whether the information is to be buffered or not.
Definition: BESInfo.h:123
informational response object
Definition: BESInfo.h:68
static void set_mime_html(ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition: BESUtil.cc:91
static void set_mime_text(ostream &strm)
Generate an HTTP 1.0 response header for a text document.
Definition: BESUtil.cc:72
Structure storing information used by the BES to handle the request.
virtual void print(ostream &strm)
print the information from this informational object to the specified stream
Definition: BESInfo.cc:249
Abstract base class representing a specific set of information in response to a request to the BES...