00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "WebExpositorBundle.h"
00037 #include <core/ObjectFactory.h>
00038 #include <core/PersistenceManager.h>
00039 #include <core/BusManager.h>
00040 #include <core/FileSystem.h>
00041 #include <core/WoshKernel.h>
00042 #include <core/MethodsCommon.h>
00043 #include <core/DataSet.h>
00044 #include <core/BusRing.h>
00045 #include <core/SystemInfo.h>
00046 #include <cstdio>
00047
00048
00049 using namespace std;
00050 namespace wosh {
00051 namespace bundles {
00052
00053 WOSH_REGISTER(wosh::bundles::WebExpositorBundle, wosh::interfaces::network::WebExpositor, _WebExpositor_VERSION, bundles_WebExpositorBundle1)
00054 WOSH_REGISTER(wosh::bundles::WebExpositorBundle, wosh::BundleGeneric, _WebExpositor_VERSION, bundles_WebExpositorBundle2)
00055
00056
00057
00058
00059 WebExpositorBundle::WebExpositorBundle()
00060 : BundleGeneric(), webServer(NULL) {
00061 BundleGeneric::setName( _WebExpositor_NAME, false );
00062 Properties.update( _KEY_Version, _WebExpositor_VERSION );
00063 Requirements.add<wosh::network::WebServer>();
00064
00065 Log.setLevel(LOG_DEBUG);
00066
00067 Log(LOG_DEBUG, " Setting default properties and permissions.." );
00068
00069
00070
00071
00072 Log(LOG_DEBUG, " Registering methods.." );
00073
00074 Log(LOG_DEBUG, " Setting up WebServer.." );
00075 this->webServer = ObjectFactory::createTypeOf<wosh::network::WebServer>(false);
00076 if ( this->webServer != NULL ) {
00077 this->webServer->setListener(this);
00078 this->webServer->setParentObject(this);
00079 }
00080
00081 setBundleState(Bundle::STATE_CREATED, false);
00082 }
00083
00084 WebExpositorBundle::~WebExpositorBundle() {
00085 Log(LOG_INFO, " Destroying.." );
00086 if ( isBundleRunning() ) {
00087 Log(LOG_WARNING, "~WebExpositorBundle() : Destroying while RUNNING! Trying to stop.." );
00088 bundleStop();
00089 }
00090 Log(LOG_DEBUG, ":~WebExpositorBundle() : Destroying Scheduler.." );
00091 WRESULT destroyed = ObjectFactory::destroy(this->webServer);
00092 this->webServer = NULL;
00093 if ( WFAILED(destroyed) ) {
00094 }
00095 Log(LOG_VERBOSE, ":~WebExpositorBundle() : Destroyed." );
00096 }
00097
00098
00099
00100
00101
00102 WRESULT WebExpositorBundle::bundleStart() {
00103 if ( !BundleGeneric::bundleValidate_StartStop(Bundle::STATE_STARTING) ) return WRET_ERR_WRONG_STATE;
00104 setBundleState( Bundle::STATE_STARTING );
00105
00106 WRESULT ret = WRET_OK;
00107 if ( this->webServer == NULL ) {
00108 ret = WRET_ERR_INTERNAL;
00109 SystemInfo::raise(this, &Log, SystemInfo::TYPE_ERROR, SystemInfo::MODE_PERMANENT, SystemInfo::SCOPE_SYSTEM, SystemInfo::PRIORITY_FAILURE,
00110 "Internal Error",
00111 "bundleStart() FAILED Creating WebServer.." );
00112 }
00113 else {
00114 ret = this->webServer->startServer();
00115
00116 }
00117
00118 if ( WSUCCEEDED(ret) )
00119 setBundleState( Bundle::STATE_STARTED );
00120 return ret;
00121 }
00122
00123 WRESULT WebExpositorBundle::bundleStop() {
00124 if ( !BundleGeneric::bundleValidate_StartStop(Bundle::STATE_STOPPING) ) return WRET_ERR_WRONG_STATE;
00125 setBundleState( Bundle::STATE_STOPPING );
00126
00127 WRESULT ret = WRET_ERR_INTERNAL;
00128 if ( this->webServer != NULL ) {
00129 ret = this->webServer->stopServer();
00130 }
00131
00132 setBundleState( Bundle::STATE_STOPPED );
00133 return ret;
00134 }
00135
00136
00137
00138
00139 void WebExpositorBundle::busMessage( const Message& message, const Bus* source ) {
00140 if ( message.isEmpty() ) return;
00141 if ( !MessageFilter::filterReplica(message, &BusCore) ) return;
00142 BundleGeneric::busMessage(message, source);
00143 }
00144
00145
00146
00147
00148 bool WebExpositorBundle::updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source ) {
00149 return BundleGeneric::updatingProperty(do_update, value_proposed, property_current, source);
00150 }
00151
00152
00153
00154
00155
00156 bool WebExpositorBundle::incomingConnection( const std::string& address_port, WebServer* ) {
00157
00158 return true;
00159 }
00160
00161 bool WebExpositorBundle::webrequest_evaluate( WebServerRequest* request, WebServer* ) {
00162 SecurityToken* session_token = NULL;
00163 Request* msg_request = new Request();
00164 URI target;
00165
00166
00167 vector<string> vpath;
00168 String::split(vpath, request->url, "/", _String_SPLIT_OPTION_NOT_EMPTY | _String_SPLIT_OPTION_TRIM);
00169 target.setKernelName(vpath.front());
00170 if ( !vpath.empty() )
00171 vpath.erase(vpath.begin());
00172 target.setPath(vpath);
00173 target.setName(vpath.back());
00174 if ( !target.getPath().empty() )
00175 target.getPath().pop_back();
00176 Log(LOG_DEBUG, ":webrequest_evaluate() Parsed url '%s' as URI '%s'", request->url, target.toString().c_str() );
00177
00178 msg_request->setMethod(_METHOD_ListProperties);
00179 Log(LOG_DEBUG, ":webrequest_evaluate() Parsed method '%s'", msg_request->getMethod().c_str() );
00180
00181
00182
00183 unsigned long timeout = 15 * 1000L;
00184 Message* message = new Message(msg_request);
00185 int64 id = message->getID();
00186 message->setSource(this);
00187 message->setDestinationBus(_Bus_ANY);
00188 message->setDestination(target);
00189 signMessage(message, session_token);
00190 Log(LOG_VERBOSE, ":webrequest_evaluate() Posting Message#%"PRId64" (waiting reply %ul)", id, timeout );
00191 Message* reply = BusCore.postMessageWaitReply(message, timeout);
00192 if ( reply == NULL ) {
00193 Log(LOG_WARNING, ":webrequest_evaluate() Timeout Message#%"PRId64"", id );
00194 return false;
00195 }
00196 if ( reply->isEmpty() || !reply->getContent()->isResponse() || !reply->getContent()->asResponse()->hasData() ) {
00197 const char* page = WebServer::getDefaultPage();
00198 request->replyResponse(page);
00199 return true;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 return true;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 };
00227 };