CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

servletcontext.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Ilya A. Volynets-Evenbakh                       *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include <serverconfig/serverconfig.h>
00022 
00023 namespace container {
00024 namespace serverconfig {
00025 
00033 ServletContext::ServletContext(Context* parent, const std::string& path, const std::string& dso, bool hidden)
00034     : Context(parent->getServerConfig(), parent)
00035     , m_dso(dso)
00036     , m_hidden(hidden)
00037     , m_path(path)
00038     , m_maxRequestSize(-1)
00039     , m_maxFileSize(-1)
00040 {
00041     m_paramregistry.getParamList(getUnsetParams());
00042 }
00043 
00047 bool ServletContext::onSetParam(const ConfigNode& node)
00048 {
00049     return m_paramregistry.setParam(node, this);
00050 }
00051 
00052 void ServletContext::registerContexts(ContextRegistry&)
00053 {
00054     //No subcontexts for servlet.
00055 }
00056 
00057 void ServletContext::registerParams(ParamRegistry<ServletContext>& reg)
00058 {
00059     reg.registerParam("parameter",&ServletContext::addInitParam,0);
00060     reg.registerParam("max_request_size",&ServletContext::setMaxRequestSize,PARAM_INHERITABLE|PARAM_SINGLE_OF_TYPE);
00061     reg.registerParam("max_file_size",&ServletContext::setMaxFileSize,PARAM_INHERITABLE|PARAM_SINGLE_OF_TYPE);
00062 }
00063 
00064 bool ServletContext::addInitParam(const ConfigNode& val)
00065 {
00066     util::param_t::const_iterator name=val.getAttrs().find("name");
00067     util::param_t::const_iterator value=val.getAttrs().find("value");
00068     if(value == val.getAttrs().end()) {
00069         std::cerr<<"Servlet parameter has no name"<<std::endl;
00070         return false;
00071     }
00072     if(name == val.getAttrs().end()) {
00073         std::cerr<<"Servlet parameter has no value";
00074         return false;
00075     }
00076     m_params.push_back(std::pair<std::string,std::string>(name->second,value->second));
00077     return true;
00078 }
00079 
00080 bool ServletContext::setMaxRequestSize(const ConfigNode& val)
00081 {
00082     util::param_t::const_iterator value=val.getAttrs().find("value");
00083     if(value == val.getAttrs().end()) {
00084         std::cerr<<"Maximum request size parameter has no value"<<std::endl;
00085         return false;
00086     }
00087     m_maxRequestSize = atol(value->second.c_str());
00088     return true;
00089 }
00090 
00091 bool ServletContext::setMaxFileSize(const ConfigNode& val)
00092 {
00093     util::param_t::const_iterator value=val.getAttrs().find("value");
00094     if(value == val.getAttrs().end()) {
00095         std::cerr<<"Maximum file size parameter has no value"<<std::endl;
00096         return false;
00097     }
00098     m_maxFileSize = atol(value->second.c_str());
00099     return true;
00100 }
00101 
00102 Context* ServletContext::contextCreator(const ConfigNode& n, Context* parent)
00103 {
00104     util::param_t::const_iterator dso = n.getAttrs().find("dso");
00105     if(dso == n.getAttrs().end())
00106         ServerConfig::fart("app=>servlets[]->dso");
00107     util::param_t::const_iterator ithidden = n.getAttrs().find("hidden");
00108     bool hidden = false;
00109     if(ithidden != n.getAttrs().end() && ithidden->second == "true")
00110         hidden = true;
00111     util::param_t::const_iterator pathit = n.getAttrs().find("path");
00112     std::string path;
00113     if(pathit != n.getAttrs().end())
00114         path = pathit->second;
00115     return new ServletContext(parent, path, dso->second, hidden);
00116 }
00117 
00118 bool ServletContext::onPostComplete()
00119 {
00120     AppContext* app=static_cast<AppContext*>(getParent());
00121     if(m_path.empty())
00122         m_path = getName();
00123     ServletConfigImpl* conf=app->addServlet(m_path, getServletName(), m_dso, m_hidden, m_maxRequestSize, m_maxFileSize);
00124     if(!conf){
00125         std::cerr<<"Unable to add servlet "<<m_path<<" "<<getServletName()<<std::endl;
00126         return false;
00127     }
00128     for(util::pairlist_t::iterator it=m_params.begin(); it!=m_params.end(); it++){
00129         conf->addParam(it->first,it->second);
00130     }
00131     return true;
00132 }
00133 
00134 std::string ServletContext::getServletName()
00135 {
00136     return getName();
00137 }
00138 
00139 ParamRegistry<ServletContext> ServletContext::m_paramregistry(ServletContext::registerParams);
00140 ContextRegistry ServletContext::m_contextregistry(ServletContext::registerContexts);
00141 
00142 }
00143 }

SourceForge.net Logo