001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import java.util.Collection;
019: import java.util.Vector;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import org.acegisecurity.context.SecurityContextHolder;
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.commons.lang.SystemUtils;
025: import org.jamwiki.Environment;
026: import org.jamwiki.WikiBase;
027: import org.jamwiki.WikiConfiguration;
028: import org.jamwiki.WikiException;
029: import org.jamwiki.WikiMessage;
030: import org.jamwiki.WikiVersion;
031: import org.jamwiki.authentication.JAMWikiAnonymousProcessingFilter;
032: import org.jamwiki.authentication.WikiUserAuth;
033: import org.jamwiki.db.DatabaseConnection;
034: import org.jamwiki.db.WikiDatabase;
035: import org.jamwiki.model.Role;
036: import org.jamwiki.model.WikiUser;
037: import org.jamwiki.utils.Encryption;
038: import org.jamwiki.utils.WikiLogger;
039: import org.jamwiki.utils.WikiUtil;
040: import org.springframework.web.servlet.ModelAndView;
041:
042: /**
043: * Used to handle JAMWiki setup, including setting and validating JAMWiki
044: * configuration values.
045: *
046: * @see org.jamwiki.servlets.UpgradeServlet
047: */
048: public class SetupServlet extends JAMWikiServlet {
049:
050: private static final WikiLogger logger = WikiLogger
051: .getLogger(SetupServlet.class.getName());
052: protected static final String JSP_SETUP = "setup.jsp";
053: private static final int MINIMUM_JDK_VERSION = 140;
054:
055: /**
056: * This method handles the request after its parent class receives control.
057: *
058: * @param request - Standard HttpServletRequest object.
059: * @param response - Standard HttpServletResponse object.
060: * @return A <code>ModelAndView</code> object to be handled by the rest of the Spring framework.
061: */
062: protected ModelAndView handleJAMWikiRequest(
063: HttpServletRequest request, HttpServletResponse response,
064: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
065: if (!WikiUtil.isFirstUse()) {
066: throw new WikiException(new WikiMessage(
067: "setup.error.notrequired"));
068: }
069: String function = (request.getParameter("function") == null) ? request
070: .getParameter("override")
071: : request.getParameter("function");
072: if (function == null) {
073: function = "";
074: }
075: try {
076: if (!SystemUtils.isJavaVersionAtLeast(MINIMUM_JDK_VERSION)) {
077: throw new WikiException(new WikiMessage(
078: "setup.error.jdk", new Integer(
079: MINIMUM_JDK_VERSION).toString(), System
080: .getProperty("java.version")));
081: }
082: if (!StringUtils.isBlank(function)
083: && initialize(request, next, pageInfo)) {
084: ServletUtil
085: .redirect(
086: next,
087: WikiBase.DEFAULT_VWIKI,
088: Environment
089: .getValue(Environment.PROP_BASE_DEFAULT_TOPIC));
090: } else {
091: view(request, next, pageInfo);
092: }
093: } catch (Exception e) {
094: handleSetupError(request, next, pageInfo, e);
095: }
096: return next;
097: }
098:
099: /**
100: *
101: */
102: private void handleSetupError(HttpServletRequest request,
103: ModelAndView next, WikiPageInfo pageInfo, Exception e) {
104: // reset properties
105: Environment.setBooleanValue(Environment.PROP_BASE_INITIALIZED,
106: false);
107: if (!(e instanceof WikiException)) {
108: logger.severe("Setup error", e);
109: }
110: try {
111: this .view(request, next, pageInfo);
112: } catch (Exception ex) {
113: logger.severe(
114: "Unable to set up page view object for setup.jsp",
115: ex);
116: }
117: if (e instanceof WikiException) {
118: WikiException we = (WikiException) e;
119: next.addObject("messageObject", we.getWikiMessage());
120: } else {
121: next.addObject("messageObject", new WikiMessage(
122: "error.unknown", e.getMessage()));
123: }
124: }
125:
126: /**
127: *
128: */
129: protected void initParams() {
130: this .layout = false;
131: this .displayJSP = "setup";
132: }
133:
134: /**
135: *
136: */
137: private boolean initialize(HttpServletRequest request,
138: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
139: setProperties(request, next);
140: WikiUserAuth user = setAdminUser(request);
141: Vector errors = validate(request, user);
142: if (!errors.isEmpty()) {
143: this .view(request, next, pageInfo);
144: next.addObject("errors", errors);
145: next.addObject("username", user.getUsername());
146: next.addObject("newPassword", request
147: .getParameter("newPassword"));
148: next.addObject("confirmPassword", request
149: .getParameter("confirmPassword"));
150: return false;
151: }
152: if (previousInstall()
153: && request.getParameter("override") == null) {
154: // user is trying to do a new install when a previous installation exists
155: next.addObject("upgrade", "true");
156: next.addObject("username", user.getUsername());
157: next.addObject("newPassword", request
158: .getParameter("newPassword"));
159: next.addObject("confirmPassword", request
160: .getParameter("confirmPassword"));
161: return false;
162: }
163: Environment.setBooleanValue(Environment.PROP_BASE_INITIALIZED,
164: true);
165: Environment.setValue(Environment.PROP_BASE_WIKI_VERSION,
166: WikiVersion.CURRENT_WIKI_VERSION);
167: if (user == null || !user.hasRole(Role.ROLE_USER)) {
168: throw new IllegalArgumentException(
169: "Cannot pass null or anonymous WikiUserAuth object to setupAdminUser");
170: }
171: WikiBase.reset(request.getLocale(), user);
172: JAMWikiAnonymousProcessingFilter.reset();
173: WikiUserAuth.resetDefaultGroupRoles();
174: Environment.saveProperties();
175: // the setup process does not add new topics to the index (currently)
176: // TODO - remove this once setup uses safe connection handling
177: WikiBase.getSearchEngine().refreshIndex();
178: // force current user credentials to be removed and re-validated.
179: SecurityContextHolder.clearContext();
180: return true;
181: }
182:
183: /**
184: *
185: */
186: private boolean previousInstall() {
187: String driver = Environment
188: .getValue(Environment.PROP_DB_DRIVER);
189: String url = Environment.getValue(Environment.PROP_DB_URL);
190: String userName = Environment
191: .getValue(Environment.PROP_DB_USERNAME);
192: String password = Encryption.getEncryptedProperty(
193: Environment.PROP_DB_PASSWORD, null);
194: try {
195: DatabaseConnection.testDatabase(driver, url, userName,
196: password, true);
197: } catch (Exception e) {
198: // no previous database, all good
199: return false;
200: }
201: return true;
202: }
203:
204: /**
205: *
206: */
207: private WikiUserAuth setAdminUser(HttpServletRequest request)
208: throws Exception {
209: String username = request.getParameter("username");
210: WikiUserAuth user = new WikiUserAuth(username);
211: user.setPassword(Encryption.encrypt(request
212: .getParameter("newPassword")));
213: user.setCreateIpAddress(ServletUtil.getIpAddress(request));
214: user.setLastLoginIpAddress(ServletUtil.getIpAddress(request));
215: return user;
216: }
217:
218: /**
219: *
220: */
221: private void setProperties(HttpServletRequest request,
222: ModelAndView next) throws Exception {
223: Environment.setValue(Environment.PROP_BASE_FILE_DIR, request
224: .getParameter(Environment.PROP_BASE_FILE_DIR));
225: Environment
226: .setValue(
227: Environment.PROP_FILE_DIR_FULL_PATH,
228: request
229: .getParameter(Environment.PROP_FILE_DIR_FULL_PATH));
230: Environment
231: .setValue(
232: Environment.PROP_FILE_DIR_RELATIVE_PATH,
233: request
234: .getParameter(Environment.PROP_FILE_DIR_RELATIVE_PATH));
235: Environment
236: .setValue(
237: Environment.PROP_BASE_PERSISTENCE_TYPE,
238: request
239: .getParameter(Environment.PROP_BASE_PERSISTENCE_TYPE));
240: if (Environment
241: .getValue(Environment.PROP_BASE_PERSISTENCE_TYPE)
242: .equals(WikiBase.PERSISTENCE_EXTERNAL)) {
243: Environment.setValue(Environment.PROP_DB_DRIVER, request
244: .getParameter(Environment.PROP_DB_DRIVER));
245: Environment.setValue(Environment.PROP_DB_TYPE, request
246: .getParameter(Environment.PROP_DB_TYPE));
247: Environment.setValue(Environment.PROP_DB_URL, request
248: .getParameter(Environment.PROP_DB_URL));
249: Environment.setValue(Environment.PROP_DB_USERNAME, request
250: .getParameter(Environment.PROP_DB_USERNAME));
251: Encryption.setEncryptedProperty(
252: Environment.PROP_DB_PASSWORD,
253: request.getParameter(Environment.PROP_DB_PASSWORD),
254: null);
255: next.addObject("dbPassword", request
256: .getParameter(Environment.PROP_DB_PASSWORD));
257: } else {
258: WikiDatabase
259: .setupDefaultDatabase(Environment.getInstance());
260: }
261: }
262:
263: /**
264: *
265: */
266: private Vector validate(HttpServletRequest request, WikiUser user)
267: throws Exception {
268: Vector errors = ServletUtil.validateSystemSettings(Environment
269: .getInstance());
270: if (StringUtils.isBlank(user.getUsername())) {
271: errors.add(new WikiMessage("error.loginempty"));
272: }
273: String newPassword = request.getParameter("newPassword");
274: String confirmPassword = request
275: .getParameter("confirmPassword");
276: if (newPassword != null || confirmPassword != null) {
277: if (newPassword == null) {
278: errors.add(new WikiMessage("error.newpasswordempty"));
279: } else if (confirmPassword == null) {
280: errors.add(new WikiMessage("error.passwordconfirm"));
281: } else if (!newPassword.equals(confirmPassword)) {
282: errors.add(new WikiMessage(
283: "admin.message.passwordsnomatch"));
284: }
285: }
286: return errors;
287: }
288:
289: /**
290: *
291: */
292: private void view(HttpServletRequest request, ModelAndView next,
293: WikiPageInfo pageInfo) throws Exception {
294: pageInfo.setContentJsp(JSP_SETUP);
295: pageInfo.setSpecial(true);
296: pageInfo.setPageTitle(new WikiMessage("setup.title"));
297: Collection dataHandlers = WikiConfiguration.getInstance()
298: .getDataHandlers();
299: next.addObject("dataHandlers", dataHandlers);
300: WikiMessage logMessage = new WikiMessage("setup.help.logfile",
301: WikiLogger.getDefaultLogFile(), WikiLogger
302: .getLogConfigFile());
303: next.addObject("logMessage", logMessage);
304: }
305: }
|