001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 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: AuthnSvcClient.java,v 1.2 2008/06/25 05:47:05 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.authnsvc;
031
032import java.util.List;
033import org.w3c.dom.Element;
034
035import com.sun.identity.liberty.ws.authnsvc.protocol.SASLResponse;
036import com.sun.identity.liberty.ws.authnsvc.protocol.SASLRequest;
037import com.sun.identity.liberty.ws.soapbinding.Client;
038import com.sun.identity.liberty.ws.soapbinding.Message;
039
040/**
041 * The <code>AuthnSvcClient</code> class provides web service clients with
042 * a method to <code>SASL</code> request to the Authentication Service and
043 * receive <code>SASL</code> response.
044 * @supported.all.api
045 */
046public class AuthnSvcClient {
047
048    /**
049     * Sends a <code>SASL</code> request to the Authentication Service SOAP
050     * endpoint and returns a <code>SASL</code> response.
051     *
052     * @param saslReq a <code>SASL</code> request
053     * @param connectTo the SOAP endpoint URL
054     * @return a <code>SASL</code> response from the Authentication Service
055     * @exception AuthnSvcException if authentication service is not available
056     *            or there is an error in <code>SASL</code> request
057     */
058    public static SASLResponse sendRequest(
059        SASLRequest saslReq,
060        String connectTo
061    ) throws AuthnSvcException {
062        Message req = new Message();
063        req.setSOAPBody(saslReq.toElement());
064        req.getCorrelationHeader()
065           .setRefToMessageID(saslReq.getRefToMessageID());
066
067        Message resp = null;
068        try {
069            resp = Client.sendRequest(req, connectTo);
070        } catch (Exception ex) {
071            AuthnSvcUtils.debug.error("AuthnSvcClient.sendRequest:", ex);
072            throw new AuthnSvcException(ex);  
073        }
074
075        List list = resp.getBodies(AuthnSvcConstants.NS_AUTHN_SVC,
076                                   AuthnSvcConstants.TAG_SASL_RESPONSE);
077        if (list.isEmpty()) {
078            throw new AuthnSvcException("missingSASLResponse");
079        } else if (list.size() > 1) {
080            throw new AuthnSvcException("tooManySASLResponse");
081        }
082
083        SASLResponse saslResp = new SASLResponse((Element)list.get(0));
084        saslResp.setMessageID(resp.getCorrelationHeader().getMessageID());
085        saslResp.setRefToMessageID(resp.getCorrelationHeader()
086                                       .getRefToMessageID());
087
088        return saslResp;
089    }
090}