MoreMotionRequest Class

Top  Previous  Next

When we develop services or other types of classes for MoreMotion environment we use org.moremotion.servlet.MoreMotionRequest class all the time.

Notes for version 5.0

Before version 5.0 the MoreMotion AF units strongly depended on HttpServletRequest and HttpSession objects of the servlet container. This dependency was broken with release 5.0 and MoreMotionRequest become an abstract base class that represents a generic MoreMotion request. New class MoreMotionServletRequest took the whole responsibility of the older MoreMotionRequest class.

To keep up the compatibility some methods still provided on MoreMotioRequest class but marked as deprecated.

The reason for this change is obvious; The MoreMotion AF is no longer a "Http addicted" framework. Only way to start an action in the system was to make request via http but from version 5.0 on any type of request (e.g. a scheduled batch request) can trigger a MoreMotion service.

Request Parameter Decoding with MoreMotionServletRequest

When a request is sent to the server the parameters existing in the request are encoded by the browsers using an encoding, e.g. ISO-8859-1. When an application server receives a request, it decodes these parameters and prepare a request object to pass to the Servlet.

If the application server does not use the same encoding that the browser used to encode the parameters during the decoding, the request parameters can get corrupted. Unfortunately this is the case for Tomcat. Tomcat always decode the request parameters using ISO-8859-1.

MoreMotion solves this problem with the help of a request parameter called _enc. This parameter should contain the encoding that was used in encoding the request parameters.

If you are getting the value of a request parameter from the MoreMotionServletRequest object, the parameters are converted using the value supplied with this _enc parameter.

  package mypack;
 
  import org.moremotion.servlet.*;
  import org.moremotion.action.*;
 
  public class MyActionService implements ActionService {
 
    public void doService(ActionServiceContext asc)
    throws ServiceException, java.io.IOException {
 
      MoreMotionRequest request = asc.getRequest();
      String name = request.getParameter("name",null);
      //..
    }
  } 

The name string will get the converted value of the "name" parameter.

However, some application servers can nicely guess the case and reads request parameters correctly. In that case you should disable the character conversion by setting a sysinfo.xml parameter to false.

Here is the section that you have to change:

  <param name="enableParameterConversion">true</param>

 

HttpServletResponse Objects

Since an MoreMotion programmer does not need to write directly to the HttpServletResponse, this object is not passed as a separate parameter to the other MoreMotion Classes. Most of the time you will have only MoreMotionRequest which contains all the necessary objects in it.

  import org.moremotion.servlet.*;
  import org.moremotion.action.*;
 
  class MyService implements ActionService {
 
    public void doService(ActionServiceContext asc) throws ServiceException, java.io.IOException {
 
      MoreMotionRequest request = asc.getRequest();
 
    } 

  }

 

MoreMotionRequest Methods

 
All the required objects can be accessed via the methods supplied by the MoreMotionRequest object.

 
Accessing to the HttpServletRequest object

HttpServletRequest hsr = ((MoreMotionServletRequest)request).getHttpServletRequest();

 

Accessing to HttpServletResponse object

HttpServletResponse response = ((MoreMotionServletRequest)request).getResponse();

 

 

Accessing to ServletContext object

ServletContext context = request.getContext();

MoreMotionRequest is one of the most important classes of the MoreMotion API since it provides a rich set of methods to perform the common tasks of a MoreMotion Service. See the examples below.
 

 

  // Accessing to the Request Parameters

  String name = request.getParameter("name","defvalue");

  int age = request.getParameterAsInt("age",22);

  boolean retired = request.getParameterAsBoolean("retired",false);

 

  // Accessing to the HttpServlet Objects 

  HttpServletResponse response = ((MoreMotionServletRequest)request).getResponse();

  HttpServletRequest httpRequest = ((MoreMotionServletRequest)request).getHttpServletRequest();

  ServletContext context = request.getContext();

 

  // Managing Cookies

  String val = request.getCookieValue("cookiename");

 

  int oneMonth = 60 * 60 * 24 * 30;

  request.setCookieValue("cookiename","cookievalue",oneMonth);

 

  // Creating Multi-lingual Messages (Creating Message ADOMs)

  request.createMessage("myunit","MY_MESSAGE",

                        new String[]{"argument1", "argument2"}, ex);

 

  // Displaying Pages

  request.generatePage("mypage");

  request.generateDefaultErrorPage(); 

 

 

  // Resolving MScript Functions

  String resolvedStr = request.resolve("Name is : @vof(name)"); 

 

  // Accessing Existing ADOMs

  ADOM adom = request.getSessionADOM("adomname");     

  ADOM adom = request.getApplicationADOM("adomname"); 

 

  // Creating new ADOMs

  ADOM adom = request.newRequestADOM("adomname");     

  ADOM adom = request.newSessionADOM("adomname");     

 

  // Removing ADOMs

  request.removeSessionADOM("adomname");