001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.soap;
014
015import static org.apache.juneau.soap.SoapXmlSerializer.*;
016
017import java.io.IOException;
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.serializer.*;
022import org.apache.juneau.utils.*;
023import org.apache.juneau.xml.*;
024
025/**
026 * Session object that lives for the duration of a single use of {@link SoapXmlSerializer}.
027 *
028 * <p>
029 * This class is NOT thread safe.
030 * It is typically discarded after one-time use although it can be reused within the same thread.
031 */
032public class SoapXmlSerializerSession extends XmlSerializerSession {
033
034   private final String soapAction;
035
036   /**
037    * Create a new session using properties specified in the context.
038    *
039    * @param ctx
040    *    The context creating this session object.
041    *    The context contains all the configuration settings for this object.
042    * @param args
043    *    Runtime arguments.
044    *    These specify session-level information such as locale and URI context.
045    *    It also include session-level properties that override the properties defined on the bean and
046    *    serializer contexts.
047    */
048   public SoapXmlSerializerSession(SoapXmlSerializer ctx, SerializerSessionArgs args) {
049      super(ctx, args);
050
051      soapAction = getProperty(SOAPXML_SOAPAction, String.class, ctx.soapAction);
052   }
053
054   //-----------------------------------------------------------------------------------------------------------------
055   // Overridden methods
056   //-----------------------------------------------------------------------------------------------------------------
057
058   @Override /* SerializerSession */
059   protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException {
060      try (XmlWriter w = getXmlWriter(out)) {
061         w.append("<?xml")
062            .attr("version", "1.0")
063            .attr("encoding", "UTF-8")
064            .appendln("?>");
065         w.oTag("soap", "Envelope")
066            .attr("xmlns", "soap", soapAction)
067            .appendln(">");
068         w.sTag(1, "soap", "Body").nl(1);
069         indent += 2;
070         w.flush();
071         super.doSerialize(out, o);
072         w.ie(1).eTag("soap", "Body").nl(1);
073         w.eTag("soap", "Envelope").nl(0);
074      }
075   }
076
077   @Override /* Serializer */
078   public Map<String,String> getResponseHeaders() {
079      return new AMap<String,String>().append("SOAPAction", soapAction);
080   }
081
082   //-----------------------------------------------------------------------------------------------------------------
083   // Properties
084   //-----------------------------------------------------------------------------------------------------------------
085
086   /**
087    * Configuration property:  The SOAPAction HTTP header value to set on responses.
088    *
089    * @see SoapXmlSerializer#SOAPXML_SOAPAction
090    * @return
091    *    The SOAPAction HTTP header value to set on responses.
092    */
093   public String getSoapAction() {
094      return soapAction;
095   }
096
097   //-----------------------------------------------------------------------------------------------------------------
098   // Other methods
099   //-----------------------------------------------------------------------------------------------------------------
100
101   @Override /* Session */
102   public ObjectMap toMap() {
103      return super.toMap()
104         .append("SoapXmlSerializerSession", new DefaultFilteringObjectMap()
105            .append("soapAction", soapAction)
106         );
107   }
108}