Mantid
Loading...
Searching...
No Matches
ISISDataArchive.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
14
16#include <Poco/Exception.h>
17#include <Poco/File.h>
18#include <Poco/Path.h>
19
20namespace Mantid::DataHandling {
21namespace {
23Kernel::Logger g_log("ISISDataArchive");
24} // namespace
25
26DECLARE_ARCHIVESEARCH(ISISDataArchive, ISISDataSearch)
27
28namespace {
29#ifdef _WIN32
30const char *URL_PREFIX = "http://data.isis.rl.ac.uk/where.py/windir?name=";
31#else
32const char *URL_PREFIX = "http://data.isis.rl.ac.uk/where.py/unixdir?name=";
33#endif
34} // namespace
35
44std::string ISISDataArchive::getArchivePath(const std::set<std::string> &filenames,
45 const std::vector<std::string> &exts) const {
46 if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
47 for (const auto &filename : filenames) {
48 g_log.debug() << filename << ")\n";
49 }
50 for (const auto &ext : exts) {
51 g_log.debug() << ext << ")\n";
52 }
53 }
54
55 for (const auto &filename : filenames) {
56 const std::string path_without_extension = getPath(filename);
57 if (!path_without_extension.empty()) {
58 std::string fullPath = getCorrectExtension(path_without_extension, exts);
59 if (!fullPath.empty())
60 return fullPath;
61 }
62 }
63 return "";
64}
65
71std::string ISISDataArchive::getPath(const std::string &fName) const {
72 g_log.debug() << "ISISDataArchive::getPath() - fName=" << fName << "\n";
73 if (fName.empty())
74 return ""; // Avoid pointless call to service
75
76 std::ostringstream os = sendRequest(fName);
77 os << Poco::Path::separator() << fName;
78 const std::string expectedPath = os.str();
79 return expectedPath;
80}
81
90std::ostringstream ISISDataArchive::sendRequest(const std::string &fName) const {
91 Kernel::InternetHelper inetHelper;
92 std::ostringstream os;
93 try {
94 inetHelper.sendRequest(URL_PREFIX + fName, os);
96 g_log.warning() << "Could not access archive index." << ie.what();
97 }
98 return os;
99}
100
110std::string ISISDataArchive::getCorrectExtension(const std::string &path, const std::vector<std::string> &exts) const {
111 for (const auto &ext : exts) {
112 std::string temp_path = path + ext;
113 if (fileExists(temp_path))
114 return temp_path;
115 }
116 return "";
117}
118
126bool ISISDataArchive::fileExists(const std::string &path) const {
127 try {
128 if (Poco::File(path).exists())
129 return true;
130 } catch (Poco::Exception &) {
131 }
132 return false;
133}
134
135} // namespace Mantid::DataHandling
#define DECLARE_ARCHIVESEARCH(classname, facility)
std::string getPath(const std::string &fName) const
Gets the path to the file, or most recent set of files.
std::string getArchivePath(const std::set< std::string > &filenames, const std::vector< std::string > &exts) const override
Returns the path to a filename given the list of extensions to try.
virtual std::string getCorrectExtension(const std::string &path, const std::vector< std::string > &exts) const
Public and virtual for testing purposes.
virtual bool fileExists(const std::string &path) const
Checks if the given file path exists or not.
virtual std::ostringstream sendRequest(const std::string &fName) const
Queries the archive & returns the path to a single file.
Exception thrown when error occurs accessing an internet resource.
Definition: Exception.h:321
const char * what() const noexcept override
Overloaded reporting method.
Definition: Exception.cpp:311
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
virtual HTTPStatus sendRequest(const std::string &url, std::ostream &responseStream)
Performs a request using http or https depending on the url.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
bool is(int level) const
Returns true if at least the given log level is set.
Definition: Logger.cpp:146
Kernel::Logger g_log("ExperimentInfo")
static logger object
bool exists(::NeXus::File &file, const std::string &name)
Based on the current group in the file, does the named sub-entry exist?