CurlWrapper.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 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 terralib/ws/core/CurlWrapper.h
22 
23  \brief A Wrapper for Lib Curl.
24 
25  \author Emerson Moraes
26  \author Vinicius Campanha
27  */
28 
29 #ifndef __TERRALIB_WS_CORE_CURLWRAPPER_H
30 #define __TERRALIB_WS_CORE_CURLWRAPPER_H
31 
32 //STL
33 #include <memory>
34 #include <mutex>
35 #include <vector>
36 #include <string>
37 
38 #include "Config.h"
39 #include "../../common/progress/TaskProgress.h"
40 #include "../../core/uri/URI.h"
41 
42 namespace te
43 {
44  namespace ws
45  {
46  namespace core
47  {
48 
50  {
51  NOT_AUTH = 0,
54  };
55 
56  /*!
57  \class CurlWrapper
58 
59  \brief An abstraction to Lib Curl functions.
60  */
62  {
63  public:
64  CurlWrapper();
65  CurlWrapper(CurlWrapper&& other);
66 
67  virtual ~CurlWrapper();
68 
69  /*!
70  \brief Method to download a file retrieved from given URL and save into the specified file path.
71 
72  \param url URL that the file will be downloaded.
73  \param filePath file path where downloaded file will be saved.
74  */
75  virtual void downloadFile(const std::string& url, const std::string& filePath, te::common::TaskProgress* taskProgress = 0);
76 
77  /*!
78  \brief Method to download a file retrieved from given URL and save into the specified File.
79 
80  \param url URL that the file will be downloaded.
81  \param file File where downloaded file will be write.
82  */
83  virtual void downloadFile(const std::string& url, std::FILE* file, te::common::TaskProgress* taskProgress = 0);
84 
85 
86  /*!
87  \brief This method gets the message that will be used on Progress Bar when
88  this object is downloading some file.
89  */
90  virtual std::string getTaskMessage() const;
91 
92  /*!
93  \brief This method sets the message that will be used on Progress Bar when
94  this object is downloading some file.
95 
96  \param taskMessage new message.
97  */
98  virtual void setTaskMessage(const std::string& taskMessage);
99 
100  /*!
101  * \brief Method to request a post in a determined URI
102  * \param uri The URI with the address information to post
103  * \param postFields The parameters and values to post
104  * \param header A header to the post request
105  */
106  virtual void post(const te::core::URI& uri, const std::string &postFields, const::std::string &header);
107 
108  /*!
109  * \brief Method to request a put with a file in a determined URI
110  * \param uri The URI with the address information
111  * \param file Path The full path of the file to put
112  * \param header A header to the put request
113  */
114  virtual void putFile(const te::core::URI& uri, const std::string &filePath, const::std::string &header);
115 
116  /*!
117  * \brief Method to request a put a file in a determined URI
118  * \param uri The URI with the address information
119  * \param file Contains the file opened by the fstream
120  * \param header A header to the put request
121  */
122  virtual void putFile(const te::core::URI& uri, const std::fstream& file, const::std::string &header);
123 
124  /*!
125  * \brief Method to make a custom request, useful for performing a HTTP DELETE request.
126  * \param uri The URI with the address information
127  * \param request The custom request
128  */
129  virtual void customRequest(const te::core::URI& uri, const std::string& request, const std::string& body = std::string(""), const::std::string &header = std::string(""));
130 
131  /*!
132  * \brief Method to make a GET request.
133  * \param uri The URI with the address information
134  * \param buffer Where the server answer will be write.
135  */
136  virtual void get(const te::core::URI &uri, std::string& buffer);
137 
138 
139  /*!
140  * \brief Returns the last operation response code
141  * \return The response code
142  */
143  virtual const long responseCode() const;
144 
145  /*!
146  * \brief Returns the last operation response if the protocol allows it
147  * \return The response
148  */
149  virtual const std::string& response() const;
150 
151  /*!
152  * \brief Sets the authentication method that will be used on the requests.
153  *
154  * \param method (e. g.: Basic or Digest)
155  */
156  virtual void setAuthenticationMethod(const AuthenticationMethod& method);
157 
158  /*!
159  * \brief Gets current AuthenticationMethod.
160  *
161  * \return current AuthenticationMethod.
162  */
163  virtual AuthenticationMethod getAuthenticationMethod() const;
164 
165  /*!
166  * \brief Sets the user name that will be used when an athentication method is set.
167  *
168  * \param username
169  */
170  virtual void setUsername(const std::string& username);
171 
172  /*!
173  * \brief Gets the user name that will be used when an athentication method is set.
174  *
175  * \return username
176  */
177  virtual std::string getUsername() const;
178 
179  /*!
180  * \brief Sets the password that will be used when an athentication method is set.
181  *
182  * \param password
183  */
184  virtual void setPassword(const std::string& password);
185 
186  /*!
187  \brief Verify if the URI is reacheble.
188 
189  \exception te::ws::core::Exception Rise if the URI cannot be reached.
190  */
191  virtual void verifyURL(std::string url, uint32_t timeout) const;
192 
193  /*!
194  \brief Method to list files in a URI.
195 
196  \param url URL that the files will be listed.
197  */
198  virtual std::vector<std::string> listFiles(const te::core::URI& uri);
199 
200  protected:
201 
202  /*!
203  * \brief Reset the curl handle and the class members
204  */
205  virtual void clean();
206 
207  /*!
208  * \brief Adds HTTP Authentication parameters according to the current AuthenticationMethod.
209  */
210  virtual void addAuthParameters();
211 
212  /*!
213  \brief Private unsafe method to download a file retrieved from given URL and save into the specified File.
214 
215  \warning NOT thread safe, call getLock() to acquire lock before using.
216  \warning NOT CURL safe, call clean() before using
217  \param url URL that the file will be downloaded.
218  \param file File where downloaded file will be write.
219  */
220  virtual void downloadFile_(const std::string& url, std::FILE* file, te::common::TaskProgress* taskProgress = 0);
221 
222  /*!
223  \brief Private unsafe method to list files in a URI.
224 
225  \warning NOT thread safe, call getLock() to acquire lock before using.
226  \warning NOT CURL safe, call clean() before using
227  \param url URL that the files will be listed.
228  */
229  virtual std::vector<std::string> listFiles_(const te::core::URI& uri);
230 
231  void setOption(int option, std::string value);
232  void setOption(int option, char* value);
233  void setOption(int option, int value);
234 
235  std::unique_lock<std::mutex> getLock();
236 
237  private:
238  struct Impl;
239 
240  std::unique_ptr<Impl> m_pimpl;
241 
242  /*!
243  \class CurlGlobalScope
244 
245  \brief RAII class to init curl
246  */
248  {
249  public:
250  CurlGlobalScope();
251  ~CurlGlobalScope();
252 
253  CurlGlobalScope(const CurlGlobalScope& other) = delete;
254  CurlGlobalScope(CurlGlobalScope&& other) = delete;
255  CurlGlobalScope& operator=(const CurlGlobalScope& other) = delete;
256  CurlGlobalScope& operator=(CurlGlobalScope&& other) = delete;
257  };
258 
260  };
261 
262  }
263  }
264 }
265 
266 #endif
Configuration flags for the Terrralib WS Core module.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
An abstraction to Lib Curl functions.
Definition: CurlWrapper.h:61
std::unique_ptr< Impl > m_pimpl
Definition: CurlWrapper.h:238
URI C++ Library.
static CurlGlobalScope curlScope_
Definition: CurlWrapper.h:259
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
#define TEWSCOREEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:62