我正在开发Java Web服务。此时我可以获得Http头请求。但我想添加更多标头请求。
我目前正在servlet过滤器中执行此操作。
@WebFilter(urlPatterns = {"/*"})
public class AddHeader implements Filter {
@Resource
private WebServiceContext context;
public AddHeader() {
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(
ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request.getContentLength() != -1 && context != null) {
MessageContext mc = context.getMessageContext();
((HttpServletResponse) response).addHeader(
"Operation", "something"
);
}
chain.doFilter(request, response);
}
}
此策略的问题在于添加的标头是静态的。
使用SoapHandler类,我可以获得SOAP消息 - 动态:
public class SoapClass implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext messageContext) {
log(messageContext);
return true;
}
@Override
public Set<QName> getHeaders() {
Set<QName> qNames = Collections.EMPTY_SET;
return qNames;
}
@Override
public boolean handleFault(SOAPMessageContext messageContext) {
log(messageContext);
return true;
}
@Override
public void close(MessageContext context) {
}
public static String getMsgAsString(SOAPMessage message) {
String msg = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
msg = baos.toString();
} catch (SOAPException | IOException soape) {
}
return msg;
}
private String soapToString(SOAPMessage message, boolean indent) {
final StringWriter sw = new StringWriter();
try {
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(message.getSOAPPart()),
new StreamResult(sw));
} catch (TransformerException e) {
throw new RuntimeException(e);
}
return (indent ? sw.toString() : sw.toString().replaceAll("[\\r\\n]", ""));
}
所以,我真正想要的是用过滤器加入动态肥皂消息。我怎样才能做到这一点?
答案 0 :(得分:6)
虽然我还不完全清楚你真正想要的是什么,但我将描述如何在SOAP Web服务响应中添加HTTP头。最简洁的地方是在JAX-WS处理程序中,与SoapClass
形式的处理程序不同。
要在SOAPHandler中执行此操作,您必须首先获得对基础HttpServletResponse
中的标头列表的访问权限。此列表由SOAPMessageContext
:
@Override
public boolean handleMessage(SOAPMessageContext context) {
//This property checks whether the handler is being invoked for a service response
boolean response= ((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();
if (response) {
//this is a JAX-WS-provided map of HTTP headers
Map<String, List<String>> headers = (Map<String, List<String>>) context.get(MessageContext.HTTP_RESPONSE_HEADERS);
if (null == headers) {
//create a new map of HTTP headers if there isn't already one
headers = new HashMap<String, List<String>>();
}
//add your desired header
headers.put("Operation",Collections.singletonList("something");
}
return true;
}
另一种方法是访问Web服务响应所依据的HttpServletResponse
:
@Override
public boolean handleMessage(SOAPMessageContext context) {
//This property checks whether the handler is being invoked for a service response
boolean response= ((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();
if (response) {
//this is underlying http response object
HttpServletResponse response = (HttpServletResponse) context.get(MessageContext.SERVLET_RESPONSE);
//add your desired header
response.addHeader("Operation", "something");
}
return true;
}