16
16
17
17
import java .util .EnumSet ;
18
18
import java .util .Map ;
19
+
19
20
import org .slf4j .LoggerFactory ;
20
21
import org .springframework .beans .factory .annotation .Autowired ;
21
22
import org .springframework .beans .factory .annotation .Qualifier ;
23
+ import org .springframework .beans .factory .annotation .Value ;
22
24
import org .springframework .context .annotation .ComponentScan ;
23
25
import org .springframework .http .HttpEntity ;
24
26
import org .springframework .http .HttpHeaders ;
25
27
import org .springframework .http .HttpStatus ;
26
28
import org .springframework .http .MediaType ;
27
29
import org .springframework .http .ResponseEntity ;
30
+ import org .springframework .security .core .Authentication ;
31
+ import org .springframework .security .core .context .SecurityContextHolder ;
32
+ import org .springframework .security .core .userdetails .UserDetails ;
28
33
import org .springframework .web .bind .annotation .RequestBody ;
29
34
import org .springframework .web .bind .annotation .RequestMapping ;
30
35
import org .springframework .web .bind .annotation .RequestMethod ;
35
40
import org .springframework .web .client .RestTemplate ;
36
41
37
42
import com .ericsson .eiffel .remrem .protocol .MsgService ;
43
+ import com .ericsson .eiffel .remrem .publish .exception .RemRemPublishException ;
38
44
import com .ericsson .eiffel .remrem .publish .helper .PublishUtils ;
39
45
import com .ericsson .eiffel .remrem .publish .helper .RMQHelper ;
40
46
import com .ericsson .eiffel .remrem .publish .service .EventTemplateHandler ;
47
+ import com .ericsson .eiffel .remrem .publish .service .GenerateURLTemplate ;
41
48
import com .ericsson .eiffel .remrem .publish .service .MessageService ;
42
49
import com .ericsson .eiffel .remrem .publish .service .SendResult ;
43
- import com .ericsson .eiffel .remrem .publish .service .GenerateURLTemplate ;
44
- import com .ericsson .eiffel .remrem .publish .exception .RemRemPublishException ;
45
50
import com .fasterxml .jackson .databind .JsonNode ;
46
51
import com .google .gson .JsonElement ;
47
52
import com .google .gson .JsonObject ;
@@ -73,6 +78,9 @@ public class ProducerController {
73
78
@ Autowired
74
79
private GenerateURLTemplate generateURLTemplate ;
75
80
81
+ @ Value ("${activedirectory.publish.enabled}" )
82
+ private boolean isAuthenticationEnabled ;
83
+
76
84
private RestTemplate restTemplate = new RestTemplate ();
77
85
78
86
private JsonParser parser = new JsonParser ();
@@ -87,6 +95,18 @@ public void setRestTemplate(RestTemplate restTemplate) {
87
95
this .restTemplate = restTemplate ;
88
96
}
89
97
98
+ public void logUserName () {
99
+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
100
+ // Check if the user is authenticated
101
+ if (authentication != null && authentication .isAuthenticated ()) {
102
+ // Get the UserDetails object, which contains user information
103
+ UserDetails userDetails = (UserDetails ) authentication .getPrincipal ();
104
+ // Get the username of the authenticated user
105
+ String username = userDetails .getUsername ();
106
+ log .info ("User name: {} " , username );
107
+ }
108
+ }
109
+
90
110
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
91
111
@ ApiOperation (value = "To publish eiffel event to message bus" , response = String .class )
92
112
@ ApiResponses (value = { @ ApiResponse (code = 200 , message = "Event sent successfully" ),
@@ -96,22 +116,30 @@ public void setRestTemplate(RestTemplate restTemplate) {
96
116
@ ApiResponse (code = 503 , message = "Service Unavailable" ) })
97
117
@ RequestMapping (value = "/producer/msg" , method = RequestMethod .POST )
98
118
@ ResponseBody
99
- public ResponseEntity send (@ ApiParam (value = "message protocol" , required = true ) @ RequestParam (value = "mp" ) final String msgProtocol ,
100
- @ ApiParam (value = "user domain" ) @ RequestParam (value = "ud" , required = false ) final String userDomain ,
101
- @ ApiParam (value = "tag" ) @ RequestParam (value = "tag" , required = false ) final String tag ,
102
- @ ApiParam (value = "routing key" ) @ RequestParam (value = "rk" , required = false ) final String routingKey ,
103
- @ ApiParam (value = "eiffel event" , required = true ) @ RequestBody final JsonElement body ) {
119
+ public ResponseEntity send (
120
+ @ ApiParam (value = "message protocol" , required = true ) @ RequestParam (value = "mp" ) final String msgProtocol ,
121
+ @ ApiParam (value = "user domain" ) @ RequestParam (value = "ud" , required = false ) final String userDomain ,
122
+ @ ApiParam (value = "tag" ) @ RequestParam (value = "tag" , required = false ) final String tag ,
123
+ @ ApiParam (value = "routing key" ) @ RequestParam (value = "rk" , required = false ) final String routingKey ,
124
+ @ ApiParam (value = "eiffel event" , required = true ) @ RequestBody final JsonElement body ) {
125
+ if (isAuthenticationEnabled ) {
126
+ logUserName ();
127
+ }
128
+
104
129
MsgService msgService = PublishUtils .getMessageService (msgProtocol , msgServices );
105
130
log .debug ("mp: " + msgProtocol );
106
131
log .debug ("body: " + body );
107
- log .debug ("user domain suffix: " + userDomain + " tag: " + tag + " Routing Key: " + routingKey );
132
+ log .debug ("user domain suffix: " + userDomain + " tag: " + tag + " Routing Key: "
133
+ + routingKey );
134
+
108
135
if (msgService != null && msgProtocol != null ) {
109
136
try {
110
137
rmqHelper .rabbitMqPropertiesInit (msgProtocol );
111
138
} catch (RemRemPublishException e ) {
112
139
return new ResponseEntity (e .getMessage (), HttpStatus .NOT_FOUND );
113
140
}
114
- } synchronized (this ) {
141
+ }
142
+ synchronized (this ) {
115
143
SendResult result = messageService .send (body , msgService , userDomain , tag , routingKey );
116
144
log .info ("HTTP Status: {}" , messageService .getHttpStatus ().value ());
117
145
return new ResponseEntity (result , messageService .getHttpStatus ());
@@ -166,6 +194,9 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r
166
194
+ "event fields from the input event data that does not validate successfully, "
167
195
+ "and add those removed field information into customData/remremGenerateFailures" ) @ RequestParam (value = "okToLeaveOutInvalidOptionalFields" , required = false , defaultValue = "false" ) final Boolean okToLeaveOutInvalidOptionalFields ,
168
196
@ ApiParam (value = "JSON message" , required = true ) @ RequestBody final JsonObject bodyJson ) {
197
+ if (isAuthenticationEnabled ) {
198
+ logUserName ();
199
+ }
169
200
170
201
String bodyJsonOut = null ;
171
202
if (parseData ) {
0 commit comments