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.commons.utils.AssertionUtils.*; 020import static org.apache.juneau.commons.utils.CollectionUtils.*; 021 022import java.io.*; 023import java.lang.reflect.*; 024import java.nio.charset.*; 025import java.util.*; 026import java.util.function.*; 027 028import org.apache.juneau.*; 029import org.apache.juneau.httppart.*; 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 */ 042@SuppressWarnings("resource") 043public class SoapXmlSerializerSession extends XmlSerializerSession { 044 /** 045 * Builder class. 046 */ 047 public static class Builder extends XmlSerializerSession.Builder { 048 049 private SoapXmlSerializer ctx; 050 051 /** 052 * Constructor 053 * 054 * @param ctx The context creating this session. 055 * <br>Cannot be <jk>null</jk>. 056 */ 057 protected Builder(SoapXmlSerializer ctx) { 058 super(assertArgNotNull("ctx", ctx)); 059 this.ctx = ctx; 060 } 061 062 @Override /* Overridden from Builder */ 063 public <T> Builder apply(Class<T> type, Consumer<T> apply) { 064 super.apply(type, apply); 065 return this; 066 } 067 068 @Override 069 public SoapXmlSerializerSession build() { 070 return new SoapXmlSerializerSession(this); 071 } 072 073 @Override /* Overridden from Builder */ 074 public Builder debug(Boolean value) { 075 super.debug(value); 076 return this; 077 } 078 079 @Override /* Overridden from Builder */ 080 public Builder fileCharset(Charset value) { 081 super.fileCharset(value); 082 return this; 083 } 084 085 @Override /* Overridden from Builder */ 086 public Builder javaMethod(Method value) { 087 super.javaMethod(value); 088 return this; 089 } 090 091 @Override /* Overridden from Builder */ 092 public Builder locale(Locale value) { 093 super.locale(value); 094 return this; 095 } 096 097 @Override /* Overridden from Builder */ 098 public Builder mediaType(MediaType value) { 099 super.mediaType(value); 100 return this; 101 } 102 103 @Override /* Overridden from Builder */ 104 public Builder mediaTypeDefault(MediaType value) { 105 super.mediaTypeDefault(value); 106 return this; 107 } 108 109 @Override /* Overridden from Builder */ 110 public Builder properties(Map<String,Object> value) { 111 super.properties(value); 112 return this; 113 } 114 115 @Override /* Overridden from Builder */ 116 public Builder property(String key, Object value) { 117 super.property(key, value); 118 return this; 119 } 120 121 @Override /* Overridden from Builder */ 122 public Builder resolver(VarResolverSession value) { 123 super.resolver(value); 124 return this; 125 } 126 127 @Override /* Overridden from Builder */ 128 public Builder schema(HttpPartSchema value) { 129 super.schema(value); 130 return this; 131 } 132 133 @Override /* Overridden from Builder */ 134 public Builder schemaDefault(HttpPartSchema value) { 135 super.schemaDefault(value); 136 return this; 137 } 138 139 @Override /* Overridden from Builder */ 140 public Builder streamCharset(Charset value) { 141 super.streamCharset(value); 142 return this; 143 } 144 145 @Override /* Overridden from Builder */ 146 public Builder timeZone(TimeZone value) { 147 super.timeZone(value); 148 return this; 149 } 150 151 @Override /* Overridden from Builder */ 152 public Builder timeZoneDefault(TimeZone value) { 153 super.timeZoneDefault(value); 154 return this; 155 } 156 157 @Override /* Overridden from Builder */ 158 public Builder unmodifiable() { 159 super.unmodifiable(); 160 return this; 161 } 162 163 @Override /* Overridden from Builder */ 164 public Builder uriContext(UriContext value) { 165 super.uriContext(value); 166 return this; 167 } 168 169 @Override /* Overridden from Builder */ 170 public Builder useWhitespace(Boolean value) { 171 super.useWhitespace(value); 172 return this; 173 } 174 } 175 176 /** 177 * Creates a new builder for this object. 178 * 179 * @param ctx The context creating this session. 180 * <br>Cannot be <jk>null</jk>. 181 * @return A new builder. 182 */ 183 public static Builder create(SoapXmlSerializer ctx) { 184 return new Builder(assertArgNotNull("ctx", ctx)); 185 } 186 187 private final SoapXmlSerializer ctx; 188 189 /** 190 * Constructor. 191 * 192 * @param builder The builder for this object. 193 */ 194 protected SoapXmlSerializerSession(Builder builder) { 195 super(builder); 196 197 ctx = builder.ctx; 198 } 199 200 @Override /* Overridden from Serializer */ 201 public Map<String,String> getResponseHeaders() { return map("SOAPAction", getSoapAction()); } 202 203 /** 204 * The SOAPAction HTTP header value to set on responses. 205 * 206 * @see SoapXmlSerializer.Builder#soapAction(String) 207 * @return 208 * The SOAPAction HTTP header value to set on responses. 209 */ 210 public String getSoapAction() { return ctx.getSoapAction(); } 211 212 @Override /* Overridden from SerializerSession */ 213 protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException { 214 try (XmlWriter w = getXmlWriter(out)) { 215 // @formatter:off 216 w.append("<?xml") 217 .attr("version", "1.0") 218 .attr("encoding", "UTF-8") 219 .appendln("?>"); 220 w.oTag("soap", "Envelope") 221 .attr("xmlns", "soap", getSoapAction()) 222 .appendln(">"); 223 w.sTag(1, "soap", "Body").nl(1); 224 indent += 2; 225 w.flush(); 226 super.doSerialize(out, o); 227 w.ie(1).eTag("soap", "Body").nl(1); 228 w.eTag("soap", "Envelope").nl(0); 229 // @formatter:on 230 } 231 } 232}