Newer
Older
Roman Tolchenov
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/ISISDataArchive.h"
#include "MantidAPI/ArchiveSearchFactory.h"
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/URI.h>
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/StringTokenizer.h>
#include <Poco/Exception.h>
Roman Tolchenov
committed
#include <iostream>
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::URI;
namespace Mantid
{
namespace DataHandling
{
Mantid::Kernel::Logger & ISISDataArchive::g_log = Mantid::Kernel::Logger::get("ISISDataArchive");
Roman Tolchenov
committed
DECLARE_ARCHIVESEARCH(ISISDataArchive,ISISDataSearch);
Roman Tolchenov
committed
Roman Tolchenov
committed
/**
* Calls a web service to get a full path to a file
Janik Zikovsky
committed
* @param fName :: The file name.
Roman Tolchenov
committed
* @return The path to the file or empty string in case of error.
*/
Roman Tolchenov
committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
std::string ISISDataArchive::getPath(const std::string& fName)const
{
#ifdef _WIN32
std::string URL("http://data.isis.rl.ac.uk/where.py/windir?name=");
#else
std::string URL("http://data.isis.rl.ac.uk/where.py/unixdir?name=");
#endif
URL += fName;
URI uri(URL);
std::string path(uri.getPathAndQuery());
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
session.sendRequest(req);
HTTPResponse res;
std::istream& rs = session.receiveResponse(res);
std::string out;
char buff[300];
std::streamsize n;
do
{
rs.read(&buff[0],300);
n = rs.gcount();
out.append(&buff[0],n);
}
while(n == 300);
if ( !out.empty() )
{
if (out[0] == '<' || out.find("ERROR") != std::string::npos)
{// if error return empty string
out.clear();
}
else
{// append the file name
out += out[0] + fName;
}
}
return out;
}
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
std::string ISISDataArchive::getArchivePath(const std::set<std::string>& filenames, const std::vector<std::string>& exts)const
{
std::set<std::string>::const_iterator iter = filenames.begin();
for(; iter!=filenames.end(); ++iter)
{
g_log.debug() << *iter << ")\n";
}
std::vector<std::string>::const_iterator iter2 = exts.begin();
for(; iter2!=exts.end(); ++iter2)
{
g_log.debug() << *iter2 << ")\n";
}
std::vector<std::string>::const_iterator ext = exts.begin();
for (; ext != exts.end(); ++ext)
{
std::set<std::string>::const_iterator it = filenames.begin();
for(; it!=filenames.end(); ++it)
{
std::string path;
path = getPath(*it + *ext);
try
{
if (!path.empty() && Poco::File(path).exists())
{
return path;
}
}
catch(std::exception& e)
{
g_log.error() << "Cannot open file " << path << ": " << e.what() << '\n';
return "";
}
} // it
} // ext
return "";
}
Roman Tolchenov
committed
} // namespace DataHandling
} // namespace Mantid