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
import edu.ucsb.nceas.metacat.service.PropertyService;
42
import edu.ucsb.nceas.metacat.service.SessionService;
43
import edu.ucsb.nceas.metacat.util.LDAPUtil;
44
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
45 4159 daigle
import edu.ucsb.nceas.metacat.util.OrganizationUtil;
46 4080 daigle
import edu.ucsb.nceas.metacat.util.RequestUtil;
47
import edu.ucsb.nceas.metacat.util.SkinUtil;
48
import edu.ucsb.nceas.metacat.util.SystemUtil;
49
import edu.ucsb.nceas.metacat.util.UtilException;
50
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
51
52
/**
53
 * Entry servlet for the metadata configuration utility
54
 */
55
public class MetaCatAdminServlet extends HttpServlet {
56
57
	private static final long serialVersionUID = 1L;
58
59
	private Logger logMetacat = Logger.getLogger(MetaCatAdminServlet.class);
60
61
    /**
62
	 * Initialize the servlet
63
	 */
64
	public void init(ServletConfig config) throws ServletException {
65
		super.init(config);
66
	}
67
68
    /** Handle "GET" method requests from HTTP clients */
69
	public void doGet(HttpServletRequest request, HttpServletResponse response)
70
			throws ServletException, IOException {
71
72
		// Process the data and send back the response
73
		handleGetOrPost(request, response);
74
	}
75
76
	/** Handle "POST" method requests from HTTP clients */
77
	public void doPost(HttpServletRequest request, HttpServletResponse response)
78
			throws ServletException, IOException {
79
80
		// Process the data and send back the response
81
		handleGetOrPost(request, response);
82
	}
83
84
    /**
85
	 * Control servlet response depending on the action parameter specified
86
	 *
87
	 * @param request
88
	 *            the http request information
89
	 * @param response
90
	 *            the http response to be sent back to the client
91
	 */
92
	private void handleGetOrPost(HttpServletRequest request,
93
			HttpServletResponse response) throws ServletException, IOException {
94
		String action = request.getParameter("configureType");
95
		logMetacat.debug("Processing admin action: " + action);
96
		Vector<String> processingMessage = new Vector<String>();
97
		Vector<String> processingErrors = new Vector<String>();
98
99
		try {
100
			// Update the last update time for this user if they are not new
101
			HttpSession httpSession = request.getSession(false);
102
			if (httpSession != null) {
103
				SessionService.touchSession(httpSession.getId());
104
			}
105
106
			// if ldap isn't configured, change the action to ldap.  ldap
107
			// needs to be set up before we do anything else
108
			if (!LDAPUtil.isLDAPConfigured()) {
109
				processingMessage.add("You must configure LDAP before "
110
						+ "you can continue with MetaCat configuration.");
111
				RequestUtil.setRequestMessage(request, processingMessage);
112
				action = "ldap";
113
				logMetacat.debug("Admin action changed to 'ldap'");
114
			}
115
116
			// Once ldap is configured, see if the user is logged in
117
			// as an administrator.  If not, they need to log in before
118
			// they can continue with configuration.
119
			if (LDAPUtil.isLDAPConfigured()
120
					&& !LDAPUtil.isUserLoggedInAsAdmin(request)) {
121
				processingMessage.add("You must log in as an administrative " + "" +
122
						"user before you can continue with MetaCat configuration.");
123
				RequestUtil.setRequestMessage(request, processingMessage);
124
				action = "login";
125
				logMetacat.debug("Admin action changed to 'login'");
126
			}
127
128
			if (action == null || action.equals("configure")) {
129
				// Forward the request main configuration page
130
				request.setAttribute("metaCatVersion", SystemUtil.getMetacatVersion());
131
			    request.setAttribute("propsConfigured", new Boolean(PropertyService.arePropertiesConfigured()));
132
			    if (PropertyService.arePropertiesConfigured()) {
133
					request.setAttribute("databaseVersion",
134
							DBAdmin.getInstance().getDBVersion());
135
				}
136
			    request.setAttribute("ldapConfigured", new Boolean(LDAPUtil.isLDAPConfigured()));
137 4159 daigle
			    request.setAttribute("orgsConfigured", new Boolean(OrganizationUtil.areOrganizationsConfigured()));
138 4080 daigle
			    request.setAttribute("skinsConfigured", new Boolean(SkinUtil.areSkinsConfigured()));
139
			    request.setAttribute("metacatConfigured", new Boolean(MetaCatUtil.isMetacatConfigured()));
140
				RequestUtil.forwardRequest(request, response,
141
						"/admin/metacat-configuration.jsp?configureType=configure");
142
				return;
143
			} else if (action.equals("properties")) {
144
				// process properties
145
				PropertiesAdmin.getInstance().configureProperties(request,
146
						response);
147
				return;
148
			} else if (action.equals("skins")) {
149
				// process skins
150
				SkinsAdmin.getInstance().configureSkins(request, response);
151
				return;
152
			} else if (action.equals("database")) {
153 4159 daigle
				// process database
154 4080 daigle
				DBAdmin.getInstance().configureDatabase(request, response);
155
				return;
156
			} else if (action.equals("ldap")) {
157 4159 daigle
				// process ldap
158 4080 daigle
				LDAPAdmin.getInstance().configureLDAP(request, response);
159
				return;
160 4159 daigle
			} else if (action.equals("organization")) {
161
				// process organization
162
				OrganizationAdmin.getInstance().configureOrganization(request, response);
163
				return;
164 4080 daigle
			} else if (action.equals("login")) {
165 4159 daigle
				// process login
166 4080 daigle
				AuthAdmin.getInstance().authenticateUser(request, response);
167
				return;
168
			} else {
169
				String errorMessage = "Invalid action in configuration request: " + action;
170
				logMetacat.error(errorMessage);
171
				processingErrors.add(errorMessage);
172
			}
173
		} catch (PropertyNotFoundException pnfe) {
174
			String errorMessage =
175
				"Property problem while handling request: " + pnfe.getMessage();
176
			logMetacat.error(errorMessage);
177
			processingErrors.add(errorMessage);
178
		} catch (AdminException ae) {
179
			String errorMessage =
180
				"Admin problem while handling request: " + ae.getMessage();
181
			logMetacat.error(errorMessage);
182
			processingErrors.add(errorMessage);
183
		} catch (UtilException ue) {
184
			String errorMessage =
185
				"Utility problem while handling request: " + ue.getMessage();
186
			logMetacat.error(errorMessage);
187
			processingErrors.add(errorMessage);
188
		}
189
190
		if (processingErrors.size() > 0) {
191
			RequestUtil.clearRequestMessages(request);
192
			RequestUtil.setRequestErrors(request,processingErrors);
193
			RequestUtil.forwardRequest(request, response, "/admin/metacat-configuration.jsp");
194
		}
195
	}
196
}