/** * Overridden method for rest json message sending. * * @param publisherEvent Published event name * @throws NotificationManagementException */ @Override public void sendMessage(PublisherEvent publisherEvent) throws NotificationManagementException { // Publisher event will not be null since it is handled by the mgt component. // Read the relevant subscription from configurations. JsonSubscription jsonSubscription = subscriptionMap.get(publisherEvent.getEventName()); // New list for aggregated set of endpoints. ie configured and dynamic List<JsonEndpointInfo> endpoints; if (jsonSubscription != null) { endpoints = jsonSubscription.getEndpointInfoList(); HttpClient client = new DefaultHttpClient(); // Get all configured endpoints for message sending // Send messages to each endpoint in endpoints list for (JsonEndpointInfo endpoint : endpoints) { HttpPost post = new HttpPost(endpoint.getEndpoint()); HttpResponse response = null; StringEntity entity; post.setHeader( JsonModuleConstants.CONTENT_TYPE_LABEL, JsonModuleConstants.CONTENT_TYPE_JSON_LABEL); // Adding basic authentication header to post. If required information are not found, // building subscription will fail at the time of configuration building if (endpoint.isAuthenticationRequired()) { if (log.isDebugEnabled()) { log.debug("Setting authentication information on request to " + endpoint.getEndpoint()); } post.setHeader( JsonModuleConstants.AUTHORIZATION_HEADER, getBase64EncodedBasicAuthHeader( endpoint.getUsername(), String.valueOf(endpoint.getPassword()))); } else { if (log.isDebugEnabled()) { log.debug("No authentication required to endpoint " + endpoint.getEndpoint()); } } // Read JSON content from endpoint configurations. If not present, // get content form event configurations. String jsonContent = endpoint.getJsonConfigString(); if (StringUtils.isEmpty(jsonContent)) { jsonContent = jsonSubscription.getJsonContent(); } if (StringUtils.isEmpty(jsonContent)) { log.error( "No content template found either for event or endpoint " + endpoint.getEndpoint() + " on event " + publisherEvent.getEventName() + ", message sending aborted"); continue; } String jsonMessage = getJSONData( jsonContent, jsonSubscription.getSubscriptionProperties(), endpoint.getEndpointsParams(), publisherEvent.getEventProperties()); try { entity = new StringEntity(jsonMessage); post.setEntity(entity); response = client.execute(post); log.info( "Notification message has been posted to " + endpoint.getEndpoint() + " on event " + publisherEvent.getEventName()); if (log.isDebugEnabled() && response != null) { log.debug( "Response of HTTP post is " + response.getStatusLine() + "on post operation to " + "endpoint " + endpoint.getEndpoint() + " on event " + publisherEvent.getEventName()); } } catch (UnsupportedEncodingException e) { log.error( "Error while creating StringEntry from given JSON string on event " + publisherEvent.getEventName() + "to endpoint " + endpoint.getEndpoint() + " JSON string : " + jsonMessage, e); } catch (ClientProtocolException e) { log.error( "Error in HTTP protocol, error while executing POST operation to endpoint " + endpoint.getEndpoint() + "on event " + publisherEvent.getEventName(), e); } catch (IOException e) { log.error( "Error while executing POST operation to endpoint " + endpoint.getEndpoint() + "on " + "event " + publisherEvent.getEventName(), e); } finally { // Finally closing connections. post.abort(); } } } }
/** * Whether a particular type of event is subscribed by this module or not * * @return Whether this module is subscribed or not */ @Override public boolean isSubscribed(PublisherEvent publisherEvent) throws NotificationManagementException { return publisherEvent != null && subscriptionMap.containsKey(publisherEvent.getEventName()); }