17
17
package com .ericsson .ei .frontend ;
18
18
19
19
import com .ericsson .ei .frontend .model .BackEndInformation ;
20
- import com .ericsson .ei .frontend .model .Index ;
21
- import com .ericsson .ei .frontend .model .ListWrapper ;
22
- import com .fasterxml .jackson .core .JsonGenerator ;
23
- import com .fasterxml .jackson .databind .ObjectMapper ;
20
+ import com .ericsson .ei .frontend .utils .BackEndInstancesUtils ;
24
21
import org .json .JSONArray ;
25
22
import org .json .JSONObject ;
26
23
import org .slf4j .Logger ;
34
31
import org .springframework .web .bind .annotation .RequestMapping ;
35
32
import org .springframework .web .bind .annotation .RequestMethod ;
36
33
37
- import javax .annotation .PostConstruct ;
38
34
import javax .servlet .http .HttpServletRequest ;
39
- import java .io .FileOutputStream ;
40
- import java .io .IOException ;
41
- import java .nio .file .Files ;
42
- import java .nio .file .Paths ;
43
- import java .util .ArrayList ;
44
- import java .util .List ;
45
35
import java .util .stream .Collectors ;
46
36
47
37
@ Controller
@@ -61,55 +51,11 @@ public class WebController {
61
51
@ Value ("${ei.eiffelDocumentationUrls}" )
62
52
private String eiffelDocumentationUrls ;
63
53
64
- @ Value ("${ei.backendInstancesPath}" )
65
- private String eiInstancesPath ;
66
-
67
- @ Value ("${ei.backendServerHost}" )
68
- private String host ;
69
-
70
- @ Value ("${ei.backendServerPort}" )
71
- private int port ;
72
-
73
- @ Value ("${ei.backendContextPath}" )
74
- private String path ;
75
-
76
- @ Value ("${ei.useSecureHttp}" )
77
- private boolean https ;
78
-
79
54
@ Autowired
80
55
private BackEndInformation backEndInformation ;
81
56
82
57
@ Autowired
83
- private ListWrapper wrapper ;
84
-
85
- @ Autowired
86
- private Index index ;
87
-
88
- private List <BackEndInformation > information = new ArrayList <>();
89
-
90
- private JSONArray instances = new JSONArray ();
91
-
92
- @ PostConstruct
93
- public void init () {
94
- instances .put (getCurrentInstance ());
95
- // index.setIndex(0);
96
- // information.add(backEndInformation);
97
- // if (eiInstancesPath != null) {
98
- // try {
99
- // JSONArray inputBackEndInstances = new JSONArray(new String(Files.readAllBytes(Paths.get(eiInstancesPath))));
100
- // for (Object o : inputBackEndInstances) {
101
- // JSONObject instance = (JSONObject) o;
102
- // BackEndInformation backEndInformations = new ObjectMapper().readValue(instance.toString(), BackEndInformation.class);
103
- // if (!checkIfInstanceAlreadyExist(backEndInformations)) {
104
- // information.add(backEndInformations);
105
- // }
106
- // }
107
- // } catch (IOException e) {
108
- // LOG.error("Failure when try to parse json file" + e.getMessage());
109
- // }
110
- // }
111
- // writeIntoFile();
112
- }
58
+ private BackEndInstancesUtils utils ;
113
59
114
60
@ RequestMapping ("/" )
115
61
public String greeting (Model model ) {
@@ -183,14 +129,14 @@ public String addInstance(Model model) {
183
129
public ResponseEntity <String > switchBackEndInstance (Model model , HttpServletRequest request ) {
184
130
try {
185
131
String body = request .getReader ().lines ().collect (Collectors .joining (System .lineSeparator ()));
186
- instances = new JSONArray (body );
187
- LOG . info ( instances . toString () );
188
- // for(int i = 0; i < instances.length (); i++) {
189
- // if(instances.getJSONObject(i).get("checked").equals(true ) {
190
- // instances.getJSONObject(i);
191
- // break ;
192
- // }
193
- // }
132
+ utils . setInstances ( new JSONArray (body ) );
133
+ utils . writeIntoFile ( );
134
+ utils . parseBackEndInstancesFile ();
135
+ for ( BackEndInformation backEndInformation : utils . getInformation () ) {
136
+ if ( backEndInformation . isChecked ()) {
137
+ utils . setBackEndProperties ( backEndInformation ) ;
138
+ }
139
+ }
194
140
return new ResponseEntity <>(HttpStatus .OK );
195
141
} catch (Exception e ) {
196
142
return new ResponseEntity <>("Internal error" , HttpStatus .INTERNAL_SERVER_ERROR );
@@ -202,10 +148,10 @@ public ResponseEntity<String> addInstanceInformation(Model model, HttpServletReq
202
148
try {
203
149
String body = request .getReader ().lines ().collect (Collectors .joining (System .lineSeparator ()));
204
150
JSONObject instance = new JSONObject (body );
205
- if (! checkIfInstanceAlreadyExist (instance )) {
151
+ if (! utils . checkIfInstanceAlreadyExist (instance )) {
206
152
instance .put ("checked" , false );
207
- instances .put (instance );
208
- LOG . info ( instances . toString () );
153
+ utils . getInstances () .put (instance );
154
+ utils . writeIntoFile ( );
209
155
return new ResponseEntity <>(HttpStatus .OK );
210
156
} else {
211
157
return new ResponseEntity <>("Instance already exist" , HttpStatus .BAD_REQUEST );
@@ -217,46 +163,6 @@ public ResponseEntity<String> addInstanceInformation(Model model, HttpServletReq
217
163
218
164
@ RequestMapping (value = "/get-instances" , method = RequestMethod .GET )
219
165
public ResponseEntity <String > getInstances (Model model ) {
220
- return new ResponseEntity <>(instances .toString (), HttpStatus .OK );
221
- }
222
-
223
- private JSONObject getCurrentInstance () {
224
- JSONObject instance = new JSONObject ();
225
- instance .put ("name" , "core" );
226
- instance .put ("host" , host );
227
- instance .put ("port" , port );
228
- instance .put ("path" , path );
229
- instance .put ("https" , https );
230
- instance .put ("checked" , true );
231
- return instance ;
232
- }
233
-
234
- private void setBackEndProperties (JSONObject instance ) {
235
- backEndInformation .setName (information .get (index .getIndex ()).getName ());
236
- backEndInformation .setHost (information .get (index .getIndex ()).getHost ());
237
- backEndInformation .setPort (information .get (index .getIndex ()).getPort ());
238
- backEndInformation .setPath (information .get (index .getIndex ()).getPath ());
239
- backEndInformation .setHttps (information .get (index .getIndex ()).isHttps ());
240
- }
241
-
242
- private boolean checkIfInstanceAlreadyExist (JSONObject instance ) {
243
- for (int i = 0 ; i < instances .length (); i ++) {
244
- if (instances .getJSONObject (i ).get ("host" ).equals (instance .get ("host" )) &&
245
- instances .getJSONObject (i ).get ("port" ).equals (instance .get ("port" ))) {
246
- return true ;
247
- }
248
- }
249
- return false ;
250
- }
251
-
252
- private void writeIntoFile () {
253
- if (eiInstancesPath != null ) {
254
- ObjectMapper mapper = new ObjectMapper ();
255
- try (JsonGenerator add = mapper .getFactory ().createGenerator (new FileOutputStream (eiInstancesPath ))) {
256
- mapper .writeValue (add , information );
257
- } catch (IOException e ) {
258
- LOG .error ("Couldn't add instance to file " + e .getMessage ());
259
- }
260
- }
166
+ return new ResponseEntity <>(utils .getInstances ().toString (), HttpStatus .OK );
261
167
}
262
168
}
0 commit comments