NCBI C++ ToolKit
json_version_info.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: json_version_info.cpp 45359 2020-07-14 14:09:56Z evgeniev $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladislav Evgeniev
27  *
28  * File Description: CCgiVersionInfo implementatoin
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <sstream>
34 #include <corelib/ncbi_system.hpp>
35 #include <corelib/ncbireg.hpp>
36 #include <corelib/ncbiapp.hpp>
37 #include <corelib/ncbistre.hpp>
38 #include <cgi/cgi_exception.hpp>
39 
41 
42 using namespace std;
43 
45 
46 bool ReportJSONVersion(const CCgiRequest& request, CCgiResponse& response)
47 {
48  string ver_type;
49  bool found = false;
50  ver_type = request.GetEntry("version", &found);
51  if (!found)
52  return false;
53 
54  bool hasCallback = !request.GetEntry("callback").empty();
55 
56  CNcbiOstream &out = response.out();
57 
58  if (hasCallback)
59  response.SetContentType("text/javascript");
60  else
61  response.SetContentType("text/plain");
62 
63  response.WriteHeader();
64 
65  const auto app = CNcbiApplication::Instance();
66  if (ver_type.empty() || ver_type == "short") {
67  out << app->GetFullVersion().PrintJson(kEmptyStr, CVersion::fVersionInfo);
68  }
69  else if (ver_type == "full") {
70  out << app->GetFullVersion().PrintJson(app->GetAppName());
71  }
72  else {
74  "Unsupported ncbi_version argument value");
75  }
76 
77  return true;
78 }
79 
80 void TestDelay(const CCgiRequest& request)
81 {
82  try {
83  if (request.GetEntry("delaytest").empty())
84  return;
85 
86  const CNcbiRegistry& reg(CNcbiApplication::Instance()->GetConfig());
87 
88  // get whether delay is allowed
89  int max_delaytest = reg.GetInt("debug", "max_delaytest", 0);
90 
91  if (max_delaytest == 0)
92  return;
93 
94  int delay_sec = NStr::StringToInt(request.GetEntry("delaytest"));
95 
96  SleepSec(delay_sec < max_delaytest ? delay_sec : max_delaytest);
97  }
98  catch(...) {
99  // Ignore the potential numeric conversion exceptions, just don't do a delay
100  }
101 }
102 
Exception classes used by the NCBI CGI framework.
CCgiRequestException –.
CCgiRequest::
Definition: ncbicgi.hpp:685
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:244
CNcbiRegistry –.
Definition: ncbireg.hpp:913
std::ofstream out("events_result.xml")
main entry point for tests
bool empty() const
Definition: ncbicgi.hpp:538
CNcbiOstream & out(void) const
Get output stream. Throw exception if GetOutput() is NULL.
Definition: ncbicgir.cpp:257
void SetContentType(const string &type)
Set content type (text/html by default if not provided)
Definition: ncbicgir.hpp:337
CNcbiOstream & WriteHeader(void) const
Write HTTP response header to the output stream.
Definition: ncbicgir.hpp:396
const CCgiEntry & GetEntry(const string &name, bool *is_found=0) const
Get entry value by name.
Definition: ncbicgi.cpp:1449
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
virtual int GetInt(const string &section, const string &name, int default_value, TFlags flags=0, EErrAction err_action=eThrow) const
Get integer value of specified parameter name.
Definition: ncbireg.cpp:362
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define kEmptyStr
Definition: ncbistr.hpp:123
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
@ fVersionInfo
Print version info.
bool ReportJSONVersion(const CCgiRequest &request, CCgiResponse &response)
Reports version information in JSON format.
void TestDelay(const CCgiRequest &request)
Delay test, simulating Grid framework delay.
Define Returns binary version information in JSON format.
void SleepSec(unsigned long sec, EInterruptOnSignal onsignal=eRestartOnSignal)
Sleep.
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Process information in the NCBI Registry, including working with configuration files.
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
Modified on Wed Dec 06 07:16:43 2023 by modify_doxy.py rev. 669887