1 /*
   2  * Copyright (c) 1997, 2015, 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 
  26 package com.sun.xml.internal.ws.api.message;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.util.ArrayList;
  31 import java.util.HashMap;
  32 import java.util.Iterator;
  33 import java.util.List;
  34 import java.util.Map;
  35 
  36 import javax.xml.soap.MimeHeader;
  37 import javax.xml.soap.MimeHeaders;
  38 import javax.xml.soap.SOAPMessage;
  39 import javax.xml.transform.Source;
  40 import javax.xml.ws.WebServiceFeature;
  41 import javax.xml.ws.soap.MTOMFeature;
  42 
  43 import com.oracle.webservices.internal.api.EnvelopeStyle;
  44 import com.oracle.webservices.internal.api.EnvelopeStyleFeature;
  45 import com.oracle.webservices.internal.api.message.MessageContext;
  46 import com.sun.xml.internal.ws.api.SOAPVersion;
  47 import com.sun.xml.internal.ws.api.WSFeatureList;
  48 import com.sun.xml.internal.ws.api.pipe.Codec;
  49 import com.sun.xml.internal.ws.api.pipe.Codecs;
  50 import static com.sun.xml.internal.ws.transport.http.HttpAdapter.fixQuotesAroundSoapAction;
  51 
  52 /**
  53  * The MessageContextFactory implements com.oracle.webservices.internal.api.message.MessageContextFactory as
  54  * a factory of Packet and public facade of Codec(s).
  55  *
  56  * @author shih-chang.chen@oracle.com
  57  */
  58 public class MessageContextFactory extends com.oracle.webservices.internal.api.message.MessageContextFactory {
  59 
  60     private WSFeatureList features;
  61     private Codec soapCodec;
  62     private Codec xmlCodec;
  63     private EnvelopeStyleFeature envelopeStyle;
  64     private EnvelopeStyle.Style singleSoapStyle;
  65 
  66     public MessageContextFactory(WebServiceFeature[] wsf) {
  67         this(new com.sun.xml.internal.ws.binding.WebServiceFeatureList(wsf));
  68     }
  69 
  70     public MessageContextFactory(WSFeatureList wsf) {
  71         features = wsf;
  72         envelopeStyle = features.get(EnvelopeStyleFeature.class);
  73         if (envelopeStyle == null) {//Default to SOAP11
  74             envelopeStyle = new EnvelopeStyleFeature(new EnvelopeStyle.Style[]{EnvelopeStyle.Style.SOAP11});
  75             features.mergeFeatures(new WebServiceFeature[]{envelopeStyle}, false);
  76         }
  77         for (EnvelopeStyle.Style s : envelopeStyle.getStyles()) {
  78             if (s.isXML()) {
  79                 if (xmlCodec == null) xmlCodec = Codecs.createXMLCodec(features);
  80             } else {
  81                 if (soapCodec == null) soapCodec = Codecs.createSOAPBindingCodec(features);
  82                 singleSoapStyle = s;
  83             }
  84         }
  85     }
  86 
  87     protected com.oracle.webservices.internal.api.message.MessageContextFactory newFactory(WebServiceFeature... f) {
  88         return new com.sun.xml.internal.ws.api.message.MessageContextFactory(f);
  89     }
  90 
  91 
  92     public com.oracle.webservices.internal.api.message.MessageContext createContext() {
  93         return packet(null);
  94     }
  95 
  96     public com.oracle.webservices.internal.api.message.MessageContext createContext(SOAPMessage soap) {
  97         throwIfIllegalMessageArgument(soap);
  98         if (saajFactory!= null) return packet(saajFactory.createMessage(soap));
  99         return packet(Messages.create(soap));
 100     }
 101 
 102     public MessageContext createContext(Source m, com.oracle.webservices.internal.api.EnvelopeStyle.Style envelopeStyle) {
 103         throwIfIllegalMessageArgument(m);
 104         return packet(Messages.create(m, SOAPVersion.from(envelopeStyle)));
 105     }
 106 
 107     public com.oracle.webservices.internal.api.message.MessageContext createContext(Source m) {
 108         throwIfIllegalMessageArgument(m);
 109         return packet(Messages.create(m, SOAPVersion.from(singleSoapStyle)));
 110     }
 111 
 112     public com.oracle.webservices.internal.api.message.MessageContext createContext(InputStream in, String contentType) throws IOException {
 113         throwIfIllegalMessageArgument(in);
 114         //TODO when do we use xmlCodec?
 115         Packet p = packet(null);
 116         soapCodec.decode(in, contentType, p);
 117         return p;
 118     }
 119 
 120     /**
 121      * @deprecated http://java.net/jira/browse/JAX_WS-1077
 122      */
 123     @Deprecated
 124     public com.oracle.webservices.internal.api.message.MessageContext createContext(InputStream in, MimeHeaders headers) throws IOException {
 125         String contentType = getHeader(headers, "Content-Type");
 126         Packet packet = (Packet) createContext(in, contentType);
 127         packet.acceptableMimeTypes = getHeader(headers, "Accept");
 128         packet.soapAction = fixQuotesAroundSoapAction(getHeader(headers, "SOAPAction"));
 129 //      packet.put(Packet.INBOUND_TRANSPORT_HEADERS, toMap(headers));
 130         return packet;
 131     }
 132 
 133     static String getHeader(MimeHeaders headers, String name) {
 134         String[] values = headers.getHeader(name);
 135         return (values != null && values.length > 0) ? values[0] : null;
 136     }
 137 
 138     static Map<String, List<String>> toMap(MimeHeaders headers) {
 139         HashMap<String, List<String>> map = new HashMap<String, List<String>>();
 140         for (Iterator<MimeHeader> i = headers.getAllHeaders(); i.hasNext();) {
 141             MimeHeader mh = i.next();
 142             List<String> values = map.get(mh.getName());
 143             if (values == null) {
 144                 values = new ArrayList<String>();
 145                 map.put(mh.getName(), values);
 146             }
 147             values.add(mh.getValue());
 148         }
 149         return map;
 150     }
 151 
 152     public MessageContext createContext(Message m) {
 153         throwIfIllegalMessageArgument(m);
 154         return packet(m);
 155     }
 156 
 157     private Packet packet(Message m) {
 158         final Packet p = new Packet();
 159         //TODO when do we use xmlCodec?
 160         p.codec = soapCodec;
 161         if (m != null) p.setMessage(m);
 162         MTOMFeature mf = features.get(MTOMFeature.class);
 163         if (mf != null) {
 164             p.setMtomFeature(mf);
 165         }
 166         p.setSAAJFactory(saajFactory);
 167         return p;
 168     }
 169 
 170     private void throwIfIllegalMessageArgument(Object message)
 171         throws IllegalArgumentException
 172     {
 173         if (message == null) {
 174             throw new IllegalArgumentException("null messages are not allowed.  Consider using MessageContextFactory.createContext()");
 175         }
 176     }
 177 
 178     @Deprecated
 179     public com.oracle.webservices.internal.api.message.MessageContext doCreate() {
 180         return packet(null);
 181     }
 182     @Deprecated
 183     public com.oracle.webservices.internal.api.message.MessageContext doCreate(SOAPMessage m) {
 184         return createContext(m);
 185     }
 186     @Deprecated
 187     public com.oracle.webservices.internal.api.message.MessageContext doCreate(Source x, SOAPVersion soapVersion) {
 188         return packet(Messages.create(x, soapVersion));
 189     }
 190 }
--- EOF ---