Project

General

Profile

1
/**
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: daigle $'
9
 *     '$Date: 2008-10-10 17:11:35 -0700 (Fri, 10 Oct 2008) $'
10
 * '$Revision: 4441 $'
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.MetaCatServlet;
42
import edu.ucsb.nceas.metacat.service.PropertyService;
43
import edu.ucsb.nceas.metacat.service.SessionService;
44
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
45
import edu.ucsb.nceas.metacat.util.LDAPUtil;
46
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
47
//import edu.ucsb.nceas.metacat.util.OrganizationUtil;
48
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
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
53

    
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
			// if ldap isn't configured, change the action to ldap.  ldap
109
			// needs to be set up before we do anything else
110
			if (!LDAPUtil.isLDAPConfigured()) {
111
				processingMessage.add("You must configure LDAP before "
112
						+ "you can continue with MetaCat configuration.");
113
				RequestUtil.setRequestMessage(request, processingMessage);
114
				action = "ldap";
115
				logMetacat.debug("Admin action changed to 'ldap'");
116
			}
117

    
118
			// Once ldap is configured, see if the user is logged in
119
			// as an administrator.  If not, they need to log in before
120
			// they can continue with configuration.
121
			if (LDAPUtil.isLDAPConfigured()
122
					&& !LDAPUtil.isUserLoggedInAsAdmin(request)) {
123
				processingMessage.add("You must log in as an administrative " + "" +
124
						"user before you can continue with MetaCat configuration.");
125
				RequestUtil.setRequestMessage(request, processingMessage);
126
				action = "login";
127
				logMetacat.debug("Admin action changed to 'login'");
128
			}
129

    
130
			if (action == null || action.equals("configure")) {
131
				// Forward the request main configuration page
132
				request.setAttribute("metaCatVersion", SystemUtil.getMetacatVersion()); 
133
			    request.setAttribute("propsConfigured", new Boolean(PropertyService.arePropertiesConfigured()));
134
			    if (PropertyService.arePropertiesConfigured()) {
135
					request.setAttribute("databaseVersion", 
136
							DBAdmin.getInstance().getDBVersion());
137
				}
138
			    request.setAttribute("ldapConfigured", new Boolean(LDAPUtil.isLDAPConfigured()));
139
			    // TODO MCD figure out if we still need an org section
140
			    //request.setAttribute("orgsConfigured", new Boolean(OrganizationUtil.areOrganizationsConfigured()));
141
			    request.setAttribute("skinsConfigured", new Boolean(SkinUtil.areSkinsConfigured()));
142
			    request.setAttribute("metacatConfigured", new Boolean(MetaCatUtil.isMetacatConfigured()));	
143
			    request.setAttribute("geoserverConfigured", 
144
			    		PropertyService.getProperty("configutil.geoserverConfigured"));
145
			    request.setAttribute("metcatServletInitialized", MetaCatServlet.isFullyInitialized());
146
				RequestUtil.forwardRequest(request, response,
147
						"/admin/metacat-configuration.jsp?configureType=configure");
148
				return;
149
			} else if (action.equals("properties")) {
150
				// process properties
151
				PropertiesAdmin.getInstance().configureProperties(request,
152
						response);
153
				return;
154
			} else if (action.equals("skins")) {
155
				// process skins
156
				SkinsAdmin.getInstance().configureSkins(request, response);
157
				return;
158
			} else if (action.equals("database")) {
159
				// process database
160
				DBAdmin.getInstance().configureDatabase(request, response);
161
				return;
162
			} else if (action.equals("ldap")) {
163
				// process ldap
164
				LDAPAdmin.getInstance().configureLDAP(request, response);
165
				return;
166
			// TODO MCD figure out if org configuration is still needed	
167
//			} else if (action.equals("organization")) {
168
//				// process organization
169
//				OrganizationAdmin.getInstance().configureOrganization(request, response);
170
//				return;
171
			} else if (action.equals("login")) {
172
				// process login
173
				AuthAdmin.getInstance().authenticateUser(request, response);
174
				return;
175
			} else if (action.equals("geoserver")) {
176
				// process geoserver password change
177
				GeoserverAdmin.getInstance().configureGeoserver(request, response);
178
				return;
179
			} else {
180
				String errorMessage = "Invalid action in configuration request: " + action;
181
				logMetacat.error(errorMessage);
182
				processingErrors.add(errorMessage);
183
			} 
184
		} catch (PropertyNotFoundException pnfe) {
185
			String errorMessage = 
186
				"Property problem while handling request: " + pnfe.getMessage();
187
			logMetacat.error(errorMessage);
188
			processingErrors.add(errorMessage);
189
		} catch (AdminException ae) {
190
			String errorMessage = 
191
				"Admin problem while handling request: " + ae.getMessage();
192
			logMetacat.error(errorMessage);
193
			processingErrors.add(errorMessage);
194
		} catch (UtilException ue) {
195
			String errorMessage = 
196
				"Utility problem while handling request: " + ue.getMessage();
197
			logMetacat.error(errorMessage);
198
			processingErrors.add(errorMessage);
199
		}
200
		
201
		if (processingErrors.size() > 0) {
202
			RequestUtil.clearRequestMessages(request);
203
			RequestUtil.setRequestErrors(request,processingErrors);
204
			RequestUtil.forwardRequest(request, response, "/admin/metacat-configuration.jsp");
205
		}
206
	}       	
207
}
(7-7/10)