@Test @JUnitHttpServer(port=7081,basicAuth=true) public void testFetchHypericAlerts() throws Exception { { List<HypericAckProcessor.HypericAlertStatus> alerts = HypericAckProcessor.fetchHypericAlerts("http: assertEquals(5, alerts.size()); for (HypericAckProcessor.HypericAlertStatus alert : alerts) { System.out.println(alert.toString()); } alerts = HypericAckProcessor.fetchHypericAlerts("http: assertEquals(5, alerts.size()); } { boolean caughtAuthFailure = false; for (String url : new String[] { "http: "http: "http: "http: "http: "http: }) { try { HypericAckProcessor.fetchHypericAlerts(url, Arrays.asList(new String[] { "1", "2", "3" })); } catch (JAXBException e) { caughtAuthFailure = true; } assertTrue("Did not catch expected authorization failure for URL: " + url, caughtAuthFailure); } } }
public static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds) throws IOException, JAXBException, XMLStreamException { List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>(); if (alertIds.size() < 1) { return retval; } for (int i = 0; i < alertIds.size(); i++) { StringBuffer alertIdString = new StringBuffer(); alertIdString.append("?"); for (int j = 0; (j < ALERTS_PER_HTTP_TRANSACTION) && (i < alertIds.size()); j++,i++) { if (j > 0) alertIdString.append("&"); alertIdString.append("id=").append(alertIds.get(i)); } DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000); HttpUriRequest httpMethod = new HttpGet(hypericUrl + alertIdString.toString()); httpMethod.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenNMS-Ackd.HypericAckProcessor"); String userinfo = null; try { URI hypericUri = new URI(hypericUrl); userinfo = hypericUri.getUserInfo(); } catch (URISyntaxException e) { log().warn("Could not parse URI to get username/password stanza: " + hypericUrl, e); } if (userinfo != null && !"".equals(userinfo)) { httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userinfo)); HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.update(new BasicScheme(), creds); } } } }; httpClient.addRequestInterceptor(preemptiveAuth, 0); } try { HttpResponse response = httpClient.execute(httpMethod); retval = parseHypericAlerts(new StringReader(EntityUtils.toString(response.getEntity()))); } finally{ } } return retval; }
HypericAckProcessor implements AckProcessor { public static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds) throws IOException, JAXBException, XMLStreamException { List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>(); if (alertIds.size() < 1) { return retval; } for (int i = 0; i < alertIds.size(); i++) { StringBuffer alertIdString = new StringBuffer(); alertIdString.append("?"); for (int j = 0; (j < ALERTS_PER_HTTP_TRANSACTION) && (i < alertIds.size()); j++,i++) { if (j > 0) alertIdString.append("&"); alertIdString.append("id=").append(alertIds.get(i)); } DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000); HttpUriRequest httpMethod = new HttpGet(hypericUrl + alertIdString.toString()); httpMethod.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenNMS-Ackd.HypericAckProcessor"); String userinfo = null; try { URI hypericUri = new URI(hypericUrl); userinfo = hypericUri.getUserInfo(); } catch (URISyntaxException e) { log().warn("Could not parse URI to get username/password stanza: " + hypericUrl, e); } if (userinfo != null && !"".equals(userinfo)) { httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userinfo)); HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.update(new BasicScheme(), creds); } } } }; httpClient.addRequestInterceptor(preemptiveAuth, 0); } try { HttpResponse response = httpClient.execute(httpMethod); retval = parseHypericAlerts(new StringReader(EntityUtils.toString(response.getEntity()))); } finally{ } } return retval; } }
HypericAckProcessor implements AckProcessor { public static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds) throws IOException, JAXBException, XMLStreamException { List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>(); if (alertIds.size() < 1) { return retval; } for (int i = 0; i < alertIds.size(); i++) { StringBuffer alertIdString = new StringBuffer(); alertIdString.append("?"); for (int j = 0; (j < ALERTS_PER_HTTP_TRANSACTION) && (i < alertIds.size()); j++,i++) { if (j > 0) alertIdString.append("&"); alertIdString.append("id=").append(alertIds.get(i)); } DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000); HttpUriRequest httpMethod = new HttpGet(hypericUrl + alertIdString.toString()); httpMethod.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenNMS-Ackd.HypericAckProcessor"); String userinfo = null; try { URI hypericUri = new URI(hypericUrl); userinfo = hypericUri.getUserInfo(); } catch (URISyntaxException e) { log().warn("Could not parse URI to get username/password stanza: " + hypericUrl, e); } if (userinfo != null && !"".equals(userinfo)) { httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userinfo)); HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.update(new BasicScheme(), creds); } } } }; httpClient.addRequestInterceptor(preemptiveAuth, 0); } try { HttpResponse response = httpClient.execute(httpMethod); retval = parseHypericAlerts(new StringReader(EntityUtils.toString(response.getEntity()))); } finally{ } } return retval; }  }
HypericAckProcessor implements AckProcessor { public static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds) throws IOException, JAXBException, XMLStreamException { List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>(); if (alertIds.size() < 1) { return retval; } for (int i = 0; i < alertIds.size(); i++) { StringBuffer alertIdString = new StringBuffer(); alertIdString.append("?"); for (int j = 0; (j < ALERTS_PER_HTTP_TRANSACTION) && (i < alertIds.size()); j++,i++) { if (j > 0) alertIdString.append("&"); alertIdString.append("id=").append(alertIds.get(i)); } DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000); HttpUriRequest httpMethod = new HttpGet(hypericUrl + alertIdString.toString()); httpMethod.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenNMS-Ackd.HypericAckProcessor"); String userinfo = null; try { URI hypericUri = new URI(hypericUrl); userinfo = hypericUri.getUserInfo(); } catch (URISyntaxException e) { log().warn("Could not parse URI to get username/password stanza: " + hypericUrl, e); } if (userinfo != null && !"".equals(userinfo)) { httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userinfo)); HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.update(new BasicScheme(), creds); } } } }; httpClient.addRequestInterceptor(preemptiveAuth, 0); } try { HttpResponse response = httpClient.execute(httpMethod); retval = parseHypericAlerts(new StringReader(EntityUtils.toString(response.getEntity()))); } finally{ } } return retval; }  void reloadConfigs(); List<OnmsAlarm> fetchUnclearedHypericAlarms(); String getUrlForHypericSource(String source); void run(); static OnmsAlarm findAlarmForHypericAlert(List<OnmsAlarm> alarms, String platformId, HypericAlertStatus alert); static String getAlertSourceParmValue(OnmsAlarm alarm); static String getAlertIdParmValue(OnmsAlarm alarm); static String getParmValueByRegex(OnmsAlarm alarm, String regex); static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds); static List<HypericAlertStatus> parseHypericAlerts(Reader reader); synchronized void setAckdConfigDao(final AckdConfigurationDao configDao); synchronized void setAckService(final AckService ackService); @Override void afterPropertiesSet(); synchronized void setAlarmDao(final AlarmDao dao); }
HypericAckProcessor implements AckProcessor { public static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds) throws IOException, JAXBException, XMLStreamException { List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>(); if (alertIds.size() < 1) { return retval; } for (int i = 0; i < alertIds.size(); i++) { StringBuffer alertIdString = new StringBuffer(); alertIdString.append("?"); for (int j = 0; (j < ALERTS_PER_HTTP_TRANSACTION) && (i < alertIds.size()); j++,i++) { if (j > 0) alertIdString.append("&"); alertIdString.append("id=").append(alertIds.get(i)); } DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 3000); HttpUriRequest httpMethod = new HttpGet(hypericUrl + alertIdString.toString()); httpMethod.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenNMS-Ackd.HypericAckProcessor"); String userinfo = null; try { URI hypericUri = new URI(hypericUrl); userinfo = hypericUri.getUserInfo(); } catch (URISyntaxException e) { log().warn("Could not parse URI to get username/password stanza: " + hypericUrl, e); } if (userinfo != null && !"".equals(userinfo)) { httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userinfo)); HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws IOException { AuthState authState = (AuthState)context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.update(new BasicScheme(), creds); } } } }; httpClient.addRequestInterceptor(preemptiveAuth, 0); } try { HttpResponse response = httpClient.execute(httpMethod); retval = parseHypericAlerts(new StringReader(EntityUtils.toString(response.getEntity()))); } finally{ } } return retval; }  void reloadConfigs(); List<OnmsAlarm> fetchUnclearedHypericAlarms(); String getUrlForHypericSource(String source); void run(); static OnmsAlarm findAlarmForHypericAlert(List<OnmsAlarm> alarms, String platformId, HypericAlertStatus alert); static String getAlertSourceParmValue(OnmsAlarm alarm); static String getAlertIdParmValue(OnmsAlarm alarm); static String getParmValueByRegex(OnmsAlarm alarm, String regex); static List<HypericAlertStatus> fetchHypericAlerts(String hypericUrl, List<String> alertIds); static List<HypericAlertStatus> parseHypericAlerts(Reader reader); synchronized void setAckdConfigDao(final AckdConfigurationDao configDao); synchronized void setAckService(final AckService ackService); @Override void afterPropertiesSet(); synchronized void setAlarmDao(final AlarmDao dao); static final String READER_NAME_HYPERIC; static final String PARAMETER_PREFIX_HYPERIC_SOURCE; static final int ALERTS_PER_HTTP_TRANSACTION; }