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.util.*; 018 019import org.apache.juneau.serializer.*; 020import org.apache.juneau.utils.*; 021import org.apache.juneau.xml.*; 022 023/** 024 * Session object that lives for the duration of a single use of {@link SoapXmlSerializer}. 025 * 026 * <p> 027 * This class is NOT thread safe. 028 * It is typically discarded after one-time use although it can be reused within the same thread. 029 */ 030public class SoapXmlSerializerSession extends XmlSerializerSession { 031 032 private final String soapAction; 033 034 /** 035 * Create a new session using properties specified in the context. 036 * 037 * @param ctx 038 * The context creating this session object. 039 * The context contains all the configuration settings for this object. 040 * @param args 041 * Runtime arguments. 042 * These specify session-level information such as locale and URI context. 043 * It also include session-level properties that override the properties defined on the bean and 044 * serializer contexts. 045 */ 046 public SoapXmlSerializerSession(SoapXmlSerializer ctx, SerializerSessionArgs args) { 047 super(ctx, args); 048 049 soapAction = getProperty(SOAPXML_SOAPAction, String.class, ctx.soapAction); 050 } 051 052 //-------------------------------------------------------------------------------- 053 // Overridden methods 054 //-------------------------------------------------------------------------------- 055 056 @Override /* SerializerSession */ 057 protected void doSerialize(SerializerPipe out, Object o) throws Exception { 058 try (XmlWriter w = getXmlWriter(out)) { 059 w.append("<?xml") 060 .attr("version", "1.0") 061 .attr("encoding", "UTF-8") 062 .appendln("?>"); 063 w.oTag("soap", "Envelope") 064 .attr("xmlns", "soap", soapAction) 065 .appendln(">"); 066 w.sTag(1, "soap", "Body").nl(1); 067 indent += 2; 068 w.flush(); 069 super.doSerialize(out, o); 070 w.ie(1).eTag("soap", "Body").nl(1); 071 w.eTag("soap", "Envelope").nl(0); 072 } 073 } 074 075 @Override /* Serializer */ 076 public Map<String,String> getResponseHeaders() { 077 return new AMap<String,String>().append("SOAPAction", soapAction); 078 } 079}