-
Notifications
You must be signed in to change notification settings - Fork 1
/
AuditLogHandler.java
158 lines (129 loc) · 5.11 KB
/
AuditLogHandler.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
/*
* 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.kernel.audit.AuditMessage;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
import it.dontesta.labs.liferay.webservice.customusers.service.ws.audit.AuditEventKeys;
import it.dontesta.labs.liferay.webservice.customusers.service.ws.audit.AuditEventUtil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
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 org.osgi.service.component.annotations.Component;
/**
* @author Antonio Musarra
*/
@Component(
immediate = true, property = "audit.log.jax.ws.handler.filters=true",
service = Handler.class
)
public class AuditLogHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public void close(MessageContext context) {
}
@Override
public Set<QName> getHeaders() {
return new HashSet<>();
}
@Override
public boolean handleFault(SOAPMessageContext context) {
_doHandleMessage(context);
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
_doHandleMessage(context);
return true;
}
private void _doHandleMessage(SOAPMessageContext context) {
Boolean isOutBound = (Boolean)context.get(
MessageContext.MESSAGE_OUTBOUND_PROPERTY);
SOAPMessage soapMessage = context.getMessage();
String newCorrelationId = PortalUUIDUtil.generate();
String currentCorrelationId = GetterUtil.getString(
context.get(_CORRELATION_ID));
if (Validator.isNull(currentCorrelationId)) {
currentCorrelationId = newCorrelationId;
context.put(_CORRELATION_ID, newCorrelationId);
context.setScope(_CORRELATION_ID, MessageContext.Scope.APPLICATION);
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
AuditMessage auditMessage;
Map<String, Serializable> additionalInfo = new HashMap<>();
soapMessage.writeTo(baos);
if (isOutBound) {
additionalInfo.put(
_OUT_BOUND_MESSAGE, new String(baos.toByteArray()));
auditMessage = AuditEventUtil.getAuditMessage(
additionalInfo, AuditLogHandler.class.getName(),
currentCorrelationId,
AuditEventKeys.SERVICE_SOAP_TRACE_OUTBOUND_EVENT.
getAuditEventTypeCodeDescription(),
AuditEventKeys.SERVICE_SOAP_TRACE_OUTBOUND_EVENT.
getAuditEventTypeCode(),
AuditEventKeys.SERVICE_SOAP_TRACE_OUTBOUND_EVENT.
getAuditEventTypeId(),
AuditEventKeys.SERVICE_SOAP_TRACE_OUTBOUND_EVENT.
getAuditEventTypeCodeDescription());
}
else {
additionalInfo.put(
_IN_BOUND_MESSAGE, new String(baos.toByteArray()));
auditMessage = AuditEventUtil.getAuditMessage(
additionalInfo, AuditLogHandler.class.getName(),
currentCorrelationId,
AuditEventKeys.SERVICE_SOAP_TRACE_INBOUND_EVENT.
getAuditEventTypeCodeDescription(),
AuditEventKeys.SERVICE_SOAP_TRACE_INBOUND_EVENT.
getAuditEventTypeCode(),
AuditEventKeys.SERVICE_SOAP_TRACE_INBOUND_EVENT.
getAuditEventTypeId(),
AuditEventKeys.SERVICE_SOAP_TRACE_INBOUND_EVENT.
getAuditEventTypeCodeDescription());
}
AuditEventUtil.sendAuditMessage(auditMessage);
}
catch (IOException | SOAPException e) {
if (_log.isErrorEnabled()) {
_log.error(e.getMessage(), e);
}
}
}
private static Log _log = LogFactoryUtil.getLog(AuditLogHandler.class);
private final String _CORRELATION_ID = "correlationId";
private final String _IN_BOUND_MESSAGE = "inBoundMessage";
private final String _OUT_BOUND_MESSAGE = "outBoundMessage";
}