Session Class

From JRapid

Jump to: navigation, search

Contents

Introduction

This class stores data for current session and defines some helper methods.

Although you can restrict access to resources in a declarative way through the use of restrict or by customizing the authorization.properties configuration file, sometimes it's necessary to be able to enforce this in your code. It is also very useful to be able to get a reference to the current logged in user.

What you can get from this class

public long getUsername()
Returns the username of current logged user. If no user is logged in, returns null.
public long getUserId()
Returns the id of current logged user. If no user is logged in, returns 0.
public boolean hasPrivilege(String privilege)
Returns true if current logged user has privilege with specified name.
public Schema getSchema()
Returns the schema of current logged user.
public Locale getLocale()
Returns the locale of current logged user. If no user is logged in, returns HTTP request's Locale.
public HttpSession getHttpSession()
Returns the current HttpSession.

Helper methods

public GregorianCalendar parseDate(String value)
Parses a string that must be localized according to current user locale, and returns a GregorianCalendar with the same date.
public GregorianCalendar parseDateTime(String value)
Parses a string that must be localized according to current user locale, and returns a GregorianCalendar with the same date and time.
public DateFormat getDateFormat()
Returns DateFormat for current locale according to java.text.DateFormat, taking care of always returning 4 digits years. For instance,* for EN locale it's 'MM/dd/yyyy'.
public DateFormat getDateTimeFormat()
Returns DateTimeFormat for current locale. For instance, for EN locale it's 'MM/dd/yyyy HH:mm'
public DateFormat getTimeFormat()
Returns TimeFormat for current local. For intance, for EN, DE, ES, PT and FR locales it's 'HH:mm'

Examples

To get the actual Session you should call the static method getMySession().

Session session = Session.getMySession().

Get the userId and get the corresponding User using the DAO interface.

long userId = Session.getMySession.getUserId();
User user = User.DAO().findById(userId);

Getting the HttpSession:

HttpSession httpSession = Session.getMySession.getHttpSession();

Parsing a date:

GregorianCalendar date = Session.getMySession().parseDate("10/03/2010");

Get the application's files repository corresponding to the current logged in user:

File repo = Session.getMySession().getSchema().getRepository();

See also

Personal tools