plugins_processor.cpp

00001 
00002 /***************************************************************************
00003  *  plugins_processor.cpp - Web request processor for plugin info
00004  *
00005  *  Created: Thu Feb 12 13:00:37 2009
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "plugins_processor.h"
00024 #include <webview/page_reply.h>
00025 #include <webview/redirect_reply.h>
00026 
00027 #include <plugin/manager.h>
00028 
00029 #include <string>
00030 #include <cstring>
00031 #include <cstdlib>
00032 
00033 using namespace fawkes;
00034 
00035 /** @class WebviewPluginsRequestProcessor "plugins_processor.h"
00036  * Plugins web request processor.
00037  * Provides access to plugin lists and allows for loading/unloading plugins.
00038  * @author Tim Niemueller
00039  */
00040 
00041 /** Constructor.
00042  * @param baseurl Base URL where processor is mounted
00043  * @param manager PluginManager instance
00044  */
00045 WebviewPluginsRequestProcessor::WebviewPluginsRequestProcessor(const char *baseurl,
00046                                                        PluginManager *manager)
00047 {
00048   __baseurl     = strdup(baseurl);
00049   __baseurl_len = strlen(__baseurl);
00050   __manager     = manager;
00051 }
00052 
00053 
00054 /** Destructor. */
00055 WebviewPluginsRequestProcessor::~WebviewPluginsRequestProcessor()
00056 {
00057   free(__baseurl);
00058 }
00059 
00060 
00061 WebReply *
00062 WebviewPluginsRequestProcessor::process_request(const char *url,
00063                                                const char *method,
00064                                                const char *version,
00065                                                const char *upload_data,
00066                                                size_t *upload_data_size,
00067                                                void **session_data)
00068 {
00069   if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
00070     // It is in our URL prefix range
00071     std::string subpath = std::string(url).substr(__baseurl_len);
00072 
00073     if (subpath.find("/load/") == 0) {
00074       std::string plugin_name = subpath.substr(std::string("/load/").length());
00075       __manager->load(plugin_name.c_str());
00076       return new WebRedirectReply(__baseurl);
00077     } else if (subpath.find("/unload/") == 0) {
00078       std::string plugin_name = subpath.substr(std::string("/unload/").length());
00079       __manager->unload(plugin_name.c_str());
00080       return new WebRedirectReply(__baseurl);
00081     } else {
00082       WebPageReply *r = new WebPageReply("BlackBoard");
00083       *r += "<h2>Fawkes Plugins</h2>\n";
00084 
00085       *r += "<table>\n";
00086       *r += "<tr><th>Name</th><th>Description</th><th>Loaded</th><th>Action</th></tr>\n";
00087 
00088       std::list<std::pair<std::string, std::string> > available_plugins;
00089       std::list<std::pair<std::string, std::string> >::iterator i;
00090 
00091       available_plugins = __manager->get_available_plugins();
00092 
00093       for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
00094         bool is_loaded = __manager->is_loaded(i->first.c_str());
00095 
00096         const char *loaded_color = is_loaded ? "green"  : "red";
00097         const char *loaded       = is_loaded ? "Yes"    : "No";
00098         const char *action_link  = is_loaded ? "unload" : "load";
00099 
00100         r->append_body("<tr><td>%s</td><td>%s</td>"
00101                        "<td><span style=\"color:%s\">%s<span></td>"
00102                        "<td><a href=\"%s/%s/%s\">%s</a></td>\n",
00103                        i->first.c_str(), i->second.c_str(), loaded_color, loaded,
00104                        __baseurl, action_link, i->first.c_str(), action_link);
00105       }
00106 
00107       *r += "</table>\n";
00108 
00109       return r;
00110     }
00111   } else {
00112     return NULL;
00113   }
00114 }