Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

session.hh

00001 /*
00002 *  Name:      session.hh
00003 *  Authors:   Francisco Rodrigo Escobedo Robles
00004 *             Rafael Jesus Alcantara Perez
00005 *  Summary:   HTTP State Management
00006 *  Date:      $Date: 2003/04/14 00:18:31 $
00007 *  Revision:  $Revision: 1.1 $
00008 *
00009 *  Copyright (C) 2000-2001  Francisco Rodrigo Escobedo Robles <frer@pepix.net>
00010 *  Copyright (C) 2002       Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00011 *
00012 *  This program is free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  This program is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  You should have received a copy of the GNU General Public License
00023 *  along with this program; if not, write to the Free Software
00024 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00025 *  MA 02111-1307, USA.
00026 */
00027 
00028 #ifndef _MPCL_NET_CGI_SESSION__
00029 #define _MPCL_NET_CGI_SESSION__
00030 
00031 #include <iostream>
00032 #include <sstream>
00033 #include "../../io/streamable.hh"
00034 #include "../../text/regex/matcher.hh"
00035 #include "../../text/string.hh"
00036 #include "../../util/collection/map.hh"
00037 #include "exceptions.hh"
00038 
00039 
00041 namespace mpcl
00042 {
00043 
00045   namespace net
00046   {
00047 
00049     namespace cgi
00050     {
00051 
00052       using text::regex::TMatcher;
00053       using util::collection::TMap;
00054 
00056       template <typename TCookie>
00057       class TSession : public IStreamable<>
00058       {
00059 
00060         public:
00061 
00063           typedef
00064             TMap<TString, TString>
00065             TSubSubCookieValueMap;
00066 
00067 
00068         private:
00069 
00071           class TStateInformation
00072           {
00073 
00074             public:
00075 
00077               bool   gKeep;
00078 
00080               TCookie   tCookie;
00081 
00082 
00083             public:
00084 
00086               TStateInformation (const TCookie& rktCOOKIE) :
00087                 gKeep   (false)                            ,
00088                 tCookie (rktCOOKIE)                        {}
00089 
00091               TStateInformation (std::basic_istream<char_type, traits_type>& rtSOURCE_ISTREAM) :
00092                 gKeep   (false)                                                                ,
00093                 tCookie (rtSOURCE_ISTREAM)                                                     {}
00094 
00095           };  // class TStateInformation
00096 
00098           typedef
00099             TMap<TString, TStateInformation>
00100             TStateInformationMap;
00101 
00103           typedef
00104             TMap<TString, TSubSubCookieValueMap>
00105             TSubSubCookieMap;
00106 
00108           typedef
00109             TMap<TString, TSubSubCookieMap>
00110             TSubCookieMap;
00111 
00113           typedef
00114             TMap<TString, TSubCookieMap>
00115             TStructuredCookieMap;
00116 
00118           typedef
00119             typename TStateInformationMap::iterator
00120             TStateInformationMapIterator;
00121 
00122           typedef
00123             typename TSubSubCookieValueMap::iterator
00124             TSubSubCookieValueMapIterator;
00125 
00126           typedef
00127             typename TSubCookieMap::iterator
00128             TSubCookieMapIterator;
00129 
00130           typedef
00131             typename TSubSubCookieMap::iterator
00132             TSubSubCookieMapIterator;
00133 
00134           typedef
00135             typename TStructuredCookieMap::iterator
00136             TStructuredCookieMapIterator;
00137 
00139           static const char   _kcSubCookieNameSeparator    = '(';
00140           static const char   _kcSubCookieMembersSeparator = ',';
00141           static const char   _kcSubCookieSeparator        = ')';
00142           static const char   _kcSubCookieValuesSeparator  = '=';
00143 
00144           //
00145           //  An alternative notation >:
00146           //  static const char   _kcSubCookieNameSeparator = '>';
00147           //  static const char   _kcSubCookieSeparator     = ':';
00148           //
00149 
00151           mutable TStateInformationMap   tStateInformationMap;
00152 
00154           mutable TStructuredCookieMap   tStructuredCookieMap;
00155 
00157           mutable TSubCookieMap   tSubCookieMap;
00158 
00160           mutable TSubSubCookieMap   tSubSubCookieMap;
00161 
00163           mutable TSubSubCookieValueMap   tSubSubCookieValueMap;
00164 
00165 
00166         private:
00167 
00168           //
00169           //  C O N S T R U C T O R S
00170           //
00171 
00175           void decomposeCookie (TCookie tCOOKIE);
00176 
00180           TSubSubCookieValueMap decomposeSubCookie (const TString& ySOURCE_VALUE) const;
00181 
00185           void recomposeCookies (void) const;
00186 
00190           void setCookie (const TCookie& rktCOOKIE) const;
00191 
00195           void setCookie (const TString& rkyCOOKIE_NAME, const TString& rkyCOOKIE_VALUE) const
00196           {
00197             setCookie (TCookie (rkyCOOKIE_NAME, rkyCOOKIE_VALUE));
00198           }
00199 
00200 
00201         public:
00202 
00203           //
00204           //  C O N S T R U C T O R S
00205           //
00206 
00208           TSession (void)         :
00209             tStateInformationMap  () ,
00210             tStructuredCookieMap  () ,
00211             tSubCookieMap         () ,
00212             tSubSubCookieMap      () ,
00213             tSubSubCookieValueMap () {}
00214 
00219           TSession (const char* pkcSOURCE_STRING) :
00220             tStateInformationMap  ()                 ,
00221             tStructuredCookieMap  ()                 ,
00222             tSubCookieMap         ()                 ,
00223             tSubSubCookieMap      ()                 ,
00224             tSubSubCookieValueMap ()
00225           {
00226             std::basic_istringstream<char_type, traits_type>   tIstringstream (pkcSOURCE_STRING);
00227 
00228             read (tIstringstream);
00229           }
00230 
00235           TSession (const TString& rkySOURCE_STRING) :
00236             tStateInformationMap  ()                    ,
00237             tStructuredCookieMap  ()                    ,
00238             tSubCookieMap         ()                    ,
00239             tSubSubCookieMap      ()                    ,
00240             tSubSubCookieValueMap ()
00241           {
00242             std::basic_istringstream<char_type, traits_type>   tIstringstream (rkySOURCE_STRING.c_str());
00243 
00244             read (tIstringstream);
00245           }
00246 
00251           TSession (std::basic_istream<char_type, traits_type>& rtSOURCE_ISTREAM) :
00252             tStateInformationMap  ()                                                 ,
00253             tStructuredCookieMap  ()                                                 ,
00254             tSubCookieMap         ()                                                 ,
00255             tSubSubCookieMap      ()                                                 ,
00256             tSubSubCookieValueMap ()
00257           {
00258             read (rtSOURCE_ISTREAM);
00259           }
00260 
00265           void erase (const TString& rkyCOOKIE_NAME)
00266             throw (TVariableNotFoundException);
00267 
00273           void erase ( const TString& rkyCOOKIE_NAME         ,
00274                        const TString& rkySUB_COOKIE_SELECTOR )
00275             throw (TVariableNotFoundException);
00276 
00283           void erase ( const TString& rkyCOOKIE_NAME          ,
00284                        const TString& rkySUB_COOKIE_SELECTOR1 ,
00285                        const TString& rkySUB_COOKIE_SELECTOR2 )
00286             throw (TVariableNotFoundException);
00287 
00296           void erase ( const TString& rkyCOOKIE_NAME          ,
00297                        const TString& rkySUB_COOKIE_SELECTOR1 ,
00298                        const TString& rkySUB_COOKIE_SELECTOR2 ,
00299                        const TString& rkySUB_COOKIE_NAME      )
00300             throw (TVariableNotFoundException);
00301 
00307           void keep (const TString& rkyCOOKIE_NAME, bool gKEEP = true)
00308           {
00309             TStateInformationMapIterator   I = tStateInformationMap.find (rkyCOOKIE_NAME);
00310 
00311             if ( tStateInformationMap.end() == I )
00312             {
00313               throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00314             }
00315             I->second.gKeep = gKEEP;
00316           }
00317 
00322           void set (const TCookie& rktCOOKIE)
00323           {
00324             setCookie (rktCOOKIE);
00325           }
00326 
00332           void set (const TString& rkyCOOKIE_NAME, const TString& rkyCOOKIE_VALUE)
00333           {
00334             setCookie (rkyCOOKIE_NAME, rkyCOOKIE_VALUE);
00335           }
00336 
00343           void set ( const TString&               rkyCOOKIE_NAME          ,
00344                      const TString&               rkySUB_COOKIE_SELECTOR  ,
00345                      const TSubSubCookieValueMap& rktSUB_COOKIE_VALUE_MAP );
00346 
00354           void set ( const TString&               rkyCOOKIE_NAME          ,
00355                      const TString&               rkySUB_COOKIE_SELECTOR1 ,
00356                      const TString&               rkySUB_COOKIE_SELECTOR2 ,
00357                      const TSubSubCookieValueMap& rktSUB_COOKIE_VALUE_MAP );
00358 
00359 
00360         protected:
00361 
00362           //
00363           //  S E L E C T O R S
00364           //
00365 
00370           void read (std::basic_istream<char_type, traits_type>& rtSOURCE_ISTREAM);
00371 
00376           void write (std::basic_ostream<char_type, traits_type>& rtTARGET_OSTREAM) const;
00377 
00378 
00379         public:
00380 
00381           //
00382           //  S E L E C T O R S
00383           //
00384 
00389           bool isEmpty (void) const
00390           {
00391             return ( tStructuredCookieMap.empty() &&
00392                      tSubCookieMap.empty()        &&
00393                      tStateInformationMap.empty()  );
00394           }
00395 
00401           TString get (const TString& rkyCOOKIE_NAME) const
00402             throw (TVariableNotFoundException);
00403 
00410           const TSubSubCookieValueMap& get ( const TString& rkyCOOKIE_NAME         ,
00411                                              const TString& rkySUB_COOKIE_SELECTOR ) const
00412             throw (TVariableNotFoundException);
00413 
00420           TSubSubCookieValueMap& get ( const TString& rkyCOOKIE_NAME         ,
00421                                        const TString& rkySUB_COOKIE_SELECTOR )
00422             throw (TVariableNotFoundException);
00423 
00430           void get ( const TString&         rkyCOOKIE_NAME         ,
00431                      const TString&         rkySUB_COOKIE_SELECTOR ,
00432                      TSubSubCookieValueMap& rtSUB_COOKIE_VALUES    ) const
00433             throw (TVariableNotFoundException);
00434 
00442           const TSubSubCookieValueMap& get ( const TString& rkyCOOKIE_NAME          ,
00443                                              const TString& rkySUB_COOKIE_SELECTOR1 ,
00444                                              const TString& rkySUB_COOKIE_SELECTOR2 ) const
00445             throw (TVariableNotFoundException);
00446 
00454           TSubSubCookieValueMap& get ( const TString& rkyCOOKIE_NAME          ,
00455                                        const TString& rkySUB_COOKIE_SELECTOR1 ,
00456                                        const TString& rkySUB_COOKIE_SELECTOR2 )
00457             throw (TVariableNotFoundException);
00458 
00466           void get ( const TString&         rkyCOOKIE_NAME          ,
00467                      const TString&         rkySUB_COOKIE_SELECTOR1 ,
00468                      const TString&         rkySUB_COOKIE_SELECTOR2 ,
00469                      TSubSubCookieValueMap& rtSUB_COOKIE_VALUES     ) const
00470             throw (TVariableNotFoundException);
00471 
00477           bool has (const TString& rkyCOOKIE_NAME) const
00478           {
00479             TStateInformationMapIterator   I = tStateInformationMap.find (rkyCOOKIE_NAME);
00480 
00481             return ( I != tStateInformationMap.end() );
00482           }
00483 
00484       };  // class TSession
00485 
00486     }  // namespace cgi
00487 
00488   }  // namespace net
00489 
00490 }  // namespace mpcl
00491 
00492 
00493 //
00494 //  C O N S T R U C T O R S
00495 //
00496 
00497 template <typename TCookie>
00498 void mpcl::net::cgi::TSession<TCookie>::
00499 decomposeCookie (TCookie tCOOKIE)
00500 {
00501 
00502   using text::Format;
00503   using text::TString;
00504 
00505   static const TString   _kyDoubleSelectorCookieFormat =
00506     Format ("%%s%c%%s%c%%t%c"            ,
00507             _kcSubCookieMembersSeparator ,
00508             _kcSubCookieNameSeparator    ,
00509             _kcSubCookieSeparator        );
00510 
00511   static const TString   _kySingleSelectorCookieFormat =
00512     Format ("%%s%c%%t%c"              ,
00513             _kcSubCookieNameSeparator ,
00514             _kcSubCookieSeparator     );
00515 
00516   const TString   kyName (tCOOKIE.name());
00517   TMatcher        tMatcher (tCOOKIE.value());
00518   TString         ySelector1;
00519   TString         ySelector2;
00520   TString         yValue;
00521 
00522   //
00523   //  First check for  a cookie with 2 selectors, then  for 1 selector and
00524   //  then for a simple value.
00525   //
00526   if ( tMatcher.match (_kyDoubleSelectorCookieFormat.c_str()) )
00527   {
00528     while ( tMatcher.match (_kyDoubleSelectorCookieFormat.c_str()) )
00529     {
00530       tMatcher.scan (_kyDoubleSelectorCookieFormat.c_str(), &ySelector1, &ySelector2, &yValue, NULL);
00531       set (kyName, ySelector1, ySelector2, decomposeSubCookie (yValue));
00532     }
00533     return;
00534   }
00535   else if ( tMatcher.match (_kySingleSelectorCookieFormat.c_str()) )
00536   {
00537     while ( tMatcher.match (_kySingleSelectorCookieFormat.c_str()) )
00538     {
00539       tMatcher.scan (_kySingleSelectorCookieFormat.c_str(), &ySelector1, &yValue, NULL);
00540       set (kyName, ySelector1, decomposeSubCookie (yValue));
00541     }
00542     return;
00543   }
00544   else
00545   {
00546     set (tCOOKIE);
00547   }
00548 
00549 }  // decomposeCookie()
00550 
00551 
00552 template <typename TCookie>
00553 typename mpcl::net::cgi::TSession<TCookie>::TSubSubCookieValueMap
00554 mpcl::net::cgi::TSession<TCookie>::
00555 decomposeSubCookie (const TString& ySOURCE_VALUE) const
00556 {
00557 
00558   using text::Format;
00559   using text::TString;
00560 
00561   static const TString   _kySimpleCookieFormat =
00562     Format
00563     (
00564       "%c%%s%c%%s"                 ,
00565       _kcSubCookieMembersSeparator ,
00566       _kcSubCookieValuesSeparator
00567     );
00568 
00569   static const TString   _kySimpleCookieFormatFirst =
00570     Format ("%%s%c%%s", _kcSubCookieValuesSeparator);
00571 
00572   TMatcher                tMatcher (ySOURCE_VALUE);
00573   TSubSubCookieValueMap   tRet;
00574   TString                 yName;
00575   TString                 yValue;
00576 
00577   if ( tMatcher.match (_kySimpleCookieFormatFirst.c_str()) )
00578   {
00579     tMatcher.scan (_kySimpleCookieFormatFirst.c_str(), &yName, &yValue, NULL);
00580     tRet.bind (yName, yValue);
00581   }
00582   while ( tMatcher.match (_kySimpleCookieFormat.c_str()) )
00583   {
00584     tMatcher.scan (_kySimpleCookieFormat.c_str(), &yName, &yValue, NULL);
00585     tRet.bind (yName, yValue);
00586   }
00587   return tRet;
00588 
00589 }  // decomposeSubCookie()
00590 
00591 
00592 template <typename TCookie>
00593 void mpcl::net::cgi::TSession<TCookie>::
00594 erase (const TString& rkyCOOKIE_NAME)
00595   throw (TVariableNotFoundException)
00596 {
00597 
00598   TStructuredCookieMapIterator   I = tStructuredCookieMap.find (rkyCOOKIE_NAME);
00599   TSubCookieMapIterator          J = tSubCookieMap.find        (rkyCOOKIE_NAME);
00600   TStateInformationMapIterator   K = tStateInformationMap.find (rkyCOOKIE_NAME);
00601 
00602   //
00603   //  First check for a Cookie with 2 selectors, then 1, then a simple one.
00604   //
00605   if ( tStructuredCookieMap.end() != I )
00606   {
00607     tStructuredCookieMap.erase (I);
00608   }
00609   else if ( tSubCookieMap.end() != J )
00610   {
00611     tSubCookieMap.erase (J);
00612   }
00613   else if ( tStateInformationMap.end() == K )
00614   {
00615     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00616   }
00617   tStateInformationMap.erase (K);
00618 
00619 }  // erase()
00620 
00621 
00622 template <typename TCookie>
00623 void mpcl::net::cgi::TSession<TCookie>::
00624 erase ( const TString& rkyCOOKIE_NAME         ,
00625         const TString& rkySUB_COOKIE_SELECTOR )
00626   throw (TVariableNotFoundException)
00627 {
00628 
00629   TSubSubCookieMapIterator   J;
00630   TSubCookieMapIterator      I = tSubCookieMap.find (rkyCOOKIE_NAME);
00631 
00632   if ( tSubCookieMap.end() == I )
00633   {
00634     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00635   }
00636   else
00637   {
00638     J = I->second.find (rkySUB_COOKIE_SELECTOR);
00639     if ( I->second.end() == J )
00640     {
00641       throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00642     }
00643   }
00644   I->second.erase(J);
00645 
00646 }  // erase()
00647 
00648 
00649 template <typename TCookie>
00650 void mpcl::net::cgi::TSession<TCookie>::
00651 erase ( const TString& rkyCOOKIE_NAME          ,
00652         const TString& rkySUB_COOKIE_SELECTOR1 ,
00653         const TString& rkySUB_COOKIE_SELECTOR2 )
00654   throw (TVariableNotFoundException)
00655 {
00656 
00657   TSubCookieMapIterator          J;
00658   TSubSubCookieMapIterator       K;
00659   TStructuredCookieMapIterator   I = tStructuredCookieMap.find (rkyCOOKIE_NAME);
00660 
00661   if ( tStructuredCookieMap.end() == I )
00662   {
00663     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00664   }
00665   else
00666   {
00667     J = I->second.find (rkySUB_COOKIE_SELECTOR1);
00668     if ( I->second.end() == J )
00669     {
00670       throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00671     }
00672     else
00673     {
00674       K = J->second.find (rkySUB_COOKIE_SELECTOR2);
00675       if ( J->second.end() == K )
00676       {
00677         throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00678       }
00679     }
00680   }
00681   J->second.erase(K);
00682 
00683 }  // erase()
00684 
00685 
00686 template <typename TCookie>
00687 void mpcl::net::cgi::TSession<TCookie>::
00688 erase ( const TString& rkyCOOKIE_NAME          ,
00689         const TString& rkySUB_COOKIE_SELECTOR1 ,
00690         const TString& rkySUB_COOKIE_SELECTOR2 ,
00691         const TString& rkySUB_COOKIE_NAME      )
00692   throw (TVariableNotFoundException)
00693 {
00694 
00695   TSubCookieMapIterator           J;
00696   TSubSubCookieMapIterator        K;
00697   TSubSubCookieValueMapIterator   L;
00698   TStructuredCookieMapIterator    I = tStructuredCookieMap.find (rkyCOOKIE_NAME);
00699 
00700   if ( tStructuredCookieMap.end() == I )
00701   {
00702     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00703   }
00704   else
00705   {
00706     J = I->second.find (rkySUB_COOKIE_SELECTOR1);
00707     if ( I->second.end() == J )
00708     {
00709       throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00710     }
00711     else
00712     {
00713       K = J->second.find (rkySUB_COOKIE_SELECTOR2);
00714       if ( J->second.end() == K )
00715       {
00716         throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00717       }
00718       else
00719       {
00720         L = K->second.find (rkySUB_COOKIE_NAME);
00721         if ( K->second.end() == L )
00722         {
00723           throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00724         }
00725       }
00726     }
00727   }
00728   K->second.erase(L);
00729 
00730 }  // erase()
00731 
00732 
00733 //
00734 //  S E L E C T O R S
00735 //
00736 
00737 template <typename TCookie>
00738 mpcl::text::TString mpcl::net::cgi::TSession<TCookie>::
00739 get (const text::TString& rkyCOOKIE_NAME) const
00740   throw (TVariableNotFoundException)
00741 {
00742 
00743   TStateInformationMapIterator   I = tStateInformationMap.find (rkyCOOKIE_NAME);
00744 
00745   if ( tStateInformationMap.end() == I )
00746   {
00747     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00748   }
00749   return I->second.tCookie.value();
00750 
00751 }  // get()
00752 
00753 
00754 template <typename TCookie>
00755 inline const typename mpcl::net::cgi::TSession<TCookie>::TSubSubCookieValueMap&
00756 mpcl::net::cgi::TSession<TCookie>::
00757 get (const TString& rkyCOOKIE_NAME, const TString& rkySUB_COOKIE_SELECTOR) const
00758   throw (TVariableNotFoundException)
00759 {
00760 
00761   return (const TSubSubCookieValueMap&) get (rkyCOOKIE_NAME, rkySUB_COOKIE_SELECTOR);
00762 
00763 }  // get()
00764 
00765 
00766 template <typename TCookie>
00767 typename mpcl::net::cgi::TSession<TCookie>::TSubSubCookieValueMap& mpcl::net::cgi::TSession<TCookie>::
00768 get ( const TString& rkyCOOKIE_NAME         ,
00769       const TString& rkySUB_COOKIE_SELECTOR )
00770   throw (TVariableNotFoundException)
00771 {
00772 
00773   TSubSubCookieMapIterator   J;
00774   TSubCookieMapIterator      I = tSubCookieMap.find (rkyCOOKIE_NAME);
00775 
00776   if ( tSubCookieMap.end() == I )
00777   {
00778     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00779   }
00780   else
00781   {
00782     J = I->second.find (rkySUB_COOKIE_SELECTOR);
00783     if ( I->second.end() == J )
00784     {
00785       throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00786     }
00787   }
00788   return J->second;
00789 
00790 }  // get()
00791 
00792 
00793 template <typename TCookie>
00794 void mpcl::net::cgi::TSession<TCookie>::
00795 get ( const TString&         rkyCOOKIE_NAME         ,
00796       const TString&         rkySUB_COOKIE_SELECTOR ,
00797       TSubSubCookieValueMap& rtSUB_COOKIE_VALUES    ) const
00798   throw (TVariableNotFoundException)
00799 {
00800 
00801   rtSUB_COOKIE_VALUES = get (rkyCOOKIE_NAME, rkySUB_COOKIE_SELECTOR);
00802 
00803 }  // get()
00804 
00805 
00806 template <typename TCookie>
00807 typename mpcl::net::cgi::TSession<TCookie>::TSubSubCookieValueMap& mpcl::net::cgi::TSession<TCookie>::
00808 get ( const TString& rkyCOOKIE_NAME          ,
00809       const TString& rkySUB_COOKIE_SELECTOR1 ,
00810       const TString& rkySUB_COOKIE_SELECTOR2 )
00811   throw (TVariableNotFoundException)
00812 {
00813 
00814   TSubCookieMapIterator          J;
00815   TSubSubCookieMapIterator       K;
00816   TStructuredCookieMapIterator   I = tStructuredCookieMap.find (rkyCOOKIE_NAME);
00817 
00818   if ( tStructuredCookieMap.end() == I )
00819   {
00820     throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00821   }
00822   else
00823   {
00824     J = I->second.find (rkySUB_COOKIE_SELECTOR1);
00825     if ( I->second.end() == J )
00826     {
00827       throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00828     }
00829     else
00830     {
00831       K = J->second.find (rkySUB_COOKIE_SELECTOR2);
00832       if ( J->second.end() == K )
00833       {
00834         throw (TVariableNotFoundException ("variable not found", __FILE__, __LINE__));
00835       }
00836     }
00837   }
00838   return K->second;
00839 
00840 }  // get()
00841 
00842 
00843 template <typename TCookie>
00844 inline const typename mpcl::net::cgi::TSession<TCookie>::TSubSubCookieValueMap& mpcl::net::cgi::TSession<TCookie>::
00845 get ( const TString& rkyCOOKIE_NAME          ,
00846       const TString& rkySUB_COOKIE_SELECTOR1 ,
00847       const TString& rkySUB_COOKIE_SELECTOR2 ) const
00848   throw (TVariableNotFoundException)
00849 {
00850 
00851   return (const TSubSubCookieValueMap&) get ( rkyCOOKIE_NAME          ,
00852                                               rkySUB_COOKIE_SELECTOR1 ,
00853                                               rkySUB_COOKIE_SELECTOR2 );
00854 
00855 }  // get()
00856 
00857 
00858 template <typename TCookie>
00859 void mpcl::net::cgi::TSession<TCookie>::
00860 get ( const TString&         rkyCOOKIE_NAME          ,
00861       const TString&         rkySUB_COOKIE_SELECTOR1 ,
00862       const TString&         rkySUB_COOKIE_SELECTOR2 ,
00863       TSubSubCookieValueMap& rtSUB_COOKIE_VALUES     ) const
00864   throw (TVariableNotFoundException)
00865 {
00866 
00867   rtSUB_COOKIE_VALUES = get (rkyCOOKIE_NAME, rkySUB_COOKIE_SELECTOR1, rkySUB_COOKIE_SELECTOR2);
00868 
00869 }  // get()
00870 
00871 
00872 template <typename TCookie>
00873 void mpcl::net::cgi::TSession<TCookie>::
00874 read (std::basic_istream<char_type, traits_type>& rtSOURCE_ISTREAM)
00875 {
00876 
00877   TStateInformation   tStateInformation (rtSOURCE_ISTREAM);
00878 
00879 // <FIXME id="cookies" comment="there should exists a cookie factory class">
00880   while ( rtSOURCE_ISTREAM.good() && !tStateInformation.tCookie.name().empty() )
00881   {
00882     tStateInformationMap.bind (tStateInformation.tCookie.name(), tStateInformation);
00883     decomposeCookie (tStateInformation.tCookie);
00884     rtSOURCE_ISTREAM >> tStateInformation.tCookie;
00885   }
00886   if ( !tStateInformation.tCookie.name().empty() )
00887   {
00888     tStateInformationMap.bind (tStateInformation.tCookie.name(), tStateInformation);
00889     decomposeCookie (tStateInformation.tCookie);
00890   }
00891 // </FIXME>
00892 
00893 }  // read()
00894 
00895 
00896 template <typename TCookie>
00897 void mpcl::net::cgi::TSession<TCookie>::recomposeCookies (void) const
00898 {
00899 
00900   TStructuredCookieMapIterator    tStructuredCookieMapIndex;
00901   TSubCookieMapIterator           tSubCookieMapIndex;
00902   TSubSubCookieMapIterator        tSubSubCookieMapIndex;
00903   TSubSubCookieValueMapIterator   tSubSubCookieValueMapIndex;
00904   TString                         ySubCookie;
00905   TString                         ySubSubCookie;
00906   TString                         ySubSubCookieValues;
00907 
00908   //
00909   //  Structured Cookies with 1 selector.
00910   //
00911   for ( tSubCookieMapIndex = tSubCookieMap.begin()    ;
00912         ( tSubCookieMap.end() != tSubCookieMapIndex ) ;
00913         ++tSubCookieMapIndex                          )
00914   {
00915     for ( tSubSubCookieMapIndex = tSubCookieMapIndex->second.begin(), ySubSubCookie = "" ;
00916           ( tSubCookieMapIndex->second.end() != tSubSubCookieMapIndex )                  ;
00917           ++tSubSubCookieMapIndex                                                        )
00918     {
00919       for ( tSubSubCookieValueMapIndex = tSubSubCookieMapIndex->second.begin(), ySubSubCookieValues = "" ;
00920             ( tSubSubCookieMapIndex->second.end() != tSubSubCookieValueMapIndex )                        ;
00921             ++tSubSubCookieValueMapIndex                                                                 )
00922       {
00923         ySubSubCookieValues += tSubSubCookieValueMapIndex->first;
00924         ySubSubCookieValues += _kcSubCookieValuesSeparator;
00925         ySubSubCookieValues += tSubSubCookieValueMapIndex->second;
00926         ySubSubCookieValues += _kcSubCookieMembersSeparator;
00927       }
00928       if ( !ySubSubCookieValues.empty() )
00929       {
00930         ySubSubCookieValues.resize (ySubSubCookieValues.size() - 1);
00931       }
00932       ySubSubCookie += tSubSubCookieMapIndex->first;
00933       ySubSubCookie += _kcSubCookieNameSeparator;
00934       ySubSubCookie += ySubSubCookieValues;
00935       ySubSubCookie += _kcSubCookieSeparator;
00936     }
00937     setCookie (tSubCookieMapIndex->first, ySubSubCookie);
00938   }
00939 
00940   //
00941   //  Structured Cookies with 2 selectors.
00942   //
00943   for ( tStructuredCookieMapIndex = tStructuredCookieMap.begin()    ;
00944         ( tStructuredCookieMap.end() != tStructuredCookieMapIndex ) ;
00945         ++tStructuredCookieMapIndex                                 )
00946   {
00947     for ( tSubCookieMapIndex = tStructuredCookieMapIndex->second.begin(), ySubCookie = "" ;
00948           ( tStructuredCookieMapIndex->second.end() != tSubCookieMapIndex )               ;
00949           ++tSubCookieMapIndex                                                            )
00950     {
00951       for ( tSubSubCookieMapIndex = tSubCookieMapIndex->second.begin(), ySubSubCookie = "" ;
00952             ( tSubCookieMapIndex->second.end() != tSubSubCookieMapIndex )                  ;
00953             ++tSubSubCookieMapIndex                                                        )
00954       {
00955         for ( tSubSubCookieValueMapIndex = tSubSubCookieMapIndex->second.begin(), ySubSubCookieValues = "" ;
00956               ( tSubSubCookieMapIndex->second.end() != tSubSubCookieValueMapIndex )                        ;
00957               ++tSubSubCookieValueMapIndex                                                                 )
00958         {
00959           ySubSubCookieValues += tSubSubCookieValueMapIndex->first  +
00960                                  _kcSubCookieValuesSeparator        +
00961                                  tSubSubCookieValueMapIndex->second +
00962                                  _kcSubCookieMembersSeparator       ;
00963         }
00964         if ( !ySubSubCookieValues.empty() )
00965         {
00966           ySubSubCookieValues.resize (ySubSubCookieValues.size() - 1);
00967         }
00968         ySubSubCookie += tSubSubCookieMapIndex->first +
00969                          _kcSubCookieNameSeparator    +
00970                          ySubSubCookieValues          +
00971                          _kcSubCookieSeparator        ;
00972       }
00973       ySubCookie += tSubCookieMapIndex->first    +
00974                     _kcSubCookieMembersSeparator +
00975                     ySubSubCookie                ;
00976     }
00977     setCookie (tStructuredCookieMapIndex->first, ySubCookie);
00978   }
00979 
00980 }  // recomposeCookies()
00981 
00982 
00983 template <typename TCookie>
00984 void mpcl::net::cgi::TSession<TCookie>::
00985 set ( const TString&               rkyCOOKIE_NAME          ,
00986       const TString&               rkySUB_COOKIE_SELECTOR  ,
00987       const TSubSubCookieValueMap& rktSUB_COOKIE_VALUE_MAP )
00988 {
00989 
00990   TSubSubCookieMapIterator   J;
00991   TSubSubCookieMap           tSubSubCookieMapTmp;
00992   TSubCookieMapIterator      I = tSubCookieMap.find (rkyCOOKIE_NAME);
00993 
00994   if ( tSubCookieMap.end() != I )
00995   {
00996     J = I->second.find (rkySUB_COOKIE_SELECTOR);
00997     if ( I->second.end() != J )
00998     {
00999       J->second = rktSUB_COOKIE_VALUE_MAP;
01000     }
01001     else
01002     {
01003       I->second.bind (rkySUB_COOKIE_SELECTOR, rktSUB_COOKIE_VALUE_MAP);
01004     }
01005   }
01006   else
01007   {
01008     tSubSubCookieMapTmp.bind (rkySUB_COOKIE_SELECTOR, rktSUB_COOKIE_VALUE_MAP);
01009     tSubCookieMap.bind       (rkyCOOKIE_NAME,         tSubSubCookieMapTmp);
01010   }
01011 
01012 }  // set()
01013 
01014 
01015 template <typename TCookie>
01016 void mpcl::net::cgi::TSession<TCookie>::
01017 set ( const TString&               rkyCOOKIE_NAME          ,
01018       const TString&               rkySUB_COOKIE_SELECTOR1 ,
01019       const TString&               rkySUB_COOKIE_SELECTOR2 ,
01020       const TSubSubCookieValueMap& rktSUB_COOKIE_VALUE_MAP )
01021 {
01022 
01023   TSubCookieMapIterator          J;
01024   TSubSubCookieMapIterator       K;
01025   TSubCookieMap                  tSubCookieMapTmp;
01026   TSubSubCookieMap               tSubSubCookieMapTmp;
01027   TStructuredCookieMapIterator   I = tStructuredCookieMap.find (rkyCOOKIE_NAME);
01028 
01029   if ( tStructuredCookieMap.end() != I )
01030   {
01031     J = I->second.find (rkySUB_COOKIE_SELECTOR1);
01032     if ( I->second.end() != J )
01033     {
01034       K = J->second.find (rkySUB_COOKIE_SELECTOR2);
01035       if ( J->second.end() != K )
01036       {
01037         K->second = rktSUB_COOKIE_VALUE_MAP;
01038       }
01039       else
01040       {
01041         J->second.bind (rkySUB_COOKIE_SELECTOR2, rktSUB_COOKIE_VALUE_MAP);
01042       }
01043     }
01044     else
01045     {
01046       tSubSubCookieMapTmp.bind (rkySUB_COOKIE_SELECTOR2, rktSUB_COOKIE_VALUE_MAP);
01047       I->second.bind           (rkySUB_COOKIE_SELECTOR1, tSubSubCookieMapTmp);
01048     }
01049   }
01050   else
01051   {
01052     tSubSubCookieMapTmp.bind  (rkySUB_COOKIE_SELECTOR2, rktSUB_COOKIE_VALUE_MAP);
01053     tSubCookieMapTmp.bind     (rkySUB_COOKIE_SELECTOR1, tSubSubCookieMapTmp);
01054     tStructuredCookieMap.bind (rkyCOOKIE_NAME,          tSubCookieMapTmp);
01055   }
01056 
01057 }  // set()
01058 
01059 
01060 template <typename TCookie>
01061 void mpcl::net::cgi::TSession<TCookie>::
01062 setCookie (const TCookie& rktCOOKIE) const
01063 {
01064 
01065   TStateInformationMapIterator   I = tStateInformationMap.find (rktCOOKIE.name());
01066 
01067   if ( tStateInformationMap.end() != I )
01068   {
01069     I->second.tCookie = rktCOOKIE;
01070   }
01071   else
01072   {
01073     tStateInformationMap.bind (rktCOOKIE.name(), TStateInformation (rktCOOKIE));
01074   }
01075 
01076 }  // setCookie()
01077 
01078 
01079 template <typename TCookie>
01080 void mpcl::net::cgi::TSession<TCookie>::
01081 write (std::basic_ostream<char_type, traits_type>& rtTARGET_OSTREAM) const
01082 {
01083 
01084   TStateInformationMapIterator   I;
01085 
01086   recomposeCookies();
01087   for (I = tStateInformationMap.begin(); ( tStateInformationMap.end() != I ) ;++I)
01088   {
01089     if ( I->second.gKeep )
01090     {
01091       rtTARGET_OSTREAM << I->second.tCookie;
01092     }
01093   }
01094 
01095 }  // write()
01096 
01097 
01098 #endif  // not _MPCL_NET_CGI_SESSION__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18