8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
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 * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
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 * $Id: PLLClient.java,v 1.8 2008/06/25 05:41:33 qcheng Exp $
46b65b93439b7e12e0f609fbd6dfe80d4342617bSam Fraser * Portions Copyrighted 2011-2016 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.comm.share.PLLBundle;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.comm.share.RequestSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.comm.share.ResponseSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.naming.WebtopNaming.SiteMonitor;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.HttpURLConnectionManager;
4aa5badd5f4858577979ba105aa80ff34e84feccCraig McDonnellimport org.forgerock.http.header.TransactionIdHeader;
aa3a2aee90d1c61946a08c839d9838be7658b453Craig McDonnellimport org.forgerock.openam.audit.context.AuditRequestContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The <code>PLLClient</code> class is used to send RequestSet XML documents
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to the URL specified in the send() method. The high level services and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * application can use Naming Service to find the service specific URL. This
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * class provides static methods to register notification handlers.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see com.iplanet.services.comm.share.RequestSet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see com.iplanet.services.comm.share.Response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see com.iplanet.services.comm.client.SendRequestException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @see com.iplanet.services.comm.client.NotificationHandler
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /** notification handlers */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Hashtable notificationHandlers = new Hashtable();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Debug debug = Debug.getInstance("PLLClient");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static boolean useCache = Boolean.getBoolean(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SystemProperties.get(Constants.URL_CONNECTION_USE_CACHE, "false"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Translates the Java object to an XML RequestSet document and sends the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * corresponding XML document to the specified URL.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param url
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The destination URL for the RequestSet XML document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The RequestSet Java object to be translated to an XML
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * RequestSet document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SendRequestException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if there is an error in sending the XML document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Vector send(URL url, RequestSet set)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Translates the Java object to an XML RequestSet document and sends the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * corresponding XML document to the specified URL.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param url
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The destination URL for the RequestSet XML document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cookies
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The value for Http Request Header 'Cookie'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The RequestSet Java object to be translated to an XML
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * RequestSet document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SendRequestException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if there is an error in sending the XML document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Vector send(URL url, String cookies, RequestSet set)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Translates the Java object to an XML RequestSet document and sends the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * corresponding XML document to the specified URL.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param url
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The destination URL for the RequestSet XML document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The RequestSet Java object to be translated to an XML
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * RequestSet document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cookieTable
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The HashMap that constains cookies to be replayed and stores
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * cookies retrieved from the response.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SendRequestException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if there is an error in sending the XML document.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Vector send(URL url, RequestSet set, HashMap cookieTable)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // The private method that implements the above interfaces.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // HashMap cookieTable passes in the cookies that will be replayed. It also
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // is the place holder to retrieve additional cookies if any from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // URL connection response.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Vector send(URL url, String cookies, RequestSet set,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HashMap cookieTable) throws SendRequestException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("Site " + url.toString() + " is down.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SendRequestException("Site is down.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn = HttpURLConnectionManager.getConnection(url);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // replay cookies
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieTable != null && !cookieTable.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator it = cookieTable.values().iterator(); it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieStr.append(cookie.getName()).append("=").append(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
4aa5badd5f4858577979ba105aa80ff34e84feccCraig McDonnell conn.setRequestProperty(TransactionIdHeader.NAME, AuditRequestContext.createSubTransactionIdValue());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Output ...
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // compute and set length, just in case iWS set arbitrary length
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int requestLength = xml.getBytes("UTF-8").length;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setRequestProperty("Content-Length", Integer
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Input ...
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster in = new BufferedReader(new InputStreamReader(conn
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((len = in.read(buf, 0, buf.length)) != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // retrieves cookies from the response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResponseSet resset = ResponseSet.parseXML(in_string);
46b65b93439b7e12e0f609fbd6dfe80d4342617bSam Fraser debug.warning("PLLClient.send URL=" + url + " : exception: ", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SendRequestException(e.getMessage());
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas // to allow connection re-use when an IOException occurs, the
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas // error stream should be read and then closed.
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas InputStream errorStream = conn.getErrorStream();
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas debug.warning("Error stream content is " + IOUtils.readStream(errorStream));
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas debug.warning("Error while reading the error stream", ioe);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Parses the cookies from the response header and stores them in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in cookieTable
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param headers
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The Map containig headers
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cookieTable
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The HashMap that constains cookies to be replayed and stores
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * cookies retrieved from the response.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void parseCookies(Map headers, HashMap cookieTable) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("header in parseCookies(): " + headers);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator hrs = headers.entrySet().iterator(); hrs.hasNext();) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key != null && (key.equalsIgnoreCase("Set-cookie") ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator it = list.iterator(); it.hasNext();) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer stz = new StringTokenizer(cookieStr, ";");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tmpName = nameValue.substring(0, index).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds a notification handler for a service. This handler is used by the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Platform Low Level servlet to pass notifications for processing.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The name of the service such as session, profile
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param handler
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The handler for notification processing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void addNotificationHandler(String service,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NotificationHandler handler) throws AlreadyRegisteredException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (notificationHandlers.containsKey(service)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes a notification handler of a service.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The name of the service whose handler needs to be removed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void removeNotificationHandler(String service) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets a notification handler of a service.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The name of the service whose handler needs to be returned
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static NotificationHandler getNotificationHandler(String service) {