bes  Updated for version 3.17.0
BESStreamResponseHandler.cc
1 // BESStreamResponseHandler.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 Foundatiion; 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 <cerrno>
34 #include <iostream>
35 #include <fstream>
36 #include <string>
37 #include <cstring>
38 
39 using std::ifstream ;
40 using std::ios ;
41 using std::endl ;
42 using std::string ;
43 
44 #include "BESStreamResponseHandler.h"
45 #include "BESRequestHandlerList.h"
46 #include "BESNotFoundError.h"
47 #include "BESInternalError.h"
48 #include "BESDataNames.h"
49 #include "BESContainer.h"
50 
51 #define BES_STREAM_BUFFER_SIZE 4096
52 
53 BESStreamResponseHandler::BESStreamResponseHandler( const string &name )
54  : BESResponseHandler( name )
55 {
56 }
57 
58 BESStreamResponseHandler::~BESStreamResponseHandler( )
59 {
60 }
61 
72 void
74 {
75  _response = 0 ;
76 
77  // What if there is a special way to stream back a data file?
78  // Should we pass this off to the request handlers and put
79  // this code into a different class for reuse? For now
80  // just keep it here. pcw 10/11/06
81 
82  // I thought about putting this in the transmit method below
83  // but decided that this is like executing a non-buffered
84  // request, so kept it here. Plus the idea expressed above
85  // led me to leave the code in the execute method.
86  // pcw 10/11/06
87  if( dhi.containers.size() != 1 )
88  {
89  string err = (string)"Unable to stream file: "
90  + "no container specified" ;
91  throw BESInternalError( err, __FILE__, __LINE__ ) ;
92  }
93 
94  dhi.first_container() ;
95  BESContainer *container = dhi.container ;
96  string filename = container->access() ;
97  if( filename.empty() )
98  {
99  string err = (string)"Unable to stream file: "
100  + "filename not specified" ;
101  throw BESInternalError( err, __FILE__, __LINE__ ) ;
102  }
103 
104  int bytes = 0 ;
105  ifstream os ;
106  os.open( filename.c_str(), ios::in ) ;
107  int myerrno = errno ;
108  if( !os )
109  {
110  string serr = (string)"Unable to stream file: "
111  + "cannot open file "
112  + filename + ": " ;
113  char *err = strerror( myerrno ) ;
114  if( err )
115  serr += err ;
116  else
117  serr += "Unknown error" ;
118 
119  throw BESNotFoundError( err, __FILE__, __LINE__ ) ;
120  }
121 
122  int nbytes ;
123  char block[BES_STREAM_BUFFER_SIZE] ;
124  os.read( block, sizeof block ) ;
125  nbytes = os.gcount() ;
126  while( nbytes )
127  {
128  bytes += nbytes ;
129  dhi.get_output_stream().write( (char*)block, nbytes ) ;
130  os.read( block, sizeof block ) ;
131  nbytes = os.gcount() ;
132  }
133  os.close() ;
134 }
135 
143 void
145  BESDataHandlerInterface &/*dhi*/ )
146 {
147  // The Data is transmitted when it is read, dumped to stdout, so there is nothing
148  // to transmot here.
149 }
150 
157 void
158 BESStreamResponseHandler::dump( ostream &strm ) const
159 {
160  strm << BESIndent::LMarg << "BESStreamResponseHandler::dump - ("
161  << (void *)this << ")" << endl ;
162  BESIndent::Indent() ;
163  BESResponseHandler::dump( strm ) ;
164  BESIndent::UnIndent() ;
165 }
166 
168 BESStreamResponseHandler::BESStreamResponseBuilder( const string &name )
169 {
170  return new BESStreamResponseHandler( name ) ;
171 }
172 
error thrown if the resource requested cannot be found
exception thrown if inernal error encountered
virtual void execute(BESDataHandlerInterface &r)
executes the command &#39;get file <filename>;&#39; by streaming the specified file
virtual string access()=0
returns the true name of this container
handler object that knows how to create a specific response object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &r)
transmit the file, streaming it back to the client
virtual void dump(ostream &strm) const
dumps information about this object
Structure storing information used by the BES to handle the request.
virtual void dump(ostream &strm) const
dumps information about this object
void first_container()
set the container pointer to the first container in the containers list
A container is something that holds data. I.E. a netcdf file or a database entry. ...
Definition: BESContainer.h:60
BESContainer * container
pointer to current container in this interface