GeopackagePublisher.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9 
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19 
20 /*!
21 \file src/terraMobilePlugin/core/GeoPackagePublisher.cpp
22 
23 \brief This file is used to Publisher operation.
24 */
25 
26 //TerraLib
27 #include "GeopackagePublisher.h"
28 #include "../../../../core/encoding/CharEncoding.h"
29 
30 // libcurl
31 #include <curl/curl.h>
32 
33 // Boost
34 #include <boost/foreach.hpp>
35 #include <boost/property_tree/ptree.hpp>
36 #include <boost/property_tree/json_parser.hpp>
37 #include <boost/uuid/random_generator.hpp>
38 #include <boost/uuid/uuid_io.hpp>
39 
41 
43 
44 int progressFunc(void *bar, double donwTotal, double downNow, double upTotal, double upNow)
45 {
47  {
48  te::qt::plugins::terramobile::GeopackagePublisher::m_staticPointer->emitSignal(downNow, donwTotal);
49  }
51  {
52  te::qt::plugins::terramobile::GeopackagePublisher::m_staticPointer->emitSignal(upNow, upTotal);
53  }
54 
55  return 0;
56 }
57 
59 {
60  m_staticPointer = this;
61 }
62 
64 {
65 
66 }
67 
69 {
70  return m_errorMessage;
71 }
72 
73 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
74 {
75  ((std::string*)userp)->append((char*)contents, size * nmemb);
76  return size * nmemb;
77 }
78 
80 {
81  url += "/listprojects";
82 
83  CURL *curl;
84  CURLcode res;
85 
86  struct curl_httppost *formpost = NULL;
87  struct curl_httppost *lastptr = NULL;
88  struct curl_slist *headerlist = NULL;
89  static const char buf[] = "Expect:";
90 
91  curl_global_init(CURL_GLOBAL_ALL);
92 
93  /* Fill in the file upload field */
94  curl_formadd(&formpost,
95  &lastptr,
96  CURLFORM_COPYNAME, "user",
97  CURLFORM_COPYCONTENTS, "userName",
98  CURLFORM_END);
99 
100  curl_formadd(&formpost,
101  &lastptr,
102  CURLFORM_COPYNAME, "password",
103  CURLFORM_COPYCONTENTS, "password",
104  CURLFORM_END);
105 
106  curl_formadd(&formpost,
107  &lastptr,
108  CURLFORM_COPYNAME, "projectStatus",
109  CURLFORM_COPYCONTENTS, "1",
110  CURLFORM_END);
111 
112  std::string readBuffer;
113 
114  curl = curl_easy_init();
115 
116  headerlist = curl_slist_append(headerlist, buf);
117 
118  if (curl)
119  {
120  /* what URL that receives this POST */
121  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
122 
123  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
124 
125  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
126 
127  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
128 
129  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
130 
131  /* Perform the request, res will get the return code */
132  res = curl_easy_perform(curl);
133 
134  /* Check for errors */
135  if (res != CURLE_OK)
136  {
137  m_errorMessage = curl_easy_strerror(res);
138  throw;
139  }
140 
141  /* always cleanup */
142  curl_easy_cleanup(curl);
143 
144  /* then cleanup the formpost chain */
145  curl_formfree(formpost);
146  /* free slist */
147  curl_slist_free_all(headerlist);
148  }
149 
150  return readJSONInfo(readBuffer);
151 }
152 
153 size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
154 {
155  size_t written;
156  written = fwrite(ptr, size, nmemb, stream);
157  return written;
158 }
159 
161 {
163 
164  url += "/downloadproject";
165 
166  CURL *curl;
167  CURLcode res;
168 
169  FILE *fp;
170 
171  std::string outfilename = pathDir + "/" + gpkgFile.m_name;
172 
173  struct curl_httppost *formpost = NULL;
174  struct curl_httppost *lastptr = NULL;
175  struct curl_slist *headerlist = NULL;
176  static const char buf[] = "Expect:";
177 
178  curl_global_init(CURL_GLOBAL_ALL);
179 
180  /* Fill in the file upload field */
181  curl_formadd(&formpost,
182  &lastptr,
183  CURLFORM_COPYNAME, "user",
184  CURLFORM_COPYCONTENTS, "userName",
185  CURLFORM_END);
186 
187  curl_formadd(&formpost,
188  &lastptr,
189  CURLFORM_COPYNAME, "password",
190  CURLFORM_COPYCONTENTS, "password",
191  CURLFORM_END);
192 
193  curl_formadd(&formpost,
194  &lastptr,
195  CURLFORM_COPYNAME, "projectId",
196  CURLFORM_COPYCONTENTS, gpkgFile.m_objId.c_str(),
197  CURLFORM_END);
198 
199  curl_formadd(&formpost,
200  &lastptr,
201  CURLFORM_COPYNAME, "projectStatus",
202  CURLFORM_COPYCONTENTS, "1",
203  CURLFORM_END);
204 
205  curl_formadd(&formpost,
206  &lastptr,
207  CURLFORM_COPYNAME, "projectName",
208  CURLFORM_COPYCONTENTS, gpkgFile.m_name.c_str() ,
209  CURLFORM_END);
210 
211 
212  curl = curl_easy_init();
213 
214  headerlist = curl_slist_append(headerlist, buf);
215 
216  if (curl)
217  {
218  fp = fopen(te::core::CharEncoding::fromUTF8(outfilename).c_str(), "wb");
219 
220  /* what URL that receives this POST */
221  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
222 
223  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
224 
225  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
226 
227  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
228 
229  curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
230 
231  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
232 
233  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
234 
235  curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progressFunc);
236 
237  /* Perform the request, res will get the return code */
238  res = curl_easy_perform(curl);
239 
240  /* Check for errors */
241  if (res != CURLE_OK)
242  {
243  m_errorMessage = curl_easy_strerror(res);
245  throw;
246  }
247 
248  /* always cleanup */
249  curl_easy_cleanup(curl);
250 
251  /* then cleanup the formpost chain */
252  curl_formfree(formpost);
253 
254  /* free slist */
255  curl_slist_free_all(headerlist);
256 
257  fclose(fp);
258  }
259 
261 }
262 
263 void te::qt::plugins::terramobile::GeopackagePublisher::uploadGeopackageFile(std::string url, std::string filePath, std::string fileName)
264 {
266 
267  url += "/uploadproject";
268 
269  CURL *curl;
270  CURLcode res;
271 
272  struct curl_httppost *formpost = NULL;
273  struct curl_httppost *lastptr = NULL;
274  struct curl_slist *headerlist = NULL;
275  static const char buf[] = "Expect:";
276 
277  curl_global_init(CURL_GLOBAL_ALL);
278 
279  /* Fill in the file upload field */
280  curl_formadd(&formpost,
281  &lastptr,
282  CURLFORM_COPYNAME, "user",
283  CURLFORM_COPYCONTENTS, "userName",
284  CURLFORM_END);
285 
286  curl_formadd(&formpost,
287  &lastptr,
288  CURLFORM_COPYNAME, "password",
289  CURLFORM_COPYCONTENTS, "password",
290  CURLFORM_END);
291 
292  curl_formadd(&formpost,
293  &lastptr,
294  CURLFORM_COPYNAME, "fileName",
295  CURLFORM_COPYCONTENTS, fileName.c_str(),
296  CURLFORM_END);
297 
298  curl_formadd(&formpost,
299  &lastptr,
300  CURLFORM_COPYNAME, "file",
301  CURLFORM_FILE, filePath.c_str(),
302  CURLFORM_END);
303 
304  curl = curl_easy_init();
305 
306  headerlist = curl_slist_append(headerlist, buf);
307 
308  if (curl)
309  {
310  /* what URL that receives this POST */
311  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
312 
313  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
314 
315  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
316 
317  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
318 
319  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
320 
321  curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progressFunc);
322 
323  /* Perform the request, res will get the return code */
324  res = curl_easy_perform(curl);
325 
326  /* Check for errors */
327  if (res != CURLE_OK)
328  {
329  m_errorMessage = curl_easy_strerror(res);
331  throw;
332  }
333 
334  /* always cleanup */
335  curl_easy_cleanup(curl);
336 
337  /* then cleanup the formpost chain */
338  curl_formfree(formpost);
339 
340  /* free slist */
341  curl_slist_free_all(headerlist);
342  }
343 
345 }
346 
348 {
349  std::stringstream ss(stream);
350 
352 
353  try
354  {
355  boost::property_tree::ptree pt;
356  boost::property_tree::json_parser::read_json(ss, pt);
357 
358  BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("projects"))
359  {
361 
362  gpkgFile.m_name = v.second.get<std::string>("project_name");
363  gpkgFile.m_status = v.second.get<std::string>("project_status");
364  gpkgFile.m_objId = v.second.get<std::string>("project_id");
365  gpkgFile.m_desc = v.second.get<std::string>("project_description");
366 
367  gpkgFiles.push_back(gpkgFile);
368  }
369  }
370  catch (boost::property_tree::json_parser::json_parser_error &je)
371  {
372  m_errorMessage = je.message();
373  }
374  catch (std::exception const& e)
375  {
376  m_errorMessage = e.what();
377  }
378 
379  return gpkgFiles;
380 }
381 
383 {
384  std::string msg = "";
385 
387  {
388  msg = "Downloading Geopackage File...";
389  }
391  {
392  msg = "Uploading Geopackage File...";
393  }
394 
395  emit setCurrentStep(curStep, totalStep, msg);
396 }
GeopackageFiles readJSONInfo(std::string stream)
void uploadGeopackageFile(std::string url, std::string filePath, std::string fileName)
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
static GeopackagePublisherOperation m_gpkgOpType
void downloadGeopackageFile(std::string url, GeopackageFile gpkgFile, std::string pathDir)
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
void setCurrentStep(double curStep, double totalStep, std::string msg)
int progressFunc(void *bar, double donwTotal, double downNow, double upTotal, double upNow)
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
std::vector< GeopackageFile > GeopackageFiles
void emitSignal(double curStep, double totalStep)