ClientConfiguratorFilter.java revision 4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1c
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: ClientConfiguratorFilter.java,v 1.7 2009/12/05 02:33:36 222713 Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.setup;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.File;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.FileInputStream;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.FileNotFoundException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.Filter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.FilterChain;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.FilterConfig;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.RequestDispatcher;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.ServletContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.ServletException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.ServletRequest;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.ServletResponse;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletRequest;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletResponse;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Properties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This filter brings user to a configuration page when client sample WAR
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is not configured yeti, it will set the configuration properties if
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the WAR is already configured.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic final class ClientConfiguratorFilter implements Filter {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private FilterConfig config;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ServletContext servletCtx;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // see if the configure.jsp page is executed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static boolean isConfigured = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String SETUP_URI = "/Configurator.jsp";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String configFile = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean passThrough = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Redirects request to configuration page if the product is not yet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * configured.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param request Servlet Request.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param response Servlet Response.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param filterChain Filter Chain.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServletException if there are errors in the servlet space.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void doFilter(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServletRequest request,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServletResponse response,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster FilterChain filterChain
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws IOException, ServletException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletRequest httpRequest = (HttpServletRequest) request;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletResponse httpResponse = (HttpServletResponse) response ;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // pass through on Configurator page
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (httpRequest.getRequestURI().endsWith(SETUP_URI)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster passThrough = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filterChain.doFilter(httpRequest, httpResponse);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (passThrough) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filterChain.doFilter(httpRequest, httpResponse);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (!isConfigured) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String url = httpRequest.getScheme() + "://" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster httpRequest.getServerName() + ":" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster httpRequest.getServerPort() +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster httpRequest.getContextPath() + SETUP_URI;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster httpResponse.sendRedirect(url);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster passThrough = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster filterChain.doFilter(httpRequest, httpResponse);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch(Exception ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException("ClientConfiguratorFilter.doFilter", ex);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Destroy the filter config on sever shutdowm
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void destroy() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster config = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Initializes the filter.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param filterConfig Filter Configuration.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void init(FilterConfig filterConfig) throws ServletException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setFilterConfig(filterConfig);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster servletCtx = filterConfig.getServletContext();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configFile = System.getProperty("openssoclient.config.folder");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (configFile == null || configFile.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configFile = System.getProperty("user.home");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configFile = configFile + File.separator +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SetupClientWARSamples.CLIENT_WAR_CONFIG_TOP_DIR + File.separator +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SetupClientWARSamples.getNormalizedRealPath(servletCtx) +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "AMConfig.properties";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File file = new File(configFile);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (file.exists()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setAMConfigProperties(configFile);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Initializes the filter configuration.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param fconfig Filter Configuration.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setFilterConfig(FilterConfig fconfig) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster config = fconfig;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sets properties from AMConfig.properties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param configFile path to the AMConfig.properties file
* @throws ServletException when error occurs
*/
private void setAMConfigProperties(String configFile)
throws ServletException {
FileInputStream fileStr = null;
try {
fileStr = new FileInputStream(configFile);
if (fileStr != null) {
Properties props = new Properties();
props.load(fileStr);
SystemProperties.initializeProperties(props);
isConfigured = true;
} else {
throw new ServletException("Unable to open: " + configFile);
}
} catch (FileNotFoundException fexp) {
fexp.printStackTrace();
throw new ServletException(fexp.getMessage());
} catch (IOException ioexp) {
ioexp.printStackTrace();
throw new ServletException(ioexp.getMessage());
} finally {
if (fileStr != null) {
try {
fileStr.close();
} catch (IOException ioe) {
}
}
}
}
}