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 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
43 4080 daigle
import edu.ucsb.nceas.metacat.service.SessionService;
44 5015 daigle
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
45 6669 leinfelder
import edu.ucsb.nceas.metacat.shared.ServiceException;
46 4592 daigle
import edu.ucsb.nceas.metacat.util.AuthUtil;
47 5026 daigle
import edu.ucsb.nceas.metacat.util.ConfigurationUtil;
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 4760 daigle
import edu.ucsb.nceas.utilities.GeneralPropertyException;
52 4080 daigle
53
/**
54
 * Entry servlet for the metadata configuration utility
55
 */
56 5026 daigle
public class MetacatAdminServlet extends HttpServlet {
57 4080 daigle
58
	private static final long serialVersionUID = 1L;
59
60 5026 daigle
	private Logger logMetacat = Logger.getLogger(MetacatAdminServlet.class);
61 4080 daigle
62
    /**
63
	 * Initialize the servlet
64
	 */
65
	public void init(ServletConfig config) throws ServletException {
66
		super.init(config);
67
	}
68
69
    /** Handle "GET" method requests from HTTP clients */
70
	public void doGet(HttpServletRequest request, HttpServletResponse response)
71
			throws ServletException, IOException {
72
73
		// Process the data and send back the response
74
		handleGetOrPost(request, response);
75
	}
76
77
	/** Handle "POST" method requests from HTTP clients */
78
	public void doPost(HttpServletRequest request, HttpServletResponse response)
79
			throws ServletException, IOException {
80
81
		// Process the data and send back the response
82
		handleGetOrPost(request, response);
83
	}
84
85
    /**
86
	 * Control servlet response depending on the action parameter specified
87
	 *
88
	 * @param request
89
	 *            the http request information
90
	 * @param response
91
	 *            the http response to be sent back to the client
92
	 */
93
	private void handleGetOrPost(HttpServletRequest request,
94
			HttpServletResponse response) throws ServletException, IOException {
95
		String action = request.getParameter("configureType");
96 5030 daigle
		logMetacat.debug("MetacatAdminServlet.handleGetOrPost - Processing admin action: " + action);
97 4080 daigle
		Vector<String> processingMessage = new Vector<String>();
98
		Vector<String> processingErrors = new Vector<String>();
99
100
		try {
101
			// Update the last update time for this user if they are not new
102
			HttpSession httpSession = request.getSession(false);
103
			if (httpSession != null) {
104 5374 berkley
				SessionService.getInstance().touchSession(httpSession.getId());
105 4080 daigle
			}
106
107 5026 daigle
			if (!ConfigurationUtil.isBackupDirConfigured()) {
108 4762 daigle
				// if the backup dir has not been configured, then show the
109
				// backup directory configuration screen.
110
				processingMessage.add("You must configure the backup directory"
111
						+ " before you can continue with Metacat configuration.");
112
				RequestUtil.setRequestMessage(request, processingMessage);
113
				action = "backup";
114 5030 daigle
				logMetacat.debug("MetacatAdminServlet.handleGetOrPost - Admin action changed to 'backup'");
115 4704 daigle
			} else if (!AuthUtil.isAuthConfigured()) {
116
				// if authentication isn't configured, change the action to auth.
117
				// Authentication needs to be set up before we do anything else
118 4592 daigle
				processingMessage.add("You must configure authentication before "
119 4080 daigle
						+ "you can continue with MetaCat configuration.");
120
				RequestUtil.setRequestMessage(request, processingMessage);
121 4592 daigle
				action = "auth";
122 5030 daigle
				logMetacat.debug("MetacatAdminServlet.handleGetOrPost - Admin action changed to 'auth'");
123 4704 daigle
			} else if (!AuthUtil.isUserLoggedInAsAdmin(request)) {
124
				// If auth is configured, see if the user is logged in
125
				// as an administrator.  If not, they need to log in before
126
				// they can continue with configuration.
127 4080 daigle
				processingMessage.add("You must log in as an administrative " + "" +
128
						"user before you can continue with MetaCat configuration.");
129
				RequestUtil.setRequestMessage(request, processingMessage);
130
				action = "login";
131 5030 daigle
				logMetacat.debug("MetacatAdminServlet.handleGetOrPost - Admin action changed to 'login'");
132 4704 daigle
			}
133 4080 daigle
134
			if (action == null || action.equals("configure")) {
135
				// Forward the request main configuration page
136
				request.setAttribute("metaCatVersion", SystemUtil.getMetacatVersion());
137
			    request.setAttribute("propsConfigured", new Boolean(PropertyService.arePropertiesConfigured()));
138 4592 daigle
			    request.setAttribute("authConfigured", new Boolean(AuthUtil.isAuthConfigured()));
139 4200 daigle
			    // TODO MCD figure out if we still need an org section
140
			    //request.setAttribute("orgsConfigured", new Boolean(OrganizationUtil.areOrganizationsConfigured()));
141 4080 daigle
			    request.setAttribute("skinsConfigured", new Boolean(SkinUtil.areSkinsConfigured()));
142 5026 daigle
			    request.setAttribute("metacatConfigured", new Boolean(ConfigurationUtil.isMetacatConfigured()));
143 4200 daigle
			    request.setAttribute("geoserverConfigured",
144
			    		PropertyService.getProperty("configutil.geoserverConfigured"));
145 6635 leinfelder
			    request.setAttribute("dataoneConfigured",
146
			    		PropertyService.getProperty("configutil.dataoneConfigured"));
147 4200 daigle
			    request.setAttribute("metcatServletInitialized", MetaCatServlet.isFullyInitialized());
148 4479 daigle
			    if (PropertyService.arePropertiesConfigured()) {
149
					request.setAttribute("databaseVersion",
150
							DBAdmin.getInstance().getDBVersion());
151 5004 daigle
					request.setAttribute("contextURL", SystemUtil.getContextURL());
152 4479 daigle
				}
153 4080 daigle
				RequestUtil.forwardRequest(request, response,
154 5026 daigle
						"/admin/metacat-configuration.jsp?configureType=configure", null);
155 4080 daigle
				return;
156
			} else if (action.equals("properties")) {
157
				// process properties
158
				PropertiesAdmin.getInstance().configureProperties(request,
159
						response);
160
				return;
161
			} else if (action.equals("skins")) {
162
				// process skins
163
				SkinsAdmin.getInstance().configureSkins(request, response);
164
				return;
165
			} else if (action.equals("database")) {
166 4159 daigle
				// process database
167 4080 daigle
				DBAdmin.getInstance().configureDatabase(request, response);
168
				return;
169 4592 daigle
			} else if (action.equals("auth")) {
170
				// process authentication
171
				AuthAdmin.getInstance().configureAuth(request, response);
172 4080 daigle
				return;
173
			} else if (action.equals("login")) {
174 4159 daigle
				// process login
175 4592 daigle
				LoginAdmin.getInstance().authenticateUser(request, response);
176 4080 daigle
				return;
177 4704 daigle
			} else if (action.equals("backup")) {
178
				// process login
179
				BackupAdmin.getInstance().configureBackup(request, response);
180
				return;
181 4200 daigle
			} else if (action.equals("geoserver")) {
182
				// process geoserver password change
183
				GeoserverAdmin.getInstance().configureGeoserver(request, response);
184
				return;
185 6635 leinfelder
			} else if (action.equals("dataone")) {
186
				// process dataone config
187
				D1Admin.getInstance().configureDataONE(request, response);
188
				return;
189 6669 leinfelder
			} else if (action.equals("replication")) {
190
				// process replication config
191
				ReplicationAdmin.getInstance().handleRequest(request, response);
192
				return;
193 4080 daigle
			} else {
194 5030 daigle
				String errorMessage = "MetacatAdminServlet.handleGetOrPost - Invalid action in configuration request: " + action;
195 4080 daigle
				logMetacat.error(errorMessage);
196
				processingErrors.add(errorMessage);
197 4200 daigle
			}
198 5076 daigle
199
			if (processingErrors.size() > 0) {
200
				RequestUtil.clearRequestMessages(request);
201
				RequestUtil.setRequestErrors(request,processingErrors);
202
				// if the action that threw an exception was "configure" just go straight to the metacat
203
				// configuration.  This will avoid a loop.  Otherwise, call the admin servlet with
204
				// configuration action.
205
				if (action != null && action.equals("configure")) {
206
					RequestUtil.forwardRequest(request, response, "/admin/metacat-configuration.jsp", null);
207
				} else {
208
					RequestUtil.forwardRequest(request, response, "/admin?configureType=configure", null);
209
				}
210
			}
211 4760 daigle
		} catch (GeneralPropertyException ge) {
212 4080 daigle
			String errorMessage =
213 5030 daigle
				"MetacatAdminServlet.handleGetOrPost - Property problem while handling request: " + ge.getMessage();
214 4080 daigle
			logMetacat.error(errorMessage);
215
			processingErrors.add(errorMessage);
216
		} catch (AdminException ae) {
217
			String errorMessage =
218 5030 daigle
				"MetacatAdminServlet.handleGetOrPost - Admin problem while handling request: " + ae.getMessage();
219 4080 daigle
			logMetacat.error(errorMessage);
220
			processingErrors.add(errorMessage);
221 4854 daigle
		} catch (MetacatUtilException ue) {
222 4080 daigle
			String errorMessage =
223 5030 daigle
				"MetacatAdminServlet.handleGetOrPost - Utility problem while handling request: " + ue.getMessage();
224 4080 daigle
			logMetacat.error(errorMessage);
225
			processingErrors.add(errorMessage);
226 6669 leinfelder
		} catch (ServiceException e) {
227
			String errorMessage =
228
				"MetacatAdminServlet.handleGetOrPost - Service problem while handling request: " + e.getMessage();
229
			logMetacat.error(errorMessage);
230
			processingErrors.add(errorMessage);
231 4080 daigle
		}
232
	}
233
}