001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.soap;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import java.io.*;
023import java.util.*;
024
025import org.apache.juneau.marshall.serializer.*;
026import org.apache.juneau.marshall.xml.*;
027
028/**
029 * Session object that lives for the duration of a single use of {@link SoapXmlSerializer}.
030 *
031 * <h5 class='section'>Notes:</h5><ul>
032 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
033 * </ul>
034 *
035 */
036@SuppressWarnings({
037   "java:S110", // Inheritance depth acceptable for this class hierarchy
038   "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants
039   "resource" // Resource management handled externally
040})
041public class SoapXmlSerializerSession extends XmlSerializerSession {
042
043   /**
044    * Builder class.
045    */
046   public static class Builder extends XmlSerializerSession.Builder<Builder> {
047
048      private final SoapXmlSerializer ctx;
049
050      /**
051       * Constructor
052       *
053       * @param ctx The context creating this session.
054       *    <br>Cannot be <jk>null</jk>.
055       */
056      protected Builder(SoapXmlSerializer ctx) {
057         super(reqnn("ctx", ctx));
058         this.ctx = ctx;
059      }
060
061      @Override
062      public SoapXmlSerializerSession build() {
063         return new SoapXmlSerializerSession(this);
064      }
065
066   }
067
068   /**
069    * Creates a new builder for this object.
070    *
071    * @param ctx The context creating this session.
072    *    <br>Cannot be <jk>null</jk>.
073    * @return A new builder.
074    */
075   public static Builder create(SoapXmlSerializer ctx) {
076      return new Builder(reqnn("ctx", ctx));
077   }
078
079   private final SoapXmlSerializer ctx;
080
081   /**
082    * Constructor.
083    *
084    * @param builder The builder for this object.
085    */
086   protected SoapXmlSerializerSession(Builder builder) {
087      super(builder);
088
089      ctx = builder.ctx;
090   }
091
092   @Override /* Overridden from Serializer */
093   public Map<String,String> getResponseHeaders() { return map("SOAPAction", getSoapAction()); }
094
095   /**
096    * The SOAPAction HTTP header value to set on responses.
097    *
098    * @see SoapXmlSerializer.Builder#soapAction(String)
099    * @return
100    *    The SOAPAction HTTP header value to set on responses.
101    */
102   public String getSoapAction() { return ctx.getSoapAction(); }
103
104   @Override /* Overridden from SerializerSession */
105   protected void doWrite(SerializerPipe out, Object o) throws IOException, SerializeException {
106      try (XmlWriter<?> w = getXmlWriter(out)) {
107         // @formatter:off
108         w.append("<?xml")
109            .attr("version", "1.0")
110            .attr("encoding", "UTF-8")
111            .appendln("?>");
112         w.oTag("soap", "Envelope")
113            .attr("xmlns", "soap", getSoapAction())
114            .appendln(">");
115         w.sTag(1, "soap", "Body").nl(1);
116         indent += 2;
117         w.flush();
118         super.doWrite(out, o);
119         w.ie(1).eTag("soap", "Body").nl(1);
120         w.eTag("soap", "Envelope").nl(0);
121         // @formatter:on
122      }
123   }
124}