1
+ /*
2
+ Copyright 2017 Ericsson AB.
3
+ For a full list of individual contributors, please see the commit history.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+ package com .ericsson .ei .frontend ;
18
+
19
+ import java .io .BufferedReader ;
20
+ import java .io .IOException ;
21
+ import java .io .InputStream ;
22
+ import java .io .InputStreamReader ;
23
+ import java .io .OutputStreamWriter ;
24
+ import java .net .HttpURLConnection ;
25
+ import java .net .URL ;
26
+ import java .net .URLConnection ;
27
+ import java .net .UnknownServiceException ;
28
+ import java .util .List ;
29
+
30
+ import javax .servlet .http .HttpServletRequest ;
31
+ import javax .servlet .http .HttpServletResponse ;
32
+ import javax .servlet .http .HttpSession ;
33
+
34
+ import org .slf4j .Logger ;
35
+ import org .slf4j .LoggerFactory ;
36
+
37
+ import org .apache .http .HttpEntity ;
38
+ import org .apache .http .HttpResponse ;
39
+ import org .apache .http .client .HttpClient ;
40
+ import org .apache .http .client .methods .HttpDelete ;
41
+ import org .apache .http .client .methods .HttpGet ;
42
+ import org .apache .http .client .methods .HttpPost ;
43
+ import org .apache .http .client .methods .HttpPut ;
44
+ import org .apache .http .entity .ByteArrayEntity ;
45
+ import org .apache .http .protocol .HTTP ;
46
+ import org .springframework .beans .factory .annotation .Value ;
47
+ import org .springframework .boot .context .properties .ConfigurationProperties ;
48
+ import org .springframework .http .HttpHeaders ;
49
+ import org .springframework .http .HttpMethod ;
50
+ import org .springframework .http .HttpStatus ;
51
+ import org .springframework .http .MediaType ;
52
+ import org .springframework .http .ResponseEntity ;
53
+ import org .springframework .stereotype .Controller ;
54
+ import org .springframework .ui .Model ;
55
+ import org .springframework .web .bind .annotation .CrossOrigin ;
56
+ import org .springframework .web .bind .annotation .GetMapping ;
57
+ import org .springframework .web .bind .annotation .PathVariable ;
58
+ import org .springframework .web .bind .annotation .RequestBody ;
59
+ import org .springframework .web .bind .annotation .RequestMapping ;
60
+ import org .springframework .web .bind .annotation .RequestMethod ;
61
+ import org .springframework .web .bind .annotation .RestController ;
62
+ import org .springframework .web .servlet .ModelAndView ;
63
+ import org .apache .http .impl .client .HttpClients ;
64
+
65
+
66
+ @ RestController
67
+ @ ConfigurationProperties (prefix ="ei" )
68
+ //@RequestMapping(value = "")
69
+ public class EIRequestsController {
70
+
71
+ private static final Logger LOG = LoggerFactory .getLogger (EIRequestsController .class );
72
+
73
+ private String backendServerHost ;
74
+ private int backendServerPort ;
75
+
76
+ private static final String APPLICATION_JSON = "application/json" ;
77
+
78
+ // Backend host and port (Getter & Setters), application.properties -> greeting.xxx
79
+ public String getBackendServerHost () {
80
+ return backendServerHost ;
81
+ }
82
+
83
+ public void setBackendServerHost (String backendServerHost ) {
84
+ this .backendServerHost = backendServerHost ;
85
+ }
86
+
87
+ public int getBackendServerPort () {
88
+ return backendServerPort ;
89
+ }
90
+
91
+ public void setBackendServerPort (int backendServerPort ) {
92
+ this .backendServerPort = backendServerPort ;
93
+ }
94
+
95
+ public String getEIBackendSubscriptionAddress () {
96
+ return "http://" + getBackendServerHost () + ":" + getBackendServerPort ();
97
+ }
98
+
99
+
100
+ /**
101
+ * Bridge all EI Http Requests with GET method. Used for fetching Subscription by id or all subscriptions and EI Env Info.
102
+ *
103
+ */
104
+ @ CrossOrigin
105
+ @ RequestMapping (value = {"/subscriptions" , "/subscriptions/*" , "/information" }, method = RequestMethod .GET )
106
+ public ResponseEntity <String > getRequests (Model model , HttpServletRequest request ) {
107
+ String eiBackendAddressSuffix = request .getServletPath ();
108
+ String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
109
+ LOG .info ("Got HTTP Request with method GET.\n UrlSuffix: " + eiBackendAddressSuffix +
110
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
111
+
112
+
113
+ HttpClient client = HttpClients .createDefault ();
114
+ HttpGet eiRequest = new HttpGet (newRequestUrl );
115
+
116
+ String JsonContent = "" ;
117
+ HttpResponse eiResponse = null ;
118
+ try {
119
+ eiResponse = client .execute (eiRequest );
120
+
121
+ InputStream inStream = eiResponse .getEntity ().getContent ();
122
+ BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
123
+ for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
124
+ JsonContent += line ;
125
+ }
126
+ bufReader .close ();
127
+ inStream .close ();
128
+ }
129
+ catch (IOException e ) {
130
+ LOG .error ("Forward Request Errors: " + e );
131
+ }
132
+
133
+ LOG .debug ("Http Status Code: " + eiResponse .getStatusLine ().getStatusCode ());
134
+ LOG .debug ("Recevied JsonContent:\n " + JsonContent );
135
+
136
+ if (JsonContent .isEmpty ()) {
137
+ JsonContent ="[]" ;
138
+ }
139
+
140
+ HttpHeaders headers = new HttpHeaders ();
141
+ headers .setContentType (MediaType .APPLICATION_JSON );
142
+
143
+ ResponseEntity <String > responseEntity = new ResponseEntity <>(JsonContent ,
144
+ headers ,
145
+ HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
146
+ return responseEntity ;
147
+ }
148
+
149
+
150
+
151
+ /**
152
+ * Bridge all EI Http Requests with POST method. E.g. Making Create Subscription Request.
153
+ *
154
+ */
155
+ @ CrossOrigin
156
+ @ RequestMapping (value = "/subscriptions" , method = RequestMethod .POST )
157
+ public ResponseEntity <String > postRequests (Model model , HttpServletRequest request ) {
158
+
159
+ String eiBackendAddressSuffix = request .getServletPath ();
160
+ String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
161
+ LOG .info ("Got HTTP Request with method POST.\n UrlSuffix: " + eiBackendAddressSuffix +
162
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
163
+
164
+ String inputReqJsonContent = "" ;
165
+ try {
166
+ BufferedReader inputBufReader = new BufferedReader (request .getReader ());
167
+ for (String line = inputBufReader .readLine (); line != null ; line = inputBufReader .readLine ()) {
168
+ inputReqJsonContent += line ;
169
+ }
170
+ inputBufReader .close ();
171
+ }
172
+ catch (IOException e ) {
173
+ LOG .error ("Forward Request Errors: " + e );
174
+ }
175
+
176
+ LOG .debug ("Input Request JSON Content to be forwarded:\n " + inputReqJsonContent );
177
+ HttpEntity inputReqJsonEntity = new ByteArrayEntity (inputReqJsonContent .getBytes ());
178
+
179
+
180
+ HttpClient client = HttpClients .createDefault ();
181
+ HttpPost eiRequest = new HttpPost (newRequestUrl );
182
+ eiRequest .setEntity (inputReqJsonEntity );
183
+ eiRequest .setHeader ("Content-type" , "application/json" );
184
+
185
+
186
+ String JsonContent = "" ;
187
+ HttpResponse eiResponse = null ;
188
+ try {
189
+ eiResponse = client .execute (eiRequest );
190
+
191
+ InputStream inStream = eiResponse .getEntity ().getContent ();
192
+ BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
193
+ for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
194
+ JsonContent += line ;
195
+ }
196
+ bufReader .close ();
197
+ inStream .close ();
198
+ }
199
+ catch (IOException e ) {
200
+ LOG .error ("Forward Request Errors: " + e );
201
+ }
202
+
203
+ LOG .debug ("Http Status Code: " + eiResponse .getStatusLine ().getStatusCode ());
204
+ LOG .debug ("Recevied JsonContent: \n " + JsonContent );
205
+
206
+ if (JsonContent .isEmpty ()) {
207
+ JsonContent ="[]" ;
208
+ }
209
+
210
+
211
+ HttpHeaders headers = new HttpHeaders ();
212
+ headers .setContentType (MediaType .APPLICATION_JSON );
213
+ ResponseEntity <String > responseEntity = new ResponseEntity <>(JsonContent ,
214
+ headers ,
215
+ HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
216
+ return responseEntity ;
217
+ }
218
+
219
+ /**
220
+ * Bridge all EI Http Requests with PUT method. E.g. Making Update Subscription Request.
221
+ *
222
+ */
223
+ @ CrossOrigin
224
+ @ RequestMapping (value = "/subscriptions" , method = RequestMethod .PUT )
225
+ public ResponseEntity <String > putRequests (Model model , HttpServletRequest request ) {
226
+
227
+ String eiBackendAddressSuffix = request .getServletPath ();
228
+ String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
229
+ LOG .info ("Got HTTP Request with method PUT.\n UrlSuffix: " + eiBackendAddressSuffix +
230
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
231
+
232
+ String inputReqJsonContent = "" ;
233
+ try {
234
+ BufferedReader inputBufReader = new BufferedReader (request .getReader ());
235
+ for (String line = inputBufReader .readLine (); line != null ; line = inputBufReader .readLine ()) {
236
+ inputReqJsonContent += line ;
237
+ }
238
+ inputBufReader .close ();
239
+ }
240
+ catch (IOException e ) {
241
+ LOG .error ("Forward Request Errors: " + e );
242
+ }
243
+
244
+ LOG .debug ("Input Request JSON Content to be forwarded:\n " + inputReqJsonContent );
245
+ HttpEntity inputReqJsonEntity = new ByteArrayEntity (inputReqJsonContent .getBytes ());
246
+
247
+
248
+ HttpClient client = HttpClients .createDefault ();
249
+ HttpPut eiRequest = new HttpPut (newRequestUrl );
250
+ eiRequest .setEntity (inputReqJsonEntity );
251
+ eiRequest .setHeader ("Content-type" , "application/json" );
252
+
253
+
254
+ String JsonContent = "" ;
255
+ HttpResponse eiResponse = null ;
256
+ try {
257
+ eiResponse = client .execute (eiRequest );
258
+
259
+ InputStream inStream = eiResponse .getEntity ().getContent ();
260
+ BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
261
+ for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
262
+ JsonContent += line ;
263
+ }
264
+ bufReader .close ();
265
+ inStream .close ();
266
+ }
267
+ catch (IOException e ) {
268
+ LOG .error ("Forward Request Errors: " + e );
269
+ }
270
+
271
+ LOG .debug ("Http Status Code: " + eiResponse .getStatusLine ().getStatusCode ());
272
+ LOG .debug ("Recevied JsonContent:\n " + JsonContent );
273
+
274
+ if (JsonContent .isEmpty ()) {
275
+ JsonContent ="[]" ;
276
+ }
277
+
278
+ HttpHeaders headers = new HttpHeaders ();
279
+ headers .setContentType (MediaType .APPLICATION_JSON );
280
+
281
+ ResponseEntity <String > responseEntity = new ResponseEntity <String >(JsonContent ,
282
+ headers ,
283
+ HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
284
+
285
+ return responseEntity ;
286
+ }
287
+
288
+
289
+ /**
290
+ * Bridge all EI Http Requests with DELETE method. Used for DELETE subscriptions.
291
+ *
292
+ */
293
+ @ CrossOrigin
294
+ @ RequestMapping (value = "/subscriptions/*" , method = RequestMethod .DELETE )
295
+ public ResponseEntity <String > deleteRequests (Model model , HttpServletRequest request ) {
296
+ String eiBackendAddressSuffix = request .getServletPath ();
297
+ String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
298
+ LOG .info ("Got HTTP Request with method DELETE.\n UrlSuffix: " + eiBackendAddressSuffix +
299
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
300
+
301
+ HttpClient client = HttpClients .createDefault ();
302
+ HttpDelete eiRequest = new HttpDelete (newRequestUrl );
303
+
304
+ String JsonContent = "" ;
305
+ HttpResponse eiResponse = null ;
306
+ try {
307
+ eiResponse = client .execute (eiRequest );
308
+
309
+ InputStream inStream = eiResponse .getEntity ().getContent ();
310
+ BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
311
+ for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
312
+ JsonContent += line ;
313
+ }
314
+ bufReader .close ();
315
+ inStream .close ();
316
+ }
317
+ catch (IOException e ) {
318
+ LOG .error ("Forward Request Errors: " + e );
319
+ }
320
+
321
+ LOG .debug ("Http Status Code: " + eiResponse .getStatusLine ().getStatusCode ());
322
+ LOG .debug ("Recevied JsonContent:\n " + JsonContent );
323
+
324
+ if (JsonContent .isEmpty ()) {
325
+ JsonContent ="[]" ;
326
+ }
327
+
328
+ HttpHeaders headers = new HttpHeaders ();
329
+ headers .setContentType (MediaType .APPLICATION_JSON );
330
+
331
+ ResponseEntity <String > responseEntity = new ResponseEntity <>(JsonContent ,
332
+ headers ,
333
+ HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
334
+ return responseEntity ;
335
+ }
336
+
337
+ }
0 commit comments