forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consumer.java
83 lines (72 loc) · 2.71 KB
/
Consumer.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
package mock.contract;
import com.intuit.karate.JsonUtils;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import javax.jms.TextMessage;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class Consumer {
private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
private final String paymentServiceUrl;
private final String proxyHost;
private final Integer proxyPort;
private final QueueConsumer queueConsumer;
public Consumer(String paymentServiceUrl, String queueName) {
this(paymentServiceUrl, null, null, queueName);
}
public Consumer(String paymentServiceUrl, String proxyHost, Integer proxyPort, String queueName) {
this.paymentServiceUrl = paymentServiceUrl;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
queueConsumer = new QueueConsumer(queueName);
}
private HttpURLConnection getConnection(String path) throws Exception {
URL url = new URL(paymentServiceUrl + path);
if (proxyHost != null) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
return (HttpURLConnection) url.openConnection(proxy);
} else {
return (HttpURLConnection) url.openConnection();
}
}
public Payment create(Payment payment) {
try {
HttpURLConnection con = getConnection("/payments");
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
String json = JsonUtils.toJson(payment);
IOUtils.write(json, con.getOutputStream(), "utf-8");
int status = con.getResponseCode();
if (status != 200) {
throw new RuntimeException("status code was " + status);
}
String content = IOUtils.toString(con.getInputStream(), "utf-8");
return JsonUtils.fromJson(content, Payment.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void listen(java.util.function.Consumer<String> handler) {
queueConsumer.setMessageListener(message -> {
try {
TextMessage tm = (TextMessage) message;
String json = tm.getText();
logger.info("*** received message: {}", json);
handler.accept(json);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
public void stopQueueConsumer() {
queueConsumer.stop();
}
}