Creating an action that extends the BaseAction class

In this lesson, you create a Java package for a class, and a new MyNewAction action class. Then, you add the referrer-cookie functionality for the new Struts action.

About this task

Examine the source code and note the following code elements:
  • MyNewAction class extends BaseAction by adding functionality to the preProcess method.
  • The preProcess method of MyNewAction has a signature identical to that of BaseAction, as expected by the BaseAction's execute method (which MyNewAction uses verbatim).
  • The flow of MyNewAction's preProcess method is as follows:
    1. Check whether the referrer cookie exists. If the cookie exists the customer's visit to your store is not the customer's first visit to your store during the predetermined time. Call the BaseAction preProcess method to carry out the WebSphere Commerce specific preprocessing logic and to exit.
    2. If the referrer cookie does not exist, check who the current referrer is by examining the Referer header of the HTTP request.
    3. If the referring website is one of the predefined set of referring websites (partner sites), create a referrer cookie with the referring website URL as the value.
    4. Call BaseAction's preProcess method to carry out the WebSphere Commerce - specific pre-processing logic, and exit.
  • The preceding implementation uses the EC_COOKIE_ExternalReferrer and EC_HTTP_ReferrerHeader constants that are defined in the WebSphere Commerce ECConstants class for the name of the referrer cookie and the name of the referrer header, respectively.
  • To make testing easier, the preceding snippet makes the following simplifications:
    • The set of partner sites, which is implemented by the isPartnerSite helper method, is limited to pages that contain the string localhost in the page URL.
    • The referrer cookie that you create uses the default negative value for its maximum age. The negative value means that the cookie expires as soon as the browser instance closes. Normally, you would set the cookie to be stored on the client for a longer length of time, such as two weeks. A higher value for the maximum age, allows for customers to browse upon their first visit to your store, and then return to complete a purchase. To set the maximum age, add the following code after you create myCookie, but before you add myCookie to the response:
      
      myCookie.setMaxAge(
      age);
      

      Where age is the time to expiry in seconds.

Procedure

  1. In the Enterprise Explorer view, navigate to WebSphereCommerceServerExtensionsLogic/src.
  2. Right-click the src folder and select New > Package.
  3. In the New Java Package dialog that opens, enter com.ibm.commerce.sample.struts in the Name field.
  4. Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field, and click Finish.
  5. Create the MyNewAction class:
    1. Navigate to WebSphereCommerceServerExtensionsLogic/src/com.ibm.commerce.sample.struts.
    2. Right-click the com.ibm.commerce.sample.struts package and select New > Class.
    3. In the New Java Class window, enter MyNewAction in the Name field.
    4. Click the Browse button next to the Superclass field. In the Superclass Selection window, select the BaseAction class from the com.ibm.commerce.struts package. If you do not see the BaseAction class in the dialog, in the Choose a type file, type BaseAction. In the Matching items dialog, select the BaseAction class from the com.ibm.commerce.struts package.
    5. Ensure that WebSphereCommerceServerExtensionsLogic/src is specified in the Source Folder field and that com.ibm.commerce.sample.struts is specified in the Package field.
    6. Leave all other options unchanged; click Finish.
  6. Add the referrer-cookie functionality to the struts action:
    1. If the MyNewAction.java class does not open for editing automatically, navigate to WebSphereCommerceServerExtensionsLogic/src/com.ibm.commerce.sample.struts and open the class for editing.
    2. Copy the following code snippet into the body of the MyNewAction.java class:
      
      protected TypedProperty preProcess(RequestHandle handle, ActionMapping mapping, ActionForm form, 
        Map requestParameters, HttpServletRequest request, HttpServletResponse response,String viewName) 
        throws RequestException 
        {
         
          /// See if the referrer cookie already exists.
          
          // Get all the cookies the client has sent with this request.
          Cookie[] cookies = request.getCookies();
          
          // If there are any, see if the referrer cookie is one of them.
          if (cookies != null) 
          {
            for (int i = 0; i < cookies.length; i++)
            {
              String name = cookies[i].getName();
              
              // If it is, done; carry on with BaseAction's preprocessing.
              if
              (name.equals(ECConstants.EC_COOKIE_ExternalReferrer)) 
              {
                return super.preProcess(handle, mapping, form,
                requestParameters, request, response, viewName);
              }
            }
          }
          
          /// No cookies at all or no referrer cookie.
          
          // Get referrer info from header.
          String referrerURL = request
                                     
          .getHeader(ECConstants.EC_HTTP_ReferrerHeader);
          
          // If the referrer is a partner site ...
          if (isPartnerSite(referrerURL)) {
          
          // ... create a new cookie
          Cookie myCookie = new Cookie(
          
          ECConstants.EC_COOKIE_ExternalReferrer, referrerURL);
          
          // ... add it to the response.
          response.addCookie(myCookie);
        }
        
        // Carry on with BaseAction's preprocessing.
        return super.preProcess(handle, mapping, form, requestParameters,
        request, response, viewName);
        
      }
      
      
      private boolean isPartnerSite(String url) 
      {
        return ((url != null) &&
        (url.indexOf("localhost") != -1));
      }
      
    3. In the class editor, the MyNewAction.java class contains several errors. Resolve these errors by adding the appropriate import statements
      1. Right-click the MyNewAction.java class; select Source > Organize Imports.
      2. In the Organize Imports dialog, select com.ibm.commerce.server.ECConstants; click Next.
      3. Select javax.servlet.http.Cookie; click Next.
      4. Select java.util.map; click Next.
      5. Select com.ibm.commerce.datatype.TypedProperty; Click Finish
    4. Save your changes and close the editor.