001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: AMClientDetector.java,v 1.8 2008/09/04 16:16:34 dillidorai Exp $
026 *
027 */
028
029/*
030 * Portions Copyrighted [2010-2011] [ForgeRock AS]
031 */
032package com.iplanet.am.util;
033
034import com.iplanet.services.cdm.ClientDetectionInterface;
035import com.iplanet.services.cdm.ClientTypesManager;
036import com.iplanet.services.cdm.DefaultClientTypesManager;
037import com.iplanet.sso.SSOToken;
038import com.sun.identity.security.AdminTokenAction;
039import com.sun.identity.shared.debug.Debug;
040import com.sun.identity.sm.ServiceSchema;
041import com.sun.identity.sm.ServiceSchemaManager;
042import java.security.AccessController;
043import java.util.Map;
044import java.util.Set;
045import javax.servlet.http.HttpServletRequest;
046
047/**
048 * This is an utility to get the client type. This utility executes Client
049 * Detection Class provided in Client Detection Module Service and gets the
050 * client type. Default client type will be returned if there is no Client
051 * Detection Implementation provided
052 * @supported.all.api
053 */
054public class AMClientDetector {
055
056    private static ClientTypesManager clientManager;
057
058    private static Map attrs;
059
060    private static boolean detectionEnabled;
061
062    private ClientDetectionInterface clientDetector;
063
064    private static ClientDetectionInterface defaultClientDetector;
065
066    private static final String CDM_SERVICE_NAME = "iPlanetAMClientDetection";
067
068    private static final String CDM_CLASS_NAME = 
069        "iplanet-am-client-detection-class";
070
071    private static final String CLIENT_TYPES_MANAGER_CLASS_NAME = 
072        "iplanet-am-client-types-manager-class";
073
074    private static final String CDM_ENABLED_ATTR = 
075        "iplanet-am-client-detection-enabled";
076
077    private static Debug debug = Debug.getInstance("amClientDetection");
078
079    private static String ClientTypesManagerImpl = null;
080
081    private static String clientDetectionClass = null;
082
083    private static boolean servicePassed = true;
084
085    static {
086        getServiceSchemaManager();
087        if (servicePassed) {
088            getClientAttributes();
089            if (detectionEnabled) {
090                initClientTypesManager();
091                executeClientDetector();
092            }
093        }
094    }
095
096    /**
097     * Constructs a <code>AMClientDetector</code> instance.
098     */
099    public AMClientDetector() {
100        clientDetector = defaultClientDetector;
101    }
102
103    /**
104     * Applications can provide client detector implementation class
105     * 
106     * @param className
107     *            the name of the implementation class
108     */
109    public AMClientDetector(String className) {
110        if (className != null) {
111            try {
112                clientDetector = (ClientDetectionInterface) (Class
113                        .forName(className).newInstance());
114            } catch (Exception ex) {
115                clientDetector = defaultClientDetector;
116            }
117        } else {
118            clientDetector = defaultClientDetector;
119        }
120    }
121
122    /**
123     * Application provide custom detection class.
124     * 
125     * @param cd
126     *            class that implements <code>ClientDetectionInterface</code>.
127     */
128    public AMClientDetector(ClientDetectionInterface cd) {
129        clientDetector = cd;
130    }
131
132    /**
133     * Returns the client type by executing client
134     * detector class provided in Client Detection Service.
135     * 
136     * @param request
137     *            HTTP Servlet Request.
138     * @return client type . Default client type will be returned if either the
139     *         client detection is not enabled or the client detector class is
140     *         not provided in <code>cdm</code> service. If it throws any
141     *         exception by executing this class, this method will just return
142     *         null .
143     */
144    public String getClientType(HttpServletRequest request) {
145        String clientType = null;
146        debug.message("AMClientDetector.getClientType()");
147        // Check whether the client detection is enabled or not
148        if (detectionEnabled) {
149            try {
150                if (clientDetector != null) {
151                    clientType = clientDetector.getClientType(request);
152                }
153                if (debug.messageEnabled()) {
154                    debug.message("AMClientDetector: Client Type : "
155                                    + clientType);
156                }                
157            } catch (Exception ex) {
158                clientType = clientManager.getDefaultClientType();
159            }
160        }        
161        
162        if (clientType == null || clientType.length() == 0)
163            clientType = "genericHTML";
164        
165        if (debug.messageEnabled()) {
166            debug.message("AMClientDetector: Default Client Type : " 
167                + clientType);
168        }
169        return clientType;
170    }
171
172    /**
173     * Returns <code>true</code> if the client detection is enabled.
174     * 
175     * @return <code>true</code> if the client detection is enabled.
176     */
177    public boolean isDetectionEnabled() {
178        return detectionEnabled;
179    }
180
181    /**
182     * Returns the <code>ClientTypesManager</code> Instance.
183     * 
184     * @return the <code>ClientTypesManager</code> Instance.
185     */
186    public static ClientTypesManager getClientTypesManagerInstance() {
187        return clientManager;
188    }
189
190    /* create instance of ClientTypesManager */
191
192    private static void initClientTypesManager() {
193
194        if ((ClientTypesManagerImpl != null)
195                && (ClientTypesManagerImpl.length() > 0)) {
196
197            try {
198                clientManager = (ClientTypesManager) Class.forName(
199                        ClientTypesManagerImpl).newInstance();
200            } catch (Exception ex) {
201                debug.error("Unable to instantiate class ", ex);
202                clientManager = new DefaultClientTypesManager();
203            }
204        } else {
205            clientManager = new DefaultClientTypesManager();
206        }
207        clientManager.initManager();
208        Set allClientTypes = clientManager.getAllClientTypes();
209        if (allClientTypes == null || allClientTypes.isEmpty()) {
210            if (debug.warningEnabled()) {
211                debug.warning("AMClientDetector.initClientManager():"
212                        + " no client types found, "
213                        + " setting detectionEnabled to false");
214            }
215            detectionEnabled = false;
216        }
217
218        return;
219    }
220
221    /* retrieve client attributes from the ClientDetection service */
222    private static void getServiceSchemaManager() {
223        try {
224            SSOToken adminToken = (SSOToken) AccessController
225                    .doPrivileged(AdminTokenAction.getInstance());
226            ServiceSchemaManager ssm = new ServiceSchemaManager(
227                    CDM_SERVICE_NAME, adminToken);
228
229            ServiceSchema gsc = ssm.getGlobalSchema();
230
231            attrs = gsc.getAttributeDefaults();
232        } catch (Exception e) {
233            debug.error("AMClientDetector.static: ", e);
234            servicePassed = false;
235            return;
236        }
237    }
238
239    private static void getClientAttributes() {
240        if (attrs != null) {
241            String det = Misc.getMapAttr(attrs, CDM_ENABLED_ATTR);
242            if (det != null && det.equalsIgnoreCase("true")) {
243                detectionEnabled = true;
244            } else {
245                detectionEnabled = false;
246            }
247            if (debug.messageEnabled()) {
248                debug.message("AMClientDetector: ClientDetection enable : "
249                        + detectionEnabled);
250            }
251            clientDetectionClass = Misc.getMapAttr(attrs, CDM_CLASS_NAME);
252
253            ClientTypesManagerImpl = Misc.getMapAttr(attrs,
254                    CLIENT_TYPES_MANAGER_CLASS_NAME);
255        }
256    }
257
258    private static void executeClientDetector() {
259
260        if ((clientDetectionClass != null)
261                && (clientDetectionClass.length() != 0)) {
262            try {
263                defaultClientDetector = (ClientDetectionInterface) (Class
264                        .forName(clientDetectionClass).newInstance());
265            } catch (ClassNotFoundException ex) {
266                debug.warning("AMClientDetector.executeClientDetector():"
267                        + " ClassNotFound: " + ex.getMessage());
268            } catch (InstantiationException ex) {
269                debug.warning("AMClientDetector.executeClientDetector():"
270                        + " not able to instantiate: " + clientDetectionClass);
271            } catch (IllegalAccessException ex) {
272                debug.warning("AMClientDetector.executeClientDetector():"
273                        + " IllegalAccessException: " + ex.getMessage());
274            }
275        }
276    }
277
278    /**
279     * Returns true if the client detection service is present or false if the
280     * client detection service is not present and the client attributes could
281     * not be retrieved.
282     * 
283     * @return false if no Service is present. The default value is true.
284     */
285    public static boolean isServicePassed() {
286        return servicePassed;
287    }
288}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.