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