1 /*
   2  * Copyright (c) 1997, 2011, 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 
  31 import javax.xml.soap.SOAPMessage;
  32 import javax.xml.transform.Source;
  33 import javax.xml.ws.WebServiceFeature;
  34 
  35 import com.sun.xml.internal.org.jvnet.ws.EnvelopeStyle;
  36 import com.sun.xml.internal.org.jvnet.ws.EnvelopeStyleFeature;
  37 import com.sun.xml.internal.org.jvnet.ws.message.MessageContext;
  38 
  39 import com.sun.xml.internal.ws.api.SOAPVersion;
  40 import com.sun.xml.internal.ws.api.WSFeatureList;
  41 import com.sun.xml.internal.ws.api.pipe.Codec;
  42 import com.sun.xml.internal.ws.api.pipe.Codecs;
  43 
  44 /**
  45  * The MessageContextFactory implements com.sun.xml.internal.org.jvnet.ws.message.MessageContextFactory as
  46  * a factory of Packet and public facade of Codec(s).
  47  *
  48  * @author shih-chang.chen@oracle.com
  49  */
  50 public class MessageContextFactory extends com.sun.xml.internal.org.jvnet.ws.message.MessageContextFactory {
  51 
  52     private WSFeatureList features;
  53     private Codec soapCodec;
  54     private Codec xmlCodec;
  55     private EnvelopeStyleFeature envelopeStyle;
  56     private EnvelopeStyle.Style singleSoapStyle;
  57 
  58     public MessageContextFactory(WebServiceFeature[] wsf) {
  59         this(new com.sun.xml.internal.ws.binding.WebServiceFeatureList(wsf));
  60     }
  61 
  62     public MessageContextFactory(WSFeatureList wsf) {
  63         features = wsf;
  64         envelopeStyle = features.get(EnvelopeStyleFeature.class);
  65         if (envelopeStyle == null) {//Default to SOAP11
  66             envelopeStyle = new EnvelopeStyleFeature(new EnvelopeStyle.Style[]{EnvelopeStyle.Style.SOAP11});
  67             features.mergeFeatures(new WebServiceFeature[]{envelopeStyle}, false);
  68         }
  69         for (EnvelopeStyle.Style s : envelopeStyle.getStyles()) {
  70             if (s.isXML()) {
  71                 if (xmlCodec == null) xmlCodec = Codecs.createXMLCodec(features);
  72             } else {
  73                 if (soapCodec == null) soapCodec = Codecs.createSOAPBindingCodec(features);
  74                 singleSoapStyle = s;
  75             }
  76         }
  77     }
  78 
  79     protected com.sun.xml.internal.org.jvnet.ws.message.MessageContextFactory newFactory(WebServiceFeature... f) {
  80         return new com.sun.xml.internal.ws.api.message.MessageContextFactory(f);
  81     }
  82 
  83     public MessageContext createContext(SOAPMessage soap) {
  84         return packet(Messages.create(soap));
  85     }
  86 
  87     public MessageContext createContext(Source m, EnvelopeStyle.Style envelopeStyle) {
  88         return packet(Messages.create(m, SOAPVersion.from(envelopeStyle)));
  89     }
  90 
  91     public MessageContext createContext(Source m) {
  92         return packet(Messages.create(m, SOAPVersion.from(singleSoapStyle)));
  93     }
  94 
  95     public MessageContext createContext(InputStream in, String contentType) throws IOException {
  96         //TODO when do we use xmlCodec?
  97         Packet p = packet(null);
  98         soapCodec.decode(in, contentType, p);
  99         return p;
 100     }
 101 
 102     private Packet packet(Message m) {
 103         final Packet p = new Packet();
 104         //TODO when do we use xmlCodec?
 105         p.codec = soapCodec;
 106         if (m != null) p.setMessage(m);
 107         return p;
 108     }
 109 
 110 
 111 
 112     public MessageContext doCreate() {
 113         return packet(null);
 114     }
 115     public MessageContext doCreate(SOAPMessage m) {
 116         return createContext(m);
 117     }
 118     public MessageContext doCreate(Source x, SOAPVersion soapVersion) {
 119         return packet(Messages.create(x, soapVersion));
 120     }
 121 }