Project

General

Profile

1 4080 daigle
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements utility methods for a metadata catalog
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.admin;
28
29
import java.io.IOException;
30
import java.util.Vector;
31
32
import javax.servlet.ServletConfig;
33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpServlet;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpSession;
38
39
import org.apache.log4j.Logger;
40
41 4200 daigle
import edu.ucsb.nceas.metacat.MetaCatServlet;
42 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
43 4760 daigle
import edu.ucsb.nceas.metacat.service.ServiceException;
44
import edu.ucsb.nceas.metacat.service.ServiceService;
45 4080 daigle
import edu.ucsb.nceas.metacat.service.SessionService;
46 4592 daigle
import edu.ucsb.nceas.metacat.util.AuthUtil;
47 4704 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
48 4080 daigle
import edu.ucsb.nceas.metacat.util.RequestUtil;
49
import edu.ucsb.nceas.metacat.util.SkinUtil;
50
import edu.ucsb.nceas.metacat.util.SystemUtil;
51
import edu.ucsb.nceas.metacat.util.UtilException;
52 4760 daigle
import edu.ucsb.nceas.utilities.GeneralPropertyException;
53 4080 daigle
54
/**
55
 * Entry servlet for the metadata configuration utility
56
 */
57
public class MetaCatAdminServlet extends HttpServlet {
58
59
	private static final long serialVersionUID = 1L;
60
61
	private Logger logMetacat = Logger.getLogger(MetaCatAdminServlet.class);
62
63
    /**
64
	 * Initialize the servlet
65
	 */
66
	public void init(ServletConfig config) throws ServletException {
67
		super.init(config);
68
	}
69
70
    /** Handle "GET" method requests from HTTP clients */
71
	public void doGet(HttpServletRequest request, HttpServletResponse response)
72
			throws ServletException, IOException {
73
74
		// Process the data and send back the response
75
		handleGetOrPost(request, response);
76
	}
77
78
	/** Handle "POST" method requests from HTTP clients */
79
	public void doPost(HttpServletRequest request, HttpServletResponse response)
80
			throws ServletException, IOException {
81
82
		// Process the data and send back the response
83
		handleGetOrPost(request, response);
84
	}
85
86
    /**
87
	 * Control servlet response depending on the action parameter specified
88
	 *
89
	 * @param request
90
	 *            the http request information
91
	 * @param response
92
	 *            the http response to be sent back to the client
93
	 */
94
	private void handleGetOrPost(HttpServletRequest request,
95
			HttpServletResponse response) throws ServletException, IOException {
96
		String action = request.getParameter("configureType");
97
		logMetacat.debug("Processing admin action: " + action);
98
		Vector<String> processingMessage = new Vector<String>();
99
		Vector<String> processingErrors = new Vector<String>();
100
101
		try {
102
			// Update the last update time for this user if they are not new
103
			HttpSession httpSession = request.getSession(false);
104
			if (httpSession != null) {
105
				SessionService.touchSession(httpSession.getId());
106
			}
107
108 4760 daigle
			if (!MetacatUtil.isBackupDirConfigured()) {
109
				String discoveredBackupDir = SystemUtil.discoverExternalBaseDir();
110
				if (discoveredBackupDir == null) {
111
					// if the backup dir has not been configured, and the system
112
					// cannot find a suitable directory, then show the backup
113
					// directory configuration screen.
114
					processingMessage.add("You must configure the backup directory"
115
							+ " before you can continue with Metacat configuration.");
116
					RequestUtil.setRequestMessage(request, processingMessage);
117
					action = "backup";
118
					logMetacat.debug("Admin action changed to 'backup'");
119
				} else {
120
					// if the backup dir has not been configured, and the system
121
					// can find a suitable directory, then set the application.backupDir
122
					// property to the discovered value
123
					PropertyService.setProperty("application.backupDir", discoveredBackupDir);
124
					ServiceService.refreshService("PropertyService");
125
				}
126 4704 daigle
			} else if (!AuthUtil.isAuthConfigured()) {
127
				// if authentication isn't configured, change the action to auth.
128
				// Authentication needs to be set up before we do anything else
129 4592 daigle
				processingMessage.add("You must configure authentication before "
130 4080 daigle
						+ "you can continue with MetaCat configuration.");
131
				RequestUtil.setRequestMessage(request, processingMessage);
132 4592 daigle
				action = "auth";
133
				logMetacat.debug("Admin action changed to 'auth'");
134 4704 daigle
			} else if (!AuthUtil.isUserLoggedInAsAdmin(request)) {
135
				// If auth is configured, see if the user is logged in
136
				// as an administrator.  If not, they need to log in before
137
				// they can continue with configuration.
138 4080 daigle
				processingMessage.add("You must log in as an administrative " + "" +
139
						"user before you can continue with MetaCat configuration.");
140
				RequestUtil.setRequestMessage(request, processingMessage);
141
				action = "login";
142
				logMetacat.debug("Admin action changed to 'login'");
143 4704 daigle
			}
144 4080 daigle
145
			if (action == null || action.equals("configure")) {
146
				// Forward the request main configuration page
147
				request.setAttribute("metaCatVersion", SystemUtil.getMetacatVersion());
148
			    request.setAttribute("propsConfigured", new Boolean(PropertyService.arePropertiesConfigured()));
149 4592 daigle
			    request.setAttribute("authConfigured", new Boolean(AuthUtil.isAuthConfigured()));
150 4200 daigle
			    // TODO MCD figure out if we still need an org section
151
			    //request.setAttribute("orgsConfigured", new Boolean(OrganizationUtil.areOrganizationsConfigured()));
152 4080 daigle
			    request.setAttribute("skinsConfigured", new Boolean(SkinUtil.areSkinsConfigured()));
153 4704 daigle
			    request.setAttribute("metacatConfigured", new Boolean(MetacatUtil.isMetacatConfigured()));
154 4200 daigle
			    request.setAttribute("geoserverConfigured",
155
			    		PropertyService.getProperty("configutil.geoserverConfigured"));
156
			    request.setAttribute("metcatServletInitialized", MetaCatServlet.isFullyInitialized());
157 4479 daigle
			    if (PropertyService.arePropertiesConfigured()) {
158
					request.setAttribute("databaseVersion",
159
							DBAdmin.getInstance().getDBVersion());
160
				}
161 4080 daigle
				RequestUtil.forwardRequest(request, response,
162
						"/admin/metacat-configuration.jsp?configureType=configure");
163
				return;
164
			} else if (action.equals("properties")) {
165
				// process properties
166
				PropertiesAdmin.getInstance().configureProperties(request,
167
						response);
168
				return;
169
			} else if (action.equals("skins")) {
170
				// process skins
171
				SkinsAdmin.getInstance().configureSkins(request, response);
172
				return;
173
			} else if (action.equals("database")) {
174 4159 daigle
				// process database
175 4080 daigle
				DBAdmin.getInstance().configureDatabase(request, response);
176
				return;
177 4592 daigle
			} else if (action.equals("auth")) {
178
				// process authentication
179
				AuthAdmin.getInstance().configureAuth(request, response);
180 4080 daigle
				return;
181
			} else if (action.equals("login")) {
182 4159 daigle
				// process login
183 4592 daigle
				LoginAdmin.getInstance().authenticateUser(request, response);
184 4080 daigle
				return;
185 4704 daigle
			} else if (action.equals("backup")) {
186
				// process login
187
				BackupAdmin.getInstance().configureBackup(request, response);
188
				return;
189 4200 daigle
			} else if (action.equals("geoserver")) {
190
				// process geoserver password change
191
				GeoserverAdmin.getInstance().configureGeoserver(request, response);
192
				return;
193 4080 daigle
			} else {
194
				String errorMessage = "Invalid action in configuration request: " + action;
195
				logMetacat.error(errorMessage);
196
				processingErrors.add(errorMessage);
197 4200 daigle
			}
198 4760 daigle
		} catch (GeneralPropertyException ge) {
199 4080 daigle
			String errorMessage =
200 4760 daigle
				"Property problem while handling request: " + ge.getMessage();
201 4080 daigle
			logMetacat.error(errorMessage);
202
			processingErrors.add(errorMessage);
203
		} catch (AdminException ae) {
204
			String errorMessage =
205
				"Admin problem while handling request: " + ae.getMessage();
206
			logMetacat.error(errorMessage);
207
			processingErrors.add(errorMessage);
208
		} catch (UtilException ue) {
209
			String errorMessage =
210
				"Utility problem while handling request: " + ue.getMessage();
211
			logMetacat.error(errorMessage);
212
			processingErrors.add(errorMessage);
213 4760 daigle
		} catch (ServiceException se) {
214
			String errorMessage =
215
				"Utility problem while handling request: " + se.getMessage();
216
			logMetacat.error(errorMessage);
217
			processingErrors.add(errorMessage);
218 4080 daigle
		}
219
220
		if (processingErrors.size() > 0) {
221
			RequestUtil.clearRequestMessages(request);
222
			RequestUtil.setRequestErrors(request,processingErrors);
223 4479 daigle
			// if the action that threw an exception was "configure" just go straight to the metacat
224
			// configuration.  This will avoid a loop.  Otherwise, call the admin servlet with
225
			// configuration action.
226
			if (action != null && action.equals("configure")) {
227
				RequestUtil.forwardRequest(request, response, "/admin/metacat-configuration.jsp");
228
			} else {
229
				RequestUtil.forwardRequest(request, response, "/admin?configureType=configure");
230
			}
231 4080 daigle
		}
232
	}
233
}