17
17
*/
18
18
package com .ericsson .ei .erqueryservice ;
19
19
20
- import java .net .MalformedURLException ;
20
+ import java .io .IOException ;
21
+ import java .net .URISyntaxException ;
21
22
import java .util .ArrayList ;
22
23
import java .util .Collections ;
23
24
import java .util .List ;
24
25
25
- import javax .annotation .PostConstruct ;
26
-
27
26
import org .apache .commons .lang3 .StringUtils ;
28
- import org .apache .commons . lang3 . exception . ExceptionUtils ;
27
+ import org .apache .http . client . ClientProtocolException ;
29
28
import org .apache .http .entity .ContentType ;
30
29
import org .slf4j .Logger ;
31
30
import org .slf4j .LoggerFactory ;
32
31
import org .springframework .beans .factory .annotation .Value ;
33
32
import org .springframework .stereotype .Component ;
34
33
34
+ import com .ericsson .ei .exception .HttpRequestFailedException ;
35
+ import com .ericsson .ei .exception .PropertyNotFoundException ;
35
36
import com .ericsson .eiffelcommons .utils .HttpRequest ;
36
37
import com .ericsson .eiffelcommons .utils .HttpRequest .HttpMethod ;
37
38
import com .ericsson .eiffelcommons .utils .ResponseEntity ;
38
39
39
40
import lombok .Getter ;
41
+
40
42
/**
41
43
* @author evasiba
42
44
*/
@@ -60,97 +62,62 @@ public void setHttpRequest(HttpRequest request) {
60
62
}
61
63
62
64
/**
63
- * This method only extracts the event information from ER2.0 based on the
64
- * eventID.
65
+ * This method is used to fetch only the upstream or downstream or both event information for
66
+ * ER based on the eventID and searchOption conditions .
65
67
*
66
- * @param eventId
67
- * the id of the event.
68
+ * @param eventId the id of the event.
69
+ * @param searchOption the SearchOption to indicate whether to search up, down or both ways from
70
+ * the eventId.
71
+ * @param limit sets the limit of how many events up and/or down stream from the eventId
72
+ * to include in the result.
73
+ * @param levels sets the limit of how many levels up and/or down stream from the eventId
74
+ * to include in the result.
75
+ * @param tree whether or not to retain the tree structure in the result.
68
76
* @return ResponseEntity
77
+ * @throws PropertyNotFoundException
69
78
*/
70
- public ResponseEntity getEventDataById (String eventId ) {
71
- String erUrl = null ;
72
-
79
+ public ResponseEntity getEventStreamDataById (String eventId , SearchOption searchOption ,
80
+ int limit , int levels , boolean tree ) throws PropertyNotFoundException , Exception {
73
81
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
- LOGGER .trace ("The response is : {}" , response .toString ());
85
- } else {
86
- LOGGER .info ("The URL to ER is not provided" );
87
- }
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 );
82
+ ResponseEntity responseFromEr = sendRequestToER (eventId , searchOption , limit , levels ,
83
+ tree );
84
+ return responseFromEr ;
85
+ } catch (IOException | URISyntaxException e ) {
86
+ throw new HttpRequestFailedException ("Error occurred while executing REST POST" , e );
92
87
}
93
-
94
- return null ;
95
88
}
96
89
97
- /**
98
- * This method is used to fetch only the upstream or downstream or both
99
- * event information from ER2.0 based on the eventID and searchOption
100
- * conditions.
101
- *
102
- * @param eventId
103
- * the id of the event.
104
- * @param searchOption
105
- * the SearchOption to indicate whether to search up, down or
106
- * both ways from the eventId.
107
- * @param limit
108
- * sets the limit of how many events up and/or down stream from
109
- * the eventId to include in the result.
110
- * @param levels
111
- * sets the limit of how many levels up and/or down stream from
112
- * the eventId to include in the result.
113
- * @param tree
114
- * whether or not to retain the tree structure in the result.
115
- * @return ResponseEntity
116
- */
117
- public ResponseEntity getEventStreamDataById (String eventId , SearchOption searchOption , int limit ,
118
- int levels , boolean tree ) {
119
-
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" );
140
- }
141
- }
142
- catch (Exception e ) {
143
- LOGGER .error ("Error occurred while executing REST POST to {}, stacktrace: {}" , uri , e );
90
+ private ResponseEntity sendRequestToER (String eventId , SearchOption searchOption , int limit ,
91
+ int levels , boolean tree ) throws IOException , URISyntaxException ,
92
+ ClientProtocolException , PropertyNotFoundException {
93
+ if (StringUtils .isBlank (erBaseUrl )) {
94
+ throw new PropertyNotFoundException ("The URL to ER is not provided" );
144
95
}
145
96
146
- return null ;
97
+ prepareRequest (eventId , searchOption , limit , levels , tree );
98
+ return request .performRequest ();
99
+ }
100
+
101
+ private void prepareRequest (String eventId , SearchOption searchOption , int limit ,
102
+ int levels , boolean tree ) throws IOException , URISyntaxException {
103
+ final SearchParameters searchParameters = getSearchParameters (searchOption );
104
+ request
105
+ .setHttpMethod (HttpMethod .POST )
106
+ .setBaseUrl (erBaseUrl )
107
+ .setEndpoint (eventId )
108
+ .addParam ("limit" , Integer .toString (limit ))
109
+ .addParam ("levels" , Integer .toString (levels ))
110
+ .addParam ("tree" , Boolean .toString (tree ))
111
+ .setBody (searchParameters .getAsJsonString (), ContentType .APPLICATION_JSON );
112
+
113
+ String uri = request .getURI ().toString ();
114
+ LOGGER .debug ("The URL to ER is: {}" , uri );
147
115
}
148
116
149
117
/**
150
118
* Build the search parameters to be used to query ER.
151
119
*
152
- * @param searchOption
153
- * one of UP_STREAM, DOWN_STREAM or UP_AND_DOWN_STREAM
120
+ * @param searchOption one of UP_STREAM, DOWN_STREAM or UP_AND_DOWN_STREAM
154
121
* @return the search parameters to be used
155
122
*/
156
123
private SearchParameters getSearchParameters (SearchOption searchOption ) {
@@ -173,10 +140,4 @@ private SearchParameters getSearchParameters(SearchOption searchOption) {
173
140
174
141
return searchParameters ;
175
142
}
176
-
177
- @ PostConstruct
178
- public void init () {
179
- // TODO: is this needed?
180
- LOGGER .debug ("The url parameter is : {}" , erBaseUrl );
181
- }
182
143
}
0 commit comments