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