View Javadoc
1   package org.metricshub.winrm.service.client;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WinRM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2024 Metricshub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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   * Code from io.cloudsoft.winrm4j.client.StripShellResponseHandler
39   * release 0.12.3 @link https://github.com/cloudsoft/winrm4j
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  		// Do nothing
89  	}
90  
91  	@Override
92  	public Set<QName> getHeaders() {
93  		return Collections.emptySet();
94  	}
95  }