24
24
import javax .servlet .http .HttpServletRequest ;
25
25
26
26
import org .apache .http .HttpEntity ;
27
- import org .apache .http .HttpResponse ;
28
- import org .apache .http .client .HttpClient ;
29
- import org .apache .http .client .methods .HttpDelete ;
30
- import org .apache .http .client .methods .HttpGet ;
31
- import org .apache .http .client .methods .HttpPost ;
32
- import org .apache .http .client .methods .HttpPut ;
27
+ import org .apache .http .client .methods .*;
33
28
import org .apache .http .entity .ByteArrayEntity ;
34
- import org .apache .http .impl .client .HttpClients ;
29
+ import org .apache .http .impl .client .CloseableHttpClient ;
30
+ import org .apache .http .impl .client .HttpClientBuilder ;
35
31
import org .slf4j .Logger ;
36
32
import org .slf4j .LoggerFactory ;
37
33
import org .springframework .boot .context .properties .ConfigurationProperties ;
47
43
48
44
@ RestController
49
45
@ ConfigurationProperties (prefix = "ei" )
50
- // @RequestMapping(value = "")
51
46
public class EIRequestsController {
52
47
53
48
private static final Logger LOG = LoggerFactory .getLogger (EIRequestsController .class );
54
49
50
+ private CloseableHttpClient client = HttpClientBuilder .create ().build ();
51
+
55
52
private String backendServerHost ;
56
53
private int backendServerPort ;
57
54
private String backendContextPath ;
58
55
private boolean useSecureHttp ;
59
56
60
- private static final String APPLICATION_JSON = "application/json" ;
61
-
62
57
// Backend host and port (Getter & Setters), application.properties ->
63
58
// greeting.xxx
64
59
public String getBackendServerHost () {
@@ -93,17 +88,33 @@ public void setUseSecureHttp(boolean useSecureHttp) {
93
88
this .useSecureHttp = useSecureHttp ;
94
89
}
95
90
96
- public String getEIBackendSubscriptionAddress () {
97
- String httpMethod = "http" ;
98
- if (useSecureHttp ) {
99
- httpMethod = "https" ;
91
+ /**
92
+ * Bridge authorized EI Http Requests with GET method. Used for login and logout
93
+ *
94
+ */
95
+ @ CrossOrigin
96
+ @ RequestMapping (value = "/auth/*" , method = RequestMethod .GET )
97
+ public ResponseEntity <String > getAuthRequests (Model model , HttpServletRequest request ) {
98
+ String eiBackendAddressSuffix = request .getServletPath ();
99
+ String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
100
+ LOG .info ("Got HTTP Request with method GET.\n UrlSuffix: " + eiBackendAddressSuffix +
101
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
102
+
103
+ try {
104
+ client .close ();
105
+ client = HttpClientBuilder .create ().build ();
106
+ } catch (IOException e ) {
107
+ LOG .error ("Failed to close HTTP Client" );
100
108
}
101
109
102
- if (backendContextPath != null && !backendContextPath .isEmpty ()) {
103
- return httpMethod + "://" + this .getBackendServerHost () + ":" + this .getBackendServerPort () + "/"
104
- + backendContextPath ;
110
+ HttpGet eiRequest = new HttpGet (newRequestUrl );
111
+
112
+ String header = request .getHeader ("Authorization" );
113
+ if (header != null ) {
114
+ eiRequest .addHeader ("Authorization" , header );
105
115
}
106
- return httpMethod + "://" + this .getBackendServerHost () + ":" + this .getBackendServerPort ();
116
+
117
+ return getResponse (eiRequest );
107
118
}
108
119
109
120
/**
@@ -120,38 +131,9 @@ public ResponseEntity<String> getRequests(Model model, HttpServletRequest reques
120
131
LOG .info ("Got HTTP Request with method GET.\n UrlSuffix: " + eiBackendAddressSuffix
121
132
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
122
133
123
- HttpClient client = HttpClients .createDefault ();
124
134
HttpGet eiRequest = new HttpGet (newRequestUrl );
125
135
126
- String jsonContent = "" ;
127
- HttpResponse eiResponse = null ;
128
- try {
129
- eiResponse = client .execute (eiRequest );
130
-
131
- InputStream inStream = eiResponse .getEntity ().getContent ();
132
- BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
133
- for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
134
- jsonContent += line ;
135
- }
136
- bufReader .close ();
137
- inStream .close ();
138
- } catch (IOException e ) {
139
- LOG .error ("Forward Request Errors: " + e );
140
- }
141
-
142
- LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
143
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding response back to EI Frontend WebUI." );
144
-
145
- if (jsonContent .isEmpty ()) {
146
- jsonContent = "[]" ;
147
- }
148
-
149
- HttpHeaders headers = new HttpHeaders ();
150
- headers .setContentType (MediaType .APPLICATION_JSON );
151
-
152
- ResponseEntity <String > responseEntity = new ResponseEntity <>(jsonContent , headers ,
153
- HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
154
- return responseEntity ;
136
+ return getResponse (eiRequest );
155
137
}
156
138
157
139
/**
@@ -180,39 +162,11 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
180
162
LOG .debug ("Input Request JSON Content to be forwarded:\n " + inputReqJsonContent );
181
163
HttpEntity inputReqJsonEntity = new ByteArrayEntity (inputReqJsonContent .getBytes ());
182
164
183
- HttpClient client = HttpClients .createDefault ();
184
165
HttpPost eiRequest = new HttpPost (newRequestUrl );
185
166
eiRequest .setEntity (inputReqJsonEntity );
186
167
eiRequest .setHeader ("Content-type" , "application/json" );
187
168
188
- String jsonContent = "" ;
189
- HttpResponse eiResponse = null ;
190
- try {
191
- eiResponse = client .execute (eiRequest );
192
-
193
- InputStream inStream = eiResponse .getEntity ().getContent ();
194
- BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
195
- for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
196
- jsonContent += line ;
197
- }
198
- bufReader .close ();
199
- inStream .close ();
200
- } catch (IOException e ) {
201
- LOG .error ("Forward Request Errors: " + e );
202
- }
203
-
204
- LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
205
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding response back to EI Frontend WebUI." );
206
-
207
- if (jsonContent .isEmpty ()) {
208
- jsonContent = "[]" ;
209
- }
210
-
211
- HttpHeaders headers = new HttpHeaders ();
212
- headers .setContentType (MediaType .APPLICATION_JSON );
213
- ResponseEntity <String > responseEntity = new ResponseEntity <>(jsonContent , headers ,
214
- HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
215
- return responseEntity ;
169
+ return getResponse (eiRequest );
216
170
}
217
171
218
172
/**
@@ -223,7 +177,6 @@ public ResponseEntity<String> postRequests(Model model, HttpServletRequest reque
223
177
@ CrossOrigin
224
178
@ RequestMapping (value = "/subscriptions" , method = RequestMethod .PUT )
225
179
public ResponseEntity <String > putRequests (Model model , HttpServletRequest request ) {
226
-
227
180
String eiBackendAddressSuffix = request .getServletPath ();
228
181
String newRequestUrl = getEIBackendSubscriptionAddress () + eiBackendAddressSuffix ;
229
182
LOG .info ("Got HTTP Request with method PUT.\n UrlSuffix: " + eiBackendAddressSuffix
@@ -243,41 +196,11 @@ public ResponseEntity<String> putRequests(Model model, HttpServletRequest reques
243
196
LOG .debug ("Input Request JSON Content to be forwarded:\n " + inputReqJsonContent );
244
197
HttpEntity inputReqJsonEntity = new ByteArrayEntity (inputReqJsonContent .getBytes ());
245
198
246
- HttpClient client = HttpClients .createDefault ();
247
199
HttpPut eiRequest = new HttpPut (newRequestUrl );
248
200
eiRequest .setEntity (inputReqJsonEntity );
249
201
eiRequest .setHeader ("Content-type" , "application/json" );
250
202
251
- String jsonContent = "" ;
252
- HttpResponse eiResponse = null ;
253
- try {
254
- eiResponse = client .execute (eiRequest );
255
-
256
- InputStream inStream = eiResponse .getEntity ().getContent ();
257
- BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
258
- for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
259
- jsonContent += line ;
260
- }
261
- bufReader .close ();
262
- inStream .close ();
263
- } catch (IOException e ) {
264
- LOG .error ("Forward Request Errors: " + e );
265
- }
266
-
267
- LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
268
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding response back to EI Frontend WebUI." );
269
-
270
- if (jsonContent .isEmpty ()) {
271
- jsonContent = "[]" ;
272
- }
273
-
274
- HttpHeaders headers = new HttpHeaders ();
275
- headers .setContentType (MediaType .APPLICATION_JSON );
276
-
277
- ResponseEntity <String > responseEntity = new ResponseEntity <String >(jsonContent , headers ,
278
- HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
279
-
280
- return responseEntity ;
203
+ return getResponse (eiRequest );
281
204
}
282
205
283
206
/**
@@ -293,38 +216,50 @@ public ResponseEntity<String> deleteRequests(Model model, HttpServletRequest req
293
216
LOG .info ("Got HTTP Request with method DELETE.\n UrlSuffix: " + eiBackendAddressSuffix
294
217
+ "\n Forwarding Request to EI Backend with url: " + newRequestUrl );
295
218
296
- HttpClient client = HttpClients .createDefault ();
297
219
HttpDelete eiRequest = new HttpDelete (newRequestUrl );
298
220
299
- String jsonContent = "" ;
300
- HttpResponse eiResponse = null ;
301
- try {
302
- eiResponse = client .execute (eiRequest );
221
+ return getResponse (eiRequest );
222
+ }
223
+
224
+ private String getEIBackendSubscriptionAddress () {
225
+ String httpMethod = "http" ;
226
+ if (useSecureHttp ) {
227
+ httpMethod = "https" ;
228
+ }
229
+
230
+ if (backendContextPath != null && !backendContextPath .isEmpty ()) {
231
+ return httpMethod + "://" + this .getBackendServerHost () + ":" + this .getBackendServerPort () + "/"
232
+ + backendContextPath ;
233
+ }
234
+ return httpMethod + "://" + this .getBackendServerHost () + ":" + this .getBackendServerPort ();
235
+ }
303
236
237
+ private ResponseEntity <String > getResponse (HttpRequestBase request ) {
238
+ String jsonContent = "" ;
239
+ int statusCode = 0 ;
240
+ try (CloseableHttpResponse eiResponse = client .execute (request )) {
304
241
InputStream inStream = eiResponse .getEntity ().getContent ();
305
242
BufferedReader bufReader = new BufferedReader (new InputStreamReader (inStream , "UTF-8" ));
306
243
for (String line = bufReader .readLine (); line != null ; line = bufReader .readLine ()) {
307
244
jsonContent += line ;
308
245
}
246
+ if (jsonContent .isEmpty ()) {
247
+ jsonContent = "[]" ;
248
+ }
249
+ statusCode = eiResponse .getStatusLine ().getStatusCode ();
250
+ LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
251
+ + "\n EI Recevied jsonContent:\n " + jsonContent
252
+ + "\n Forwarding response back to EI Frontend WebUI." );
309
253
bufReader .close ();
310
254
inStream .close ();
311
255
} catch (IOException e ) {
312
256
LOG .error ("Forward Request Errors: " + e );
313
257
}
314
258
315
- LOG .info ("EI Http Reponse Status Code: " + eiResponse .getStatusLine ().getStatusCode ()
316
- + "\n EI Recevied jsonContent:\n " + jsonContent + "\n Forwarding response back to EI Frontend WebUI." );
317
-
318
- if (jsonContent .isEmpty ()) {
319
- jsonContent = "[]" ;
320
- }
321
-
322
259
HttpHeaders headers = new HttpHeaders ();
323
260
headers .setContentType (MediaType .APPLICATION_JSON );
324
261
325
- ResponseEntity <String > responseEntity = new ResponseEntity <>(jsonContent , headers ,
326
- HttpStatus .valueOf (eiResponse .getStatusLine ().getStatusCode ()));
327
- return responseEntity ;
262
+ return new ResponseEntity <>(jsonContent , headers , HttpStatus .valueOf (statusCode ));
328
263
}
329
264
330
265
}
0 commit comments