1616
1717import java .util .EnumSet ;
1818import java .util .Map ;
19+
1920import org .slf4j .LoggerFactory ;
2021import org .springframework .beans .factory .annotation .Autowired ;
2122import org .springframework .beans .factory .annotation .Qualifier ;
23+ import org .springframework .beans .factory .annotation .Value ;
2224import org .springframework .context .annotation .ComponentScan ;
2325import org .springframework .http .HttpEntity ;
2426import org .springframework .http .HttpHeaders ;
2527import org .springframework .http .HttpStatus ;
2628import org .springframework .http .MediaType ;
2729import org .springframework .http .ResponseEntity ;
28- import org .springframework .security .authentication .BadCredentialsException ;
2930import org .springframework .security .core .Authentication ;
3031import org .springframework .security .core .context .SecurityContextHolder ;
3132import org .springframework .security .core .userdetails .UserDetails ;
3940import org .springframework .web .client .RestTemplate ;
4041
4142import com .ericsson .eiffel .remrem .protocol .MsgService ;
43+ import com .ericsson .eiffel .remrem .publish .exception .RemRemPublishException ;
4244import com .ericsson .eiffel .remrem .publish .helper .PublishUtils ;
4345import com .ericsson .eiffel .remrem .publish .helper .RMQHelper ;
4446import com .ericsson .eiffel .remrem .publish .service .EventTemplateHandler ;
47+ import com .ericsson .eiffel .remrem .publish .service .GenerateURLTemplate ;
4548import com .ericsson .eiffel .remrem .publish .service .MessageService ;
4649import com .ericsson .eiffel .remrem .publish .service .SendResult ;
47- import com .ericsson .eiffel .remrem .publish .service .GenerateURLTemplate ;
48- import com .ericsson .eiffel .remrem .publish .exception .RemRemPublishException ;
4950import com .fasterxml .jackson .databind .JsonNode ;
5051import com .google .gson .JsonElement ;
5152import com .google .gson .JsonObject ;
@@ -77,6 +78,9 @@ public class ProducerController {
7778 @ Autowired
7879 private GenerateURLTemplate generateURLTemplate ;
7980
81+ @ Value ("${activedirectory.publish.enabled}" )
82+ private boolean isAuthenticationEnabled ;
83+
8084 private RestTemplate restTemplate = new RestTemplate ();
8185
8286 private JsonParser parser = new JsonParser ();
@@ -91,6 +95,18 @@ public void setRestTemplate(RestTemplate restTemplate) {
9195 this .restTemplate = restTemplate ;
9296 }
9397
98+ public void getUserName () {
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+
94110 @ SuppressWarnings ({ "rawtypes" , "unchecked" })
95111 @ ApiOperation (value = "To publish eiffel event to message bus" , response = String .class )
96112 @ ApiResponses (value = { @ ApiResponse (code = 200 , message = "Event sent successfully" ),
@@ -106,20 +122,16 @@ public ResponseEntity send(
106122 @ ApiParam (value = "tag" ) @ RequestParam (value = "tag" , required = false ) final String tag ,
107123 @ ApiParam (value = "routing key" ) @ RequestParam (value = "rk" , required = false ) final String routingKey ,
108124 @ ApiParam (value = "eiffel event" , required = true ) @ RequestBody final JsonElement body ) {
125+ if (isAuthenticationEnabled ) {
126+ getUserName ();
127+ }
128+
109129 MsgService msgService = PublishUtils .getMessageService (msgProtocol , msgServices );
110130 log .debug ("mp: " + msgProtocol );
111131 log .debug ("body: " + body );
112132 log .debug ("user domain suffix: " + userDomain + " tag: " + tag + " Routing Key: "
113133 + routingKey );
114- Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
115- // Check if the user is authenticated
116- if (authentication != null && authentication .isAuthenticated ()) {
117- // Get the UserDetails object, which contains user information
118- UserDetails userDetails = (UserDetails ) authentication .getPrincipal ();
119- // Get the username of the authenticated user
120- String username = userDetails .getUsername ();
121- log .info ("Authentication Successful for user {} " , username );
122- }
134+
123135 if (msgService != null && msgProtocol != null ) {
124136 try {
125137 rmqHelper .rabbitMqPropertiesInit (msgProtocol );
@@ -182,6 +194,9 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r
182194 + "event fields from the input event data that does not validate successfully, "
183195 + "and add those removed field information into customData/remremGenerateFailures" ) @ RequestParam (value = "okToLeaveOutInvalidOptionalFields" , required = false , defaultValue = "false" ) final Boolean okToLeaveOutInvalidOptionalFields ,
184196 @ ApiParam (value = "JSON message" , required = true ) @ RequestBody final JsonObject bodyJson ) {
197+ if (isAuthenticationEnabled ) {
198+ getUserName ();
199+ }
185200
186201 String bodyJsonOut = null ;
187202 if (parseData ) {
0 commit comments