1 /*
   2  * Copyright (c) 1997, 2014, 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     public SOAPMessage createMessage() throws SOAPException {
  67         throw new UnsupportedOperationException();
  68     }
  69 
  70     public SOAPMessage createMessage(String protocol) throws SOAPException {
  71         if (SOAPConstants.SOAP_1_1_PROTOCOL.equals(protocol))
  72                 return new com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl();
  73         else
  74                 return new com.sun.xml.internal.messaging.saaj.soap.ver1_2.Message1_2Impl();
  75     }
  76 
  77     public SOAPMessage createMessage(boolean isFastInfoset,
  78         boolean acceptFastInfoset) throws SOAPException
  79     {
  80         throw new UnsupportedOperationException();
  81     }
  82 
  83     public SOAPMessage createMessage(MimeHeaders headers, XMLStreamReader reader) throws SOAPException, IOException {
  84         String contentTypeString = MessageImpl.getContentType(headers);
  85 
  86         if (listener != null) {
  87             throw new SOAPException("Listener OutputStream is not supported with XMLStreamReader");
  88         }
  89 
  90         try {
  91             ContentType contentType = new ContentType(contentTypeString);
  92             int stat = MessageImpl.identifyContentType(contentType);
  93 
  94             if (MessageImpl.isSoap1_1Content(stat)) {
  95                 return new Message1_1Impl(headers,contentType,stat,reader);
  96             } else if (MessageImpl.isSoap1_2Content(stat)) {
  97                 return new Message1_2Impl(headers,contentType,stat,reader);
  98             } else {
  99                 log.severe("SAAJ0530.soap.unknown.Content-Type");
 100                 throw new SOAPExceptionImpl("Unrecognized Content-Type");
 101             }
 102         } catch (ParseException e) {
 103             log.severe("SAAJ0531.soap.cannot.parse.Content-Type");
 104             throw new SOAPExceptionImpl(
 105                 "Unable to parse content type: " + e.getMessage());
 106         }
 107     }
 108     public SOAPMessage createMessage(MimeHeaders headers, InputStream in)
 109         throws SOAPException, IOException {
 110         String contentTypeString = MessageImpl.getContentType(headers);
 111 
 112         if (listener != null) {
 113             in = new TeeInputStream(in, listener);
 114         }
 115 
 116         try {
 117             ContentType contentType = new ContentType(contentTypeString);
 118             int stat = MessageImpl.identifyContentType(contentType);
 119 
 120             if (MessageImpl.isSoap1_1Content(stat)) {
 121                 return new Message1_1Impl(headers,contentType,stat,in);
 122             } else if (MessageImpl.isSoap1_2Content(stat)) {
 123                 return new Message1_2Impl(headers,contentType,stat,in);
 124             } else {
 125                 log.severe("SAAJ0530.soap.unknown.Content-Type");
 126                 throw new SOAPExceptionImpl("Unrecognized Content-Type");
 127             }
 128         } catch (ParseException e) {
 129             log.severe("SAAJ0531.soap.cannot.parse.Content-Type");
 130             throw new SOAPExceptionImpl(
 131                 "Unable to parse content type: " + e.getMessage());
 132         }
 133     }
 134 
 135     protected static final String getContentType(MimeHeaders headers) {
 136         String[] values = headers.getHeader("Content-Type");
 137         if (values == null)
 138             return null;
 139         else
 140             return values[0];
 141     }
 142 
 143     public void setLazyAttachmentOptimization(boolean flag) {
 144         lazyAttachments = flag;
 145     }
 146 
 147 }