1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16
17 */
18
19 package org.bejug.javacareers.jobs.view.jsf.action;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpSession;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.xerces.impl.dv.util.Base64;
30 import org.bejug.javacareers.common.search.SearchCriteriaFactory;
31 import org.bejug.javacareers.common.search.SearchCriteriaService;
32 import org.bejug.javacareers.jobs.view.jsf.util.Utils;
33
34
35 /***
36 * @author Peter Symoens (last modified by $Author: shally $)
37 * @version $Revision: 1.16 $ - $Date: 2005/12/20 15:36:47 $
38 */
39 public class BaseAction {
40
41 /***
42 * The Job CommentAction logger.
43 */
44 private static final Log LOG = LogFactory.getLog(BaseAction.class);
45
46
47 /***
48 * Variable that holds the transaction token to be set on the HttpSession
49 */
50 public static final String TRANSACTION_TOKEN_KEY = "TRANSACTION_TOKEN_KEY";
51
52 /***
53 * Variable that holds the transaction token to be set on the HttpServletRequest
54 */
55 public static final String TOKEN_KEY = "TOKEN_KEY";
56
57
58 /***
59 * The navigation rule
60 */
61 protected String outcome;
62
63 /***
64 * The transaction token
65 */
66 protected String token;
67
68
69 /***
70 * the factory for the searchCriteria.
71 */
72 protected SearchCriteriaFactory searchCriteriaFactory;
73
74 /***
75 * search service to use.
76 */
77 protected SearchCriteriaService searchCriteriaService;
78
79 /***
80 * Save the transaction token on the HttpSession
81 */
82 protected void saveToken() {
83
84 HttpSession httpSession = Utils.getSession();
85 token = generateToken();
86 httpSession.setAttribute(TRANSACTION_TOKEN_KEY, token);
87 }
88
89
90 /***
91 * @return the generated token based on the HttpSession id and current time.
92 */
93 public String generateToken() {
94 String generatedtoken = Utils.getSession().getId();
95 SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmssS");
96 generatedtoken = generatedtoken.concat(dateFormat.format(new Date()));
97 generatedtoken = Base64.encode(generatedtoken.getBytes());
98 int index = generatedtoken.indexOf("\n");
99 if(index != -1){
100
101 generatedtoken = generatedtoken.substring(0, index);
102 }
103 HttpServletRequest request = (HttpServletRequest) Utils.getExternalContext().getRequest();
104 request.setAttribute(BaseAction.TOKEN_KEY, generatedtoken);
105 return generatedtoken;
106 }
107
108 /***
109 *
110 * @return true if the transaction token key equals the token key.
111 */
112 public boolean isTokenValid() {
113 HttpSession session = Utils.getSession();
114 if (session == null) {
115 return false;
116 }
117 String savedToken = (String) session.getAttribute(BaseAction.TRANSACTION_TOKEN_KEY);
118 if (savedToken == null) {
119 return false;
120 }
121 HttpServletRequest request = (HttpServletRequest) Utils.getExternalContext().getRequest();
122 String transactionToken = request.getParameter(BaseAction.TOKEN_KEY);
123
124
125 if (transactionToken == null) {
126 return false;
127 }
128 return (savedToken.equals(transactionToken));
129 }
130
131
132 /***
133 * @return outcome String
134 */
135 public String doOutcome() {
136 LOG.info("Debug: FORWARDING to " + outcome);
137 return outcome;
138 }
139
140 /***
141 *
142 * @return searchCriteriaFactory
143 */
144 public SearchCriteriaFactory getSearchCriteriaFactory() {
145 return searchCriteriaFactory;
146 }
147
148 /***
149 *
150 * @param searchCriteriaFactory .
151 */
152 public void setSearchCriteriaFactory(SearchCriteriaFactory searchCriteriaFactory) {
153 this.searchCriteriaFactory = searchCriteriaFactory;
154 }
155
156 /***
157 *
158 * @return searchCriteriaService
159 */
160 public SearchCriteriaService getSearchCriteriaService() {
161 return searchCriteriaService;
162 }
163
164 /***
165 *
166 * @param searchCriteriaService .
167 */
168 public void setSearchCriteriaService(SearchCriteriaService searchCriteriaService) {
169 this.searchCriteriaService = searchCriteriaService;
170 }
171
172 /***
173 *
174 * @return token
175 */
176 public String getToken() {
177 HttpServletRequest request = (HttpServletRequest) Utils.getExternalContext().getRequest();
178 String keytoken = (String)request.getAttribute(TOKEN_KEY);
179 return keytoken;
180 }
181
182 }
183
184 /***
185 * $Log: BaseAction.java,v $
186 * Revision 1.16 2005/12/20 15:36:47 shally
187 * CheckStyle and PMD changes.
188 *
189 * Revision 1.15 2005/12/08 14:53:46 shally
190 * Opkuis voor checkstyle.
191 *
192 * Revision 1.14 2005/09/30 14:38:07 bavo_jcs
193 * Fixed URL
194 *
195 * Revision 1.13 2005/09/20 15:09:29 schauwvliege
196 * Admin approved content V1
197 *
198 * Revision 1.12 2005/08/30 13:07:47 psong09
199 * renamed author: psong09
200 *
201 * Revision 1.11 2005/08/24 12:32:12 psong09
202 * Added spinner component and renamed commentAction
203 *
204 * Revision 1.10 2005/08/23 10:18:47 bme_jcs
205 * resolved checkstyle and pmd errors
206 *
207 * Revision 1.9 2005/08/23 08:50:38 ge0ffrey
208 * JAVACAREERS-284 part 3: resolver
209 *
210 * Revision 1.8 2005/08/22 15:25:05 ge0ffrey
211 * JAVACAREERS-284
212 *
213 * Revision 1.7 2005/08/22 13:38:34 psong09
214 * Added error page for synchronisation error
215 *
216 * Revision 1.6 2005/08/19 13:13:53 psong09
217 * introduced synchroniser token
218 *
219 * Revision 1.5 2005/08/10 09:04:49 bavo_jcs
220 * Optimized imports according to checkstyle
221 *
222 * Revision 1.4 2005/08/09 12:59:56 bavo_jcs
223 * Optimized imports
224 *
225 * Revision 1.3 2005/06/14 12:05:53 schauwvliege
226 * CheckStyle and fixing tests
227 *
228 * Revision 1.2 2005/06/09 08:18:52 bejug_cc
229 * Fix initial import
230 *
231 * Revision 1.8 2005/06/07 15:16:46 PSONG09
232 * added todo
233 *
234 * Revision 1.7 2005/06/03 15:40:10 PSONG09
235 * removed import
236 *
237 * Revision 1.6 2005/06/02 15:48:36 PSONG09
238 * added searchcriteria
239 *
240 * Revision 1.5 2005/05/19 13:55:24 PSONG09
241 * update resume component + refactoring cv to resume
242 *
243 * Revision 1.4 2005/05/17 15:39:05 PSONG09
244 * update jobsearch functionality
245 *
246 * Revision 1.3 2005/05/09 14:54:45 ssc
247 * checkstyle
248 *
249 * Revision 1.2 2005/04/29 18:17:34 sja
250 * General cleanup (naming conv., javadoc and CVS tags)
251 *
252 */