View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  package org.itracker.web.actions.base;
19  
20  import org.apache.log4j.Level;
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.Action;
23  import org.itracker.model.PermissionType;
24  import org.itracker.services.ITrackerServices;
25  import org.itracker.web.util.LoginUtilities;
26  import org.itracker.web.util.RequestHelper;
27  import org.itracker.web.util.ServletContextUtils;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.http.HttpSession;
33  import java.io.IOException;
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  import java.util.Date;
37  import java.util.Locale;
38  import java.util.Map;
39  import java.util.Set;
40  /**
41   * The base itracker Struts-Action.
42   *
43   * @author Marky Goldstein
44   */
45  public abstract class ItrackerBaseAction extends Action {
46  
47      private static final Logger log = Logger
48              .getLogger(ItrackerBaseAction.class);
49  
50      public ItrackerBaseAction() {
51          super();
52      }
53  
54  
55      /**
56       * @deprecated moved to {@link org.itracker.web.util.RequestHelper}
57       */
58      protected Map<Integer, Set<PermissionType>> getUserPermissions(
59              HttpSession session) {
60          return RequestHelper.getUserPermissions(session);
61      }
62  
63      /**
64       * @deprecated moved to {@link org.itracker.web.util.LoginUtilities}
65       */
66      protected boolean hasPermission(PermissionType permissionNeeded,
67                                      HttpServletRequest request, HttpServletResponse response)
68              throws IOException, ServletException {
69          return LoginUtilities.hasPermission(permissionNeeded, request, response);
70      }
71  
72      @Deprecated
73      protected boolean hasPermission(int permissionNeeded,
74                                      HttpServletRequest request, HttpServletResponse response)
75              throws IOException, ServletException {
76          return LoginUtilities.hasPermission(permissionNeeded, request, response);
77      }
78  
79  
80      /**
81       * @param request - request for base-url
82       * @return normalized base-url for the request
83       */
84      public String getBaseURL(HttpServletRequest request) {
85  
86          String url = getITrackerServices().getConfigurationService().getSystemBaseURL();
87          if (null == url) {
88              url = new StringBuffer(request.getScheme()).append("://").append(
89                      request.getServerName()).append(":").append(
90                      request.getServerPort()).append(request.getContextPath())
91                      .toString();
92  
93              log
94                      .warn("getBaseURL: no base-url is configured, determin from request. ("
95                              + url + ")");
96          }
97          try {
98              return new URL(url).toExternalForm();
99          } catch (MalformedURLException e) {
100             log.warn("failed to get URL normalized, returning manual url: "
101                     + url, e);
102         }
103         return url;
104     }
105 
106     /**
107      * @deprecated  moved to new static method in {@link ServletContextUtils}
108      */
109     protected ITrackerServices getITrackerServices() {
110         ITrackerServices itrackerServices = ServletContextUtils
111                 .getItrackerServices();
112         return itrackerServices;
113     }
114 
115 
116     @Override
117     public Locale getLocale(HttpServletRequest request) {
118         Locale locale = super.getLocale(request);
119         if (null == locale) {
120             locale = LoginUtilities.getCurrentLocale(request);
121         }
122         return locale;
123     }
124 
125     /**
126      * Log time passed since timestamp startTime was set. After logging, the passed startTime-Date is reset to
127      * {@link System#currentTimeMillis()}. This helps on actions performance issues.
128      */
129     protected static void logTimeMillies(String message, Date startTime, Logger log,
130                                          Level level) {
131         if (log.isEnabledFor(level)) {
132             long milliesStart = startTime.getTime();
133             long milliesEnd = System.currentTimeMillis();
134             if (null == log) {
135                 log = ItrackerBaseAction.log;
136             }
137             if (null == level) {
138                 level = Level.INFO;
139             }
140 
141             log.log(level, new StringBuilder().append("logTimeMillies: ").append(
142                     message).append(" took ").append(milliesEnd - milliesStart)
143                     .append("ms.").toString());
144 
145             // reset the timestamp for next log
146             startTime.setTime(System.currentTimeMillis());
147         }
148     }
149 
150 }