1 25 26 package com.j2biz.blogunity.web.servlet; 27 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.util.Properties ; 31 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServlet ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import com.j2biz.blogunity.exception.BlogunityException; 41 import com.j2biz.blogunity.exception.BlogunityRuntimeException; 42 import com.j2biz.blogunity.i18n.I18N; 43 import com.j2biz.blogunity.i18n.I18NStatusFactory; 44 import com.j2biz.blogunity.web.IActionResult; 45 import com.j2biz.blogunity.web.actions.AbstractAction; 46 47 54 public abstract class ActionServlet extends HttpServlet { 55 private static final Log log = LogFactory.getLog(MyServlet.class); 56 57 protected Properties actionProperties = null; 58 59 public ActionServlet() { 60 61 actionProperties = new Properties (); 63 InputStream in = getClass().getResourceAsStream(getActionPropertiesPath()); 64 65 try { 66 actionProperties.load(in); 67 in.close(); 68 } catch (IOException e) { 69 throw new BlogunityRuntimeException(I18NStatusFactory.create( 70 I18N.ERRORS.ACTION_INITIALIZATION_FAILED, e)); 71 } 72 } 73 74 protected String getActionName(HttpServletRequest request) { 75 String requestAction = request.getRequestURI(); 76 if (log.isDebugEnabled()) { 77 log.debug("Requested action: " + requestAction); 78 } 79 requestAction = requestAction.substring(request.getContextPath().length() 80 + getServletPrefix().length() + 1, requestAction.length()); 81 if (requestAction.length() == 0) { 82 requestAction = getDefaultActionName(); 83 } else 84 requestAction = requestAction.substring(1); 85 86 return requestAction; 87 88 } 89 90 96 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) 97 throws ServletException , IOException { 98 doJob(arg0, arg1); 99 } 100 101 107 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) 108 throws ServletException , IOException { 109 doJob(arg0, arg1); 110 } 111 112 protected abstract String getActionPropertiesPath(); 113 114 protected abstract void doJob(HttpServletRequest request, HttpServletResponse response) 115 throws ServletException , IOException ; 116 117 protected abstract String getServletPrefix(); 118 119 protected abstract String getDefaultActionName(); 120 121 protected void executeAction(AbstractAction action, HttpServletRequest request, 122 HttpServletResponse response) throws BlogunityException { 123 124 String actionName = "/" + getServletPrefix() + "/" + getActionName(request); 125 126 action.init(request, response, actionName); 127 IActionResult result = action.execute(request, response); 128 129 if (result != null) { 130 131 try { 132 if (result.getType() == IActionResult.FORWARD) { 133 request.getRequestDispatcher(result.getPath()).forward(request, response); 134 } else if (result.getType() == IActionResult.INCLUDE) { 135 request.getRequestDispatcher(result.getPath()).include(request, response); 136 } else if (result.getType() == IActionResult.REDIRECT) { 137 response.sendRedirect(request.getContextPath() + result.getPath()); 138 } else { 139 throw new BlogunityException(I18NStatusFactory 140 .create(I18N.ERRORS.ACTION_FORWARD_FAILED)); 141 } 142 } catch (Exception e) { 143 144 if (log.isErrorEnabled()) { 145 log.error("Error executing forward!", e); 146 } 147 148 throw new BlogunityException(I18NStatusFactory.create( 149 I18N.ERRORS.ACTION_FORWARD_FAILED, e)); 150 } 151 152 } else { 153 if (log.isDebugEnabled()) { 154 log.debug("Execution of the action='" + action.getClass() 155 + "' returned result: NULL! do nothing..."); 156 } 157 } 158 159 } 160 161 } | Popular Tags |