8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 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: PLLClient.java,v 1.8 2008/06/25 05:41:33 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
46b65b93439b7e12e0f609fbd6dfe80d4342617bSam Fraser * Portions Copyrighted 2011-2016 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.iplanet.services.comm.client;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
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.shared.Constants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.HttpURLConnectionManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.BufferedReader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomasimport java.io.InputStream;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.InputStreamReader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.OutputStream;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.net.HttpURLConnection;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.net.URL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Hashtable;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.List;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.StringTokenizer;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Vector;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.Cookie;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
4aa5badd5f4858577979ba105aa80ff34e84feccCraig McDonnellimport org.forgerock.http.header.TransactionIdHeader;
aa3a2aee90d1c61946a08c839d9838be7658b453Craig McDonnellimport org.forgerock.openam.audit.context.AuditRequestContext;
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomasimport org.forgerock.openam.utils.IOUtils;
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
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 *
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 */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class PLLClient {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /** notification handlers */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Hashtable notificationHandlers = new Hashtable();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Debug debug = Debug.getInstance("PLLClient");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static boolean useCache = Boolean.getBoolean(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SystemProperties.get(Constants.URL_CONNECTION_USE_CACHE, "false"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
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 *
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 */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Vector send(URL url, RequestSet set)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SendRequestException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return send(url, null, set, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
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 *
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 */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Vector send(URL url, String cookies, RequestSet set)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SendRequestException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return send(url, cookies, set, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
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 *
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 */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Vector send(URL url, RequestSet set, HashMap cookieTable)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SendRequestException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return send(url, null, set, cookieTable);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
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 HttpURLConnection conn = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster OutputStream out = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster BufferedReader in = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
f58c87ece2202b8f85310d8885c7e39a7f435c09Jason Lemay if ((SiteMonitor.keepMonitoring == true) &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster !SiteMonitor.isAvailable(url)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("Site " + url.toString() + " is down.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SendRequestException("Site is down.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn = HttpURLConnectionManager.getConnection(url);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setDoOutput(true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setUseCaches(useCache);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setRequestMethod("POST");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // replay cookies
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuffer cookieStr = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookies != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieStr = new StringBuffer();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieStr.append(cookies);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieTable != null && !cookieTable.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator it = cookieTable.values().iterator(); it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .hasNext();) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cookie cookie = (Cookie) it.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieStr == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieStr = new StringBuffer();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieStr.append(";");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieStr.append(cookie.getName()).append("=").append(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie.getValue());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieStr != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookies = cookieStr.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("sending cookies: " + cookies);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setRequestProperty("Cookie", cookies);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
4aa5badd5f4858577979ba105aa80ff34e84feccCraig McDonnell conn.setRequestProperty(TransactionIdHeader.NAME, AuditRequestContext.createSubTransactionIdValue());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Output ...
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String xml = set.toXMLString();
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 .toString(requestLength));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out = conn.getOutputStream();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.write(xml.getBytes("UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster out.flush();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Input ...
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster in = new BufferedReader(new InputStreamReader(conn
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getInputStream(), "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder in_buf = new StringBuilder();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int len;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster char[] buf = new char[1024];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((len = in.read(buf, 0, buf.length)) != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster in_buf.append(buf, 0, len);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String in_string = in_buf.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // retrieves cookies from the response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map headers = conn.getHeaderFields();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieTable != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster parseCookies(headers, cookieTable);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResponseSet resset = ResponseSet.parseXML(in_string);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return resset.getResponses();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
46b65b93439b7e12e0f609fbd6dfe80d4342617bSam Fraser debug.warning("PLLClient.send URL=" + url + " : exception: ", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new SendRequestException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } finally {
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas IOUtils.closeIfNotNull(out);
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas IOUtils.closeIfNotNull(in);
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas if (conn != null) {
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 if (errorStream != null) {
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas try {
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas debug.warning("Error stream content is " + IOUtils.readStream(errorStream));
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas } catch (IOException ioe) {
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas debug.warning("Error while reading the error stream", ioe);
5d10f01c87ac9763dbced4c620a5c7df623a99fcJon Jonthomas }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Parses the cookies from the response header and stores them in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in cookieTable
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
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 */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void parseCookies(Map headers, HashMap cookieTable) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (headers == null || headers.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("header in parseCookies(): " + headers);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator hrs = headers.entrySet().iterator(); hrs.hasNext();) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map.Entry me = (Map.Entry) hrs.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String) me.getKey();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key != null && (key.equalsIgnoreCase("Set-cookie") ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (key.equalsIgnoreCase("cookie")))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster List list = (List) me.getValue();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (list == null || list.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster continue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cookie cookie = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator it = list.iterator(); it.hasNext();) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cookieStr = (String) it.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("cookie: " + cookieStr);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer stz = new StringTokenizer(cookieStr, ";");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (stz.hasMoreTokens()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String nameValue = stz.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int index = nameValue.indexOf("=");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (index == -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster continue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tmpName = nameValue.substring(0, index).trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String value = nameValue.substring(index + 1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie = new Cookie(tmpName, value);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieTable.put(tmpName, cookie);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
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 *
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 */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void addNotificationHandler(String service,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NotificationHandler handler) throws AlreadyRegisteredException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (notificationHandlers.containsKey(service)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new AlreadyRegisteredException(PLLBundle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getString("alreadyRegistered")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + service);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster notificationHandlers.put(service, handler);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes a notification handler of a service.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The name of the service whose handler needs to be removed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void removeNotificationHandler(String service) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster notificationHandlers.remove(service);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets a notification handler of a service.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The name of the service whose handler needs to be returned
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static NotificationHandler getNotificationHandler(String service) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (NotificationHandler) notificationHandlers.get(service);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}