-
Notifications
You must be signed in to change notification settings - Fork 138
/
SOAPAuthParam.java
353 lines (302 loc) · 10.4 KB
/
SOAPAuthParam.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.security.jmac.provider;
import com.sun.xml.ws.api.message.Message;
import com.sun.xml.ws.api.message.Messages;
import com.sun.xml.ws.api.message.Packet;
import jakarta.xml.soap.SOAPMessage;
import java.util.HashMap;
import java.util.Map;
/**
* SOAP authentication parameter.
*
* <p> An instance of SOAPAuthParam may be created with a null response object
* (for example during a call to
* <code>ServerAuthContext.validateRequest</code>).
* If so, a response object may be created as necessary (by modules),
* and set into the SOAPAuthParam via the <code>setResponse</code> method.
*
* <p> An instance of SOAPAuthParam may also be created with a null
* request object (for example during a call to
* <code>ServerAuthContext.secureResponse</code>).
*
* @version 1.12, 06/08/04
*/
public class SOAPAuthParam {
private HashMap infoMap;
private boolean requestInPacket;
private boolean responseInPacket;
private SOAPMessage request;
private SOAPMessage response;
private static Exception classLoadingException = checkForPacket();
private static final String REQ_PACKET = "REQ_PACKET";
private static final String RES_PACKET = "RES_PACKET";
private static boolean REQUEST_PACKET = true;
private static boolean RESPONSE_PACKET = false;
private static Exception checkForPacket() {
try {
if (Class.forName("com.sun.xml.ws.api.message.Packet") != null &&
Class.forName("com.sun.xml.ws.api.message.Messages") != null) {
return null;
}
} catch (Exception e) {
// silently disables packet support
return e;
}
return null;
}
/**
* Create a SOAPAuthParam.
*
* @param request the SOAP request object, which may be null.
* @param response the SOAP response object, which may be null.
*/
public SOAPAuthParam(SOAPMessage request, SOAPMessage response) {
this.infoMap = null;
this.request = request;
this.response = response;
}
/**
* Create a SOAPAuthParam (using Packets)
*
* @param request the request Packet, which may be null.
* @param response the response Packet, which may be null.
* @param dummy int serves only to disambiguate constructors
*/
public SOAPAuthParam(Object request, Object response, int dummy) {
if (classLoadingException != null) {
throw new RuntimeException(classLoadingException);
}
if ((request == null || request instanceof Packet) &&
(response == null || response instanceof Packet)) {
this.infoMap = new HashMap();
this.infoMap.put(REQ_PACKET,request);
this.infoMap.put(RES_PACKET,response);
this.requestInPacket = (request == null ? false : true);
this.responseInPacket = (response == null ? false : true);
} else {
throw new RuntimeException("argument is not packet");
}
}
/**
* Get the SOAP request object.
*
* @return the SOAP request object, which may be null.
*/
public Map getMap() {
if (this.infoMap == null) {
this.infoMap = new HashMap();
}
return this.infoMap;
}
/**
* Get the SOAP request object.
*
* @return the SOAP request object, which may be null.
*/
public SOAPMessage getRequest() {
if (this.request == null) {
Object p = getPacket(REQUEST_PACKET,true);
if (p != null && this.requestInPacket) {
// if packet is not null, get SOAP from packet
// requestInPacket set to false as side-effect
// since packet has been consumed.
this.request = getSOAPFromPacket(REQUEST_PACKET,p);
}
}
return this.request;
}
/**
* Get the SOAP response object.
*
* @return the SOAP response object, which may be null.
*/
public SOAPMessage getResponse() {
if (this.response == null) {
Object p = getPacket(RESPONSE_PACKET,false);
if (p != null && this.responseInPacket) {
// if packet is not null, get SOAP from packet
// responseInPacket set to false as side-effect
// since packet has been consumed.
this.response = getSOAPFromPacket(RESPONSE_PACKET,p);
}
}
return this.response;
}
/**
* Set the SOAP request object.
*
* @param request the SOAP response object.
*/
public void setRequest(SOAPMessage request) {
Object p = getPacket(REQUEST_PACKET,false);
if (p != null) {
this.requestInPacket = putSOAPInPacket(request,p);
}
this.request = request;
}
/**
* Set the SOAP response object.
*
* @param response the SOAP response object.
*/
public void setResponse(SOAPMessage response) {
// XXX previously, i.e. before wsit,
// if a response had already been set (it is non-null),
// this method would return with doing anything
// The original response would not be overwritten.
// that is no longer the case.
Object p = getPacket(RESPONSE_PACKET,false);
if (p != null) {
this.responseInPacket = putSOAPInPacket(response,p);
}
this.response = response;
}
/**
* Return the request Packet.
*
* @return the request Packet, which may be null.
*/
public Object getRequestPacket() {
if (classLoadingException != null) {
throw new RuntimeException(classLoadingException);
}
return getPacket(REQUEST_PACKET,true);
}
/**
* Return the response Packet.
*
* @return the response Packet, which may be null.
*/
public Object getResponsePacket() {
if (classLoadingException != null) {
throw new RuntimeException(classLoadingException);
}
return getPacket(RESPONSE_PACKET,true);
}
/**
* Set the request Packet.
*
* <p> has the side effect of resetting the SOAP request message.
*
* @param packet the request Packet
*/
public void setRequestPacket(Object p) {
if (classLoadingException != null) {
throw new RuntimeException(classLoadingException);
}
if (p == null || p instanceof Packet) {
getMap().put(REQ_PACKET,p);
this.requestInPacket = (p == null ? false : true);
this.request = null;
} else {
throw new RuntimeException("argument is not packet");
}
}
/**
* Set the response Packet.
*
* <p> has the side effect of resetting the SOAP response message.
*
* @param packet the response Packet
*/
public void setResponsePacket(Object p) {
if (classLoadingException != null) {
throw new RuntimeException(classLoadingException);
}
if (p == null || p instanceof Packet) {
getMap().put(RES_PACKET,p);
this.responseInPacket = (p == null ? false : true);
this.response = null;
} else {
throw new RuntimeException("argument is not packet");
}
}
/**
* Return the request Packet.
*
* @return the request Packet, which may be null.
*/
private Object getPacket(boolean isRequestPacket, boolean putDesired) {
Object p = (this.infoMap == null ?
null : this.infoMap.get
(isRequestPacket ? REQ_PACKET : RES_PACKET));
if (putDesired) {
SOAPMessage m = (isRequestPacket ? this.request : this.response);
if (p != null && m != null) {
// if SOAP request message has been read from packet
// we may need to set it back in the packet before
// returning the revised packet
if (isRequestPacket) {
if (!this.requestInPacket) {
this.requestInPacket = putSOAPInPacket(m,p);
}
} else {
if (!this.responseInPacket) {
this.responseInPacket = putSOAPInPacket(m,p);
}
}
}
}
return p;
}
private SOAPMessage getSOAPFromPacket(boolean isRequestPacket,Object p) {
if (classLoadingException != null) {
throw new RuntimeException(classLoadingException);
}
SOAPMessage s = null;
if (p instanceof Packet) {
Message m = ((Packet) p).getMessage();
if (m != null) {
try {
s = m.readAsSOAPMessage();
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
if (s != null) {
// System.out.println("SOAPAuthParam.getSOAPFromPacket:");
// printSOAP(s);
if (isRequestPacket) {
this.requestInPacket = false;
} else {
this.responseInPacket = false;
}
}
return s;
}
private boolean putSOAPInPacket(SOAPMessage m, Object p) {
if (m == null) {
((Packet)p).setMessage(null);
} else {
Message msg = Messages.create(m);
((Packet)p).setMessage(msg);
}
return true;
}
public static void printSOAP(SOAPMessage s) {
try {
if (s != null) {
s.writeTo(System.out);
} else {
// System.out.println("SOAPMessage is empty");
}
} catch (Exception e) {
// System.out.println("SOAPAuthParam.printSOAP exception!");
}
}
}