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 
36 #include "Config.h"
37 #include "../../common/progress/TaskProgress.h"
38 #include "../../core/uri/URI.h"
39 
40 namespace te
41 {
42  namespace ws
43  {
44  namespace core
45  {
46 
48  {
49  NOT_AUTH = 0,
52  };
53 
54  /*!
55  \class CurlWrapper
56 
57  \brief An abstraction to Lib Curl functions.
58  */
60  {
61  public:
62  CurlWrapper();
63 
64  virtual ~CurlWrapper();
65 
66  /*!
67  \brief Method to download a file retrieved from given URL and save into the specified file path.
68 
69  \param url URL that the file will be downloaded.
70  \param filePath file path where downloaded file will be saved.
71  */
72  virtual void downloadFile(const std::string& url, const std::string& filePath, te::common::TaskProgress* taskProgress = 0);
73 
74  /*!
75  \brief Method to download a file retrieved from given URL and save into the specified File.
76 
77  \param url URL that the file will be downloaded.
78  \param file File where downloaded file will be write.
79  */
80  virtual void downloadFile(const std::string& url, std::FILE* file, te::common::TaskProgress* taskProgress = 0);
81 
82 
83  /*!
84  \brief This method gets the message that will be used on Progress Bar when
85  this object is downloading some file.
86  */
87  virtual std::string getTaskMessage() const;
88 
89  /*!
90  \brief This method sets the message that will be used on Progress Bar when
91  this object is downloading some file.
92 
93  \param taskMessage new message.
94  */
95  virtual void setTaskMessage(const std::string& taskMessage);
96 
97  /*!
98  * \brief Method to request a post in a determined URI
99  * \param uri The URI with the address information to post
100  * \param postFields The parameters and values to post
101  * \param header A header to the post request
102  */
103  virtual void post(const te::core::URI& uri, const std::string &postFields, const::std::string &header);
104 
105  /*!
106  * \brief Method to request a put with a file in a determined URI
107  * \param uri The URI with the address information
108  * \param file Path The full path of the file to put
109  * \param header A header to the put request
110  */
111  virtual void putFile(const te::core::URI& uri, const std::string &filePath, const::std::string &header);
112 
113  /*!
114  * \brief Method to request a put a file in a determined URI
115  * \param uri The URI with the address information
116  * \param file Contains the file opened by the fstream
117  * \param header A header to the put request
118  */
119  virtual void putFile(const te::core::URI& uri, const std::fstream& file, const::std::string &header);
120 
121  /*!
122  * \brief Method to make a custom request, useful for performing a HTTP DELETE request.
123  * \param uri The URI with the address information
124  * \param request The custom request
125  */
126  virtual void customRequest(const te::core::URI& uri, const std::string& request, const std::string& body = std::string(""), const::std::string &header = std::string(""));
127 
128  /*!
129  * \brief Method to make a GET request.
130  * \param uri The URI with the address information
131  * \param buffer Where the server answer will be write.
132  */
133  virtual void get(const te::core::URI &uri, std::string& buffer);
134 
135 
136  /*!
137  * \brief Returns the last operation response code
138  * \return The response code
139  */
140  virtual const long responseCode() const;
141 
142  /*!
143  * \brief Returns the last operation response if the protocol allows it
144  * \return The response
145  */
146  virtual const std::string& response() const;
147 
148  /*!
149  * \brief Sets the authentication method that will be used on the requests.
150  *
151  * \param method (e. g.: Basic or Digest)
152  */
153  virtual void setAuthenticationMethod(const AuthenticationMethod& method);
154 
155  /*!
156  * \brief Gets current AuthenticationMethod.
157  *
158  * \return current AuthenticationMethod.
159  */
160  virtual AuthenticationMethod getAuthenticationMethod() const;
161 
162  /*!
163  * \brief Sets the user name that will be used when an athentication method is set.
164  *
165  * \param username
166  */
167  virtual void setUsername(const std::string& username);
168 
169  /*!
170  * \brief Gets the user name that will be used when an athentication method is set.
171  *
172  * \return username
173  */
174  virtual std::string getUsername() const;
175 
176  /*!
177  * \brief Sets the password that will be used when an athentication method is set.
178  *
179  * \param password
180  */
181  virtual void setPassword(const std::string& password);
182 
183  protected:
184 
185  /*!
186  * \brief Reset the curl handle and the class members
187  */
188  virtual void clean();
189 
190  /*!
191  * \brief Adds HTTP Authentication parameters according to the current AuthenticationMethod.
192  */
193  virtual void addAuthParameters();
194 
195  private:
196  struct Impl;
197 
198  std::unique_ptr<Impl> m_pimpl;
199  };
200 
201  }
202  }
203 }
204 
205 #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:59
std::unique_ptr< Impl > m_pimpl
Definition: CurlWrapper.h:196
URI C++ Library.
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