Loguj żądanie i odpowiedź http jax-ws
Muszę zarejestrować pełne żądanie http i odpowiedź w wywołaniu usługi WebService JAX-WS. Na żądanie potrzebuję nagłówków żądań i treści oraz odpowiedzi, nagłówków odpowiedzi i treści.
Po kilku badaniach odkryłem, że mogę uzyskać te informacje wraz z właściwością:
-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true
i pokaż informacje, których potrzebuję, ale zrzuca je do konsoli i muszę je zapisać w bazie danych z wewnętrznym identyfikatorem żądania.
Próbowałem zaimplementować program obsługi:
public class LoggingHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext context) {
Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
System.out.println("SOAP outbound!!!!!");
Map<String, List<String>> responseHeaders = (Map<String, List<String>>) context
.get(SOAPMessageContext.HTTP_RESPONSE_HEADERS);
try {
String headers = getHeaders(responseHeaders);
System.out.println(headers);
String body = getBody(context.getMessage());
System.out.println(body);
} catch (Exception ex) {
// TODO: What do I have to do in this case?
}
} else {
System.out.println("SOAP inbound!!!!!");
Map<String, List<String>> requestHeaders = (Map<String, List<String>>) context
.get(SOAPMessageContext.HTTP_REQUEST_HEADERS);
try {
String headers = getHeaders(requestHeaders);
System.out.println(headers);
String body = getBody(context.getMessage());
System.out.println(body);
} catch (Exception ex) {
// TODO: What do I have to do in this case?
}
}
return true;
}
private String getBody(SOAPMessage message) throws SOAPException, IOException {
OutputStream stream = new ByteArrayOutputStream();
message.writeTo(stream);
return stream.toString();
}
public String getFullHttpRequest(HttpServletRequest request) throws IOException {
InputStream in = request.getInputStream();
String encoding = request.getCharacterEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
return body;
}
private String getHeaders(Map<String, List<String>> headers) throws IOException {
StringBuffer result = new StringBuffer();
if (headers != null) {
for (Entry<String, List<String>> header : headers.entrySet()) {
if (header.getValue().isEmpty()) {
// I don't think this is legal, but let's just dump it,
// as the point of the dump is to uncover problems.
result.append(header.getValue());
} else {
for (String value : header.getValue()) {
result.append(header.getKey() + ": " + value);
}
}
result.append("\n");
}
}
return result.toString();
}
}
ale w tym przypadku mogę uzyskać nagłówki i treść żądania http, ale w odpowiedzi otrzymuję tylko treść, nagłówki odpowiedzi http są zawsze puste.
Masz jakiś pomysł, jak to zrobić? Celem jest umożliwienie przechowywania pełnego żądania i odpowiedzi http w bazie danych.
Dzięki!!