14
14
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
See the License for the specific language governing permissions and
16
16
limitations under the License.
17
- */
17
+ */
18
18
package com .ericsson .ei .erqueryservice ;
19
19
20
- import com .fasterxml .jackson .databind .JsonNode ;
21
- import lombok .Getter ;
20
+ import java .net .MalformedURLException ;
21
+ import java .util .ArrayList ;
22
+ import java .util .Collections ;
23
+ import java .util .List ;
24
+
25
+ import javax .annotation .PostConstruct ;
26
+
22
27
import org .apache .commons .lang3 .StringUtils ;
28
+ import org .apache .commons .lang3 .exception .ExceptionUtils ;
29
+ import org .apache .http .entity .ContentType ;
23
30
import org .slf4j .Logger ;
24
31
import org .slf4j .LoggerFactory ;
25
32
import org .springframework .beans .factory .annotation .Value ;
26
- import org .springframework .boot .web .client .RestTemplateBuilder ;
27
- import org .springframework .http .HttpEntity ;
28
- import org .springframework .http .HttpHeaders ;
29
- import org .springframework .http .HttpMethod ;
30
- import org .springframework .http .MediaType ;
31
- import org .springframework .http .ResponseEntity ;
32
33
import org .springframework .stereotype .Component ;
33
- import org .springframework .web .client .RestClientException ;
34
- import org .springframework .web .client .RestOperations ;
35
- import org .springframework .web .util .UriComponents ;
36
- import org .springframework .web .util .UriComponentsBuilder ;
37
34
38
- import javax .annotation .PostConstruct ;
39
- import java .net .URI ;
40
- import java .util .ArrayList ;
41
- import java .util .Collections ;
42
- import java .util .List ;
43
- import java .util .Map ;
35
+ import com .ericsson .eiffelcommons .utils .HttpRequest ;
36
+ import com .ericsson .eiffelcommons .utils .HttpRequest .HttpMethod ;
37
+ import com .ericsson .eiffelcommons .utils .ResponseEntity ;
44
38
39
+ import lombok .Getter ;
45
40
/**
46
41
* @author evasiba
47
42
*/
43
+
48
44
@ Component
49
45
public class ERQueryService {
50
-
51
46
private static final Logger LOGGER = LoggerFactory .getLogger (ERQueryService .class );
52
47
53
- private RestOperations rest ;
48
+ private HttpRequest request ;
54
49
55
50
@ Getter
56
51
@ Value ("${er.url}" )
57
- private String url ;
52
+ private String erBaseUrl ;
58
53
59
- public ERQueryService (RestTemplateBuilder builder ) {
60
- rest = builder . build ();
54
+ public ERQueryService () {
55
+ this . request = new HttpRequest ();
61
56
}
62
57
63
- public void setRest ( RestOperations rest ) {
64
- this .rest = rest ;
58
+ public void setHttpRequest ( HttpRequest request ) {
59
+ this .request = request ;
65
60
}
66
61
67
62
/**
@@ -72,21 +67,28 @@ public void setRest(RestOperations rest) {
72
67
* the id of the event.
73
68
* @return ResponseEntity
74
69
*/
75
- public ResponseEntity <String > getEventDataById (String eventId ) {
76
- if (StringUtils .isNotBlank (url )) {
77
- final String erUrl = URI .create (url .trim () + "/" + "{id}" ).normalize ().toString ();
78
- LOGGER .debug ("The URL to ER is: {}" , erUrl );
79
-
80
- final Map <String , String > params = Collections .singletonMap ("id" , eventId );
81
- LOGGER .trace ("The ID parameter is set" );
82
- try {
83
- final ResponseEntity <String > response = rest .getForEntity (erUrl , String .class , params );
70
+ public ResponseEntity getEventDataById (String eventId ) {
71
+ String erUrl = null ;
72
+
73
+ try {
74
+ if (StringUtils .isNotBlank (erBaseUrl )) {
75
+ request
76
+ .setHttpMethod (HttpMethod .GET )
77
+ .setBaseUrl (erBaseUrl )
78
+ .setEndpoint ("{id}" )
79
+ .addParam ("id" , eventId );
80
+
81
+ erUrl = request .getURI ().toString ();
82
+ LOGGER .debug ("The URL to ER is: {}" , erUrl );
83
+ ResponseEntity response = request .performRequest ();
84
84
LOGGER .trace ("The response is : {}" , response .toString ());
85
- } catch ( RestClientException e ) {
86
- LOGGER .error ( "Error occurred while executing REST GET to: {} for {}" , erUrl , eventId , e );
85
+ } else {
86
+ LOGGER .info ( "The URL to ER is not provided" );
87
87
}
88
- } else {
89
- LOGGER .info ("The URL to ER is not provided" );
88
+ } catch (MalformedURLException e ) {
89
+ LOGGER .error ("Error while building the ER url. Stacktrace: {}" , ExceptionUtils .getStackTrace (e ));
90
+ } catch (Exception e ) {
91
+ LOGGER .error ("Error occurred while executing REST GET to: {} for {}" , erUrl , eventId , e );
90
92
}
91
93
92
94
return null ;
@@ -112,33 +114,33 @@ public ResponseEntity<String> getEventDataById(String eventId) {
112
114
* whether or not to retain the tree structure in the result.
113
115
* @return ResponseEntity
114
116
*/
115
- public ResponseEntity < JsonNode > getEventStreamDataById (String eventId , SearchOption searchOption , int limit ,
117
+ public ResponseEntity getEventStreamDataById (String eventId , SearchOption searchOption , int limit ,
116
118
int levels , boolean tree ) {
117
119
118
- if (StringUtils .isNotBlank (url )) {
119
- final String erUrl = URI .create (url .trim () + "/" + eventId ).normalize ().toString ();
120
- LOGGER .debug ("The URL to ER is: {}" , erUrl );
121
-
122
- // Request Body parameters
123
- final SearchParameters searchParameters = getSearchParameters (searchOption );
124
-
125
- // Build query parameters
126
- final UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (erUrl ).queryParam ("limit" , limit )
127
- .queryParam ("levels" , levels ).queryParam ("tree" , tree );
128
- final HttpHeaders headers = new HttpHeaders ();
129
- headers .setContentType (MediaType .APPLICATION_JSON );
130
-
131
- final HttpEntity <SearchParameters > requestEntity = new HttpEntity <>(searchParameters , headers );
132
- final UriComponents uriComponents = builder .buildAndExpand (searchParameters );
133
- LOGGER .debug ("The request is : {}" , uriComponents .toUri ().toString ());
134
-
135
- try {
136
- return rest .exchange (uriComponents .toUri (), HttpMethod .POST , requestEntity , JsonNode .class );
137
- } catch (RestClientException e ) {
138
- LOGGER .error ("Error occurred while executing REST POST to: {} for\n {}" , erUrl , requestEntity , e );
120
+ String uri = null ;
121
+ try {
122
+ if (StringUtils .isNotBlank (erBaseUrl )) {
123
+ // Request Body parameters
124
+ final SearchParameters searchParameters = getSearchParameters (searchOption );
125
+ request
126
+ .setHttpMethod (HttpMethod .POST )
127
+ .setBaseUrl (erBaseUrl )
128
+ .setEndpoint (eventId )
129
+ .addParam ("limit" , Integer .toString (limit ))
130
+ .addParam ("levels" , Integer .toString (levels ))
131
+ .addParam ("tree" , Boolean .toString (tree ))
132
+ .setBody (searchParameters .getAsJsonString (), ContentType .APPLICATION_JSON );
133
+
134
+ uri = request .getURI ().toString ();
135
+ LOGGER .debug ("The URL to ER is: {}" , uri );
136
+
137
+ return request .performRequest ();
138
+ } else {
139
+ LOGGER .info ("The URL to ER is not provided" );
139
140
}
140
- } else {
141
- LOGGER .info ("The URL to ER is not provided" );
141
+ }
142
+ catch (Exception e ) {
143
+ LOGGER .error ("Error occurred while executing REST POST to {}, stacktrace: {}" , uri , e );
142
144
}
143
145
144
146
return null ;
@@ -156,16 +158,16 @@ private SearchParameters getSearchParameters(SearchOption searchOption) {
156
158
final List <LinkType > allLinkTypes = Collections .singletonList (LinkType .ALL );
157
159
switch (searchOption ) {
158
160
case DOWN_STREAM :
159
- searchParameters .setUlt (new ArrayList <>());
160
- searchParameters .setDlt (allLinkTypes );
161
+ searchParameters .setUpstreamLinkType (new ArrayList <>());
162
+ searchParameters .setDownstreamLinkType (allLinkTypes );
161
163
break ;
162
164
case UP_STREAM :
163
- searchParameters .setUlt (allLinkTypes );
164
- searchParameters .setDlt (new ArrayList <>());
165
+ searchParameters .setUpstreamLinkType (allLinkTypes );
166
+ searchParameters .setDownstreamLinkType (new ArrayList <>());
165
167
break ;
166
168
case UP_AND_DOWN_STREAM :
167
- searchParameters .setUlt (allLinkTypes );
168
- searchParameters .setDlt (allLinkTypes );
169
+ searchParameters .setUpstreamLinkType (allLinkTypes );
170
+ searchParameters .setDownstreamLinkType (allLinkTypes );
169
171
break ;
170
172
}
171
173
@@ -175,6 +177,6 @@ private SearchParameters getSearchParameters(SearchOption searchOption) {
175
177
@ PostConstruct
176
178
public void init () {
177
179
// TODO: is this needed?
178
- LOGGER .debug ("The url parameter is : {}" , url );
180
+ LOGGER .debug ("The url parameter is : {}" , erBaseUrl );
179
181
}
180
182
}
0 commit comments