Time for action - ­ writing the session bean

The SessionBean class definition is straightforward. It will be placed in the package com.packtpub.felix.bookshelf.webapp.beans.

public class SessionBean
{
static final String OSGI_BUNDLECONTEXT = "osgi-bundlecontext";
private BundleContext ctx;
private String sessionId;
public void initialize(ServletContext context) {
this.ctx = (BundleContext)
context.getAttribute(OSGI_BUNDLECONTEXT);
}

The bean's initialize() and getBookshelf() methods together hold similar code to the code that would have been embedded in the JSP, as described previously.

Here, initialize() takes the ServletContext as the parameter and keeps it for later use in getBookshelf() to retrieve the service reference:

public BookshelfService getBookshelf() {
ServiceReference ref = ctx.getServiceReference(
BookshelfService.class.getName());
BookshelfService bookshelf =
(BookshelfService) ctx.getService(ref);
return bookshelf;
}

It also holds a shortcut for checking if the session is valid:

public boolean sessionIsValid() {
return getBookshelf().sessionIsValid(getSessionId());
}

The rest of its methods are the setters and getters for the bookshelf and sessionId properties.

This bean will be defined in a common JSP, init-no-check.inc.jsp, which declares the variable sessionBean and initializes it with the servlet context when the bean is created:

<jsp:useBean id="sessionBean"
class="com.packtpub.felix.bookshelf.webapp.beans.SessionBean"
scope="session">
<% sessionBean.initialize(getServletContext()); %>
</jsp:useBean>

From this point on, any JSP that includes this block has reference to the bookshelf service by calling sessionBean.getBookshelf().

What just happened?

At the point when the Web Container picks up a JSP and makes a servlet out of it, it will create a servlet context assigned to it. This servlet context is populated with a reference to the bundle context of the bundle holding the JSP.

Here we had passed this servlet context to our bean when it was created. It will use it to initialize its BookshelfService reference, which will be available through its getter.

Complete the authentication pages

The main initialization JSP is init.inc.jsp, which in addition to initializing also checks if the sessionId is valid.

<%@ include file="init-no-check.inc.jsp" %>
<% // check session
if (!sessionBean.sessionIsValid()) {
response.sendRedirect("login.jsp");
}
%>

If the session is not valid, it redirects the user to the login.jsp page. Otherwise, the rest of the page is loaded.

The login.jsp page is a simple username/password submit form that sends the request to the login.action.jsp page. It will look like the following:

Complete the authentication pages

The login.action.jsp page takes user and pass as parameters, attempts a login with the bookshelf service, and then updates the sessionId if it is successful:

<% // get authentication paramters
String user = request.getParameter("user");
String pass = request.getParameter("pass");
if (user==null || user.equals("")) {
response.sendRedirect("login.jsp");
}
%>

As this page is setting the authentication information, we include the init-no-check.inc.jsp which does not check for the validity of the session:

<%@ include file="init-no-check.inc.jsp" %>

Then just set the session to the one newly assigned by the bookshelf service:

<% try {
sessionBean.setSessionId(sessionBean.getBookshelf().
login(user, pass.toCharArray()));
}
catch (Throwable t) {
response.sendRedirect("login.jsp");
}
// if success then forward to index.jsp
response.sendRedirect("index.jsp");
%>

The JSP init.inc.jsp will be included at the beginning of all remaining JSPs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.124