Login

The Glast Commons web application provides code to be used by other web applications. Using these commons reduces code duplication, makes it possible to easily have a uniform look across Glast web applications and makes it easy to propagate changes across all the applications using the commons.

We also provide a set of utility filters distributed via the org-glast-base package.

Look & Feel

Style Sheet

A common style sheet is provided. It can be embedded in your web application by adding the following code in the head of your code.

<link href="http://glast-ground.slac.stanford.edu/Commons/css/glastCommons.jsp" rel="stylesheet" type="text/css">

The style sheet above can be viewed here.

To overwrite the look and feel of this style sheet just add your application specific style sheet after the line above.

The following is a list of the selectors (i.e. html tags) and classes for which styles are provided.

Selector Class Description
table datatable To be used for Displaytag tables. Within the display:table tag set the class argument to datatable.
table filtertable For tables in forms. It sets a margin to the right and draws a box around the form.
div pageBody To set margin around the body. Include the body of the page in a div tag with the give tag. Easier is used with sitemesh.

Glast Logo

A servlet is provided to create on the fly the image of the Glast logo for your application. Just add the following lines:

<img src="http://glast-ground.slac.stanford.edu/Commons/logoServlet.jsp?title=${title}"/>

where ${title} is the title of your application like shown below:

Title

Date Picker

The Glast Commons also provide Java Scripts and images from Future Shock to create a calendar to pick dates. This calendar can be easily embedded in your web applications as shown in the example below:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<head>
    <script language="JavaScript" src="http://glast-ground.slac.stanford.edu/Commons/scripts/FSdateSelect.jsp"></script>
    <link rel="stylesheet" href="http://glast-ground.slac.stanford.edu/Commons/css/FSdateSelect.css" type="text/css">        
</head>

<c:set var="date" value="${empty param.date ? 'None' : param.date}"/>
<form name="DateForm">
    <table cellpadding="5" cellspacing="5" class="filterTable">
        <tr>
            <th>Choose a Date</th>
            <td> 
            <script language="JavaScript">
                FSfncWriteFieldHTML("DateForm","date","${empty date ? 'None' : date}",100,
                "http://glast-ground.slac.stanford.edu/Commons/images/FSdateSelector/","US",false,true)
            </script></td>
            <th><input type="submit" value="Submit" name="filter"></th>
        </tr>
    </table>
</form>

Menu Bar

The menu bar on top of this page helps users to navigate across GLAST web applications. It can be easily added to any other web application by adding the following include statement on either the desired page or the sitemesh decorator template:

            <c:import url="http://glast-ground.slac.stanford.edu/Commons/menuBar.jsp"/>

IE7 Compatibility: for the menu bar to work in IE7 there has to be a DOCTYPE statement at the beginning of the page:

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">

Filters

In order to include the filters below in your web application you have to add the artifact org.gast.base.web to your project's dependencies:

        <dependency>
            <groupId>glast</groupId>
            <artifactId>org-glast-base-web</artifactId>
            <version>0.3.11</version>
            <type>jar</type>
            <properties>
                <war.bundle>true</war.bundle>
            </properties>
        </dependency>            
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.0</version>
            <properties>
                <war.bundle>true</war.bundle>
            </properties>
        </dependency>
            

Check Cookies Filter

Most of our web application use the tomcat session to store variables; for this to work it is required that the browser has the cookies enabled. This filter checks if any incoming request has the cookies enabled. If the cookies are disabled it prints a message requesting the user to enable the cookies.

To include this filter in your web application add the following lines to WEB-INF/web.xml:

        <!-- Filter to check the cookies -->
        <filter>
            <filter-name>CheckCookiesFilter</filter-name>
            <filter-class>org.glast.base.web.checkcookies.filter.CheckCookiesFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CheckCookiesFilter</filter-name>
            <url-pattern>*.jsp</url-pattern>
        </filter-mapping>
            

Login Filter

Some of the GLAST web application use a CAS server to allow users to login. One of the features of this server is that it is a single sign on service. Once the user is logged in in one application it is logged in in all applications and the username and password are not asked. Nevertheless when a user goes from one application in which he/she has succesfully logged in to another application, the latter application has no way of knowing that the user is already logged in. For this reason the user has to click again on the Log in link to login without being asked username and password. The same holds if a users logs out.

This filter checks if incoming requests come from users that have already logged in (or logged out) with the CAS server. If the user is logged in it sets the user's name in the session's variable userName, if the user is logged out it removes the variable userName from the session's scope.

By adding the Login Filter and testing on the value of the session's variable userName, your web application will be able to automatically detect if a user is already logged in (or out), reducing unnecessary clicking on the user's side.

To include this filter in your web application add the following lines to WEB-INF/web.xml. Make sure that the login filter is added before any other filter that might use the information stored in the session's variable userName.

        <!-- Login Filter -->
        <filter>
            <filter-name>LoginFilter</filter-name>
            <filter-class>org.glast.base.web.login.filter.LoginFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>LoginFilter</filter-name>
            <url-pattern>*.jsp</url-pattern>
        </filter-mapping>
            

Login Tag

To add a login link to your application include the utils taglib and the login tag as shown below. By default, after loggin in with CAS, you will be redirected back to page from which the login request was issued. If you want to go to a different page you can do so by specifying the optional _url_ parameter in the login tag.

                <%@taglib prefix="utils" uri="http://glast-ground.slac.stanford.edu/utils" %>
                <utils:login />
            

MultiPart Filter

This filter allows for form data, including uploaded files, to be submitted using enctype="multipart/form-data".

To include this filter in your web application add the following lines to WEB-INF/web.xml.

        <!-- MultiPart Filter -->
        <filter>
            <filter-name>MultipartFilter</filter-name>
            <filter-class>org.glast.base.web.multipart.filter.MultipartFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>MultipartFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
            

Data Source Filter

This filter allows applications to easily define a pool of possible default data sources, and provides the possibility to switch between them. This is particularly useful when, for example, your application has a production, development and/or test database and you need to switch between them.

The default data source is used in all your sql queries that do not specify a dataSource attribute. So the use of this filter will allow you to redirect all your queries to a different database in just one click.

Filter Definition

To include this filter in your web application add the following lines to WEB-INF/web.xml. The definition of the databases pool is part of the filter's initialization. In this example we define a production and a development database. Please note that the databases still have to be defined in your application server and the name of the database has to be defined in your META_INF/context.xml as an external resource. We also define the order in which the databases will be displayed in the database choose tag (see below) and the default database to be used when a new session is created.

            <filter>
                <filter-name>datasource</filter-name>
                <filter-class>org.glast.base.web.datasource.DataSourceFilter</filter-class>
                <init-param>
                    <param-name>Default</param-name>
                    <param-value>Prod</param-value>
                </init-param>    
                <init-param>
                    <param-name>Order</param-name>
                    <param-value>Prod,Dev</param-value>
                </init-param>    
                <init-param>
                    <param-name>Prod</param-name>
                    <param-value>jdbc/glastisoc</param-value>
                </init-param>    
                <init-param>
                    <param-name>Dev</param-name>
                    <param-value>jdbc/glastisocDEV</param-value>
                </init-param>    
            </filter>
            
            <filter-mapping>
                <filter-name>datasource</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
            

Data Source Chooser Tag

To switch between the pool of databases we provide a simple tag that you can add to your jsp or decorator code. This tag allows a user to switch databases within a session.

                <%@taglib prefix="utils" uri="http://glast-ground.slac.stanford.edu/utils" %>
                <utils:dataSourceChooser/>
            

The tag takes an optional href parameter to redirect to a given url when switching databases.

Default Data Source

To changed the default database the filter provides an administration page with a simple drop down menu to choose the desired database.

Just go to the dataSourceAdmin page within your application and the request will be intercepted and processed by the filter.

Refresh Filter

The RefreshFilter allows developers to define a set of pages that can refresh. This filter works in conjunction with a tag that, for these pages creates a form that allows users to start/stop the refreshing of the page and to change the refresh rate. Once a page is refreshing the RefreshFilter keep track of how many times the page refreshed and stops the refresh after the page has reached the configurable maximum numbers of refresh. If a user leaves a page that is refreshing, the filter stops the refresh and, going back to that page, the user will have to restart it.

Filter Definition

To include this filter in your web application add the following lines to WEB-INF/web.xml. The following are the parameters that can be provided to configure the filter:

  • minRefreshRate the minimum allowed refresh rate. If a user tries to set a lower value, the change will not take effect.
  • refreshRate the initial refresh rate.
  • maxNumberOfRefresh the maximum number of time a page is allowed to refres. Once this limit is reached the page stops refreshing.
  • pagesToRefresh a comma separated list of the pages that can refresh.
  • defaultPage if you want the root page to refresh (i.e. the welcome page) you will have to which one it is in this paramter.
  • preferencesName if you want the filter to use automatically preferences, this is the name of the preference (see below for preferences.
  • preferencesAutoSave true if you want the preferences to be saved automatically when the refresh rate value changes.

The filter saves the refreshing status in a session scope boolean attribute called refreshIsOn. This attribute should be used by application developers to update their application on refresh (like updating the time interval).

    <filter>
        <filter-name>RefreshFilter</filter-name>
        <filter-class>org.glast.base.web.refresh.filter.RefreshFilter</filter-class>
        <init-param>
            <param-name>minRefreshRate</param-name>
            <param-value>30</param-value>
        </init-param>    
        <init-param>
            <param-name>refreshRate</param-name>
            <param-value>30</param-value>
        </init-param>    
        <init-param>
            <param-name>maxNumberOfRefresh</param-name>
            <param-value>30</param-value>
        </init-param>    
        <init-param>
            <param-name>pagesToRefresh</param-name>
            <param-value>index.jsp</param-value>
        </init-param>    
        <init-param>
            <param-name>defaultPage</param-name>
            <param-value>index.jsp</param-value>
        </init-param>    
        <init-param>
            <param-name>preferencesName</param-name>
            <param-value>refreshPreferences</param-value>
        </init-param>    
        <init-param>
            <param-name>preferencesAutoSave</param-name>
            <param-value>true</param-value>
        </init-param>    
    </filter>
    <filter-mapping>
        <filter-name>RefreshFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>    
            

Refresh Tag

Add this tag in your pages, or in your decorator. It will produce a button to turn on/off refreshing for the pages listed in the filter configuration above. When the refresh is turned on, it will also display an input text box with the current refresh rate, so that a user can modify its value.

                <%@taglib prefix="utils" uri="http://glast-ground.slac.stanford.edu/utils" %>
                <utils:refresh />
            

The tag also takes an optional refresh parameter, that is an alternative initial value for the refresh rate. This is particularly useful if used in conjunction with user preferences.

Preferences Filter

The PreferencesFilter allows developers to register any number of preferences beans. Such beans can be easily stored/loaded to/from a database. The filter loads and updates such beans in the session scope of the application depending on the login status of the user. If the user is not logged in default beans are loaded. When the user is logged in, the beans are loaded from a database.

This filter, together with a set of utility tags, allow developers to store simple user preferences, Lists of values, Maps of key value pairs. Please refer to the following documentation on how to write beans and how to use the tags.

Filter Definition

To include this filter in your web application add the following lines to WEB-INF/web.xml. The following are the parameters that can be provided to configure the filter:

  • preferencesDatabase the database used to store the preferences.
  • application the name of your application.
  • ..... any other parameter is taken as a preference name. Its value if the class of the Preference Bean to be loaded.

    <filter>
        <filter-name>PreferencesFilter</filter-name>
        <filter-class>org.glast.base.web.preferences.filter.PreferencesFilter</filter-class>
        <init-param>
            <param-name>preferencesDatabase</param-name>
            <param-value>jdbc/glastgen</param-value>
        </init-param>    
        <init-param>
            <param-name>application</param-name>
            <param-value>ISOCLogging</param-value>
        </init-param>    
        <init-param>
            <param-name>preferences</param-name>
            <param-value>org.glast.logging.application.preferences.UserPreferences</param-value>
        </init-param>    
    </filter>
    <filter-mapping>
        <filter-name>PreferencesFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>    
            

Preferences Tags

A few examples are provided below on how to store different types of preferences and how to use the preferences tags to control their value in a JSP environment. To include the tag definition add the following to your pages:

                <%@taglib prefix="utils" uri="http://glast-ground.slac.stanford.edu/utils" %>
            

Simple Preferences

By simple preference we refer to a property in your Java Bean that has a single value. These values can be manipulated in your application by using code similar to the one below:

    <utils:preferences name="preferences">
        <utils:preference name="refreshRate"      size="5" title="Refresh rate (secs): " />
        <utils:preference name="refreshLastHours" size="5" title="Hours to refresh: "/>
        <utils:preference name="timeSelection"             title="Time Selection: ">
            <utils:value value="Event"/>
            <utils:value value="Posted"/>
        </utils:preference>
    </utils:preferences>
            

The above code generates a form that allows to change the values of the user's preferences. In the case of the timeSelection property we specify that is has a set of allowed values, so a drop down menu is generated. It is also possible to specify that one property allows multiple selection.

Preferences with History

If you want to record the history of a give preference you can define a property that returns a List. We provide a tag that creates an input box; each item added to the input box is stored in the List and the history is displayed below the input box as a set of comman separated links. The links pass in the request the name of the property and its value; the tag itself extracts such parameter from the requests and updates the List.

    <utils:inputWithHistory name="preferences" property="eventSelection" size="50" value=""/>
            

As part of the base package we provide a utility list (org.glast.base.web.utils.HistoryList) that has a maximum size; each new item is added at the top of the list. Items that exceed the maximum size will be dropped.

Complex Preferences: key-value pair

Maps can also be stored as part of the preferences. They are used to store complex information, like the configuraiton of an application, a form or a long user's input. To use maps the implementer has to provide an additional method in conjunction to the getter and setter methods.

    public void addToConfiguration(String configuration, String value) {
        configurationMap.put(configuration,value);
    }
    
    public Map getConfiguration() {
        return configurationMap;
    }
    
    public void setConfiguration(Map configurationMap) {
        this.configurationMap = configurationMap;
    }
            

The above addToConfiguration method is found by introspection and used by a set of tags that allow the manipulation of Map preferences.

        <c:if test="${ ! empty param.SaveConfiguration && ! empty param.configurationName }" >
            <c:url  var="url" value="">
                <c:param name="timeSelection" value=""/>
                <c:param name="loggingLevel" value=""/>
                <c:param name="eventSelection" value=""/>
                <c:param name="tableRowsPerPage" value=""/>
            </c:url>
            <utils:addToPreferences name="preferences" property="configuration" value="" key=""/>
        </c:if>

        <c:if test="false">
            <br>
            <form>
                <table class="filtertable">
                    <tr>
                        <th colspan="3">Configuration</th>
                    </tr>
                    <tr>
                        <td>
                            Save the current configuration as: 
                        </td>
                        <td>
                            <input name="configurationName" type="text"/>
                        </td>
                        <td>
                            <input type="submit" name="SaveConfiguration" value="Save Configuration"/>
                        </td>
                    </tr>
                    <c:if test="${ utils:hasHistory('configuration',preferences) == true}" >
                        <tr>
                            <td align="right">
                                Saved values:
                            </td>
                            <td cellspan="2"><utils:showHistory name="preferences" property="configuration" href="index.jsp"/></td>
                        </tr>
                    </c:if>
                </table>
            </form>
        </c:if>
            

The code above uses the addToPreferences tag to store a url in the Map corresponding to the configuration property. The tag showHistory displays the values stored in the Map as a comma separated list of links in which the key is passed as a parameter with its corresponding value.