Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
CatalogPublish
algorithm. Refs #7636.
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef MANTID_ICAT_CATALOGPUBLISH_H | ||
#define MATIND_ICAT_CATALOGPUBLISH_H | ||
|
||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace ICat | ||
{ | ||
/** | ||
CatalogPublish is responsible for publishing user data to the data archive. | ||
@author Jay Rainey, ISIS Rutherford Appleton Laboratory | ||
@date 06/12/2013 | ||
Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://github.com/mantidproject/mantid>. | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class CatalogPublish : public API::Algorithm | ||
{ | ||
public: | ||
/// constructor | ||
CatalogPublish():API::Algorithm(){} | ||
/// Destructor | ||
~CatalogPublish(){} | ||
/// Algorithm's name for identification. | ||
virtual const std::string name() const { return "CatalogPublish"; } | ||
/// Algorithm's version for identification. | ||
virtual int version() const { return 1; } | ||
/// Algorithm's category for identification. | ||
virtual const std::string category() const { return "DataHandling\\CatalogPublish"; } | ||
|
||
private: | ||
/// Sets documentation strings for this algorithm | ||
virtual void initDocs(); | ||
/// Override algorithm initialisation method. | ||
void init(); | ||
/// Override algorithm execute method. | ||
void exec(); | ||
}; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "MantidICat/CatalogPublish.h" | ||
#include "MantidICat/CatalogAlgorithmHelper.h" | ||
|
||
#include "MantidAPI/FileProperty.h" | ||
#include "MantidAPI/WorkspaceProperty.h" | ||
#include "MantidDataObjects/Workspace2D.h" | ||
#include "MantidKernel/PropertyWithValue.h" | ||
|
||
#include <Poco/Net/HTTPSClientSession.h> | ||
#include <Poco/Net/SSLException.h> | ||
#include <Poco/Net/HTTPRequest.h> | ||
#include <Poco/Net/HTTPResponse.h> | ||
#include <Poco/Path.h> | ||
#include <Poco/StreamCopier.h> | ||
#include <Poco/URI.h> | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
namespace Mantid | ||
{ | ||
namespace ICat | ||
{ | ||
DECLARE_ALGORITHM(CatalogPublish) | ||
|
||
/// Sets documentation strings for this algorithm | ||
void CatalogPublish::initDocs() | ||
{ | ||
this->setWikiSummary("Allows the user to publish data to the catalog."); | ||
} | ||
|
||
/// Init method to declare algorithm properties | ||
void CatalogPublish::init() | ||
{ | ||
declareProperty(new Mantid::API::FileProperty("Filepath", "", Mantid::API::FileProperty::Load), "The file to publish."); | ||
declareProperty("CreateFileName","","The name to give to the file being saved"); | ||
} | ||
|
||
/// Execute the algorithm | ||
void CatalogPublish::exec() | ||
{ | ||
try | ||
{ | ||
std::string filePath = getPropertyValue("Filepath"); | ||
std::string createFileName = getPropertyValue("CreateFileName"); | ||
|
||
// Extracts the file name (e.g. CSP74683_ICPevent) from the file path. | ||
std::string dataFileName = Poco::Path(Poco::Path(filePath).getFileName()).getBaseName(); | ||
// Extracts the specific file name (e.g. CSP74683) from the file path. | ||
dataFileName = dataFileName.substr(0, dataFileName.find_first_of('_')); | ||
|
||
// Create a catalog & obtain the url to PUT (publish) the file to. | ||
std::string url = CatalogAlgorithmHelper().createCatalog()->getUploadURL(dataFileName, createFileName); | ||
Poco::URI uri(url); | ||
std::string path(uri.getPathAndQuery()); | ||
|
||
// Currently do not use any means of authentication. This should be updated IDS has signed certificate. | ||
const Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE); | ||
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context); | ||
|
||
// Send the HTTP request, and obtain the output stream to write to. E.g. the data to publish to the server. | ||
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_PUT, path, Poco::Net::HTTPMessage::HTTP_1_1); | ||
std::ostream& os = session.sendRequest(request); | ||
|
||
// Stream the contents of the file the user wants to publish & store it in file. | ||
std::ifstream file(filePath.c_str()); | ||
Poco::StreamCopier::copyStream(file, os); | ||
|
||
// Close the request by requesting a response. | ||
Poco::Net::HTTPResponse response; | ||
session.receiveResponse(response); | ||
} | ||
catch(Poco::Net::SSLException& error) | ||
{ | ||
throw std::runtime_error(error.displayText()); | ||
} | ||
catch(Poco::Exception& e) {} | ||
} | ||
} | ||
} |