1 package org.metricshub.winrm.service.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import static jakarta.xml.ws.handler.MessageContext.WSDL_OPERATION;
24
25 import jakarta.xml.soap.SOAPBody;
26 import jakarta.xml.soap.SOAPElement;
27 import jakarta.xml.soap.SOAPEnvelope;
28 import jakarta.xml.soap.SOAPException;
29 import jakarta.xml.ws.handler.MessageContext;
30 import jakarta.xml.ws.handler.soap.SOAPHandler;
31 import jakarta.xml.ws.handler.soap.SOAPMessageContext;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.Set;
35 import javax.xml.namespace.QName;
36
37
38
39
40
41 public class StripShellResponseHandler implements SOAPHandler<SOAPMessageContext> {
42
43 @Override
44 public boolean handleMessage(final SOAPMessageContext context) {
45 final Boolean messageOutbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
46 if (messageOutbound != null && messageOutbound.booleanValue()) {
47 return true;
48 }
49
50 final QName action = (QName) context.get(WSDL_OPERATION);
51 if (action != null && !"Create".equals(action.getLocalPart())) {
52 return true;
53 }
54
55 final Iterator<?> childIterator = getBodyChildren(context);
56 while (childIterator.hasNext()) {
57 final Object node = childIterator.next();
58
59 if (node instanceof SOAPElement) {
60 final SOAPElement soapElement = (SOAPElement) node;
61 if ("Shell".equals(soapElement.getLocalName())) {
62 childIterator.remove();
63 }
64 }
65 }
66
67 return true;
68 }
69
70 private Iterator<?> getBodyChildren(final SOAPMessageContext context) {
71 try {
72 final SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
73 final SOAPBody body = envelope.getBody();
74
75 return body.getChildElements();
76 } catch (final SOAPException e) {
77 throw new IllegalStateException(e);
78 }
79 }
80
81 @Override
82 public boolean handleFault(final SOAPMessageContext context) {
83 return true;
84 }
85
86 @Override
87 public void close(final MessageContext context) {
88
89 }
90
91 @Override
92 public Set<QName> getHeaders() {
93 return Collections.emptySet();
94 }
95 }