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 
  51     public static final String SOAP12_MIME_TYPE = "application/soap+xml";
  52     public static final String DEFAULT_SOAP12_CONTENT_TYPE =
  53             SOAP12_MIME_TYPE+"; charset="+SOAPBindingCodec.DEFAULT_ENCODING;
  54     private static final List<String> EXPECTED_CONTENT_TYPES = Collections.singletonList(SOAP12_MIME_TYPE);
  55 
  56     /*package*/  StreamSOAP12Codec() {
  57         super(SOAPVersion.SOAP_12);
  58     }
  59 
  60     /*package*/ StreamSOAP12Codec(WSBinding binding) {
  61         super(binding);
  62     }
  63 
  64     /*package*/  StreamSOAP12Codec(WSFeatureList features) {
  65         super(features);
  66     }
  67 
  68     public String getMimeType() {
  69         return SOAP12_MIME_TYPE;
  70     }
  71 
  72     @Override
  73     protected ContentType getContentType(Packet packet) {
  74         ContentTypeImpl.Builder b = getContenTypeBuilder(packet);
  75         // TODO: set accept header
  76         if (packet.soapAction == null) {
  77             return b.build();
  78         } else {
  79             b.contentType = b.contentType + ";action="+fixQuotesAroundSoapAction(packet.soapAction);
  80             return b.build();
  81         }
  82     }
  83 
  84     @Override
  85     public void decode(InputStream in, String contentType, Packet packet, AttachmentSet att ) throws IOException {
  86         com.sun.xml.internal.ws.encoding.ContentType ct = new com.sun.xml.internal.ws.encoding.ContentType(contentType);
  87         packet.soapAction = fixQuotesAroundSoapAction(ct.getParameter("action"));
  88         super.decode(in,contentType,packet,att);
  89     }
  90 
  91     private String fixQuotesAroundSoapAction(String soapAction) {
  92         if(soapAction != null && (!soapAction.startsWith("\"") || !soapAction.endsWith("\"")) ) {
  93             String fixedSoapAction = soapAction;
  94             if(!soapAction.startsWith("\""))
  95                 fixedSoapAction = "\"" + fixedSoapAction;
  96             if(!soapAction.endsWith("\""))
  97                 fixedSoapAction = fixedSoapAction + "\"";
  98             return fixedSoapAction;
  99         }
 100         return soapAction;
 101     }
 102 
 103     protected List<String> getExpectedContentTypes() {
 104         return EXPECTED_CONTENT_TYPES;
 105     }
 106 
 107     @Override
 108     protected String getDefaultContentType() {
 109         return DEFAULT_SOAP12_CONTENT_TYPE;
 110     }
 111 }