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.internal.CollectionUtils.*;
016
017import java.io.IOException;
018import java.lang.reflect.*;
019import java.nio.charset.*;
020import java.util.*;
021import java.util.function.*;
022
023import org.apache.juneau.*;
024import org.apache.juneau.httppart.*;
025import org.apache.juneau.internal.*;
026import org.apache.juneau.serializer.*;
027import org.apache.juneau.svl.*;
028import org.apache.juneau.xml.*;
029
030/**
031 * Session object that lives for the duration of a single use of {@link SoapXmlSerializer}.
032 *
033 * <h5 class='section'>Notes:</h5><ul>
034 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
035 * </ul>
036 *
037 * <h5 class='section'>See Also:</h5><ul>
038 * </ul>
039 */
040public class SoapXmlSerializerSession extends XmlSerializerSession {
041
042   //-----------------------------------------------------------------------------------------------------------------
043   // Static
044   //-----------------------------------------------------------------------------------------------------------------
045
046   /**
047    * Creates a new builder for this object.
048    *
049    * @param ctx The context creating this session.
050    * @return A new builder.
051    */
052   public static Builder create(SoapXmlSerializer ctx) {
053      return new Builder(ctx);
054   }
055
056   //-----------------------------------------------------------------------------------------------------------------
057   // Builder
058   //-----------------------------------------------------------------------------------------------------------------
059
060   /**
061    * Builder class.
062    */
063   @FluentSetters
064   public static class Builder extends XmlSerializerSession.Builder {
065
066      SoapXmlSerializer ctx;
067
068      /**
069       * Constructor
070       *
071       * @param ctx The context creating this session.
072       */
073      protected Builder(SoapXmlSerializer ctx) {
074         super(ctx);
075         this.ctx = ctx;
076      }
077
078      @Override
079      public SoapXmlSerializerSession build() {
080         return new SoapXmlSerializerSession(this);
081      }
082
083      // <FluentSetters>
084
085      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
086      public <T> Builder apply(Class<T> type, Consumer<T> apply) {
087         super.apply(type, apply);
088         return this;
089      }
090
091      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
092      public Builder debug(Boolean value) {
093         super.debug(value);
094         return this;
095      }
096
097      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
098      public Builder properties(Map<String,Object> value) {
099         super.properties(value);
100         return this;
101      }
102
103      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
104      public Builder property(String key, Object value) {
105         super.property(key, value);
106         return this;
107      }
108
109      @Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
110      public Builder unmodifiable() {
111         super.unmodifiable();
112         return this;
113      }
114
115      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
116      public Builder locale(Locale value) {
117         super.locale(value);
118         return this;
119      }
120
121      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
122      public Builder localeDefault(Locale value) {
123         super.localeDefault(value);
124         return this;
125      }
126
127      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
128      public Builder mediaType(MediaType value) {
129         super.mediaType(value);
130         return this;
131      }
132
133      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
134      public Builder mediaTypeDefault(MediaType value) {
135         super.mediaTypeDefault(value);
136         return this;
137      }
138
139      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
140      public Builder timeZone(TimeZone value) {
141         super.timeZone(value);
142         return this;
143      }
144
145      @Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
146      public Builder timeZoneDefault(TimeZone value) {
147         super.timeZoneDefault(value);
148         return this;
149      }
150
151      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
152      public Builder javaMethod(Method value) {
153         super.javaMethod(value);
154         return this;
155      }
156
157      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
158      public Builder resolver(VarResolverSession value) {
159         super.resolver(value);
160         return this;
161      }
162
163      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
164      public Builder schema(HttpPartSchema value) {
165         super.schema(value);
166         return this;
167      }
168
169      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
170      public Builder schemaDefault(HttpPartSchema value) {
171         super.schemaDefault(value);
172         return this;
173      }
174
175      @Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
176      public Builder uriContext(UriContext value) {
177         super.uriContext(value);
178         return this;
179      }
180
181      @Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
182      public Builder fileCharset(Charset value) {
183         super.fileCharset(value);
184         return this;
185      }
186
187      @Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
188      public Builder streamCharset(Charset value) {
189         super.streamCharset(value);
190         return this;
191      }
192
193      @Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
194      public Builder useWhitespace(Boolean value) {
195         super.useWhitespace(value);
196         return this;
197      }
198
199      // </FluentSetters>
200   }
201
202   //-----------------------------------------------------------------------------------------------------------------
203   // Instance
204   //-----------------------------------------------------------------------------------------------------------------
205
206   private final SoapXmlSerializer ctx;
207
208   /**
209    * Constructor.
210    *
211    * @param builder The builder for this object.
212    */
213   protected SoapXmlSerializerSession(Builder builder) {
214      super(builder);
215
216      ctx = builder.ctx;
217   }
218
219   //-----------------------------------------------------------------------------------------------------------------
220   // Overridden methods
221   //-----------------------------------------------------------------------------------------------------------------
222
223   @Override /* SerializerSession */
224   protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException {
225      try (XmlWriter w = getXmlWriter(out)) {
226         w.append("<?xml")
227            .attr("version", "1.0")
228            .attr("encoding", "UTF-8")
229            .appendln("?>");
230         w.oTag("soap", "Envelope")
231            .attr("xmlns", "soap", getSoapAction())
232            .appendln(">");
233         w.sTag(1, "soap", "Body").nl(1);
234         indent += 2;
235         w.flush();
236         super.doSerialize(out, o);
237         w.ie(1).eTag("soap", "Body").nl(1);
238         w.eTag("soap", "Envelope").nl(0);
239      }
240   }
241
242   @Override /* Serializer */
243   public Map<String,String> getResponseHeaders() {
244      return map("SOAPAction",getSoapAction());
245   }
246
247   //-----------------------------------------------------------------------------------------------------------------
248   // Properties
249   //-----------------------------------------------------------------------------------------------------------------
250
251   /**
252    * The SOAPAction HTTP header value to set on responses.
253    *
254    * @see SoapXmlSerializer.Builder#soapAction(String)
255    * @return
256    *    The SOAPAction HTTP header value to set on responses.
257    */
258   public String getSoapAction() {
259      return ctx.getSoapAction();
260   }
261}