Last active
August 29, 2015 14:12
-
-
Save prietopa/49c595602ca5ad9aca19 to your computer and use it in GitHub Desktop.
CXF persist/audit client Request/Response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.pp.jm.cxf.interceptor.persist.client; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.util.Date; | |
import org.apache.cxf.helpers.IOUtils; | |
import org.apache.cxf.interceptor.Fault; | |
import org.apache.cxf.io.CachedOutputStream; | |
import org.apache.cxf.management.persistence.ExchangeData; | |
import org.apache.cxf.message.Message; | |
import org.apache.cxf.phase.AbstractPhaseInterceptor; | |
import org.apache.cxf.phase.Phase; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
/** | |
* Copy/paste from org.apache.cxf.management.interceptor.PersistOutInterceptor; | |
* | |
* Only Change PHASE in constructor of the class | |
* | |
*/ | |
public abstract class AbstractClientInPersistInterceptor extends AbstractPhaseInterceptor<Message> { | |
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractClientInPersistInterceptor.class); | |
public abstract void saveData(ExchangeData exchangeData); | |
public AbstractClientInPersistInterceptor() { | |
super(Phase.RECEIVE); | |
} | |
private void saveResponse(Message message, ExchangeData exchangeData) { | |
InputStream is = message.getContent(InputStream.class); | |
if (is != null) { | |
CachedOutputStream bos = new CachedOutputStream(); | |
try { | |
IOUtils.copy(is, bos); | |
bos.flush(); | |
is.close(); | |
message.setContent(InputStream.class, bos.getInputStream()); | |
StringBuilder builder = new StringBuilder(); | |
bos.writeCacheTo(builder, bos.size()); | |
bos.close(); | |
exchangeData.setResponse(builder.toString()); | |
exchangeData.setResponseSize((int)bos.size()); | |
exchangeData.setOutDate(new Date()); | |
} catch (IOException e) { | |
throw new Fault(e); | |
} | |
} | |
} | |
private void fillIfError(Message message, ExchangeData exchangeData) { | |
if (message.getContent(Exception.class) != null) { | |
exchangeData.setStatus("ERROR"); | |
Exception exception = message.getContent(Exception.class); | |
StringWriter stringWriter = new StringWriter(); | |
if (exception.getCause() != null) { | |
exchangeData.setExceptionType(exception.getCause().getClass().getName()); | |
exception.getCause().printStackTrace(new PrintWriter(stringWriter)); | |
} else { | |
exchangeData.setExceptionType(exception.getClass().getName()); | |
exception.printStackTrace(new PrintWriter(stringWriter)); | |
} | |
exchangeData.setStackTrace(stringWriter.toString()); | |
} else { | |
exchangeData.setStatus("OK"); | |
} | |
} | |
public void handleMessage(Message message) throws Fault { | |
ExchangeData exchangeData = message.getExchange().getOutMessage().getContent(ExchangeData.class); | |
if (exchangeData != null) { | |
saveResponse(message, exchangeData); | |
fillIfError(message, exchangeData); | |
saveData(exchangeData); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.pp.jm.cxf.client; | |
import org.apache.cxf.endpoint.Client; | |
import org.apache.cxf.frontend.ClientProxy; | |
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.stereotype.Service; | |
import net.pp.jm.cxf.interceptor.persist.client.ClientInPersistInterceptor; | |
import net.pp.jm.cxf.interceptor.persist.client.ClientOutPersistInterceptor; | |
@Service("dynamicClient") | |
public class DynamicClient { | |
private static final Logger LOGGER = LoggerFactory.getLogger(DynamicClient.class); | |
@Autowired | |
private ClientInPersistInterceptor clientInPersistInterceptor; | |
@Autowired | |
private ClientOutPersistInterceptor clientOutPersistInterceptor; | |
@Value("${app.ws.client.audit}") | |
private String isAuditoriaOn; | |
public TipoRegistro callRegistryClient(AnotaRegistroWrapper anotacion) throws Excepcion { | |
LOGGER.info("URL del servicio al que se transmite la informacion: " + anotacion.getUrl()); | |
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); | |
factory.setServiceClass(WSSurexService.class); | |
factory.setAddress(anotacion.getUrl()); | |
WSSurexService client = (WSSurexService) factory.create(); | |
if(Boolean.parseBoolean(isAuditoriaOn)) { | |
Client cxfClient = ClientProxy.getClient(client); | |
cxfClient.getInInterceptors().add(clientInPersistInterceptor); | |
cxfClient.getInFaultInterceptors().add(clientInPersistInterceptor); | |
cxfClient.getOutInterceptors().add(clientOutPersistInterceptor); | |
cxfClient.getOutFaultInterceptors().add(clientOutPersistInterceptor); | |
} | |
return client.anotaRegistro(anotacion.getModo(), anotacion.getAnotacion(), anotacion.getUsuario()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.pp.jm.cxf.interceptor.persist.client; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.util.Date; | |
import org.apache.cxf.helpers.IOUtils; | |
import org.apache.cxf.interceptor.Fault; | |
import org.apache.cxf.io.CachedOutputStream; | |
import org.apache.cxf.management.persistence.ExchangeData; | |
import org.apache.cxf.message.Message; | |
import org.apache.cxf.phase.AbstractPhaseInterceptor; | |
import org.apache.cxf.phase.Phase; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
/** | |
* Copy/paste from org.apache.cxf.management.interceptor.PersistOutInterceptor; | |
* | |
* Only Change PHASE in constructor of the class | |
* | |
* @author E01740 | |
* | |
*/ | |
public class ClientInPersistInterceptor extends AbstractClientInPersistInterceptor { | |
private static final Logger LOGGER = LoggerFactory.getLogger(ClientInPersistInterceptor.class); | |
@Autowired | |
@Qualifier("jdbcWsAudit") | |
private WsAuditDao jdbcWsAudit; | |
// TODO do better | |
public void saveData(ExchangeData exchangeData) { | |
try { | |
long idAudit = jdbcWsAudit.save(exchangeData.getUri(), exchangeData.getOperation(), exchangeData.getRequest()); | |
jdbcWsAudit.updateResponse(idAudit, exchangeData.getResponse()); | |
} catch (Exception e) { | |
LOGGER.error("ERROR interceptor: ", e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.pp.jm.cxf.interceptor.persist.client; | |
import java.io.OutputStream; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.cxf.interceptor.Fault; | |
import org.apache.cxf.interceptor.LoggingMessage; | |
import org.apache.cxf.io.CacheAndWriteOutputStream; | |
import org.apache.cxf.io.CachedOutputStream; | |
import org.apache.cxf.io.CachedOutputStreamCallback; | |
import org.apache.cxf.management.persistence.ExchangeData; | |
import org.apache.cxf.management.persistence.ExchangeDataProperty; | |
import org.apache.cxf.message.Message; | |
import org.apache.cxf.phase.AbstractPhaseInterceptor; | |
import org.apache.cxf.phase.Phase; | |
import org.apache.cxf.service.Service; | |
import org.apache.cxf.service.model.OperationInfo; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* Copy/paste from org.apache.cxf.management.interceptor.PersistInInterceptor.java | |
* | |
* Only Change PHASE in constructor of the class | |
* | |
*/ | |
public class ClientOutPersistInterceptor extends AbstractPhaseInterceptor<Message> { | |
private static final Logger LOGGER = LoggerFactory.getLogger(ClientOutPersistInterceptor.class); | |
class PersistOutInterceptorClientCallback implements CachedOutputStreamCallback { | |
private final Message message; | |
private final OutputStream origStream; | |
private final ExchangeData exchange; | |
public PersistOutInterceptorClientCallback(final Message msg, final OutputStream os, | |
final ExchangeData ex) { | |
this.message = msg; | |
this.origStream = os; | |
this.exchange = ex; | |
} | |
public void onClose(CachedOutputStream cos) { | |
String id = (String)this.message.getExchange().get(LoggingMessage.ID_KEY); | |
if (id == null) { | |
id = LoggingMessage.nextId(); | |
this.message.getExchange().put(LoggingMessage.ID_KEY, id); | |
} | |
try { | |
StringBuilder buffer = new StringBuilder(); | |
cos.writeCacheTo(buffer, cos.size()); | |
this.exchange.setRequestSize((int)cos.size()); | |
this.exchange.setRequest(buffer.toString()); | |
} catch (Exception ex) { | |
// ignore | |
} | |
try { | |
// empty out the cache | |
cos.lockOutputStream(); | |
cos.resetOut(null, false); | |
} catch (Exception ex) { | |
// ignore | |
} | |
this.message.setContent(OutputStream.class, this.origStream); | |
} | |
public void onFlush(CachedOutputStream cos) { | |
} | |
} | |
public static final String CXF_CONSOLE_ADDITIONAL_PROPERTY_PREFIX = | |
"org.apache.cxf.management.interceptor.prefix"; | |
public ClientOutPersistInterceptor() { | |
super(Phase.SEND); | |
} | |
/** | |
* Copied from LoggingInInterceptor | |
* | |
* @param soapMessage | |
*/ | |
private void getSoapRequest(Message message, ExchangeData exchangeData) { | |
final CacheAndWriteOutputStream os = (CacheAndWriteOutputStream) message.getContent(OutputStream.class); | |
if (os == null) { | |
return; | |
} | |
fillProperties(message, exchangeData); | |
try { | |
// Write the output while caching it for the log message | |
message.setContent(OutputStream.class, os); | |
// newOut.registerCallback(new PersistOutInterceptorClientCallback(message, os, exchangeData)); | |
StringBuilder buffer = new StringBuilder(); | |
os.writeCacheTo(buffer); | |
String request = buffer.toString(); | |
if(request == null) { | |
request = os.toString(); | |
} | |
exchangeData.setRequestSize(request.length()); | |
exchangeData.setRequest(request); | |
} catch (Exception ex) { | |
LOGGER.error("ERROR: ", ex); | |
} | |
} | |
private void fillProperties(Message message, ExchangeData exchangeData) { | |
try { | |
String url = String.valueOf(message.getExchange().get(org.apache.cxf.message.Message.ENDPOINT_ADDRESS)); | |
Service service = message.getExchange().get(Service.class); | |
String serviceName = String.valueOf(service.getName()); | |
OperationInfo opInfo = message.getExchange().get(OperationInfo.class); | |
String operationName = opInfo == null ? null : opInfo.getName().getLocalPart(); | |
if (operationName == null) { | |
Object nameProperty = message.getExchange().get("org.apache.cxf.resource.operation.name"); | |
if (nameProperty != null) { | |
operationName = "\"" + nameProperty.toString() + "\""; | |
} | |
} | |
exchangeData.setUri(url); | |
exchangeData.setServiceName(serviceName); | |
exchangeData.setOperation(operationName); | |
// add all additional properties | |
addPropertiesFrom(exchangeData, message.getExchange().getOutMessage()); | |
addPropertiesFrom(exchangeData, message); | |
} catch (Exception e) { | |
LOGGER.error("ERROR: ", e); | |
} | |
} | |
private static void addProperty(ExchangeData exchange, String key, String value) { | |
ExchangeDataProperty exchangeProperty = new ExchangeDataProperty(); | |
exchangeProperty.setExchangeData(exchange); | |
exchangeProperty.setName(key); | |
exchangeProperty.setValue(value); | |
if (exchange.getProperties() == null) { | |
exchange.setProperties(new ArrayList<ExchangeDataProperty>()); | |
} | |
exchange.getProperties().add(exchangeProperty); | |
} | |
private void addPropertiesFrom(ExchangeData exchange, Message message) { | |
for (Map.Entry<String, Object> entry : message.entrySet()) { | |
if (entry.getKey().equals(org.apache.cxf.message.Message.ENCODING)) { | |
exchange.setEncoding((String)entry.getValue()); | |
} else if (entry.getKey().equals(org.apache.cxf.message.Message.REQUEST_URI)) { | |
exchange.setUri((String)entry.getValue()); | |
} else if (entry.getKey().equals(org.apache.cxf.message.Message.PROTOCOL_HEADERS)) { | |
if (entry.getValue() instanceof Map) { | |
List<?> userAgents = (List<?>)((Map<?, ?>)entry.getValue()).get("user-agent"); | |
if (userAgents != null && !userAgents.isEmpty()) { | |
exchange.setUserAgent(userAgents.get(0).toString()); | |
} | |
} | |
if (entry.getValue() != null) { | |
addProperty(exchange, entry.getKey(), entry.getValue().toString()); | |
} | |
} else if (entry.getKey().startsWith("org.apache.cxf.message.Message.") | |
&& (entry.getValue() instanceof String || entry.getValue() instanceof Integer || entry | |
.getValue() instanceof Boolean)) { | |
addProperty(exchange, entry.getKey(), entry.getValue().toString()); | |
} else if (entry.getKey().startsWith(CXF_CONSOLE_ADDITIONAL_PROPERTY_PREFIX)) { | |
addProperty(exchange, entry.getKey().substring( | |
CXF_CONSOLE_ADDITIONAL_PROPERTY_PREFIX | |
.length()), entry.getValue().toString()); | |
} | |
} | |
} | |
public void handleMessage(Message message) throws Fault { | |
ExchangeData soapExchange = new ExchangeData(); | |
soapExchange.setInDate(new Date()); | |
message.setContent(ExchangeData.class, soapExchange); | |
getSoapRequest(message, soapExchange); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- AUDITORIA CLIENT --> | |
<bean id="clientOutPersistInterceptor" | |
class="es.tecnocom.amtega.pcpg_vortal.registro_serverws.interceptor.presistence.ClientOutPersistInterceptor" /> | |
<bean id="clientInPersistInterceptor" | |
class="es.tecnocom.amtega.pcpg_vortal.registro_serverws.interceptor.presistence.ClientInPersistInterceptor"> | |
</bean> | |
<!-- CONFIGURACION GENERAL PARA TODOS LOS SERVICIOS --> | |
<cxf:bus> | |
<cxf:features> | |
<cxf:logging /> | |
</cxf:features> | |
</cxf:bus> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment