001 package com.bea.medrec.actions;
002
003 import com.bea.medrec.beans.ErrorBean;
004 import com.bea.medrec.utils.ClientException;
005 import com.bea.medrec.utils.MedRecLog4jFactory;
006 import com.bea.medrec.utils.MedRecMessageProperties;
007 import com.bea.medrec.utils.MedRecWebAppUtils;
008 import java.lang.reflect.Array;
009 import java.lang.reflect.Constructor;
010 import java.util.*;
011 import javax.servlet.http.HttpServletRequest;
012 import javax.servlet.http.HttpServletResponse;
013 import javax.servlet.http.HttpSession;
014 import org.apache.log4j.Logger;
015 import org.apache.struts.action.Action;
016 import org.apache.struts.action.ActionForm;
017 import org.apache.struts.action.ActionForward;
018 import org.apache.struts.action.ActionMapping;
019 import weblogic.servlet.security.ServletAuthentication;
020
021 /**
022 * <p>Base servlet encapsulating common servlet functionality.</p>
023 *
024 * @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
025 */
026 public abstract class BaseAction extends Action {
027 private static Logger logger =
028 MedRecLog4jFactory.getLogger(BaseAction.class.getName());
029
030 /**
031 * <p>Process the specified HTTP request, and create the corresponding HTTP
032 * response (or forward to another web component that will create it).
033 * Return an <code>ActionForward</code> instance describing where and how
034 * control should be forwarded.
035 * <br>
036 * All subclasses override this method with specific action functionality.
037 * </p>
038 *
039 * @param mapping The ActionMapping used to select this instance
040 * @param form The optional ActionForm bean for this request (if any)
041 * @param request The HTTP request we are processing
042 * @param response The HTTP response we are creating
043 */
044 public abstract ActionForward executeAction(ActionMapping mapping,
045 ActionForm form,
046 HttpServletRequest request,
047 HttpServletResponse response)
048 throws ClientException, Exception;
049
050 /**
051 * <p>Process the specified HTTP request, and create the corresponding HTTP
052 * response (or forward to another web component that will create it).
053 * Return an <code>ActionForward</code> instance describing where and how
054 * control should be forwarded.
055 * <br>
056 * Encapsulates common action functionality including error handling.
057 * </p>
058 *
059 * @param mapping The ActionMapping used to select this instance
060 * @param form The optional ActionForm bean for this request (if any)
061 * @param request The HTTP request we are processing
062 * @param response The HTTP response we are creating
063 */
064 public ActionForward execute(ActionMapping mapping,
065 ActionForm form,
066 HttpServletRequest request,
067 HttpServletResponse response)
068 throws Exception {
069 ActionForward forward;
070 try {
071 forward = executeAction(mapping, form, request, response);
072 if (logger.isDebugEnabled()) printSessionContents(request);
073 } catch (Exception e) {
074 return handleException(e, request, mapping);
075 }
076 return forward;
077 }
078
079 /**
080 * <p>Removes an attribute from HttpSession.</p>
081 *
082 * @param req The HTTP request we are processing
083 * @param name The name of the attribute to be removed.
084 */
085 protected void removeSessionAttribute(HttpServletRequest req, String name) {
086 logger.debug("Removing "+name+" from session.");
087 HttpSession session = req.getSession(false);
088 if (session != null) session.removeAttribute(name);
089 }
090
091 /**
092 * <p>Sets an object to the HttpSession</p>
093 *
094 * @param req The HTTP request we are processing
095 * @param name The name key of object.
096 * @param obj The object to be set on HttpSession.
097 */
098 protected void setSessionAttribute(HttpServletRequest req,
099 String name,
100 Object obj) {
101 logger.debug("Setting "+name+" of type "
102 +obj.getClass().getName()+" on session.");
103 HttpSession session = req.getSession(false);
104 if (session != null) session.setAttribute(name, obj);
105 }
106
107 /**
108 * <p>Retrieves an object from HttpSession.</p>
109 *
110 * @param req The HTTP request we are processing
111 * @param name The name of the attribute to be retrieved.
112 */
113 protected Object getSessionAttribute(HttpServletRequest req, String name) {
114 logger.debug("Getting "+name+" from session.");
115 Object obj = null;
116 HttpSession session = req.getSession(false);
117 if (session != null) obj = session.getAttribute(name);
118 return obj;
119 }
120
121 /**
122 * <p>Prints values set on the current HttpSession.</p>
123 *
124 * @param req The HTTP request we are processing
125 */
126 protected void printSessionContents(HttpServletRequest req) {
127 HttpSession session = req.getSession(false);
128 if (session != null) {
129 Enumeration enum_ = session.getAttributeNames();
130 StringBuffer strBuf = new StringBuffer();
131 strBuf.append("Session contents:");
132 int i = 0;
133 while (enum_.hasMoreElements()) {
134 String name = (String)enum_.nextElement();
135 Object obj = session.getAttribute(name);
136 strBuf.append("\n "+(++i)+") name="+name+"; type="+
137 obj.getClass().getName());
138 }
139 logger.debug(strBuf.toString());
140 }
141 }
142
143 /**
144 * <p>Sets user's locale.</p>
145 *
146 * @param request The HTTP request we are processing
147 */
148 protected void setupLocale(HttpServletRequest request) {
149 logger.debug("Setup locale.");
150 Locale locale = MedRecWebAppUtils.getLocaleFromCookie(request);
151 if (locale == null) locale = getLocale(request);
152 if (!MedRecWebAppUtils.isValidLocale(locale))
153 locale = new Locale("en", "US");
154 logger.debug("Locale: "+locale);
155 setLocale(request, locale);
156 }
157
158 /**
159 * <p>Get localize message.</p>
160 *
161 * @param key The HTTP request we are processing
162 */
163 protected String getMessage(HttpServletRequest request, String key) {
164 Locale locale = getLocale(request);
165 return getResources(request).getMessage(locale, key);
166 }
167
168 /**
169 * <p>Get instance of message properties.</p>
170 *
171 * @param request The HTTP request we are processing
172 */
173 protected MedRecMessageProperties getMessageProps(HttpServletRequest request) {
174 Locale locale = getLocale(request);
175 return MedRecMessageProperties.getInstance(locale, getResources(request));
176 }
177
178 /**
179 * <p>Formulates and throws client exception.<p>
180 *
181 * @param th
182 * @param mapping
183 * @param redirect
184 * @throws ClientException
185 */
186 protected void throwClientException(Throwable th,
187 ActionMapping mapping,
188 String redirect)
189 throws ClientException {
190 if (logger.isDebugEnabled()) th.printStackTrace();
191 else logger.error(th.getMessage());
192 String errorLink = MedRecWebAppUtils.getServletName(mapping, redirect);
193 logger.debug("errorLink: "+errorLink);
194 throw new ClientException(th, errorLink);
195 }
196
197 /**
198 * <p>Uniform way of handling exceptions.<p>
199 *
200 * @param th
201 * @param mapping
202 * @param request
203 *
204 * @return ActionForward
205 */
206 protected ActionForward handleException(Throwable th,
207 HttpServletRequest request,
208 ActionMapping mapping) {
209 if (logger == null || logger.isDebugEnabled())
210 th.printStackTrace();
211 else
212 logger.error(th);
213
214 if (th instanceof ClientException) {
215 String redirectLink = ((ClientException) th).getLink();
216 logger.info("Redirect link: "+redirectLink);
217 ErrorBean errorBean = new ErrorBean(MedRecWebAppUtils.getRootErrMsg(th),
218 redirectLink);
219 request.setAttribute("errorBean", errorBean);
220 return mapping.findForward("error");
221 } else {
222 String link = MedRecWebAppUtils.getServletName(mapping, "home");
223 logger.info("Redirect link: "+link);
224 ErrorBean errorBean = new ErrorBean(MedRecWebAppUtils.getRootErrMsg(th),
225 link);
226 request.setAttribute("errorBean", errorBean);
227 return mapping.findForward("error");
228 }
229 }
230
231 /**
232 * <p>This method determines the next page to which a successful login
233 * should be redirected too. If a user navigates to a secure page, security
234 * will redirect page to the login page. If that user provides accurrate
235 * login credentials then they are redirect page to their initial page. This
236 * is done by ServletAuthentication.getTargetURLForFormAuthentication().</p>
237 */
238 protected ActionForward getRedirectPage(HttpServletRequest request,
239 ActionMapping mapping)
240 {
241 logger.debug("Getting next redirect page.");
242 // Declare local variables.
243 String urlRedirect = null;
244 ActionForward forward = null;
245
246 // Return user to originating page.
247 urlRedirect = ServletAuthentication.getTargetURLForFormAuthentication(
248 request.getSession());
249
250 // Determine redirection.
251 // Check to see if use came from logout page.
252 // If so, redirect to login.success.
253 if (MedRecWebAppUtils.isNotEmpty(urlRedirect)
254 && urlRedirect.contains("logout")) {
255 logger.debug("Redirecting to originating page: "+urlRedirect);
256 forward = new ActionForward(urlRedirect, true);
257 } else {
258 forward = mapping.findForward("login.success");
259 }
260 return forward;
261 }
262
263 /**
264 * <p>String null check.</p>
265 *
266 * @param str
267 */
268 public boolean isNotEmpty(String str) {
269 return str != null && str.length() > 0;
270 }
271
272 /**
273 * <p>String null check.</p>
274 *
275 * @param str
276 */
277 public boolean isEmpty(String str) {
278 return !(isNotEmpty(str));
279 }
280
281 /**
282 * <p>Converts a array to array of given class.</p>
283 *
284 * @param pObjArray Array of objects
285 * @param pClazz Class of newly transformed array
286 * @return Object Array of given Class objects
287 */
288 public Object toBeanArray(Object[] pObjArray, Class pClazz) {
289 if (pObjArray != null && pObjArray.length > 0) {
290 Class cl = pObjArray.getClass().getComponentType();
291 logger.debug("Converting incoming "+cl.getName()+" array (len="+
292 pObjArray.length+") to array of "+pClazz.getName());
293 Object newObjArray = Array.newInstance(pClazz, pObjArray.length);
294 for (int i=0; i<pObjArray.length; i++) {
295 try {
296 Constructor constr = pClazz.getConstructor(new Class[]{cl});
297 logger.debug("Calling: "+constr.getName()+"("+cl+")");
298 Array.set(newObjArray, i,
299 constr.newInstance(new Object[]{pObjArray[i]}));
300 } catch (Exception e) {
301 logger.error("Unable to transform value object array.", e);
302 }
303 }
304 return newObjArray;
305 } else {
306 logger.debug("Incoming array null or len=0");
307 return Array.newInstance(pClazz, 0);
308 }
309 }
310
311 /**
312 * <p>Converts a array to array of given class.</p>
313 *
314 * @param pObjArray Array of objects
315 * @param pClazz Class of newly transformed array
316 * @return Collection Collection of given Class objects
317 */
318 public Collection toCollectionBean(Object[] pObjArray, Class pClazz) {
319 if (pObjArray != null && pObjArray.length > 0) {
320 Class cl = pObjArray.getClass().getComponentType();
321 logger.debug("Converting incoming "+cl.getName()+" array (len="+
322 pObjArray.length+") to array of "+pClazz.getName());
323 Collection<Object> newCollection = new ArrayList<Object>();
324 for (int i=0; i<pObjArray.length; i++) {
325 try {
326 Constructor constr = pClazz.getConstructor(new Class[]{cl});
327 logger.debug("Calling: "+constr.getName()+"("+cl+")");
328 newCollection.add(constr.newInstance(new Object[]{pObjArray[i]}));
329 } catch (Exception e) {
330 logger.error("Unable to transform value object array.", e);
331 }
332 }
333 return newCollection;
334 } else {
335 logger.debug("Incoming array null or len=0");
336 return new ArrayList();
337 }
338 }
339
340 /**
341 * <p>Converts a Collection to array of given class.</p>
342 *
343 * @param objCol Collection of objects
344 * @param pClazz Class of newly transformed array
345 * @return Object Array of given Class objects
346 */
347 protected Object toArray(Collection objCol, Class pClazz) {
348 if (objCol != null && objCol.size() > 0) {
349 Object newObjArray = null;
350 Constructor constr = null;
351 try {
352 newObjArray = Array.newInstance(pClazz, objCol.size());
353 Iterator itr = objCol.iterator();
354 Object obj = (Object) itr.next();
355 Class cl = obj.getClass();
356 logger.debug("Converting incoming "+cl.getName()+" array (len="+
357 objCol.size()+") to array of "+pClazz.getName());
358 logger.debug("Calling: "+constr.getName()+"("+cl+")");
359 constr = pClazz.getConstructor(new Class[]{cl});
360 int i = 0;
361 while (itr.hasNext()) {
362 obj = (Object) itr.next();
363 Array.set(newObjArray, i, constr.newInstance(new Object[]{obj}));
364 i++;
365 }
366 } catch (Exception e) {
367 logger.error("Unable to transform value object array", e);
368 }
369 return newObjArray;
370 } else {
371 logger.debug("Incoming array null or len=0");
372 return Array.newInstance(pClazz, 0);
373 }
374 }
375 }
|