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.handler;
27import com.sun.xml.internal.ws.api.message.Header;
28import com.sun.xml.internal.ws.api.message.Message;
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.WSBinding;
32import com.sun.xml.internal.ws.api.SOAPVersion;
33
34import javax.xml.bind.JAXBContext;
35import javax.xml.namespace.QName;
36import javax.xml.soap.SOAPException;
37import javax.xml.soap.SOAPMessage;
38import javax.xml.ws.WebServiceException;
39import javax.xml.ws.handler.soap.SOAPMessageContext;
40
41import java.util.ArrayList;
42import java.util.Iterator;
43import java.util.List;
44import java.util.Set;
45
46/**
47 * Implementation of {@link SOAPMessageContext}. This class is used at runtime
48 * to pass to the handlers for processing soap messages.
49 *
50 * @see MessageContextImpl
51 *
52 * @author WS Development Team
53 */
54public class SOAPMessageContextImpl extends MessageUpdatableContext implements SOAPMessageContext {
55
56    private Set<String> roles;
57    private SOAPMessage soapMsg = null;
58    private WSBinding binding;
59
60    public SOAPMessageContextImpl(WSBinding binding, Packet packet,Set<String> roles) {
61        super(packet);
62        this.binding = binding;
63        this.roles = roles;
64    }
65
66    public SOAPMessage getMessage() {
67        if(soapMsg == null) {
68            try {
69                Message m = packet.getMessage();
70                soapMsg = m != null ? m.readAsSOAPMessage() : null;
71            } catch (SOAPException e) {
72                throw new WebServiceException(e);
73            }
74        }
75        return soapMsg;
76    }
77
78    public void setMessage(SOAPMessage soapMsg) {
79        try {
80            this.soapMsg = soapMsg;
81        } catch(Exception e) {
82            throw new WebServiceException(e);
83        }
84    }
85
86    void setPacketMessage(Message newMessage){
87        if(newMessage != null) {
88            packet.setMessage(newMessage);
89            soapMsg = null;
90        }
91    }
92
93    protected void updateMessage() {
94        //Check if SOAPMessage has changed, if so construct new one,
95        // Packet are handled through MessageContext
96        if(soapMsg != null) {
97            packet.setMessage(SAAJFactory.create(soapMsg));
98            soapMsg = null;
99        }
100    }
101
102    public Object[] getHeaders(QName header, JAXBContext jaxbContext, boolean allRoles) {
103        SOAPVersion soapVersion = binding.getSOAPVersion();
104
105        List<Object> beanList = new ArrayList<Object>();
106        try {
107            Iterator<Header> itr = packet.getMessage().getHeaders().getHeaders(header,false);
108            if(allRoles) {
109                while(itr.hasNext()) {
110                    beanList.add(itr.next().readAsJAXB(jaxbContext.createUnmarshaller()));
111                }
112            } else {
113                while(itr.hasNext()) {
114                    Header soapHeader = itr.next();
115                    //Check if the role is one of the roles on this Binding
116                    String role = soapHeader.getRole(soapVersion);
117                    if(getRoles().contains(role)) {
118                        beanList.add(soapHeader.readAsJAXB(jaxbContext.createUnmarshaller()));
119                    }
120                }
121            }
122            return beanList.toArray();
123        } catch(Exception e) {
124            throw new WebServiceException(e);
125        }
126    }
127
128    public Set<String> getRoles() {
129        return roles;
130    }
131}
132