1 /*
   2  * Copyright (c) 1997, 2017, 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.messaging.saaj.soap;
  27 
  28 import java.io.*;
  29 import java.util.logging.Logger;
  30 
  31 import javax.xml.soap.*;
  32 import javax.xml.stream.XMLStreamReader;
  33 
  34 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType;
  35 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ParseException;
  36 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
  37 import com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl;
  38 import com.sun.xml.internal.messaging.saaj.soap.ver1_2.Message1_2Impl;
  39 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
  40 import com.sun.xml.internal.messaging.saaj.util.TeeInputStream;
  41 
  42 /**
  43  * A factory for creating SOAP messages.
  44  *
  45  * Converted to a placeholder for common functionality between SOAP
  46  * implementations.
  47  *
  48  * @author Phil Goodwin (phil.goodwin@sun.com)
  49  */
  50 public class MessageFactoryImpl extends MessageFactory {
  51 
  52     protected static final Logger log =
  53         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
  54                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
  55 
  56     protected  OutputStream listener;
  57 
  58     protected boolean lazyAttachments = false;
  59 
  60     public  OutputStream listen(OutputStream newListener) {
  61         OutputStream oldListener = listener;
  62         listener = newListener;
  63         return oldListener;
  64     }
  65 
  66     @Override
  67     public SOAPMessage createMessage() throws SOAPException {
  68         throw new UnsupportedOperationException();
  69     }
  70 
  71     public SOAPMessage createMessage(String protocol) throws SOAPException {
  72         if (SOAPConstants.SOAP_1_1_PROTOCOL.equals(protocol))
  73                 return new com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl();
  74         else
  75                 return new com.sun.xml.internal.messaging.saaj.soap.ver1_2.Message1_2Impl();
  76     }
  77 
  78     public SOAPMessage createMessage(boolean isFastInfoset,
  79         boolean acceptFastInfoset) throws SOAPException
  80     {
  81         throw new UnsupportedOperationException();
  82     }
  83 
  84     public SOAPMessage createMessage(MimeHeaders headers, XMLStreamReader reader) throws SOAPException, IOException {
  85         String contentTypeString = MessageImpl.getContentType(headers);
  86 
  87         if (listener != null) {
  88             throw new SOAPException("Listener OutputStream is not supported with XMLStreamReader");
  89         }
  90 
  91         try {
  92             ContentType contentType = new ContentType(contentTypeString);
  93             int stat = MessageImpl.identifyContentType(contentType);
  94 
  95             if (MessageImpl.isSoap1_1Content(stat)) {
  96                 return new Message1_1Impl(headers,contentType,stat,reader);
  97             } else if (MessageImpl.isSoap1_2Content(stat)) {
  98                 return new Message1_2Impl(headers,contentType,stat,reader);
  99             } else {
 100                 log.severe("SAAJ0530.soap.unknown.Content-Type");
 101                 throw new SOAPExceptionImpl("Unrecognized Content-Type");
 102             }
 103         } catch (ParseException e) {
 104             log.severe("SAAJ0531.soap.cannot.parse.Content-Type");
 105             throw new SOAPExceptionImpl(
 106                 "Unable to parse content type: " + e.getMessage());
 107         }
 108     }
 109     @Override
 110     public SOAPMessage createMessage(MimeHeaders headers, InputStream in)
 111         throws SOAPException, IOException {
 112         String contentTypeString = MessageImpl.getContentType(headers);
 113 
 114         if (listener != null) {
 115             in = new TeeInputStream(in, listener);
 116         }
 117 
 118         try {
 119             ContentType contentType = new ContentType(contentTypeString);
 120             int stat = MessageImpl.identifyContentType(contentType);
 121 
 122             if (MessageImpl.isSoap1_1Content(stat)) {
 123                 return new Message1_1Impl(headers,contentType,stat,in);
 124             } else if (MessageImpl.isSoap1_2Content(stat)) {
 125                 return new Message1_2Impl(headers,contentType,stat,in);
 126             } else {
 127                 log.severe("SAAJ0530.soap.unknown.Content-Type");
 128                 throw new SOAPExceptionImpl("Unrecognized Content-Type");
 129             }
 130         } catch (ParseException e) {
 131             log.severe("SAAJ0531.soap.cannot.parse.Content-Type");
 132             throw new SOAPExceptionImpl(
 133                 "Unable to parse content type: " + e.getMessage());
 134         }
 135     }
 136 
 137     protected static final String getContentType(MimeHeaders headers) {
 138         String[] values = headers.getHeader("Content-Type");
 139         if (values == null)
 140             return null;
 141         else
 142             return values[0];
 143     }
 144 
 145     public void setLazyAttachmentOptimization(boolean flag) {
 146         lazyAttachments = flag;
 147     }
 148 
 149 }