1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.client.dispatch;
27
28import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
29import com.sun.xml.internal.ws.api.message.Packet;
30import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;
31import com.sun.xml.internal.ws.api.pipe.Tube;
32import com.sun.xml.internal.ws.api.client.WSPortInfo;
33import com.sun.xml.internal.ws.binding.BindingImpl;
34import com.sun.xml.internal.ws.client.WSServiceDelegate;
35import com.sun.xml.internal.ws.client.PortInfo;
36import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
37import com.sun.xml.internal.ws.resources.DispatchMessages;
38import com.sun.xml.internal.ws.transport.Headers;
39
40import javax.xml.namespace.QName;
41import javax.xml.soap.MimeHeader;
42import javax.xml.soap.SOAPException;
43import javax.xml.soap.SOAPMessage;
44import javax.xml.ws.Service;
45import javax.xml.ws.WebServiceException;
46import javax.xml.ws.handler.MessageContext;
47
48import java.util.Iterator;
49
50/**
51 * The <code>SOAPMessageDispatch</code> class provides support
52 * for the dynamic invocation of a service endpoint operation using
53 * the <code>SOAPMessage</code> class. The <code>javax.xml.ws.Service</code>
54 * interface acts as a factory for the creation of <code>SOAPMessageDispatch</code>
55 * instances.
56 *
57 * @author WS Development Team
58 * @version 1.0
59 */
60public class SOAPMessageDispatch extends com.sun.xml.internal.ws.client.dispatch.DispatchImpl<SOAPMessage> {
61    @Deprecated
62    public SOAPMessageDispatch(QName port, Service.Mode mode, WSServiceDelegate owner, Tube pipe, BindingImpl binding, WSEndpointReference epr) {
63        super(port, mode, owner, pipe, binding, epr);
64    }
65
66    public SOAPMessageDispatch(WSPortInfo portInfo, Service.Mode mode, BindingImpl binding, WSEndpointReference epr) {
67        super(portInfo, mode, binding, epr);
68    }
69
70    Packet createPacket(SOAPMessage arg) {
71        Iterator iter = arg.getMimeHeaders().getAllHeaders();
72        Headers ch = new Headers();
73        while(iter.hasNext()) {
74            MimeHeader mh = (MimeHeader) iter.next();
75            ch.add(mh.getName(), mh.getValue());
76        }
77        Packet packet = new Packet(SAAJFactory.create(arg));
78        packet.invocationProperties.put(MessageContext.HTTP_REQUEST_HEADERS, ch);
79        return packet;
80    }
81
82    SOAPMessage toReturnValue(Packet response) {
83        try {
84
85            //not sure if this is the correct way to deal with this.
86            if ( response ==null || response.getMessage() == null )
87                     throw new WebServiceException(DispatchMessages.INVALID_RESPONSE());
88            else
89                return response.getMessage().readAsSOAPMessage();
90        } catch (SOAPException e) {
91            throw new WebServiceException(e);
92        }
93    }
94}
95