1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.ws;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import javax.xml.ws.handler.MessageContext;
21
22 import org.apache.camel.Exchange;
23 import org.apache.camel.ExchangePattern;
24 import org.apache.cxf.jaxws.context.WebServiceContextImpl;
25 import org.apache.cxf.ws.addressing.AddressingProperties;
26 import org.apache.cxf.ws.addressing.JAXWSAConstants;
27 import org.openehealth.ipf.commons.ihe.ws.correlation.AsynchronyCorrelator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35 abstract public class AbstractAsyncResponseWebService extends AbstractWebService {
36 private static final transient Logger LOG = LoggerFactory.getLogger(AbstractAsyncResponseWebService.class);
37
38
39
40
41
42
43
44
45
46
47
48 protected boolean canDropCorrelation(Object response) {
49 return true;
50 }
51
52
53
54
55
56
57 @SuppressWarnings("unchecked")
58 @Override
59 protected Exchange process(
60 Object body,
61 Map<String, Object> headers,
62 ExchangePattern exchangePattern)
63 {
64 final AsynchronyCorrelator correlator = getConsumer().getEndpoint().getCorrelator();
65
66 MessageContext messageContext = new WebServiceContextImpl().getMessageContext();
67 AddressingProperties apropos = (AddressingProperties) messageContext.get(JAXWSAConstants.ADDRESSING_PROPERTIES_INBOUND);
68 String messageId = ((apropos != null) && (apropos.getRelatesTo() != null))
69 ? apropos.getRelatesTo().getValue()
70 : null;
71
72
73 if (messageId == null) {
74 String[] alternativeKeys = getAlternativeResponseKeys(body);
75 if (alternativeKeys != null) {
76 for (String key : alternativeKeys) {
77 messageId = correlator.getMessageId(key);
78 if (messageId != null) {
79 break;
80 }
81 }
82 }
83 }
84
85 if (messageId != null) {
86
87 String correlationKey = correlator.getCorrelationKey(messageId);
88 if (correlationKey != null) {
89 if (headers == null) {
90
91 headers = new HashMap<>();
92 }
93 headers.put(AbstractWsEndpoint.CORRELATION_KEY_HEADER_NAME, correlationKey);
94 }
95
96
97 if (canDropCorrelation(body)) {
98 correlator.delete(messageId);
99 }
100 } else {
101 LOG.error("Cannot retrieve WSA RelatesTo header, message correlation not possible");
102 }
103
104 return super.process(body, headers, exchangePattern);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 protected String[] getAlternativeResponseKeys(Object response) {
123 return null;
124 }
125 }
126