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 
  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         return packet(Messages.create(soap));
  99     }
 100 
 101     public MessageContext createContext(Source m, com.oracle.webservices.internal.api.EnvelopeStyle.Style envelopeStyle) {
 102         throwIfIllegalMessageArgument(m);
 103         return packet(Messages.create(m, SOAPVersion.from(envelopeStyle)));
 104     }
 105 
 106     public com.oracle.webservices.internal.api.message.MessageContext createContext(Source m) {
 107         throwIfIllegalMessageArgument(m);
 108         return packet(Messages.create(m, SOAPVersion.from(singleSoapStyle)));
 109     }
 110 
 111     public com.oracle.webservices.internal.api.message.MessageContext createContext(InputStream in, String contentType) throws IOException {
 112         throwIfIllegalMessageArgument(in);
 113         //TODO when do we use xmlCodec?
 114         Packet p = packet(null);
 115         soapCodec.decode(in, contentType, p);
 116         return p;
 117     }
 118 
 119     /**
 120      * @deprecated http://java.net/jira/browse/JAX_WS-1077
 121      */
 122     @Deprecated
 123     public com.oracle.webservices.internal.api.message.MessageContext createContext(InputStream in, MimeHeaders headers) throws IOException {
 124         String contentType = getHeader(headers, "Content-Type");
 125         Packet packet = (Packet) createContext(in, contentType);
 126         packet.acceptableMimeTypes = getHeader(headers, "Accept");
 127         packet.soapAction = fixQuotesAroundSoapAction(getHeader(headers, "SOAPAction"));
 128 //      packet.put(Packet.INBOUND_TRANSPORT_HEADERS, toMap(headers));
 129         return packet;
 130     }
 131 
 132     static String getHeader(MimeHeaders headers, String name) {
 133         String[] values = headers.getHeader(name);
 134         return (values != null && values.length > 0) ? values[0] : null;
 135     }
 136 
 137     static Map<String, List<String>> toMap(MimeHeaders headers) {
 138         HashMap<String, List<String>> map = new HashMap<String, List<String>>();
 139         for (Iterator<MimeHeader> i = headers.getAllHeaders(); i.hasNext();) {
 140             MimeHeader mh = i.next();
 141             List<String> values = map.get(mh.getName());
 142             if (values == null) {
 143                 values = new ArrayList<String>();
 144                 map.put(mh.getName(), values);
 145             }
 146             values.add(mh.getValue());
 147         }
 148         return map;
 149     }
 150 
 151     public MessageContext createContext(Message m) {
 152         throwIfIllegalMessageArgument(m);
 153         return packet(m);
 154     }
 155 
 156     private Packet packet(Message m) {
 157         final Packet p = new Packet();
 158         //TODO when do we use xmlCodec?
 159         p.codec = soapCodec;
 160         if (m != null) p.setMessage(m);
 161         MTOMFeature mf = features.get(MTOMFeature.class);
 162         if (mf != null) {
 163             p.setMtomFeature(mf);
 164         }
 165         return p;
 166     }
 167 
 168     private void throwIfIllegalMessageArgument(Object message)
 169         throws IllegalArgumentException
 170     {
 171         if (message == null) {
 172             throw new IllegalArgumentException("null messages are not allowed.  Consider using MessageContextFactory.createContext()");
 173         }
 174     }
 175 
 176     @Deprecated
 177     public com.oracle.webservices.internal.api.message.MessageContext doCreate() {
 178         return packet(null);
 179     }
 180     @Deprecated
 181     public com.oracle.webservices.internal.api.message.MessageContext doCreate(SOAPMessage m) {
 182         return createContext(m);
 183     }
 184     @Deprecated
 185     public com.oracle.webservices.internal.api.message.MessageContext doCreate(Source x, SOAPVersion soapVersion) {
 186         return packet(Messages.create(x, soapVersion));
 187     }
 188 }