summaryrefslogtreecommitdiff
path: root/DAnCE/tools/Artifact_Installation/Http_Installation_Handler.cpp
blob: cc06b5548d27e829c80a5e1bb9c521512ca41b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "Http_Installation_Handler.h"
#include "Installation_Repository_Manager.h"
#include "dance/Logger/Log_Macros.h"
#include "ace/Auto_Ptr.h"
#include "ace/INet/HTTP_URL.h"
#include "ace/INet/HTTP_ClientRequestHandler.h"
#include "ace/INet/String_IOStream.h"

namespace DAnCE
{
  class HTTPInstallationRequestHandler
    : public ACE::HTTP::ClientRequestHandler
  {
    public:
      HTTPInstallationRequestHandler (const ACE_CString& plan_uuid)
        : plan_uuid_ (plan_uuid) {}
      virtual ~HTTPInstallationRequestHandler () {}

    protected:
      virtual void handle_response (const ACE::HTTP::URL& url, const ACE::HTTP::Response& response)
        {
          if (!response.get_status ().is_ok ())
            {
              ACE::IOS::CString_OStream serr;
              if (response.get_status ().is_valid ())
                serr << "negative HTTP response " << (int)response.get_status ().get_status ()
                      << response.get_status ().get_reason ().c_str ();
              else
                serr << "invalid HTTP response";
              serr << " : url=" << url.to_string ().c_str ();

              throw Deployment::PlanError (plan_uuid_.c_str (), serr.str ().c_str ());
            }
        }

      virtual void handle_request_error (const ACE::HTTP::URL& url)
        {
          ACE::IOS::CString_OStream serr;
          serr << "Failed to handle request for " << url.to_string ().c_str ()
               << " (" << ACE_OS::last_error () << ')';

          throw Deployment::PlanError (plan_uuid_.c_str (), serr.str ().c_str ());
        }

      virtual void handle_connection_error (const ACE::HTTP::URL& url)
        {
          ACE::IOS::CString_OStream serr;
          serr << "Failed to set up connection for " << url.to_string ().c_str ()
               << " (" << ACE_OS::last_error () << ')';

          throw Deployment::PlanError (plan_uuid_.c_str (), serr.str ().c_str ());
        }

    private:
      ACE_CString plan_uuid_;
  };


  /*
   *  HttpInstallationHandler
   */

  ACE_STATIC_SVC_DEFINE (HttpInstallationHandlerSvc,
                         ACE_TEXT ("HttpInstallationHandler"),
                         ACE_SVC_OBJ_T,
                         &ACE_SVC_NAME (HttpInstallationHandlerSvc),
                         ACE_Service_Type::DELETE_THIS
                          | ACE_Service_Type::DELETE_OBJ,
                         0)
  ACE_FACTORY_DEFINE (Artifact_Installation_Handler, HttpInstallationHandlerSvc)

  HttpInstallationHandlerSvc::HttpInstallationHandlerSvc ()
    {
    }

  HttpInstallationHandlerSvc::~HttpInstallationHandlerSvc ()
    {
      THandlerSingleton::close ();
    }

  ArtifactInstallationHandler* HttpInstallationHandlerSvc::handler_instance ()
    {
      return THandlerSingleton::instance ();
    }

  int HttpInstallationHandlerSvc::Initializer (void)
  {
    return ACE_Service_Config::process_directive (ace_svc_desc_HttpInstallationHandlerSvc);
  }

  /*
   *  HttpInstallationHandler
   */

  const std::string HttpInstallationHandler::protocol ("http");

  HttpInstallationHandler::HttpInstallationHandler ()
    {
      DANCE_TRACE ( "HttpInstallationHandler::HttpInstallationHandler");
    }

  HttpInstallationHandler::~HttpInstallationHandler ()
    {
      DANCE_TRACE ( "HttpInstallationHandler::~HttpInstallationHandler");
    }

  const std::string& HttpInstallationHandler::protocol_prefix (void)
    {
      return HttpInstallationHandler::protocol;
    }

  void HttpInstallationHandler::initialize ()
    {
    }

  void HttpInstallationHandler::clear ()
    {
      // we're finished installing; close all cached connections
      ACE::HTTP::ClientRequestHandler::connection_cache ().close_all_connections ();
    }

  void HttpInstallationHandler::install (const std::string& plan_uuid,
                                         std::string& location,
                                         const TPropertyMap& properties)
    {
      InstallationRepository::auto_ptr repo (
        InstallationRepositoryManager::instance ()->open_repository (plan_uuid));

      if (repo)
        {
          ArtifactInstallationProperties propreader (properties, this->protocol_prefix());

          ACE::HTTP::URL http_url;
          if (!http_url.parse (location.c_str ()))
            {
              ACE::IOS::CString_OStream serr;
              serr << "failed to parse artifact location : "
                   << location;
              throw Deployment::PlanError (plan_uuid.c_str (), serr.str ().c_str ());
            }
          else
            {
              std::string path = propreader.get_property (
                  ArtifactInstallationProperties::PROP_NAME);
              if (path.empty ())
                path = this->basename (location, '/');
              bool exclusive = false;
              const std::string& propval = propreader.get_property (
                  ArtifactInstallationProperties::PROP_EXCLUSIVE);
              exclusive = (!propval.empty () && (propval == "yes" || propval == "true"));
              ACE_Auto_Ptr<ostream> pofs (repo->create_artifact(path, exclusive));
              if (pofs.get ()!=0 && (*pofs))
                {
                  HTTPInstallationRequestHandler request_handler (plan_uuid.c_str ());

                  http_url.open (request_handler);
                  if (request_handler.response_stream ())
                    {
                      // copy artifact content
                      (*pofs) << request_handler.response_stream ().rdbuf ();
                      // check state
                      if (!(*pofs))
                        {
                          ACE::IOS::CString_OStream serr;
                          serr << "failed to copy artifact from "
                              << location
                              << " to "
                              << path.c_str ();
                          throw Deployment::PlanError (plan_uuid.c_str (), serr.str ().c_str ());
                        }
                      location = path.c_str ();
                    }
                  else
                    {
                      // We should never get here since the request handler should already
                      // have thrown appropriate exceptions
                      ACE::IOS::CString_OStream serr;
                      serr << "Failed to handle request for " << http_url.to_string ().c_str ()
                          << " (" << ACE_OS::last_error () << ')';

                      throw Deployment::PlanError (plan_uuid.c_str (), serr.str ().c_str ());
                    }
                }
              else
                {
                  ACE::IOS::CString_OStream serr;
                  serr << "failed to create repository artifact version for "
                      << path;
                  throw Deployment::PlanError (plan_uuid.c_str (), serr.str ().c_str ());
                }
            }
        }
      else
        {
          throw Deployment::PlanError (plan_uuid.c_str (), "failed to open installation repository");
        }
    }

  void HttpInstallationHandler::remove (const std::string& plan_uuid,
                                        const std::string& location)
    {
      InstallationRepository::auto_ptr repo (
        InstallationRepositoryManager::instance ()->open_repository (plan_uuid));

      if (repo)
        {
          if (repo->has_artifact (location))
            {
              if (!repo->remove_artifact (location))
                {
                  std::string err = "failed to remove artifact : ";
                  err += location;
                  throw Deployment::PlanError (plan_uuid.c_str (), err.c_str ());
                }
            }
          else
            {
              std::string err = "cannot remove non-existing artifact : ";
              err += location;
              throw Deployment::PlanError (plan_uuid.c_str (), err.c_str ());
            }
        }
      else
        {
          throw Deployment::PlanError (plan_uuid.c_str (), "failed to open installation repository");
        }
    }

}