2020
2121import java .util .List ;
2222
23- import javax .ws .rs .core .MediaType ;
24-
2523import sif3 .common .CommonConstants ;
2624import sif3 .common .header .HeaderProperties ;
2725import sif3 .common .header .HeaderValues .EventAction ;
2826import sif3 .common .header .HeaderValues .ServiceType ;
29- import sif3 .common .header .RequestHeaderConstants ;
3027import sif3 .common .interfaces .EventProvider ;
3128import sif3 .common .interfaces .SIFEventIterator ;
3229import sif3 .common .model .ACL .AccessRight ;
3734import sif3 .common .model .ServiceInfo ;
3835import sif3 .common .model .ZoneContextInfo ;
3936import sif3 .common .persist .model .SIF3Session ;
40- import sif3 .common .ws .BaseResponse ;
4137import sif3 .infra .common .env .mgr .BrokeredProviderEnvironmentManager ;
42- import sif3 .infra .common .env .mgr .ProviderManagerFactory ;
43- import sif3 .infra .common .interfaces .EnvironmentManager ;
4438import sif3 .infra .rest .client .EventClient ;
4539
4640
@@ -74,103 +68,6 @@ public int getMaxObjectsInEvent()
7468 return getServiceProperties ().getPropertyAsInt (CommonConstants .EVENT_MAX_OBJ , getProviderName (), 10 );
7569 }
7670
77- /*------------------------------------------------------------------------------------------------------------------------
78- * Start of 'Dynamic' HTTP Header Field override section for Events
79- *
80- * The following set of methods are used for a more configurable way how some HTTP header parameters are set.
81- * By default the following HTTP Header fields are retrieved from the provider's property file and put in corresponding
82- * HTTP Header Fields of each event:
83- *
84- * Property HTTP Header
85- * ----------------------------------------------------------------
86- * adapter.generator.id generatorId
87- * env.application.key applicationKey
88- * env.userToken authenticatedUser
89- * env.mediaType Content-Type, Accept
90- * adapter.compression.enabled Content-Encoding, Accept-Encoding
91- *
92- * Only properties that are not null or empty string will be set in the corresponding HTTP Header.
93- *
94- * There are situations where and application may need a more 'dynamic' behaviour where the above values are determined
95- * at runtime, based on other circumstances and therefore these properties must be retrieved from an other source than the
96- * providers's property file. In such a case the methods below can be overwritten to make them dynamic and controlled by
97- * the implementation rather than driven by the provider's property file. If any of the methods below is overwritten then
98- * the value of the over riding method is set in the corresponding HTTP Header field if the return value of the method
99- * is not null or an empty string.
100- *------------------------------------------------------------------------------------------------------------------------*/
101-
102- /**
103- * This method returns the value of the adapter.generator.id property from the provider's property file. If that
104- * needs to be overridden by a specific implementation then the specific sub-class should override this method.
105- *
106- * @return The adapter.generator.id property from the provider's property file
107- */
108- public String getGeneratorID ()
109- {
110- return getProviderEnvironment ().getGeneratorID ();
111- }
112-
113- /**
114- * This method returns the value of the env.application.key property from the provider's property file. If that
115- * needs to be overridden by a specific implementation then the specific sub-class should override this method.
116- *
117- * @return The env.application.key property from the provider's property file
118- */
119- public String getApplicationKey ()
120- {
121- return getProviderEnvironment ().getEnvironmentKey ().getApplicationKey ();
122- }
123-
124- /**
125- * This method returns the value of the env.userToken property from the provider's property file. If that
126- * needs to be overridden by a specific implementation then the specific sub-class should override this method.
127- *
128- * @return The env.userToken property from the provider's property file
129- */
130- public String getAuthentictedUser ()
131- {
132- return getProviderEnvironment ().getEnvironmentKey ().getUserToken ();
133- }
134-
135- /**
136- * This method returns the value of the env.mediaType property from the provider's property file. If that
137- * needs to be overridden by a specific implementation then the specific sub-class should override this method.
138- *
139- * @return The env.mediaType property from the provider's property file
140- */
141- public MediaType getRequestMediaType ()
142- {
143- return getProviderEnvironment ().getMediaType ();
144- }
145-
146- /**
147- * This method returns the value of the env.mediaType property from the provider's property file. If that
148- * needs to be overridden by a specific implementation then the specific sub-class should override this method.
149- *
150- * @return The env.mediaType property from the provider's property file
151- */
152- public MediaType getResponseMediaType ()
153- {
154- return getProviderEnvironment ().getMediaType ();
155- }
156-
157- /**
158- * This method returns the value of the adapter.compression.enabled property from the provider's property file. If
159- * that needs to be overridden by a specific implementation then the specific sub-class should override this method.
160- *
161- * @return The adapter.compression.enabled property from the provider's property file
162- */
163- public boolean getCompressionEnabled ()
164- {
165- return getProviderEnvironment ().getCompressionEnabled ();
166- }
167-
168-
169- /*------------------------------------------------------------------------------------------------------------------------
170- * End of 'Dynamic' HTTP Header Field override section
171- *-----------------------------------------------------------------------------------------------------------------------*/
172-
173-
17471 /**
17572 * This method retrieves all events to be published by calling the abstract method getSIFEvents(). The returned list
17673 * is then broadcasted to all zones known to the implementing agent.
@@ -184,13 +81,14 @@ public void broadcastEvents()
18481 int failedRecords = 0 ;
18582 int maxNumObjPerEvent = getMaxObjectsInEvent ();
18683
187- SIF3Session sif3Session = getActiveSession ();
188- if (sif3Session == null )
84+ BrokeredProviderEnvironmentManager envMgr = getBrokeredEnvironmentManager ();
85+ if (envMgr == null ) // not a brokered environment. Cannot sent events!
18986 {
190- return ; //cannot send events. Error already logged.
87+ return ; // error already logged.
19188 }
19289
193- List <ServiceInfo > servicesForProvider = getServicesForProvider (sif3Session );
90+ SIF3Session sif3Session = envMgr .getSIF3Session ();
91+ List <ServiceInfo > servicesForProvider = getServicesForProvider (sif3Session , ServiceType .OBJECT );
19492
19593 // If there are no services for this provider defined then we don't need to get any events at all.
19694 if ((servicesForProvider == null ) || (servicesForProvider .size () == 0 ))
@@ -202,7 +100,7 @@ public void broadcastEvents()
202100 {
203101 // Let's get the Event Client
204102 String serviceName = getServiceName ();
205- EventClient evtClient = new EventClient (getEnvironmentManager () , getRequestMediaType (), getResponseMediaType (), serviceName , getMarshaller (), getCompressionEnabled ());
103+ EventClient evtClient = new EventClient (envMgr , getRequestMediaType (), getResponseMediaType (), serviceName , getMarshaller (), getCompressionEnabled ());
206104 SIFEventIterator <L > iterator = getSIFEvents ();
207105 if (iterator != null )
208106 {
@@ -227,7 +125,7 @@ public void broadcastEvents()
227125 for (ServiceInfo service : servicesForProvider )
228126 {
229127 // Check if provider has rights to publish to given zone and context
230- if (hasAccess (service ))
128+ if (hasAccess (service , AccessRight . PROVIDE , AccessType . APPROVED ))
231129 {
232130 failedRecords = failedRecords + prepareEventAndSend (evtClient , sifEvents , service .getZone (), service .getContext (), eventAction );
233131 }
@@ -297,7 +195,7 @@ protected int prepareEventAndSend(EventClient evtClient, SIFEvent<L> sifEvents,
297195 //Just in case the developer has changed it. Should not be allowed :-)
298196 modifiedEvents .setEventAction (eventAction );
299197
300- if (!sendEvents (evtClient , modifiedEvents , zone , context , customHTTPHeaders ))
198+ if (!sendEvents (evtClient , modifiedEvents , zone , context , ServiceType . OBJECT , customHTTPHeaders ))
301199 {
302200 //Report back to the caller. This should also give the event back to the caller.
303201 onEventError (modifiedEvents , zone , context );
@@ -307,126 +205,4 @@ protected int prepareEventAndSend(EventClient evtClient, SIFEvent<L> sifEvents,
307205
308206 return failedRecords ;
309207 }
310-
311- private void logNoAccessRight (SIFZone zone , SIFContext context )
312- {
313- String zoneID = (zone == null ) ? "Default" : zone .getId ();
314- String contextID = (context == null ) ? CommonConstants .DEFAULT_CONTEXT_NAME : context .getId ();
315-
316- logger .debug ("The " +getProviderName ()+" does not have the ACL entry PROVIDE = APPROVED for service = " + getServiceName () + " for the Zone = " + zoneID + " and the Context = " + contextID + ". Events are NOT sent to this zone and context." );
317- // return ((sifEvents != null) ? sifEvents.getListSize() : 0);
318- }
319-
320-
321- /**
322- * If one doesn't want certain events to be published to a given zone then this method needs to be
323- * overridden. It allows to test for the event and zone and make the appropriate decision if the event
324- * shall be sent.
325- *
326- * @param event The event to be published to the zone.
327- * @param zone The zone to which the event is published to.
328- * @param customHTTPHeaders Custom HTTP Headers to be added to the event.
329- *
330- * @return TRUE: Event sent successfully. FALSE: Failed to send event. Error must be logged.
331- */
332- protected boolean sendEvents (EventClient evtClient , SIFEvent <?> sifEvents , SIFZone zone , SIFContext context , HeaderProperties customHTTPHeaders )
333- {
334- logger .debug (getPrettyName ()+" sending a " +getServiceName ()+" event with " +sifEvents .getListSize ()+" sif objects." );
335- try
336- {
337- if (logger .isDebugEnabled ())
338- {
339- logger .debug ("Custom HTTP Headers set by modifyBeforePublishing() method: " +customHTTPHeaders );
340- }
341- if (customHTTPHeaders == null )
342- {
343- customHTTPHeaders = new HeaderProperties ();
344- }
345-
346- // Add all other HTTP headers to this customHTTPHeader. This ensures that SIF managed HTTP headers will
347- // override custom HTTP Headers.
348- addSIF3OverrideHeaderProperties (customHTTPHeaders );
349-
350- BaseResponse response = evtClient .sendEvents (sifEvents , zone , context , customHTTPHeaders );
351- if (response .hasError ())
352- {
353- logger .error ("Failed to send event: " +response .getError ());
354- return false ;
355- }
356- else
357- {
358- return true ;
359- }
360- }
361- catch (Exception ex )
362- {
363- logger .error ("An error occured sending an event message. See previous error log entries." , ex );
364- return false ;
365- }
366- }
367-
368-
369- protected void addSIF3OverrideHeaderProperties (HeaderProperties hdrProps )
370- {
371- //HeaderProperties hdrProps = new HeaderProperties();
372- hdrProps .setHeaderProperty (RequestHeaderConstants .HDR_GENERATOR_ID , getGeneratorID ());
373- hdrProps .setHeaderProperty (RequestHeaderConstants .HDR_APPLICATION_KEY , getApplicationKey ());
374- hdrProps .setHeaderProperty (RequestHeaderConstants .HDR_AUTHENTICATED_USER , getAuthentictedUser ());
375-
376- //return hdrProps;
377- }
378-
379-
380- private BrokeredProviderEnvironmentManager getEnvironmentManager ()
381- {
382- EnvironmentManager envMgr = ProviderManagerFactory .getEnvironmentManager ();
383- if (envMgr != null ) // we have a proper setup and are initialised.
384- {
385- // Note: Currently we only support events for Brokered Environments, so the EnvironmentManager should be of type
386- // BrokeredProviderEnvironmentManager
387- if (envMgr instanceof BrokeredProviderEnvironmentManager )
388- {
389- return (BrokeredProviderEnvironmentManager )envMgr ;
390- }
391- else
392- {
393- logger .error ("Events are only supported for BROKERED environments. This provider is a DIRECT Environment." );
394- }
395- }
396- else
397- {
398- logger .error ("Environment Manager not initialised. Not connected to an environment. No Environment Manger available." );
399- }
400-
401- // If we get here then we don't have a valid environment manager.
402- return null ;
403- }
404-
405-
406- private List <ServiceInfo > getServicesForProvider (SIF3Session sif3Session )
407- {
408- if (sif3Session != null )
409- {
410- return sif3Session .getServiceInfoForService (getServiceName (), ServiceType .OBJECT );
411- }
412-
413- return null ;
414- }
415-
416- private SIF3Session getActiveSession ()
417- {
418- BrokeredProviderEnvironmentManager envMgr = getEnvironmentManager ();
419- if (envMgr != null )
420- {
421- return envMgr .getSIF3Session ();
422- }
423-
424- return null ; // Error already logged.
425- }
426-
427- private boolean hasAccess (ServiceInfo service )
428- {
429- // All that is required is PROVIDE right to be set to APPROVED.
430- return service .getRights ().hasRight (AccessRight .PROVIDE , AccessType .APPROVED );
431- }
432208}
0 commit comments