View Javadoc
1   package org.sentrysoftware.winrm.service;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WinRM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2024 Sentry Software
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 java.util.List;
24  
25  import javax.xml.bind.JAXBElement;
26  import javax.xml.bind.JAXBException;
27  
28  import org.apache.cxf.binding.soap.SoapMessage;
29  import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
30  import org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor;
31  import org.apache.cxf.headers.Header;
32  import org.apache.cxf.interceptor.Fault;
33  import org.apache.cxf.jaxb.JAXBDataBinding;
34  import org.apache.cxf.phase.Phase;
35  
36  import org.sentrysoftware.winrm.Utils;
37  import org.sentrysoftware.winrm.service.wsman.AttributableURI;
38  import org.sentrysoftware.winrm.service.wsman.ObjectFactory;
39  
40  /**
41   * Code from org.opennms.core.wsman.cxf.WSManHeaderInterceptor
42   * release 1.2.3 @link https://github.com/OpenNMS/wsman
43   */
44  public class WSManHeaderInterceptor extends AbstractSoapInterceptor {
45  
46  	private static final JAXBDataBinding ATTRIBUTABLE_URI_JAXB_DATA_BINDING;
47  	static {
48  		try {
49  			ATTRIBUTABLE_URI_JAXB_DATA_BINDING = new JAXBDataBinding(AttributableURI.class);
50  		} catch (final JAXBException e) {
51  			throw new RuntimeException(
52  					"Failed to create JAXBDataBinding for: AttributableURI" + AttributableURI.class,
53  					e);
54  		}
55  	}
56  
57  	private final String resourceUri;
58  
59  	public WSManHeaderInterceptor(final String resourceUri) {
60  		super(Phase.POST_LOGICAL);
61  
62  		addAfter(SoapPreProtocolOutInterceptor.class.getName());
63  
64  		Utils.checkNonNull(resourceUri, "resourceUri");
65  
66  		this.resourceUri = resourceUri;
67  	}
68  
69  	@Override
70  	public void handleMessage(final SoapMessage message) throws Fault {
71  
72  		final JAXBElement<String> resourceURI = new ObjectFactory().createResourceURI(resourceUri);
73  
74  		final List<Header> headers = message.getHeaders();
75  		headers.add(
76  				new Header(resourceURI.getName(), resourceURI, ATTRIBUTABLE_URI_JAXB_DATA_BINDING));
77  
78  		message.put(Header.HEADER_LIST, headers);
79  	}
80  
81  }