]> git.basschouten.com Git - openhab-addons.git/blob
84e6c4c6cb5ea8cf20270e0fc96301aaf0ad7edf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.helios.internal.ws.soap;
14
15 import javax.xml.namespace.QName;
16 import javax.xml.soap.SOAPEnvelope;
17 import javax.xml.soap.SOAPHeader;
18 import javax.xml.soap.SOAPHeaderElement;
19 import javax.xml.soap.SOAPMessage;
20 import javax.xml.ws.handler.MessageContext;
21 import javax.xml.ws.handler.soap.SOAPMessageContext;
22
23 import org.openhab.binding.helios.internal.handler.HeliosHandler27;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link SOAPSubscriptionActionHandler} is a custom SOAP handler that modifies
29  * some SOAP headers in order to get the Helios comm. working
30  *
31  * @author Karel Goderis - Initial contribution
32  */
33 public class SOAPSubscriptionActionHandler extends SOAPActionHandler {
34
35     private Logger logger = LoggerFactory.getLogger(SOAPSubscriptionActionHandler.class);
36
37     private HeliosHandler27 handler;
38
39     public SOAPSubscriptionActionHandler(HeliosHandler27 heliosHandler) {
40         this.handler = heliosHandler;
41     }
42
43     @Override
44     public boolean handleMessage(SOAPMessageContext context) {
45         Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
46
47         if (isRequest) {
48             try {
49                 SOAPMessage soapMsg = context.getMessage();
50                 SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
51                 SOAPHeader soapHeader = soapEnv.getHeader();
52
53                 if (soapHeader == null) {
54                     soapHeader = soapEnv.addHeader();
55                 }
56
57                 QName qname = new QName("http://www.2n.cz/2013/event", "SubscriptionId");
58                 SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);
59
60                 soapHeaderElement.addAttribute(
61                         new QName("http://www.w3.org/2005/08/addressing", "IsReferenceParameter"), "true");
62                 if (handler.getSubscriptionID() != null) {
63                     soapHeaderElement.addTextNode(handler.getSubscriptionID());
64                 }
65                 soapMsg.saveChanges();
66             } catch (Exception e) {
67                 logger.debug("An exception occurred while formatting a SOAP header : '{}'", e.getMessage());
68             }
69         }
70
71         return true;
72     }
73 }