Poco::OSP::Web

class WebSession

Library: OSPWeb
Package: Web
Header: Poco/OSP/Web/WebSession.h

Description

A WebSession is used for tracking users between different HTTP(S) requests. WebSession objects are managed by a session manager (usually, an implementation of the WebSessionService).

The WebSession can also be used to store arbitrary data (attributes) in the form of key-value pairs between different requests. Poco::Any is used for storing values, so practically any object can be attached to a session.

A WebSession has a time out. If a WebSession instance is not accessed for a given time, it will be destroyed by the session manager.

Member Summary

Member Functions: access, begin, clear, clientAddress, created, end, erase, find, get, getExpiration, id, onBundleStopping, set, timeout

Types

Attributes

typedef std::map < std::string, Poco::Any > Attributes;

Ptr

typedef Poco::SharedPtr < WebSession > Ptr;

Constructors

WebSession

WebSession(
    const std::string & id,
    int timeoutSeconds,
    const Poco::Net::IPAddress & clientAddress,
    BundleContext::Ptr pContext
);

Creates a new WebSession with the given ID and time out.

Destructor

~WebSession virtual

virtual ~WebSession();

Fires a sessionEnds event and destroys the WebSession.

Member Functions

access

void access();

Updates the expiration time.

begin inline

Attributes::const_iterator begin() const;

Returns the begin iterator for attributes.

clear

void clear();

Erases all attributes.

clientAddress inline

const Poco::Net::IPAddress & clientAddress() const;

Returns the IP address of the client holding the session.

created inline

const Poco::Timestamp & created() const;

Returns the creation time of the session, i.e. the time the user sent the first request.

end inline

Attributes::const_iterator end() const;

Returns the end iterator for attributes.

erase

void erase(
    const std::string & key
);

Erases an attribute value from the session.

find inline

Attributes::const_iterator find(
    const std::string & key
) const;

Searches for an attribute. Returns end() if not found.

get

const Poco::Any & get(
    const std::string & key
) const;

Returns the attribute with the given key. Throws a Poco::NotFoundException if the attribute does not exist.

getExpiration inline

const Poco::Timestamp & getExpiration() const;

Return the time when the session will expire.

id inline

const std::string & id() const;

The unique identifier of the session.

set

void set(
    const std::string & key,
    const Poco::Any & value
);

Sets/Overwrites an attribute value.

timeout inline

int timeout() const;

Returns the timeout of the session in seconds.

onBundleStopping protected

void onBundleStopping(
    const void * pSender,
    BundleEvent & ev
);

When the bundle owning the session is stopped, all attributes are cleared.

After the bundle owning the session has been stopped and its libraries have been unloaded, virtual destructors of objects stored in the session might no longer be available. Therefore all attributes must be removed while their object's destructors are still available.

Variables

sessionEnds

Poco::BasicEvent < const WebSession * > sessionEnds;

Fired before the session object is destroyed.