17
17
package com .ericsson .ei .frontend .utils ;
18
18
19
19
import java .io .IOException ;
20
+ import java .io .UnsupportedEncodingException ;
20
21
import java .net .URI ;
21
22
import java .net .URISyntaxException ;
23
+ import java .net .URLDecoder ;
22
24
import java .nio .charset .Charset ;
23
25
import java .util .ArrayList ;
24
- import java .util .Arrays ;
25
26
import java .util .List ;
26
27
27
28
import javax .servlet .http .HttpServletRequest ;
@@ -44,16 +45,16 @@ public class EIRequestsControllerUtils {
44
45
45
46
private static final Logger LOG = LoggerFactory .getLogger (EIRequestsControllerUtils .class );
46
47
47
- private static final List < String > REQUESTS_WITH_QUERY_PARAM = new ArrayList <>( Arrays . asList ( "/queryAggregatedObject" , "/queryMissedNotifications" , "/query" )) ;
48
- private static final String BACKEND_KEY_NAME = "backendurl " ;
48
+ private static final String BACKEND_URL_KEY_NAME = "backendurl" ;
49
+ private static final String BACKEND_NAME_KEY_NAME = "backendname " ;
49
50
50
51
@ Autowired
51
52
private BackEndInstancesUtils backEndInstancesUtils ;
52
53
53
54
/**
54
- * Processes an HttpServletRequest and extract the URL parameters from it and reformats
55
- * the requests and removes EI Front End specific parameters if any and builds a new URL
56
- * to be used to call the requested, selected or default EI back end.
55
+ * Processes an HttpServletRequest and extract the URL parameters from it and reformats the requests and removes EI
56
+ * Front End specific parameters if any and builds a new URL to be used to call the requested, selected or default
57
+ * EI back end.
57
58
*
58
59
* @param request
59
60
* @return String
@@ -62,38 +63,42 @@ public class EIRequestsControllerUtils {
62
63
public String getEIRequestURL (HttpServletRequest request ) {
63
64
String eiBackendAddressSuffix = request .getServletPath ();
64
65
String requestQuery = request .getQueryString ();
65
- String requestUrl = null ;
66
66
67
- if (requestQuery != null && requestQuery .contains (BACKEND_KEY_NAME )) {
68
- // Selecting back end from user input as parameter.
69
- List <NameValuePair > params = getParameters (requestQuery );
70
- requestUrl = extractUrlFromParameters (params );
71
- requestQuery = removeBackendDataFromQueryString (params );
72
- LOG .debug (BACKEND_KEY_NAME + " key detected, forwarding request to url '" + requestUrl + "'." );
73
- } else {
74
- BackEndInformation backEndInformation = getEIBackendInformation (request );
75
- requestUrl = backEndInformation .getUrlAsString ();
76
- }
67
+ String requestUrl = getBackEndUrl (requestQuery );
68
+ requestQuery = removeBackendDataFromQueryString (requestQuery );
77
69
78
- if ( REQUESTS_WITH_QUERY_PARAM . contains ( eiBackendAddressSuffix )) {
79
- String query = ( requestQuery != null && ! requestQuery . isEmpty ()) ? "?" + requestQuery : "" ;
70
+ if (! requestQuery . isEmpty ( )) {
71
+ String query = "?" + requestQuery ;
80
72
requestUrl = requestUrl + eiBackendAddressSuffix + query ;
81
73
} else {
82
74
requestUrl = requestUrl + eiBackendAddressSuffix ;
83
75
}
76
+
84
77
LOG .debug ("Got HTTP Request with method " + request .getMethod () + "\n UrlSuffix: " + eiBackendAddressSuffix
85
78
+ "\n Forwarding Request to EI Backend with url: " + requestUrl );
79
+
86
80
return requestUrl ;
87
81
}
88
82
89
- private BackEndInformation getEIBackendInformation ( HttpServletRequest request ) {
90
- String backEndInstanceName = null ;
83
+ private String getBackEndUrl ( String requestQuery ) {
84
+ String requestUrl = null ;
91
85
92
- if (request .getSession ().getAttribute ("backEndInstanceName" ) != null ) {
93
- backEndInstanceName = request .getSession ().getAttribute ("backEndInstanceName" ).toString ();
86
+ if (requestQuery != null ) {
87
+ List <NameValuePair > params = getParameters (requestQuery );
88
+ if (requestQuery .contains (BACKEND_URL_KEY_NAME )) {
89
+ requestUrl = extractUrlFromParameters (params );
90
+ } else {
91
+ String backEndName = extractBackEndNameFromParameters (params );
92
+ BackEndInformation backEndInformation = backEndInstancesUtils .getBackEndInformationByName (backEndName );
93
+ requestUrl = backEndInformation .getUrlAsString ();
94
+ }
95
+ } else {
96
+ BackEndInformation backEndInformation = backEndInstancesUtils .getBackEndInformationByName (null );
97
+ requestUrl = backEndInformation .getUrlAsString ();
94
98
}
95
99
96
- return backEndInstancesUtils .getBackEndInformationByName (backEndInstanceName );
100
+ LOG .debug ("Forwarding request to url '" + requestUrl + "'." );
101
+ return requestUrl ;
97
102
}
98
103
99
104
private List <NameValuePair > getParameters (String requestQuery ) {
@@ -104,31 +109,58 @@ private List<NameValuePair> getParameters(String requestQuery) {
104
109
} catch (URISyntaxException e ) {
105
110
LOG .error ("Error while encoding URL parameters: " + e );
106
111
}
112
+
107
113
return params ;
108
114
}
109
115
110
116
private String extractUrlFromParameters (List <NameValuePair > params ) {
111
117
String urlFromParams = null ;
118
+
112
119
for (NameValuePair param : params ) {
113
- if (param .getName ().equals (BACKEND_KEY_NAME )) {
114
- urlFromParams = param .getValue ();
120
+ if (param .getName ()
121
+ .equals (BACKEND_URL_KEY_NAME )) {
122
+ try {
123
+ urlFromParams = URLDecoder .decode (param .getValue (), "UTF-8" );
124
+ } catch (UnsupportedEncodingException e ) {
125
+ LOG .error ("Failed to decode back-end url" );
126
+ }
115
127
}
116
128
}
129
+
117
130
return urlFromParams ;
118
131
}
119
132
120
- private String removeBackendDataFromQueryString (List <NameValuePair > params ) {
133
+ private String extractBackEndNameFromParameters (List <NameValuePair > params ) {
134
+ String backendName = null ;
135
+
136
+ for (NameValuePair param : params ) {
137
+ if (param .getName ()
138
+ .equals (BACKEND_NAME_KEY_NAME )) {
139
+ try {
140
+ backendName = URLDecoder .decode (param .getValue (), "UTF-8" );
141
+ } catch (UnsupportedEncodingException e ) {
142
+ LOG .error ("Failed to decode back-end name" );
143
+ }
144
+ }
145
+ }
146
+
147
+ return backendName ;
148
+ }
149
+
150
+ private String removeBackendDataFromQueryString (String requestQuery ) {
151
+ List <NameValuePair > params = getParameters (requestQuery );
121
152
List <NameValuePair > processedParams = new ArrayList <>();
122
153
for (NameValuePair param : params ) {
123
154
String name = param .getName (), value = param .getValue ();
124
- if (name .equals (BACKEND_KEY_NAME )) {
155
+ if (name == null || value == null || name .equals (BACKEND_URL_KEY_NAME ) || name . equals ( BACKEND_NAME_KEY_NAME )) {
125
156
continue ;
126
157
}
127
158
processedParams .add (new BasicNameValuePair (name , value ));
128
159
}
129
160
130
- if (processedParams .size () == 0 )
131
- return null ;
161
+ if (processedParams .size () == 0 ) {
162
+ return "" ;
163
+ }
132
164
133
165
return URLEncodedUtils .format (processedParams , "UTF8" );
134
166
}
0 commit comments