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