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>
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
{
Roman Tolchenov
committed
DECLARE_ARCHIVESEARCH(ISISDataArchive,ISISDataSearch);
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
30
31
32
33
34
35
36
37
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
* @return The path to the file or empty string in case of error.
*/
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;
}
} // namespace DataHandling
} // namespace Mantid