KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > servlet > ActionServlet


1 /*
2  * $Id: ActionServlet.java,v 1.7 2005/01/05 22:51:07 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.servlet;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServlet JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
48  * @author michelson
49  * @version $$
50  * @since 0.1
51  *
52  *
53  */

54 public abstract class ActionServlet extends HttpServlet JavaDoc {
55     private static final Log log = LogFactory.getLog(MyServlet.class);
56
57     protected Properties JavaDoc actionProperties = null;
58
59     public ActionServlet() {
60
61         // init action descriptions
62
actionProperties = new Properties JavaDoc();
63         InputStream JavaDoc in = getClass().getResourceAsStream(getActionPropertiesPath());
64
65         try {
66             actionProperties.load(in);
67             in.close();
68         } catch (IOException JavaDoc e) {
69             throw new BlogunityRuntimeException(I18NStatusFactory.create(
70                     I18N.ERRORS.ACTION_INITIALIZATION_FAILED, e));
71         }
72     }
73
74     protected String JavaDoc getActionName(HttpServletRequest JavaDoc request) {
75         String JavaDoc 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     /*
91      * (non-Javadoc)
92      *
93      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
94      * javax.servlet.http.HttpServletResponse)
95      */

96     protected void doGet(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1)
97             throws ServletException JavaDoc, IOException JavaDoc {
98         doJob(arg0, arg1);
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
105      * javax.servlet.http.HttpServletResponse)
106      */

107     protected void doPost(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1)
108             throws ServletException JavaDoc, IOException JavaDoc {
109         doJob(arg0, arg1);
110     }
111
112     protected abstract String JavaDoc getActionPropertiesPath();
113
114     protected abstract void doJob(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
115             throws ServletException JavaDoc, IOException JavaDoc;
116
117     protected abstract String JavaDoc getServletPrefix();
118
119     protected abstract String JavaDoc getDefaultActionName();
120
121     protected void executeAction(AbstractAction action, HttpServletRequest JavaDoc request,
122             HttpServletResponse JavaDoc response) throws BlogunityException {
123
124         String JavaDoc 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 JavaDoc 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