00001
00002
00003 #include "Iguana/WebFramework/interface/IgWebService.h"
00004 #include "Iguana/Framework/interface/IgEnvsElement.h"
00005 #include "Iguana/WebFramework/interface/IgFileManager.h"
00006 #include <qiodevice.h>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 IgWebService::IgWebService (IgState *state)
00019 : m_state(state)
00020 {
00021 m_fileManager = new IgFileManager (new IgState(state));
00022 IgEnvsElement* envs = IgEnvsElement::get(state);
00023 std::string wedDirectory("");
00024 if (envs->getEnv ("IG_WEB_STUDIO_DIRECTORY", wedDirectory))
00025 m_fileManager->addSearchPath (wedDirectory);
00026
00027
00028 getFileManager ()->addPaths ("/share/images/", true);
00029 getFileManager ()->addPaths ("/src/", true);
00030
00031 registerCallback ("getScript", lat::CreateCallback (this, &IgWebService::getScript));
00032 registerCallback ("getPic", lat::CreateCallback (this, &IgWebService::getPic));
00033 }
00034
00035 IgWebService::~IgWebService (void)
00036 {
00037 }
00038
00039 IgState*
00040 IgWebService::state () const
00041 { return m_state; }
00042
00043 IgFileManager*
00044 IgWebService::getFileManager () const
00045 { return m_fileManager; }
00046
00047 void
00048 IgWebService::getScript (Arguments *arguments)
00049 {
00050 QIODevice *outputDevice = arguments->outputDevice ();
00051 ArgumentsMap &args = *(arguments->args ());
00052 QByteArray writeBuffer;
00053
00054 if (args.find ("name") == args.end ())
00055 return;
00056
00057 std::string fileName = args["name"];
00058
00059 writeBuffer = m_fileManager->lookup (fileName);
00060
00061 std::string type = "text/html";
00062 if (args.find ("type") != args.end ())
00063 {
00064 type = args["type"];
00065 }
00066
00067 this->sendBinary (outputDevice, writeBuffer, type);
00068 }
00069
00070 void
00071 IgWebService::getPic (Arguments *arguments)
00072 {
00073 QIODevice *outputDevice = arguments->outputDevice ();
00074 ArgumentsMap &args = *(arguments->args ());
00075 QByteArray writeBuffer;
00076
00077 if (args.find ("name") == args.end ())
00078 return;
00079
00080 std::string fileName = args["name"];
00081
00082 writeBuffer = m_fileManager->lookup (fileName);
00083
00084 std::string type = "png";
00085 if (args.find ("type") != args.end ())
00086 {
00087 type = args["type"];
00088 }
00089
00090 this->sendBinary (outputDevice, writeBuffer, "image/" + type);
00091 }
00092
00093 IgWebService::WebServiceCallback
00094 IgWebService::callback (const std::string &label)
00095 {
00096 return m_callbackMap[label];
00097 }
00098
00099
00100 void
00101 IgWebService::registerCallback (const std::string &label,
00102 const IgWebService::WebServiceCallback &cb)
00103 {
00104 m_callbackMap.insert (std::pair<std::string, WebServiceCallback > (label, cb));
00105 }
00106
00107
00108 IgWebService::Arguments::Arguments (IgState *state,
00109 QIODevice *outputDevice,
00110 ArgumentsMap *args,
00111 const std::string idCookie,
00112 QByteArray bodyData)
00113 : m_state (state),
00114 m_outputDevice (outputDevice),
00115 m_args (args),
00116 m_idCookie (idCookie),
00117 m_bodyData (bodyData)
00118 {
00119 }
00120
00121 void
00122 IgWebService::sendDone (QIODevice *socket)
00123 {
00124 QTextStream os (socket);
00125 os << "HTTP/1.1 200 Ok\r\n"
00126 "content-type: text/xml; charset=UTF-8\r\n"
00127 "\r\n"
00128 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
00129 "<done/>\r\n";
00130 }
00131
00132
00133 void
00134 IgWebService::sendError (QIODevice *socket)
00135 {
00136 QTextStream os (socket);
00137 os << "HTTP/1.1 200 Ok\r\n"
00138 "content-type: text/xml; charset=UTF-8\r\n"
00139 "\r\n"
00140 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
00141 "<error/>\r\n";
00142 }
00143
00144
00145 void
00146 IgWebService::sendXML (QIODevice *socket, std::string xmlString)
00147 {
00148 QTextStream os (socket);
00149 os << "HTTP/1.1 200 Ok\r\n"
00150 "content-type: text/xml; charset=UTF-8\r\n"
00151 "\r\n"
00152 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
00153 "" + xmlString + "\r\n";
00154 }
00155
00156 void
00157 IgWebService::sendBinary (QIODevice *socket,
00158 QByteArray &tmpBuffer,
00159 std::string mimeType)
00160 {
00161 QTextStream os (socket);
00162 os << "HTTP/1.0 200 Ok\r\n"
00163 "Content-Type: " + mimeType + ";\r\n"
00164 "\r\n";
00165
00166 socket->writeBlock (tmpBuffer.data (),
00167 tmpBuffer.size ());
00168
00169 os << "\r\n";
00170 }
00171
00172
00173 IgState *
00174 IgWebService::Arguments::state (void)
00175 { return m_state; }
00176
00177 QIODevice *
00178 IgWebService::Arguments::outputDevice (void)
00179 { return m_outputDevice; }
00180
00181 IgWebService::ArgumentsMap *
00182 IgWebService::Arguments::args (void)
00183 { return m_args; }
00184
00185 std::string
00186 IgWebService::Arguments::idCookie (void)
00187 { return m_idCookie; }
00188
00189 QByteArray
00190 IgWebService::Arguments::bodyData (void)
00191 {
00192 return m_bodyData;
00193 }