OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
GatewayContainer.cc
Go to the documentation of this file.
1 // GatewayContainer.cc
2 
3 // -*- mode: c++; c-basic-offset:4 -*-
4 
5 // This file is part of gateway_module, A C++ module that can be loaded in to
6 // the OPeNDAP Back-End Server (BES) and is able to handle remote requests.
7 
8 // Copyright (c) 2002,2003 OPeNDAP, Inc.
9 // Author: Patrick West <pwest@ucar.edu>
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
26 
27 // (c) COPYRIGHT URI/MIT 1994-1999
28 // Please read the full copyright statement in the file COPYRIGHT_URI.
29 //
30 // Authors:
31 // pcw Patrick West <pwest@ucar.edu>
32 
33 #include "GatewayContainer.h"
34 
35 #include <BESSyntaxUserError.h>
36 #include <BESInternalError.h>
37 #include <BESDebug.h>
38 #include <TheBESKeys.h>
39 #include "BESCache3.h"
40 
41 #include "GatewayRequest.h"
42 #include "GatewayUtils.h"
43 #include "GatewayResponseNames.h"
44 #include "RemoteHttpResource.h"
45 
56 GatewayContainer::GatewayContainer(const string &sym_name,
57  const string &real_name, const string &type) :
58  BESContainer(sym_name, real_name, type), _remoteResource(0) {
59  if (type.empty())
60  set_container_type("gateway");
61 
62  BESUtil::url url_parts;
63  BESUtil::url_explode(real_name, url_parts);
64  url_parts.uname = "";
65  url_parts.psswd = "";
66  string use_real_name = BESUtil::url_create(url_parts);
67 
68  vector<string>::const_iterator i = GatewayUtils::WhiteList.begin();
69  vector<string>::const_iterator e = GatewayUtils::WhiteList.end();
70  bool done = false;
71  for (; i != e && !done; i++) {
72  if ((*i).length() <= use_real_name.length()) {
73  if (use_real_name.substr(0, (*i).length()) == (*i)) {
74  done = true;
75  }
76  }
77  }
78  if (!done) {
79  string err = (string) "The specified URL " + real_name
80  + " does not match any of the accessible services in"
81  + " the white list.";
82  throw BESSyntaxUserError(err, __FILE__, __LINE__);
83  }
84 }
85 
86 GatewayContainer::GatewayContainer(const GatewayContainer &copy_from) :
87  BESContainer(copy_from), _remoteResource(copy_from._remoteResource) {
88  // we can not make a copy of this container once the request has
89  // been made
90  if (_remoteResource) {
91  string err = (string) "The Container has already been accessed, "
92  + "can not create a copy of this container.";
93  throw BESInternalError(err, __FILE__, __LINE__);
94  }
95 }
96 
98  if (copy_to._remoteResource) {
99  string err = (string) "The Container has already been accessed, "
100  + "can not duplicate this resource.";
101  throw BESInternalError(err, __FILE__, __LINE__);
102  }
103  copy_to._remoteResource = _remoteResource;
104  BESContainer::_duplicate(copy_to);
105 }
106 
107 BESContainer *
109  GatewayContainer *container = new GatewayContainer;
110  _duplicate(*container);
111  return container;
112 }
113 
115  if (_remoteResource) {
116  release();
117  }
118 }
119 
126 
127  BESDEBUG( "gateway", "GatewayContainer::access() - BEGIN" << endl);
128 
129  // Since this the Gateway we know that the real_name is a URL.
130  string url = get_real_name();
131 
132 
133  // Get a pointer to the singleton cache instance for this process.
134  BESCache3 *cache = BESCache3::get_instance(TheBESKeys::TheKeys(), (string)"BES.CacheDir",
135  (string)"BES.CachePrefix", (string)"BES.CacheSize");
136 
137  BESDEBUG( "gateway", "GatewayContainer::access() - Accessing " << url << endl);
138 
139  string type = get_container_type();
140  if (type == "gateway")
141  type = "";
142 
143  if(!_remoteResource) {
144  BESDEBUG( "gateway", "GatewayContainer::access() - Building new RemoteResource." << endl );
145  _remoteResource = new gateway::RemoteHttpResource(url);
146  _remoteResource->retrieveResource();
147  }
148  BESDEBUG( "gateway", "GatewayContainer::access() - Located remote resource." << endl );
149 
150 
151  string cachedResource = _remoteResource->getCacheFileName();
152  BESDEBUG( "gateway", "GatewayContainer::access() - Using local cache file: " << cachedResource << endl );
153 
154  type = _remoteResource->getType();
155  set_container_type(type);
156  BESDEBUG( "gateway", "GatewayContainer::access() - Type: " << type << endl );
157 
158 
159  BESDEBUG( "gateway", "GatewayContainer::access() - Done accessing " << get_real_name() << " returning cached file " << cachedResource << endl);
160  BESDEBUG( "gateway", "GatewayContainer::access() - Done accessing " << *this << endl);
161  BESDEBUG( "gateway", "GatewayContainer::access() - END" << endl);
162 
163  return cachedResource; // this should return the file name from bescache3
164 }
165 
166 
167 
175  if (_remoteResource) {
176  BESDEBUG( "gateway", "GatewayContainer::release() - Releasing RemoteResource" << endl);
177  delete _remoteResource;
178  _remoteResource = 0;
179  }
180 
181  BESDEBUG( "gateway", "done releasing gateway response" << endl);
182  return true;
183 }
184 
192 void GatewayContainer::dump(ostream &strm) const {
193  strm << BESIndent::LMarg << "GatewayContainer::dump - (" << (void *) this
194  << ")" << endl;
196  BESContainer::dump(strm);
197  if (_remoteResource) {
198  strm << BESIndent::LMarg << "RemoteResource.getCacheFileName(): " << _remoteResource->getCacheFileName()
199  << endl;
200  strm << BESIndent::LMarg << "response headers: ";
201  vector<string> *hdrs = _remoteResource->getResponseHeaders();
202  if (hdrs) {
203  strm << endl;
205  vector<string>::const_iterator i = hdrs->begin();
206  vector<string>::const_iterator e = hdrs->end();
207  for (; i != e; i++) {
208  string hdr_line = (*i);
209  strm << BESIndent::LMarg << hdr_line << endl;
210  }
212  } else {
213  strm << "none" << endl;
214  }
215  } else {
216  strm << BESIndent::LMarg << "response not yet obtained" << endl;
217  }
219 }
220 
vector< string > * getResponseHeaders()
Returns a vector of HTTP headers received along with the response from the request for the remote res...
exception thrown if inernal error encountered
void _duplicate(GatewayContainer &copy_to)
static vector< string > WhiteList
Definition: GatewayUtils.h:50
string get_container_type() const
retrieve the type of data this container holds, such as cedar or netcdf.
Definition: BESContainer.h:208
virtual void dump(ostream &strm) const
dumps information about this object
static void Indent()
Definition: BESIndent.cc:38
error thrown if there is a user syntax error in the request or any other user error ...
void set_container_type(const string &type)
set the type of data that this container represents, such as cedar or netcdf.
Definition: BESContainer.h:142
virtual string access()
access the remote target response by making the remote request
Implementation of a caching mechanism for compressed data.
Definition: BESCache3.h:60
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
string getCacheFileName()
Returns the (read-locked) cache file name on the local system in which the content of the remote reso...
This class encapsulates a remote resource available via HTTP GET.
static void url_explode(const string &url_str, BESUtil::url &url_parts)
Given a url, break the url into its different parts.
Definition: BESUtil.cc:644
string get_real_name() const
retrieve the real name for this container, such as a file name.
Definition: BESContainer.h:161
string getType()
Returns the DAP type string of the RemoteHttpResource.
void _duplicate(BESContainer &copy_to)
duplicate this instance into the passed container
Definition: BESContainer.cc:50
virtual bool release()
release the resources
static string url_create(BESUtil::url &url_parts)
Definition: BESUtil.cc:709
string uname
Definition: BESUtil.h:110
string psswd
Definition: BESUtil.h:111
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESContainer.cc:68
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
A container is something that holds data.
Definition: BESContainer.h:60
void retrieveResource()
This method will check the cache for the resource.
static void UnIndent()
Definition: BESIndent.cc:44
virtual ~GatewayContainer()
Container representing a remote request.
virtual BESContainer * ptr_duplicate()
pure abstract method to duplicate this instances of BESContainer
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48
static BESCache3 * get_instance()
Get an instance of the BESCache3 object.
Definition: BESCache3.cc:83