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.encoding;
  27 
  28 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
  29 import com.sun.xml.internal.ws.api.SOAPVersion;
  30 import com.sun.xml.internal.ws.api.WSBinding;
  31 import com.sun.xml.internal.ws.api.WSFeatureList;
  32 import com.sun.xml.internal.ws.api.message.Header;
  33 import com.sun.xml.internal.ws.api.message.Packet;
  34 import com.sun.xml.internal.ws.api.message.AttachmentSet;
  35 import com.sun.xml.internal.ws.api.pipe.ContentType;
  36 import com.sun.xml.internal.ws.message.stream.StreamHeader12;
  37 
  38 import javax.xml.stream.XMLStreamReader;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.io.InputStream;
  42 import java.io.IOException;
  43 
  44 /**
  45  * {@link StreamSOAPCodec} for SOAP 1.2.
  46  *
  47  * @author Paul.Sandoz@Sun.Com
  48  */
  49 final class StreamSOAP12Codec extends StreamSOAPCodec {
  50     static final StreamHeaderDecoder SOAP12StreamHeaderDecoder = new StreamHeaderDecoder() {
  51         @Override
  52         public Header decodeHeader(XMLStreamReader reader, XMLStreamBuffer mark) {
  53             return new StreamHeader12(reader, mark);
  54         }
  55     };
  56 
  57     public static final String SOAP12_MIME_TYPE = "application/soap+xml";
  58     public static final String DEFAULT_SOAP12_CONTENT_TYPE =
  59             SOAP12_MIME_TYPE+"; charset="+SOAPBindingCodec.DEFAULT_ENCODING;
  60     private static final List<String> EXPECTED_CONTENT_TYPES = Collections.singletonList(SOAP12_MIME_TYPE);
  61 
  62     /*package*/  StreamSOAP12Codec() {
  63         super(SOAPVersion.SOAP_12);
  64     }
  65 
  66     /*package*/ StreamSOAP12Codec(WSBinding binding) {
  67         super(binding);
  68     }
  69 
  70     /*package*/  StreamSOAP12Codec(WSFeatureList features) {
  71         super(features);
  72     }
  73 
  74     public String getMimeType() {
  75         return SOAP12_MIME_TYPE;
  76     }
  77 
  78     @Override
  79     protected ContentType getContentType(Packet packet) {
  80         ContentTypeImpl.Builder b = getContenTypeBuilder(packet);
  81         // TODO: set accept header
  82         if (packet.soapAction == null) {
  83             return b.build();
  84         } else {
  85             b.contentType = b.contentType + ";action="+fixQuotesAroundSoapAction(packet.soapAction);
  86             return b.build();
  87         }
  88     }
  89 
  90     @Override
  91     public void decode(InputStream in, String contentType, Packet packet, AttachmentSet att ) throws IOException {
  92         com.sun.xml.internal.ws.encoding.ContentType ct = new com.sun.xml.internal.ws.encoding.ContentType(contentType);
  93         packet.soapAction = fixQuotesAroundSoapAction(ct.getParameter("action"));
  94         super.decode(in,contentType,packet,att);
  95     }
  96 
  97     private String fixQuotesAroundSoapAction(String soapAction) {
  98         if(soapAction != null && (!soapAction.startsWith("\"") || !soapAction.endsWith("\"")) ) {
  99             String fixedSoapAction = soapAction;
 100             if(!soapAction.startsWith("\""))
 101                 fixedSoapAction = "\"" + fixedSoapAction;
 102             if(!soapAction.endsWith("\""))
 103                 fixedSoapAction = fixedSoapAction + "\"";
 104             return fixedSoapAction;
 105         }
 106         return soapAction;
 107     }
 108 
 109     protected List<String> getExpectedContentTypes() {
 110         return EXPECTED_CONTENT_TYPES;
 111     }
 112 
 113     @Override
 114     protected String getDefaultContentType() {
 115         return DEFAULT_SOAP12_CONTENT_TYPE;
 116     }
 117 }