Skip to content

Commit

Permalink
Added CatalogPublish algorithm. Refs #7636.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawrainey committed Dec 6, 2013
1 parent 1105462 commit cc3a7ec
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/ICat/CMakeLists.txt
@@ -1,4 +1,5 @@
set ( SRC_FILES
src/CatalogPublish.cpp
src/CatalogAlgorithmHelper.cpp
src/CatalogDownloadDataFiles.cpp
src/CatalogGetDataFiles.cpp
Expand All @@ -22,6 +23,7 @@ set ( SRC_FILES
set ( SRC_UNITY_IGNORE_FILES )

set ( INC_FILES
inc/MantidICat/CatalogPublish.h
inc/MantidICat/CatalogAlgorithmHelper.h
inc/MantidICat/CatalogDownloadDataFiles.h
inc/MantidICat/CatalogGetDataFiles.h
Expand Down
59 changes: 59 additions & 0 deletions Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h
@@ -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
80 changes: 80 additions & 0 deletions Code/Mantid/Framework/ICat/src/CatalogPublish.cpp
@@ -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) {}
}
}
}

0 comments on commit cc3a7ec

Please sign in to comment.