Fawkes API  Fawkes Development Version
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       try {
00076         __manager->load(plugin_name.c_str());
00077         return new WebRedirectReply(__baseurl);
00078       } catch (Exception &e) {
00079         WebPageReply *r = new WebPageReply("Loading plugin failed");
00080         r->append_body("<h1>Loading plugin '%s' failed</h1>", plugin_name.c_str());
00081         *r += "<p>The encountered error was:</p>";
00082         for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00083           *r += std::string(*i) + "<br/>\n";
00084         }
00085         r->append_body("<p><a href=\"%s\">Back to overview</a> - "
00086                        "<a href=\"%s\">Retry</a></p>", __baseurl, url);
00087         return r;
00088       }
00089     } else if (subpath.find("/unload/") == 0) {
00090       std::string plugin_name = subpath.substr(std::string("/unload/").length());
00091       try {
00092         __manager->unload(plugin_name.c_str());
00093         return new WebRedirectReply(__baseurl);
00094       } catch (Exception &e) {
00095         WebPageReply *r = new WebPageReply("Unloading plugin failed");
00096         r->append_body("<h1>Unloading plugin '%s' failed</h1>",
00097                        plugin_name.c_str());
00098         *r += "<p>The encountered error was:</p>";
00099         for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
00100           *r += std::string(*i) + "<br/>\n";
00101         }
00102         r->append_body("<p><a href=\"%s\">Back to overview</a> - "
00103                        "<a href=\"%s\">Retry</a></p>", __baseurl, url);
00104         return r;
00105       }
00106     } else {
00107       WebPageReply *r = new WebPageReply("Plugins");
00108       *r += "<h2>Fawkes Plugins</h2>\n";
00109 
00110       *r += "<table>\n";
00111       *r += "<tr><th>Name</th><th>Description</th><th>Loaded</th><th>Action</th></tr>\n";
00112 
00113       std::list<std::pair<std::string, std::string> > available_plugins;
00114       std::list<std::pair<std::string, std::string> >::iterator i;
00115 
00116       available_plugins = __manager->get_available_plugins();
00117 
00118       for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
00119         bool is_loaded = __manager->is_loaded(i->first.c_str());
00120 
00121         const char *loaded_color = is_loaded ? "green"  : "red";
00122         const char *loaded       = is_loaded ? "Yes"    : "No";
00123         const char *action_link  = is_loaded ? "unload" : "load";
00124 
00125         r->append_body("<tr><td>%s</td><td>%s</td>"
00126                        "<td><span style=\"color:%s\">%s<span></td>"
00127                        "<td><a href=\"%s/%s/%s\">%s</a></td>\n",
00128                        i->first.c_str(), i->second.c_str(), loaded_color, loaded,
00129                        __baseurl, action_link, i->first.c_str(), action_link);
00130       }
00131 
00132       *r += "</table>\n";
00133 
00134       return r;
00135     }
00136   } else {
00137     return NULL;
00138   }
00139 }