View Javadoc

1   /*
2    * $Id: CommandHelper.java 122 2004-11-01 13:06:54Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.util;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.talika.tarsis.Globals;
28  
29  /**
30   * Utility class for command common task in servlets and filters.
31   *
32   * @author  Jose M. Palomar
33   * @version $Revision: 122 $
34   */
35  public final class CommandHelper {
36  
37      // Constants
38  
39      // Fields
40      /**
41       * Singleton instance.
42       */
43      private static final CommandHelper INSTANCE = new CommandHelper();
44  
45      // Constructors
46      /**
47       * Creates a new <code>CommandHelper</code> instance.
48       */
49      private CommandHelper() {
50      }
51  
52      // Methods
53      /**
54       * Obtains a <code>CommandHelper</code> instance.
55       *
56       * @return CommandHelper instance.
57       */
58      public static CommandHelper getInstance() {
59          return INSTANCE;
60      }
61  
62      /**
63       * Obtains command name from request.
64       *
65       * @param request HttpServletRequest client request.
66       * @return String command name or <code>null</code> if none is found.
67       */
68      public String getCommand(HttpServletRequest request) {
69  
70          String commandName = null;
71  
72          String pathInfo = request.getPathInfo();
73          if (pathInfo != null && pathInfo.length() > 0) {
74  
75              commandName = pathInfo.substring(1);
76  
77          }
78          else {
79  
80              String commandParameter = request.getParameter(Globals.COMMAND_NAME_PARAMETER);
81              if (commandParameter != null && commandParameter.length() > 0) {
82                  commandName = commandParameter;
83              }
84  
85          }
86  
87          return commandName;
88  
89      }
90  
91  }