Salome HOME
7dada805ff54ffdf73d28e38d28c89c5c4093b08
[tools/simanio.git] / src / SimanIO_Link.cxx
1 #include "CreateConfigFile.h"
2 #include "CreateConfigFileResponse.h"
3 #include "GetFile.h"
4 #include "GetFileResponse.h"
5 #include "PutFile.h"
6 #include "PutFileResponse.h"
7 #include "SimanSalomeServiceStub.h"
8
9 #include <Environment.h>
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 #include <ServiceClient.h>
14 #include <SimanIO_Link.hxx>
15
16
17 #include <algorithm>
18
19
20 using namespace std;
21 using namespace org_splat_ws_server_service_salome;
22 using namespace wso2wsf;
23
24 SimanIO_Link::SimanIO_Link(const char* theStudyID, 
25     const char* theScenarioID, const char* theUserID)
26 : myStudy(theStudyID), myScenario(theScenarioID), myUser(theUserID)
27 {
28 }
29
30 bool SimanIO_Link::IsConnected()
31 {
32   // TO DO
33   return true;
34 }
35
36 // unification of the error handling in the RetrieveConf and StoreConf  methods
37 #define CONFIGURSION_ERROR(message) {cerr<<message<<endl;}
38
39 SimanIO_Configuration SimanIO_Link::RetrieveConf()
40 {
41   SimanIO_Configuration aResult;
42   // for now use simple initial cfg file for testings purposes only
43   // in the future this file must be downloaded from the SIMAN server somehow
44   //const char* aCFGFile = "/dn20/salome/mpv/siman/text1.conf";
45   
46   char* aSimanHost = getenv("SIMAN_WS_HOST");
47   if (aSimanHost == NULL)
48   {
49     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
50     return aResult;
51   }
52   
53   string aSimanWSHost(aSimanHost);
54   string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
55   
56   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
57   char* aClientHomeEnv = getenv("WSFCPP_HOME");
58   
59   if (aClientHomeEnv == NULL)
60   {
61     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
62     return aResult;
63   }
64   
65   string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
66   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
67   ServiceClient *client = aStub->getServiceClient();
68   
69   //Set the ServiceClient options
70   client->engageModule(AXIS2_MODULE_ADDRESSING);
71   Options *options = client->getOptions();
72   axis2_options_t *axis2options = options->getAxis2Options();
73   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:createConfigFile");
74   
75   //MTOM options
76   options->setTimeout(1000000);
77   options->setSoapVersion(AXIOM_SOAP12);
78   
79   //CreateConfigFile operation
80   CreateConfigFile* aCreateConfigFileRequest = new CreateConfigFile();
81   
82   aCreateConfigFileRequest->setArgs0(atoll(myStudy.c_str()));
83   aCreateConfigFileRequest->setArgs1(atoll(myScenario.c_str()));
84   aCreateConfigFileRequest->setArgs2(atoll(myUser.c_str()));
85   
86   //cout<<"StudyId = " << atoll(myStudy.c_str()) << "\n";
87   //cout<<"ScenarioId = " << atoll(myScenario.c_str()) << "\n";
88   //cout<<"UserId = " << atoll(myUser.c_str()) << "\n";
89   
90   CreateConfigFileResponse* aCreateConfigFileResponse = new CreateConfigFileResponse();
91   aCreateConfigFileResponse = aStub->createConfigFile(aCreateConfigFileRequest);
92   
93   string aResponseFilePath = aCreateConfigFileResponse->get_return();
94   
95   //cout << "Response file path = " << aResponseFilePath << "\n";
96   
97   string aResponseFileName = aResponseFilePath.substr(aResponseFilePath.find_last_of("/\\")+1);
98   
99   //Get the created config file to client
100   
101   //Set the options
102   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:getFile");
103   
104   GetFile* aGetFileRequest = new GetFile();
105   aGetFileRequest->setArgs0(aResponseFilePath);
106   GetFileResponse* aGetFileResponse = new GetFileResponse();
107   aGetFileResponse = aStub->getFile(aGetFileRequest);
108   
109   //Parse the response and get the filename
110   string aTmpDir = "/tmp";
111   string aClientFileDir = aTmpDir + "/SimanSalome/" + myUser + "/download/";
112   string aClientFilePath = aClientFileDir + aResponseFileName;
113   
114   //Cretae the directories
115   system(("mkdir -p " + aClientFileDir).c_str());
116   
117   //Download the siman-salome.conf file
118   ofstream outputStream;
119   outputStream.open(aClientFilePath.c_str(), ofstream::binary);
120   int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
121   int* aBufSize = new int[aBufferSize];
122   unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
123   outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
124   outputStream.flush();
125   outputStream.close();
126   delete[] aCopyBuffer;
127   
128   //Parse the downloaded siman-salome.conf file
129   aResult.Parse(aClientFilePath.c_str());
130   
131   //Set the actual file URLs and get these files onto client.
132   SimanIO_Configuration::ActivitiesIterator actIter(aResult);
133   for(; actIter.More(); actIter.Next()) {
134     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
135     for(; aDocIter.More(); aDocIter.Next()) {
136       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
137       SimanIO_Document::FilesIterator aFileIter(aDoc);
138       for(; aFileIter.More(); aFileIter.Next()) {
139         string aURL = aFileIter.URL();
140         string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
141         int startUnderlineIndex = aFileName.find_first_of("_") + 1;
142         string aSourceFileName = aFileName.substr(startUnderlineIndex);
143         
144         //cout << "aURL = " << "http://" << aSimanWSHost << "/repository/" << aURL << "\n";
145         string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
146         
147         //only for test only for WIN
148         //replace(aURL.begin(),aURL.end(),'/','\\');
149         //end only for test only for WIN
150         
151         //cout << "Path to vault" << aPathToVault + aURL << "\n";
152         
153         //Get the files
154         //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
155         aGetFileRequest->setArgs0(aPathToVault + aURL);
156         aGetFileResponse = aStub->getFile(aGetFileRequest);
157         
158         outputStream.open((aClientFileDir + aSourceFileName).c_str(), ofstream::binary);
159         int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
160         int* aBufSize = new int[aBufferSize];
161         unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
162         outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
163         outputStream.flush();
164         outputStream.close();
165         delete[] aCopyBuffer;
166
167         //Set new URLs
168         aFileIter.SetURL((aClientFileDir + aFileName).c_str());
169         //cout << "New location of the files " << aClientFileDir + aFileName << "\n";
170       }//aFileIter
171     }//aDocIter
172   }//actIter
173
174
175   //Delete objects
176   delete aCreateConfigFileRequest;
177   delete aCreateConfigFileResponse;
178   delete aGetFileResponse;
179   delete aGetFileRequest;
180   delete aStub;
181   
182   return aResult;
183 }
184
185 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
186 {
187   char* aSimanHost = getenv("SIMAN_WS_HOST");
188   if (aSimanHost == NULL)
189   {
190     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
191   }
192   
193   string aSimanWSHost(aSimanHost);
194   string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
195   
196   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
197   char* aClientHomeEnv = getenv("WSFCPP_HOME");
198   
199   if (aClientHomeEnv == NULL)
200   {
201     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
202   }
203   
204   string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
205   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
206   ServiceClient *client = aStub->getServiceClient();
207   
208   //Set the ServiceClient options
209   client->engageModule(AXIS2_MODULE_ADDRESSING);
210   Options *options = client->getOptions();
211   axis2_options_t *axis2options = options->getAxis2Options();
212   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:putFile");
213   
214   //MTOM options
215   options->setTimeout(1000000);
216   options->setSoapVersion(AXIOM_SOAP12);
217   
218   //Upload the file onto server
219   PutFile* aPutFileRequest = new PutFile();
220   PutFileResponse* aPutFileResponse = new PutFileResponse();
221   
222   //Prepare salome-siman.conf file
223   string aTmpDir = "/tmp";
224   string aClientFileDir = aTmpDir + "/SimanSalome/" + myUser + "/download/";
225   string aClientFilePath = aClientFileDir + "salome-siman.conf";
226   ofstream aSalomeSimanFile;
227   aSalomeSimanFile.open(aClientFilePath.c_str());
228   
229   //Get the actual file URLs and put these files onto server.
230   SimanIO_Configuration::ActivitiesIterator actIter(theConf);
231
232   for(; actIter.More(); actIter.Next()) {
233     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
234     for(; aDocIter.More(); aDocIter.Next()) {
235       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
236       SimanIO_Document::FilesIterator aFileIter(aDoc);
237       for(; aFileIter.More(); aFileIter.Next()) {
238         string aURL = aFileIter.URL();
239         string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
240         cout << "aURL = " << aURL << "\n";
241         cout << " aFileName= " << aFileName << "\n";
242         //get size of the input file
243         ifstream inputStream (aURL.c_str(), ifstream::binary);
244         inputStream.seekg(0,ifstream::end);
245         int aSize = inputStream.tellg();
246         inputStream.seekg(0);
247         
248         //Upload action
249         char* aCopyBuffer;
250         aCopyBuffer = new char [aSize];
251         inputStream.read(aCopyBuffer,aSize);
252         axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
253         inputStream.close();
254         delete[] aCopyBuffer;
255
256         //Set arguments
257         aPutFileRequest->setArgs0(aRequestDH);
258         aPutFileRequest->setArgs1(aFileName);
259         aPutFileRequest->setArgs2(atoll(myUser.c_str()));
260         
261         aPutFileResponse = aStub->putFile(aPutFileRequest);
262         
263         //Write information into salome-siman.conf file
264         ostringstream activityId;
265         ostringstream documentId;
266         activityId << actIter.ActivityId();
267         documentId << aDocIter.DocId();
268         aSalomeSimanFile << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() + "\n";
269
270       }//aFileIter
271     }//aDocIter
272   }//actIter
273
274   aSalomeSimanFile.close();
275   
276   //Put the salome-siman.conf file onto server
277   ifstream inputStream (aClientFilePath.c_str(), ifstream::binary);
278   inputStream.seekg(0,ifstream::end);
279   int aSize = inputStream.tellg();
280   inputStream.seekg(0);
281   
282   char* aCopyBuffer;
283   aCopyBuffer = new char [aSize];
284   inputStream.read(aCopyBuffer,aSize);
285   axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
286   inputStream.close();
287   delete[] aCopyBuffer;
288
289   //Set arguments
290   aPutFileRequest->setArgs0(aRequestDH);
291   aPutFileRequest->setArgs1("salome-siman.conf");
292   aPutFileRequest->setArgs2(atoll(myUser.c_str()));
293
294   aPutFileResponse = aStub->putFile(aPutFileRequest);
295
296   //Check in operation
297   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
298   
299   CheckIn* aCheckInRequest = new CheckIn();
300   CheckInResponse* aCheckInResponse = new CheckInResponse();
301   aCheckInRequest->setArgs0(aPutFileResponse->get_return());
302   aCheckInRequest->setArgs1(atoll(myUser.c_str()));
303   aCheckInResponse = aStub->checkIn(aCheckInRequest);
304   
305
306 }