Fawkes API  Fawkes Development Version
webview_reqproc.cpp
1 
2 /***************************************************************************
3  * webview_reqproc.cpp - Webview ROS request processor
4  *
5  * Created: Fri May 06 17:31:35 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "webview_reqproc.h"
24 #include <core/exception.h>
25 #include <logging/logger.h>
26 #include <webview/error_reply.h>
27 #include <fawkes_msgs/WebviewProcessRequest.h>
28 
29 using namespace fawkes;
30 
31 /** @class ROSWebviewRequestProcessor "webview_reqproc.h"
32  * Convert webview requests to ROS service calls.
33  * This request processor calls a ROS service to process the request and
34  * produce the reply. This reply is then passed back to webview.
35  *
36  * This class requires the webview_msgs ROS package to be available.
37  *
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param nh node handle to create service client handle
43  * @param logger logger for log output
44  * @param baseurl Base URL this processor is registered for
45  * @param srv_name the ROS service name to query for requests
46  */
48  Logger *logger,
49  std::string &baseurl,
50  std::string &srv_name)
51 {
52  __baseurl = baseurl;
53  __srv_name = srv_name;
54  __logcomp = std::string("ROSWebviewRP[") + srv_name + "]";
55 
56  __srv_client = nh->serviceClient<fawkes_msgs::WebviewProcessRequest>(srv_name);
57 }
58 
59 /** Destructor. */
61 {
62  __srv_client.shutdown();
63 }
64 
67 {
68  //logger->log_debug(__logcomp.c_str(), "Processing %s", url);
69 
70  fawkes_msgs::WebviewProcessRequest srv;
71  srv.request.url = request->url();
72  //srv.request.method = method;
73  //srv.request.version = version;
74  //srv.request.upload_data =
75  // std::vector<uint8_t>((uint8_t *)upload_data,
76  // (uint8_t *)&upload_data[*upload_data_size]);
77 
78  if (! __srv_client.exists()) {
80  "Service %s is no longer available",
81  __srv_name.c_str());
82 
83  } else if (__srv_client.call(srv)) {
84  if (srv.response.code == WebReply::HTTP_OK) {
85  WebReply *r = NULL;
86  if (srv.response.wrap_in_page) {
87  WebPageReply *pr =
88  new WebPageReply(srv.response.title, srv.response.body);
89  pr->set_html_header(srv.response.html_header);
90  r = pr;
91  } else {
92  r = new StaticWebReply(WebReply::HTTP_OK, srv.response.body);
93  }
94 
95  std::vector<std::string>::iterator h;
96  for (h = srv.response.headers.begin(); h != srv.response.headers.end(); ++h)
97  {
98  try {
99  r->add_header(*h);
100  } catch (Exception &e) {
101  // ignore
102  }
103  }
104 
105  return r;
106  } else {
107  return new WebErrorPageReply((WebReply::Code)srv.response.code,
108  "Execution of service %s failed: %s",
109  __srv_name.c_str(),
110  srv.response.error.c_str());
111  }
112  } else {
114  "Execution of service %s failed",
115  __srv_name.c_str());
116  }
117 
118  // should not happen...
119  return NULL;
120 }
virtual void set_html_header(std::string h)
Set HTML header text.
Definition: page_reply.cpp:94
Fawkes library namespace.
ROSWebviewRequestProcessor(fawkes::LockPtr< ros::NodeHandle > nh, fawkes::Logger *logger, std::string &baseurl, std::string &srv_name)
Constructor.
void add_header(std::string header, std::string content)
Add a HTTP header.
Definition: reply.cpp:97
virtual ~ROSWebviewRequestProcessor()
Destructor.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual fawkes::WebReply * process_request(const fawkes::WebRequest *request)
Process a request.
Web request meta data carrier.
Definition: request.h:42
Basic page reply.
Definition: page_reply.h:36
Basic web reply.
Definition: reply.h:36
const std::string & url() const
Get URL.
Definition: request.h:69
Code
HTTP response code.
Definition: reply.h:40
Static error page reply.
Definition: error_reply.h:33
Static web reply.
Definition: reply.h:133
Interface for logging.
Definition: logger.h:34