-
Notifications
You must be signed in to change notification settings - Fork 1
/
MacAddressValidatorHandler.java
193 lines (157 loc) · 5.58 KB
/
MacAddressValidatorHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2019 Antonio Musarra's Blog - https://www.dontesta.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package it.dontesta.labs.liferay.webservice.customusers.service.ws.handler;
import com.liferay.portal.configuration.metatype.bnd.util.ConfigurableUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.Validator;
import it.dontesta.labs.liferay.webservice.customusers.service.ws.handler.configuration.MacAddressValidatorHandlerConfiguration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import javax.xml.ws.soap.SOAPFaultException;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
/**
* @author Antonio Musarra
*/
@Component(
configurationPid = MacAddressValidatorHandlerConfiguration.PID,
immediate = true,
property = "mac.address.validator.jax.ws.handler.filters=true",
service = Handler.class
)
public class MacAddressValidatorHandler
implements SOAPHandler<SOAPMessageContext> {
@Override
public void close(MessageContext context) {
}
@Override
public Set<QName> getHeaders() {
return new HashSet<>();
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
_doValidateMacAddress(context);
return true;
}
@Activate
protected void activate(Map<String, Object> properties) {
_macAddressValidatorHandlerConfiguration =
ConfigurableUtil.createConfigurable(
MacAddressValidatorHandlerConfiguration.class, properties);
}
@Modified
protected void modified(Map<String, Object> properties) {
if (_log.isInfoEnabled()) {
_log.info(
String.format(
"The configuration %s was modified.",
MacAddressValidatorHandlerConfiguration.PID));
}
activate(properties);
}
private void _doValidateMacAddress(SOAPMessageContext context) {
Boolean isOutBound = (Boolean)context.get(
MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!isOutBound) {
try {
SOAPMessage soapMessage = context.getMessage();
SOAPEnvelope soapEnv = soapMessage.getSOAPPart(
).getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
if (Validator.isNull(soapHeader)) {
soapHeader = soapEnv.addHeader();
_generateSOAPErrorMessage(soapMessage, "No SOAP header.");
}
//Get client mac address from SOAP header
Iterator<?> headers = soapHeader.extractHeaderElements(
SOAPConstants.URI_SOAP_ACTOR_NEXT);
if (Validator.isNull(headers) || !headers.hasNext()) {
_generateSOAPErrorMessage(
soapMessage, "No header block for next actor.");
}
Node macNode = (Node)headers.next();
String macValue =
Validator.isNull(macNode) ? null : macNode.getValue();
if (Validator.isNull(macValue)) {
_generateSOAPErrorMessage(
soapMessage, "No MAC Address in header block.");
}
if (!ArrayUtil.contains(
_macAddressValidatorHandlerConfiguration.
macAddressWhiteList(),
macValue, true)) {
_generateSOAPErrorMessage(
soapMessage, "Invalid MAC Address, access is denied.");
}
}
catch (SOAPException soape) {
if (_log.isErrorEnabled()) {
_log.error(soape.getMessage(), soape);
}
_generateSOAPErrorMessage(
context.getMessage(), soape.getMessage());
}
}
}
private void _generateSOAPErrorMessage(
SOAPMessage soapMessage, String reason) {
try {
SOAPBody soapBody = soapMessage.getSOAPPart(
).getEnvelope(
).getBody();
SOAPFault soapFault = soapBody.addFault();
soapFault.setFaultString(reason);
throw new SOAPFaultException(soapFault);
}
catch (SOAPException soape) {
if (_log.isErrorEnabled()) {
_log.error(soape.getMessage(), soape);
}
}
}
private static Log _log = LogFactoryUtil.getLog(
MacAddressValidatorHandler.class);
private volatile MacAddressValidatorHandlerConfiguration
_macAddressValidatorHandlerConfiguration;
}