001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037: package com.sun.portal.psftportlet.common;
038:
039: import com.sun.portal.log.common.PortalLogger;
040: import com.sun.portal.psftportlet.holidaysportlet.PSFTHolidaysUtils;
041: import com.sun.portal.psftportlet.holidaysportlet.PSFTHolidaysConstants;
042: import com.sun.portal.iwayutil.connection.IWAYConnectionFactory;
043: import com.sun.portal.iwayutil.connection.IWAYConnection;
044: import com.sun.portal.iwayutil.connection.IWAYRequest;
045: import com.sun.portal.iwayutil.connection.IWAYResponse;
046: import com.sun.portal.iwayutil.connection.IWAYConnectionException;
047:
048: import java.util.Properties;
049: import java.util.ResourceBundle;
050: import java.util.logging.Logger;
051:
052: import javax.servlet.http.HttpServletRequest;
053:
054: import javax.portlet.RenderRequest;
055: import javax.portlet.PortletSession;
056:
057: public class PSFTAuthUtils {
058: private static Logger logger = PortalLogger
059: .getLogger(PSFTAuthUtils.class);
060:
061: private static PSFTSSOAdapterUtils getSSOAdapterUtils(
062: RenderRequest request, String configName, String channelName) {
063: HttpServletRequest httpReq = (HttpServletRequest) request
064: .getAttribute(PSFTPortletConstants.HTTPREQ_ATTR_NAME);
065: return new PSFTSSOAdapterUtils(httpReq, configName, channelName);
066: }
067:
068: public static Properties getPSFTProprties(RenderRequest request) {
069: Properties psftProps = null;
070: try {
071: ResourceBundle psftConfig = ResourceBundle
072: .getBundle(PSFTPortletConstants.PSFT_CONFIG_FILE_NAME);
073: String configName = psftConfig
074: .getString(PSFTPortletConstants.PSFT_SSOA_CONFIG_PREF_NAME);
075: String channelName = psftConfig
076: .getString(PSFTPortletConstants.PSFT_SSOA_CHANNEL_PREF_NAME);
077: PSFTSSOAdapterUtils ssoUtils = getSSOAdapterUtils(request,
078: configName, channelName);
079: psftProps = ssoUtils.getPSFTProperties();
080: } catch (Exception ex) {
081: logger.severe("Exception : " + ex.getMessage());
082: }
083: return psftProps;
084: }
085:
086: public static synchronized void checkForUserAuthentication(
087: RenderRequest request) {
088: PortletSession session = request.getPortletSession();
089: PSFTUserConfig userConfig = (PSFTUserConfig) session
090: .getAttribute(PSFTPortletConstants.SESSION_USERCONFIG,
091: PortletSession.APPLICATION_SCOPE);
092: if (userConfig == null) {
093: Properties psftProps = getPSFTProprties(request);
094: boolean isValid = checkForValidCredentials(psftProps);
095:
096: if (isValid) {
097: userConfig = new PSFTUserConfig(psftProps);
098: session.setAttribute(
099: PSFTPortletConstants.SESSION_USERCONFIG,
100: userConfig, PortletSession.APPLICATION_SCOPE);
101: }
102: }
103: }
104:
105: private static boolean checkForValidCredentials(Properties psftProps) {
106: boolean isValid = false;
107:
108: if (psftProps != null) {
109: IWAYConnection conn = null;
110: ResourceBundle objectResource = null;
111: try {
112: objectResource = ResourceBundle
113: .getBundle(PSFTHolidaysConstants.objectResourceName);
114: } catch (Exception ex) {
115: logger.severe("Failed to initialize rule resourse => "
116: + ex.getMessage());
117: }
118:
119: PSFTHolidaysUtils holidaysUtils = new PSFTHolidaysUtils(
120: objectResource);
121: IWAYRequest ireq = holidaysUtils.getFindRequest();
122: IWAYResponse ires = null;
123:
124: try {
125: conn = IWAYConnectionFactory
126: .getIWAYConnection(psftProps);
127: } catch (Exception ex) {
128: logger
129: .severe("Exception in getting iWay connection => "
130: + ex.getMessage());
131: }
132:
133: try {
134: ires = conn.getResponse(ireq);
135: } catch (IWAYConnectionException e) {
136: logger.severe("Exception in getting iWay response => "
137: + e.getMessage());
138: }
139: String xmlOutput = ires.getResponseString();
140: IWAYResponseParser parser = new IWAYResponseParser(
141: xmlOutput, objectResource);
142: isValid = parser.isValid();
143: }
144: logger.info("Returning isValid => " + isValid);
145:
146: return isValid;
147: }
148: }
|