This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
forked from pmenglund/vcap-tests
/
ServiceController.java
265 lines (233 loc) · 8.9 KB
/
ServiceController.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package org.cloudfoundry.canonical.apps;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.cloudfoundry.runtime.env.AbstractServiceInfo;
import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.cloudfoundry.runtime.env.MongoServiceInfo;
import org.cloudfoundry.runtime.env.RedisServiceInfo;
import org.cloudfoundry.runtime.service.messaging.RabbitServiceCreator;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import redis.clients.jedis.Jedis;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoException;
import com.rabbitmq.client.Channel;
@Controller
public class ServiceController {
private ReferenceDataRepository referenceRepository;
@Autowired
public void setReferenceRepository(
ReferenceDataRepository referenceRepository) {
this.referenceRepository = referenceRepository;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public void hello(HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.print("hello from spring");
}
@RequestMapping(value = "/crash", method = RequestMethod.GET)
public void crash(HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
// Thread.currentThread().interrupt();
System.exit(0);
out.println("it should not get here");
}
@RequestMapping(value = "/service/mongo/{key}", method = RequestMethod.POST)
public void mongo_post(@RequestBody String body, @PathVariable String key,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
BasicDBObject doc = new BasicDBObject();
doc.put("key", key);
doc.put("data_value", body);
DBCollection coll = loadMongo();
coll.insert(doc);
out.print(body);
}
@RequestMapping(value = "/service/mongo/{key}", method = RequestMethod.GET)
public void mongo_get(@PathVariable String key, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
DBCollection coll = loadMongo();
BasicDBObject query = new BasicDBObject();
query.put("key", key);
DBCursor cur = coll.find(query);
String value = "";
while (cur.hasNext()) {
value = (String) cur.next().get("data_value");
}
out.print(value);
}
@RequestMapping(value = "/service/redis/{key}", method = RequestMethod.POST)
public void redis_post(@RequestBody String body, @PathVariable String key,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
Jedis jedis = loadJedis();
jedis.set(key, body);
out.print(body);
}
@RequestMapping(value = "/service/redis/{key}", method = RequestMethod.GET)
public void redis_get(@PathVariable String key, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
Jedis jedis = loadJedis();
out.print(jedis.get(key));
}
@RequestMapping(value = "/service/mysql/{key}", method = RequestMethod.POST)
public void mysql_post(@RequestBody String body, @PathVariable String key,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
DataValue d = new DataValue();
d.setId(key);
d.setDataValue(body);
referenceRepository.save(d);
out.print(body);
}
@RequestMapping(value = "/service/mysql/{key}", method = RequestMethod.GET)
public void mysql_get(@PathVariable String key, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
DataValue d = referenceRepository.find(key);
out.print(d.getDataValue());
}
@RequestMapping(value = "/service/rabbit/{key}", method = RequestMethod.POST)
public void rabbit_post(@RequestBody String body, @PathVariable String key,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
RabbitTemplate amq = loadRabbit(key);
amq.convertAndSend(key, body);
out.print(body);
}
@RequestMapping(value = "/service/rabbit/{key}", method = RequestMethod.GET)
public void rabbit_get(@PathVariable String key,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
RabbitTemplate amq = loadRabbit(key);
out.print((String) amq.receiveAndConvert(key));
}
@RequestMapping(value = "/service/rabbitsrs/{key}", method = RequestMethod.POST)
public void rabbitsrs_post(@RequestBody String body,
@PathVariable String key, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
RabbitTemplate amq = loadSRSRabbit(key);
amq.convertAndSend(key, body);
out.print(body);
}
@RequestMapping(value = "/service/rabbitsrs/{key}", method = RequestMethod.GET)
public void rabbitsrs_get(@PathVariable String key,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
RabbitTemplate amq = loadSRSRabbit(key);
out.print((String) amq.receiveAndConvert(key));
}
private RabbitTemplate loadRabbit(String key) {
try {
Connection conn = connectionRabbit().createConnection();
Channel channel = conn.createChannel(true);
channel.exchangeDeclare(key, "direct");
channel.queueDeclare(key, true, false, false, null);
channel.close();
return new RabbitTemplate(connectionRabbit());
} catch (Exception e) {
e.printStackTrace();
throw new AmqpException("Failed to create rabbit template");
}
}
private RabbitTemplate loadSRSRabbit(String key) {
try {
Connection conn = connectionRabbitSRS().createConnection();
Channel channel = conn.createChannel(true);
channel.exchangeDeclare(key, "direct");
channel.queueDeclare(key, true, false, false, null);
channel.close();
return new RabbitTemplate(connectionRabbitSRS());
} catch (Exception e) {
e.printStackTrace();
throw new AmqpException("Failed to create rabbit template");
}
}
private ConnectionFactory connectionRabbitSRS() {
return new RabbitSRSServiceCreator(environment()).createService();
}
private ConnectionFactory connectionRabbit() {
return new RabbitServiceCreator(environment()).createSingletonService().service;
}
private Jedis loadJedis() {
RedisServiceInfo service = (RedisServiceInfo) getService(RedisServiceInfo.class);
Jedis jedis = new Jedis(service.getHost(), service.getPort());
jedis.auth(service.getPassword());
return jedis;
}
private DBCollection loadMongo() {
Map<String, Object> service = (Map<String, Object>) getMongoSettings()
.get("credentials");
Mongo m = null;
DBCollection coll = null;
DB db = null;
try {
// TODO: We need to rewrite this when
MongoServiceInfo mongoService = (MongoServiceInfo) getService(MongoServiceInfo.class);
// org.cloudfoundry.runtime.env.MongoServiceInfo adds getUsername
int port = ((Integer) service.get("port")).intValue();
String host = (String) service.get("hostname");
String name = (String) service.get("name");
String password = (String) service.get("password");
String username = (String) service.get("username");
String db_name = (String) service.get("db");
m = new Mongo(host, port);
db = m.getDB(db_name);
if (db.authenticate(username, password.toCharArray())) {
coll = db.getCollection(name);
}
} catch (UnknownHostException e) {
} catch (MongoException e) {
}
return coll;
}
// TODO: We need to rewrite this when
// org.cloudfoundry.runtime.env.MongoServiceInfo adds getUsername
private Map<String, Object> getMongoSettings() {
CloudEnvironment env = environment();
List<Map<String, Object>> services = env.getServices();
for (Map<String, Object> service : services) {
String label = (String) service.get("label");
if (label.indexOf("mongodb-1.8") > -1) {
return service;
}
}
return null;
}
@RequestMapping(value = "/env", method = RequestMethod.GET)
public void env(HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
CloudEnvironment env = environment();
out.println(env.getValue("VCAP_SERVICES"));
}
private <T extends AbstractServiceInfo> AbstractServiceInfo getService(
Class<T> service) {
CloudEnvironment env = environment();
return env.getServiceInfos(service).get(0);
}
@Bean
CloudEnvironment environment() {
return new CloudEnvironment();
}
}