32 #include <sys/types.h>
56 #define JPEG2000_TEMP_DIR "/tmp"
57 #define JPEG2000_GCS "WGS84"
59 string JPEG2000Transmitter::temp_dir;
83 if (JPEG2000Transmitter::temp_dir.empty()) {
86 string key =
"JPEG2000.Tempdir";
88 if (!found || JPEG2000Transmitter::temp_dir.empty()) {
91 string::size_type len = JPEG2000Transmitter::temp_dir.length();
92 if (JPEG2000Transmitter::temp_dir[len - 1] ==
'/') {
93 JPEG2000Transmitter::temp_dir = JPEG2000Transmitter::temp_dir.substr(0, len - 1);
100 string key =
"JPEG2000.Default_GCS";
130 DataDDS *dds = bdds->
get_dds();
132 throw BESInternalError(
"No DataDDS has been created for transmit", __FILE__, __LINE__);
136 throw BESInternalError(
"Output stream is not set, cannot return as", __FILE__, __LINE__);
138 BESDEBUG(
"JPEG20002",
"JPEG2000Transmitter::send_data - parsing the constraint" << endl);
143 bdds->
get_ce().parse_constraint(ce, *dds);
146 throw BESInternalError(
"Failed to parse the constraint expression: " + e.get_error_message(), __FILE__, __LINE__);
149 throw BESInternalError(
"Failed to parse the constraint expression: Unknown exception caught", __FILE__, __LINE__);
153 BESDEBUG(
"JPEG20002",
"JPEG2000Transmitter::send_data - reading data into DataDDS" << endl);
157 string temp_file_name = JPEG2000Transmitter::temp_dir +
'/' +
"jp2XXXXXX";
158 vector<char> temp_file(temp_file_name.length() + 1);
159 string::size_type len = temp_file_name.copy(&temp_file[0], temp_file_name.length());
160 temp_file[len] =
'\0';
164 mode_t original_mode = umask(077);
167 int fd = mkstemp(&temp_file[0]);
168 umask(original_mode);
171 throw BESInternalError(
"Failed to open the temporary file: " + temp_file_name, __FILE__, __LINE__);
174 BESDEBUG(
"JPEG20002",
"JPEG2000Transmitter::send_data - transforming into temporary file " << &temp_file[0] << endl);
178 if (bdds->
get_ce().function_clauses()) {
179 BESDEBUG(
"fonc",
"processing a functional constraint clause(s)." << endl);
180 DataDDS *tmp_dds = bdds->
get_ce().eval_function_clauses(*dds);
188 for (DDS::Vars_iter i = dds->var_begin(); i != dds->var_end(); i++) {
189 if ((*i)->send_p()) {
190 (*i)->intern_data(bdds->
get_ce(), *dds);
196 throw BESInternalError(
"Failed to read data: " + e.get_error_message(), __FILE__, __LINE__);
199 throw BESInternalError(
"Failed to read data: Unknown exception caught", __FILE__, __LINE__);
208 BESDEBUG(
"JPEG20002",
"JPEG2000Transmitter::send_data - transmitting temp file " << &temp_file[0] << endl );
210 JPEG2000Transmitter::return_temp_stream(&temp_file[0], strm);
214 (void) unlink(&temp_file[0]);
219 (void) unlink(&temp_file[0]);
225 (void) unlink(&temp_file[0]);
226 throw BESInternalError(
"Fileout GeoTiff, was not able to transform to jp2, unknown error", __FILE__, __LINE__);
230 (void) unlink(&temp_file[0]);
232 BESDEBUG(
"JPEG20002",
"JPEG2000Transmitter::send_data - done transmitting to jp2" << endl);
244 void JPEG2000Transmitter::return_temp_stream(
const string &filename, ostream &strm)
247 os.open(filename.c_str(), ios::binary | ios::in);
249 throw BESInternalError(
"Cannot connect to data source", __FILE__, __LINE__);
252 os.read(block,
sizeof block);
253 int nbytes = os.gcount();
256 throw BESInternalError(
"Internal server error, got zero count on stream buffer.", __FILE__, __LINE__);
260 string context =
"transmit_protocol";
262 if (protocol ==
"HTTP") {
263 strm <<
"HTTP/1.0 200 OK\n";
264 strm <<
"Content-type: application/octet-stream\n";
265 strm <<
"Content-Description: " <<
"BES dataset" <<
"\n";
266 strm <<
"Content-Disposition: filename=" << filename <<
".jp2;\n\n";
269 strm.write(block, nbytes);
272 os.read(block,
sizeof block);
273 nbytes = os.gcount();
274 strm.write(block, nbytes);
void set_dds(DataDDS *ddsIn)
Set the response object's DDS.
exception thrown if inernal error encountered
ostream & get_output_stream()
virtual bool add_method(string method_name, p_transmitter trans_method)
virtual string get_context(const string &name, bool &found)
retrieve the value of the specified context from the BES
static class NCMLUtil overview
Abstract exception class for the BES with basic string message.
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
JPEG2000Transmitter()
Construct the JPEG2000Transmitter, adding it with name geotiff to be able to transmit a data response...
static BESContextManager * TheManager()
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
ConstraintEvaluator & get_ce()
Structure storing information used by the BES to handle the request.
#define JPEG2000_TEMP_DIR
static void send_data_as_jp2(BESResponseObject *obj, BESDataHandlerInterface &dhi)
The static method registered to transmit OPeNDAP data objects as a netcdf file.
map< string, string > data
the map of string data that will be required for the current request.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
static BESKeys * TheKeys()
Abstract base class representing a specific set of information in response to a request to the BES...
static string default_gcs