1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CreateConfigFile.h"
21 #include "CreateConfigFileResponse.h"
23 #include "GetFileResponse.h"
25 #include "PutFileResponse.h"
26 #include "SimanSalomeServiceStub.h"
28 #include "SimanIO_Link.hxx"
30 #include <Environment.h>
31 #include <ServiceClient.h>
38 using namespace org_splat_ws_server_service_salome;
39 using namespace wso2wsf;
41 SimanIO_Link::SimanIO_Link(const char* theStudyID,
42 const char* theScenarioID, const char* theUserID)
43 : myStudy(theStudyID), myScenario(theScenarioID), myUser(theUserID)
47 bool SimanIO_Link::IsConnected()
53 // unification of the error handling in the RetrieveConf and StoreConf methods
54 #define CONFIGURSION_ERROR(message) {std::cerr<<message<<std::endl;}
56 SimanIO_Configuration SimanIO_Link::RetrieveConf()
58 SimanIO_Configuration aResult;
59 // for now use simple initial cfg file for testings purposes only
60 // in the future this file must be downloaded from the SIMAN server somehow
62 char* aSimanHost = getenv("SIMAN_WS_HOST");
63 if (aSimanHost == NULL)
65 CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
69 std::string aSimanWSHost(aSimanHost);
70 std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
72 Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
73 char* aClientHomeEnv = getenv("WSFCPP_HOME");
75 if (aClientHomeEnv == NULL)
77 CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
81 std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
82 SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
83 ServiceClient *client = aStub->getServiceClient();
85 //Set the ServiceClient options
86 client->engageModule(AXIS2_MODULE_ADDRESSING);
87 Options *options = client->getOptions();
88 axis2_options_t *axis2options = options->getAxis2Options();
89 axis2_options_set_action(axis2options, Environment::getEnv(), "urn:createConfigFile");
92 options->setTimeout(1000000);
93 options->setSoapVersion(AXIOM_SOAP12);
95 //CreateConfigFile operation
96 CreateConfigFile* aCreateConfigFileRequest = new CreateConfigFile();
98 aCreateConfigFileRequest->setArgs0(atoll(myStudy.c_str()));
99 aCreateConfigFileRequest->setArgs1(atoll(myScenario.c_str()));
100 aCreateConfigFileRequest->setArgs2(atoll(myUser.c_str()));
102 //std::cout<<"StudyId = " << atoll(myStudy.c_str()) << "\n";
103 //std::cout<<"ScenarioId = " << atoll(myScenario.c_str()) << "\n";
104 //std::cout<<"UserId = " << atoll(myUser.c_str()) << "\n";
106 CreateConfigFileResponse* aCreateConfigFileResponse = new CreateConfigFileResponse();
107 aCreateConfigFileResponse = aStub->createConfigFile(aCreateConfigFileRequest);
108 std::string aResponseFilePath = aCreateConfigFileResponse->get_return();
110 std::string aResponseFileName = aResponseFilePath.substr(aResponseFilePath.find_last_of("/\\")+1);
112 //Get the created config file to client
115 axis2_options_set_action(axis2options, Environment::getEnv(), "urn:getFile");
117 GetFile* aGetFileRequest = new GetFile();
118 aGetFileRequest->setArgs0(aResponseFilePath);
119 GetFileResponse* aGetFileResponse = new GetFileResponse();
120 aGetFileResponse = aStub->getFile(aGetFileRequest);
122 //Parse the response and get the filename
123 std::string aTmpDir = "/tmp";
124 std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
125 //std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myUser + "/download/";
126 std::string aClientFilePath = aClientFileDir + aResponseFileName;
128 //Create the directories
129 system(("mkdir -p " + aClientFileDir).c_str());
130 system(("chmod -Rf g+w " + aTmpDir + "/SimanSalome/").c_str());
132 //Download the siman-salome.conf file
133 std::ofstream outputStream;
134 outputStream.open(aClientFilePath.c_str(), std::ofstream::binary);
135 int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
136 int* aBufSize = new int[aBufferSize];
137 unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
138 outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
139 outputStream.flush();
140 outputStream.close();
141 delete[] aCopyBuffer;
143 //Parse the downloaded siman-salome.conf file
144 aResult.Parse(aClientFilePath.c_str());
146 //Set the actual file URLs and get these files onto client.
147 SimanIO_Configuration::ActivitiesIterator actIter(aResult);
148 for(; actIter.More(); actIter.Next()) {
149 SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
150 for(; aDocIter.More(); aDocIter.Next()) {
151 /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
152 SimanIO_Document::FilesIterator aFileIter(aDoc);
153 for(; aFileIter.More(); aFileIter.Next()) {
154 std::string aURL = aFileIter.URL();
155 std::string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
156 int startUnderlineIndex = aFileName.find_first_of("_") + 1;
157 std::string aSourceFileName = aFileName.substr(startUnderlineIndex);
159 //std::cout << "aURL = " << aURL << "\n";
160 std::string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
162 //only for test only for WIN
163 //replace(aURL.begin(),aURL.end(),'/','\\');
164 //end only for test only for WIN
167 //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
168 aGetFileRequest->setArgs0(aPathToVault + aURL);
169 aGetFileResponse = aStub->getFile(aGetFileRequest);
171 outputStream.open((aClientFileDir + aSourceFileName).c_str(), std::ofstream::binary);
172 int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
173 int* aBufSize = new int[aBufferSize];
174 unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
175 outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
176 outputStream.flush();
177 outputStream.close();
178 delete[] aCopyBuffer;
181 aFileIter.SetURL((aClientFileDir + aSourceFileName).c_str());
182 //std::cout << "New location of the files " << aClientFileDir + aFileName << "\n";
187 //delete the siman-salome.conf file.
188 // system(("rm " + aClientFilePath).c_str());
191 delete aCreateConfigFileRequest;
192 delete aCreateConfigFileResponse;
193 delete aGetFileResponse;
194 delete aGetFileRequest;
200 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
202 std::cout << "StoreConf method is started\n";
203 char* aSimanHost = getenv("SIMAN_WS_HOST");
204 if (aSimanHost == NULL)
206 CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
209 std::string aSimanWSHost(aSimanHost);
210 std::string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
212 Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
213 char* aClientHomeEnv = getenv("WSFCPP_HOME");
215 if (aClientHomeEnv == NULL)
217 CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
220 std::string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
221 SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
222 ServiceClient *client = aStub->getServiceClient();
224 //Set the ServiceClient options
225 client->engageModule(AXIS2_MODULE_ADDRESSING);
226 Options *options = client->getOptions();
227 axis2_options_t *axis2options = options->getAxis2Options();
228 axis2_options_set_action(axis2options, Environment::getEnv(), "urn:putFile");
231 options->setTimeout(1000000);
232 options->setSoapVersion(AXIOM_SOAP12);
234 //Upload the file onto server
235 PutFile* aPutFileRequest = new PutFile();
236 PutFileResponse* aPutFileResponse = new PutFileResponse();
238 //Prepare salome-siman.conf file
239 std::string aTmpDir = "/tmp";
240 std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
241 //std::string aClientFileDir = aTmpDir + "/SimanSalome/" + myUser + "/download/";
242 std::string aClientFilePath = aClientFileDir + "salome-siman.conf";
243 std::ofstream aSalomeSimanFile;
244 aSalomeSimanFile.open(aClientFilePath.c_str());
246 //Get the actual file URLs and put these files onto server.
247 SimanIO_Configuration::ActivitiesIterator actIter(theConf);
249 for(; actIter.More(); actIter.Next()) {
250 SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
251 for(; aDocIter.More(); aDocIter.Next()) {
252 /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
253 SimanIO_Document::FilesIterator aFileIter(aDoc);
254 for(; aFileIter.More(); aFileIter.Next()) {
255 std::string aURL = aFileIter.URL();
256 std::string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
257 std::cout << "aURL = " << aURL << "\n";
258 std::cout << "aFileName= " << aFileName << "\n";
259 //get size of the input file
260 std::ifstream inputStream (aURL.c_str(), std::ifstream::binary);
261 inputStream.seekg(0,std::ifstream::end);
262 int aSize = inputStream.tellg();
263 inputStream.seekg(0);
267 aCopyBuffer = new char [aSize];
268 inputStream.read(aCopyBuffer,aSize);
269 std::cout << "Upload a file with the size" << aSize << "\n";
270 axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
272 delete[] aCopyBuffer;
275 aPutFileRequest->setArgs0(aRequestDH);
276 aPutFileRequest->setArgs1(aFileName);
277 aPutFileRequest->setArgs2(atoll(myUser.c_str()));
279 aPutFileResponse = aStub->putFile(aPutFileRequest);
281 std::cout << "End of upload. New file locaton is: " << aPutFileResponse->get_return() << "\n";
283 //Write information into salome-siman.conf file
284 std::ostringstream activityId;
285 std::ostringstream documentId;
286 activityId << actIter.ActivityId();
287 documentId << aDocIter.DocId();
288 aSalomeSimanFile << myScenario + "," + myUser + "," + activityId.str() + "," + documentId.str() + "," + aPutFileResponse->get_return() + "\n";
289 std::cout << "Line in the SalomeSimanFile " << myScenario + "," + myUser + "," + activityId.str() + "," + documentId.str() + "," + aPutFileResponse->get_return() <<"\n";
290 std::cout <<"==========\n";
296 aSalomeSimanFile.close();
298 //Put the salome-siman.conf file onto server
299 std::ifstream inputStream (aClientFilePath.c_str(), std::ifstream::binary);
300 inputStream.seekg(0,std::ifstream::end);
301 int aSize = inputStream.tellg();
302 inputStream.seekg(0);
305 aCopyBuffer = new char [aSize];
306 inputStream.read(aCopyBuffer,aSize);
307 axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
309 delete[] aCopyBuffer;
312 aPutFileRequest->setArgs0(aRequestDH);
313 aPutFileRequest->setArgs1("salome-siman.conf");
314 aPutFileRequest->setArgs2(atoll(myUser.c_str()));
316 aPutFileResponse = aStub->putFile(aPutFileRequest);
319 axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
321 CheckIn* aCheckInRequest = new CheckIn();
322 CheckInResponse* aCheckInResponse = new CheckInResponse();
323 aCheckInRequest->setArgs0(aPutFileResponse->get_return());
324 aCheckInRequest->setArgs1(atoll(myScenario.c_str()));
325 aCheckInRequest->setArgs2(atoll(myUser.c_str()));
326 aCheckInResponse = aStub->checkIn(aCheckInRequest);
328 //Delete the directories
329 // system(("rm -rf " + aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/").c_str());
332 delete aPutFileRequest;
333 delete aPutFileResponse;
334 delete aCheckInRequest;
335 delete aCheckInResponse;