1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*
21  * XSEC
22  *
23  * XKMSStatusResultImpl := Implementation of StatusResult Messages
24  *
25  * $Id: XKMSStatusResultImpl.cpp 1833340 2018-06-11 15:40:13Z scantor $
26  *
27  */
28 
29 #include <xsec/framework/XSECDefs.hpp>
30 #include <xsec/framework/XSECError.hpp>
31 #include <xsec/framework/XSECEnv.hpp>
32 
33 #ifdef XSEC_XKMS_ENABLED
34 
35 #include "../../utils/XSECDOMUtils.hpp"
36 
37 #include "XKMSStatusResultImpl.hpp"
38 #include "XKMSUnverifiedKeyBindingImpl.hpp"
39 
40 #include <xsec/xkms/XKMSConstants.hpp>
41 
42 #include <xercesc/dom/DOM.hpp>
43 
44 XERCES_CPP_NAMESPACE_USE
45 
46 // --------------------------------------------------------------------------------
47 //           Construct/Destruct
48 // --------------------------------------------------------------------------------
49 
XKMSStatusResultImpl(const XSECEnv * env)50 XKMSStatusResultImpl::XKMSStatusResultImpl(
51 		const XSECEnv * env) :
52 m_result(env),
53 m_msg(m_result.m_msg) {
54 
55 	mp_successAttr = NULL;
56 	mp_failureAttr = NULL;
57 	mp_pendingAttr = NULL;
58 
59 }
60 
XKMSStatusResultImpl(const XSECEnv * env,XERCES_CPP_NAMESPACE_QUALIFIER DOMElement * node)61 XKMSStatusResultImpl::XKMSStatusResultImpl(
62 		const XSECEnv * env,
63 		XERCES_CPP_NAMESPACE_QUALIFIER DOMElement * node) :
64 m_result(env, node),
65 m_msg(m_result.m_msg) {
66 
67 	mp_successAttr = NULL;
68 	mp_failureAttr = NULL;
69 	mp_pendingAttr = NULL;
70 
71 }
72 
~XKMSStatusResultImpl()73 XKMSStatusResultImpl::~XKMSStatusResultImpl() {
74 
75 }
76 
77 
78 // --------------------------------------------------------------------------------
79 //           Load from DOM
80 // --------------------------------------------------------------------------------
81 
82 // Load elements
load()83 void XKMSStatusResultImpl::load() {
84 
85 	if (m_msg.mp_messageAbstractTypeElement == NULL) {
86 
87 		// Attempt to load an empty element
88 		throw XSECException(XSECException::XKMSError,
89 			"XKMSStatusResult::load - called on empty DOM");
90 
91 	}
92 
93 	if (!strEquals(getXKMSLocalName(m_msg.mp_messageAbstractTypeElement),
94 									XKMSConstants::s_tagStatusResult)) {
95 
96 		throw XSECException(XSECException::XKMSError,
97 			"XKMSStatusResult::load - called incorrect node");
98 
99 	}
100 
101 	// Load the base message
102 	m_result.load();
103 
104 }
105 
106 // --------------------------------------------------------------------------------
107 //           Create a blank one
108 // --------------------------------------------------------------------------------
createBlankStatusResult(const XMLCh * service,const XMLCh * id,ResultMajor rmaj,ResultMinor rmin)109 DOMElement * XKMSStatusResultImpl::createBlankStatusResult(
110 		const XMLCh * service,
111 		const XMLCh * id,
112 		ResultMajor rmaj,
113 		ResultMinor rmin) {
114 
115 	return m_result.createBlankResultType(
116 		XKMSConstants::s_tagStatusResult, service, id, rmaj, rmin);
117 
118 }
119 
120 // --------------------------------------------------------------------------------
121 //           Get interface methods
122 // --------------------------------------------------------------------------------
123 
getMessageType(void)124 XKMSMessageAbstractType::messageType XKMSStatusResultImpl::getMessageType(void) {
125 
126 	return XKMSMessageAbstractTypeImpl::StatusResult;
127 
128 }
129 
130 // --------------------------------------------------------------------------------
131 //           Result count handling
132 // --------------------------------------------------------------------------------
133 
getSuccessCount(void) const134 int XKMSStatusResultImpl::getSuccessCount(void) const {
135 
136 	if (mp_successAttr == NULL)
137 		return 0;
138 
139 	unsigned int val;
140 	XMLString::textToBin(mp_successAttr->getNodeValue(), val);
141 
142 	return (int) val;
143 
144 }
145 
getFailureCount(void) const146 int XKMSStatusResultImpl::getFailureCount(void) const {
147 
148 	if (mp_failureAttr == NULL)
149 		return 0;
150 
151 	unsigned int val;
152 	XMLString::textToBin(mp_failureAttr->getNodeValue(), val);
153 
154 	return (int) val;
155 
156 }
157 
getPendingCount(void) const158 int XKMSStatusResultImpl::getPendingCount(void) const {
159 
160 	if (mp_pendingAttr == NULL)
161 		return 0;
162 
163 	unsigned int val;
164 	XMLString::textToBin(mp_pendingAttr->getNodeValue(), val);
165 
166 	return (int) val;
167 
168 }
169 
setSuccessCount(int count)170 void XKMSStatusResultImpl::setSuccessCount(int count) {
171 
172 	if (m_msg.mp_messageAbstractTypeElement == NULL) {
173 
174 		// Attempt update when not initialised
175 		throw XSECException(XSECException::MessageAbstractTypeError,
176 			"XKMSStatusResult::setSuccessCount - called on non-initialised structure");
177 
178 	}
179 
180 	XMLCh val[16];
181 	XMLString::binToText(count, val, 16, 10);
182 
183 	m_msg.mp_messageAbstractTypeElement->setAttributeNS(NULL, XKMSConstants::s_tagSuccess, val);
184 	mp_successAttr =
185 		m_msg.mp_messageAbstractTypeElement->getAttributeNodeNS(NULL, XKMSConstants::s_tagSuccess);
186 
187 }
188 
setFailureCount(int count)189 void XKMSStatusResultImpl::setFailureCount(int count) {
190 
191 	if (m_msg.mp_messageAbstractTypeElement == NULL) {
192 
193 		// Attempt update when not initialised
194 		throw XSECException(XSECException::MessageAbstractTypeError,
195 			"XKMSStatusResult::setFailureCount - called on non-initialised structure");
196 
197 	}
198 
199 	XMLCh val[16];
200 	XMLString::binToText(count, val, 16, 10);
201 
202 	m_msg.mp_messageAbstractTypeElement->setAttributeNS(NULL, XKMSConstants::s_tagFailure, val);
203 	mp_failureAttr =
204 		m_msg.mp_messageAbstractTypeElement->getAttributeNodeNS(NULL, XKMSConstants::s_tagFailure);
205 
206 }
207 
setPendingCount(int count)208 void XKMSStatusResultImpl::setPendingCount(int count) {
209 
210 	if (m_msg.mp_messageAbstractTypeElement == NULL) {
211 
212 		// Attempt update when not initialised
213 		throw XSECException(XSECException::MessageAbstractTypeError,
214 			"XKMSStatusResult::setPendingCount - called on non-initialised structure");
215 
216 	}
217 
218 	XMLCh val[16];
219 	XMLString::binToText(count, val, 16, 10);
220 
221 	m_msg.mp_messageAbstractTypeElement->setAttributeNS(NULL, XKMSConstants::s_tagPending, val);
222 	mp_pendingAttr =
223 		m_msg.mp_messageAbstractTypeElement->getAttributeNodeNS(NULL, XKMSConstants::s_tagPending);
224 
225 }
226 
227 #endif /* XSEC_XKMS_ENABLED */
228