@Test public void testForwardAlarms() throws InterruptedException { HttpNorthbounder nb = new HttpNorthbounder(); HttpNorthbounderConfig config = new HttpNorthbounderConfig("localhost"); config.setMethod(HttpMethod.POST); config.setPath("/jms/post"); config.setPort(Integer.valueOf(10342)); nb.setConfig(config); OnmsAlarm alarm = new OnmsAlarm(); alarm.setId(1); alarm.setUei("uei.opennms.org/test/httpNorthBounder"); NorthboundAlarm a = new NorthboundAlarm(alarm); nb.onAlarm(a); Thread.sleep(10000); }
@Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { LogUtils.infof(this, "Forwarding %i alarms", alarms.size()); int connectionTimeout = 3000; int socketTimeout = 3000; Integer retryCount = Integer.valueOf(3); HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion()); String policy = CookiePolicy.BROWSER_COMPATIBILITY; URI uri = m_config.getURI(); DefaultHttpClient client = new DefaultHttpClient(buildParams(httpVersion, connectionTimeout, socketTimeout, policy, m_config.getVirtualHost())); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); if ("https".equals(uri.getScheme())) { final SchemeRegistry registry = client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); SSLSocketFactory factory = null; try { factory = new SSLSocketFactory(SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable e) { throw new NorthbounderException(e); } final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } HttpUriRequest method = null; if (HttpMethod.POST == (m_config.getMethod())) { HttpPost postMethod = new HttpPost(uri); List<NameValuePair> postParms = new ArrayList<NameValuePair>(); NameValuePair p = new BasicNameValuePair("foo", "bar"); postParms.add(p); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParms, "UTF-8"); postMethod.setEntity(entity); } catch (UnsupportedEncodingException e) { throw new NorthbounderException(e); } HttpEntity entity = null; try { entity = new StringEntity("XML HERE"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } postMethod.setEntity(entity); method = postMethod; } else if (HttpMethod.GET == m_config.getMethod()) { method = new HttpGet(uri); } method.getParams().setParameter(CoreProtocolPNames.USER_AGENT, m_config.getUserAgent()); HttpResponse response = null; try { response = client.execute(method); } catch (ClientProtocolException e) { throw new NorthbounderException(e); } catch (IOException e) { throw new NorthbounderException(e); } if (response != null) { int code = response.getStatusLine().getStatusCode(); HttpResponseRange range = new HttpResponseRange("200-399"); if (!range.contains(code)) { System.err.println("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); } } System.err.println(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); LogUtils.debugf(this, response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); }
HttpNorthbounder extends AbstractNorthbounder { @Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { LogUtils.infof(this, "Forwarding %i alarms", alarms.size()); int connectionTimeout = 3000; int socketTimeout = 3000; Integer retryCount = Integer.valueOf(3); HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion()); String policy = CookiePolicy.BROWSER_COMPATIBILITY; URI uri = m_config.getURI(); DefaultHttpClient client = new DefaultHttpClient(buildParams(httpVersion, connectionTimeout, socketTimeout, policy, m_config.getVirtualHost())); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); if ("https".equals(uri.getScheme())) { final SchemeRegistry registry = client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); SSLSocketFactory factory = null; try { factory = new SSLSocketFactory(SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable e) { throw new NorthbounderException(e); } final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } HttpUriRequest method = null; if (HttpMethod.POST == (m_config.getMethod())) { HttpPost postMethod = new HttpPost(uri); List<NameValuePair> postParms = new ArrayList<NameValuePair>(); NameValuePair p = new BasicNameValuePair("foo", "bar"); postParms.add(p); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParms, "UTF-8"); postMethod.setEntity(entity); } catch (UnsupportedEncodingException e) { throw new NorthbounderException(e); } HttpEntity entity = null; try { entity = new StringEntity("XML HERE"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } postMethod.setEntity(entity); method = postMethod; } else if (HttpMethod.GET == m_config.getMethod()) { method = new HttpGet(uri); } method.getParams().setParameter(CoreProtocolPNames.USER_AGENT, m_config.getUserAgent()); HttpResponse response = null; try { response = client.execute(method); } catch (ClientProtocolException e) { throw new NorthbounderException(e); } catch (IOException e) { throw new NorthbounderException(e); } if (response != null) { int code = response.getStatusLine().getStatusCode(); HttpResponseRange range = new HttpResponseRange("200-399"); if (!range.contains(code)) { System.err.println("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); } } System.err.println(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); LogUtils.debugf(this, response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); } }
HttpNorthbounder extends AbstractNorthbounder { @Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { LogUtils.infof(this, "Forwarding %i alarms", alarms.size()); int connectionTimeout = 3000; int socketTimeout = 3000; Integer retryCount = Integer.valueOf(3); HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion()); String policy = CookiePolicy.BROWSER_COMPATIBILITY; URI uri = m_config.getURI(); DefaultHttpClient client = new DefaultHttpClient(buildParams(httpVersion, connectionTimeout, socketTimeout, policy, m_config.getVirtualHost())); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); if ("https".equals(uri.getScheme())) { final SchemeRegistry registry = client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); SSLSocketFactory factory = null; try { factory = new SSLSocketFactory(SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable e) { throw new NorthbounderException(e); } final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } HttpUriRequest method = null; if (HttpMethod.POST == (m_config.getMethod())) { HttpPost postMethod = new HttpPost(uri); List<NameValuePair> postParms = new ArrayList<NameValuePair>(); NameValuePair p = new BasicNameValuePair("foo", "bar"); postParms.add(p); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParms, "UTF-8"); postMethod.setEntity(entity); } catch (UnsupportedEncodingException e) { throw new NorthbounderException(e); } HttpEntity entity = null; try { entity = new StringEntity("XML HERE"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } postMethod.setEntity(entity); method = postMethod; } else if (HttpMethod.GET == m_config.getMethod()) { method = new HttpGet(uri); } method.getParams().setParameter(CoreProtocolPNames.USER_AGENT, m_config.getUserAgent()); HttpResponse response = null; try { response = client.execute(method); } catch (ClientProtocolException e) { throw new NorthbounderException(e); } catch (IOException e) { throw new NorthbounderException(e); } if (response != null) { int code = response.getStatusLine().getStatusCode(); HttpResponseRange range = new HttpResponseRange("200-399"); if (!range.contains(code)) { System.err.println("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); } } System.err.println(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); LogUtils.debugf(this, response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); } protected  HttpNorthbounder(); }
HttpNorthbounder extends AbstractNorthbounder { @Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { LogUtils.infof(this, "Forwarding %i alarms", alarms.size()); int connectionTimeout = 3000; int socketTimeout = 3000; Integer retryCount = Integer.valueOf(3); HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion()); String policy = CookiePolicy.BROWSER_COMPATIBILITY; URI uri = m_config.getURI(); DefaultHttpClient client = new DefaultHttpClient(buildParams(httpVersion, connectionTimeout, socketTimeout, policy, m_config.getVirtualHost())); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); if ("https".equals(uri.getScheme())) { final SchemeRegistry registry = client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); SSLSocketFactory factory = null; try { factory = new SSLSocketFactory(SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable e) { throw new NorthbounderException(e); } final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } HttpUriRequest method = null; if (HttpMethod.POST == (m_config.getMethod())) { HttpPost postMethod = new HttpPost(uri); List<NameValuePair> postParms = new ArrayList<NameValuePair>(); NameValuePair p = new BasicNameValuePair("foo", "bar"); postParms.add(p); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParms, "UTF-8"); postMethod.setEntity(entity); } catch (UnsupportedEncodingException e) { throw new NorthbounderException(e); } HttpEntity entity = null; try { entity = new StringEntity("XML HERE"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } postMethod.setEntity(entity); method = postMethod; } else if (HttpMethod.GET == m_config.getMethod()) { method = new HttpGet(uri); } method.getParams().setParameter(CoreProtocolPNames.USER_AGENT, m_config.getUserAgent()); HttpResponse response = null; try { response = client.execute(method); } catch (ClientProtocolException e) { throw new NorthbounderException(e); } catch (IOException e) { throw new NorthbounderException(e); } if (response != null) { int code = response.getStatusLine().getStatusCode(); HttpResponseRange range = new HttpResponseRange("200-399"); if (!range.contains(code)) { System.err.println("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); } } System.err.println(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); LogUtils.debugf(this, response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); } protected  HttpNorthbounder(); @Override boolean accepts(NorthboundAlarm alarm); @Override void forwardAlarms(List<NorthboundAlarm> alarms); HttpNorthbounderConfig getConfig(); void setConfig(HttpNorthbounderConfig config); }
HttpNorthbounder extends AbstractNorthbounder { @Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { LogUtils.infof(this, "Forwarding %i alarms", alarms.size()); int connectionTimeout = 3000; int socketTimeout = 3000; Integer retryCount = Integer.valueOf(3); HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion()); String policy = CookiePolicy.BROWSER_COMPATIBILITY; URI uri = m_config.getURI(); DefaultHttpClient client = new DefaultHttpClient(buildParams(httpVersion, connectionTimeout, socketTimeout, policy, m_config.getVirtualHost())); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); if ("https".equals(uri.getScheme())) { final SchemeRegistry registry = client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); SSLSocketFactory factory = null; try { factory = new SSLSocketFactory(SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable e) { throw new NorthbounderException(e); } final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } HttpUriRequest method = null; if (HttpMethod.POST == (m_config.getMethod())) { HttpPost postMethod = new HttpPost(uri); List<NameValuePair> postParms = new ArrayList<NameValuePair>(); NameValuePair p = new BasicNameValuePair("foo", "bar"); postParms.add(p); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParms, "UTF-8"); postMethod.setEntity(entity); } catch (UnsupportedEncodingException e) { throw new NorthbounderException(e); } HttpEntity entity = null; try { entity = new StringEntity("XML HERE"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } postMethod.setEntity(entity); method = postMethod; } else if (HttpMethod.GET == m_config.getMethod()) { method = new HttpGet(uri); } method.getParams().setParameter(CoreProtocolPNames.USER_AGENT, m_config.getUserAgent()); HttpResponse response = null; try { response = client.execute(method); } catch (ClientProtocolException e) { throw new NorthbounderException(e); } catch (IOException e) { throw new NorthbounderException(e); } if (response != null) { int code = response.getStatusLine().getStatusCode(); HttpResponseRange range = new HttpResponseRange("200-399"); if (!range.contains(code)) { System.err.println("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); } } System.err.println(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); LogUtils.debugf(this, response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); } protected  HttpNorthbounder(); @Override boolean accepts(NorthboundAlarm alarm); @Override void forwardAlarms(List<NorthboundAlarm> alarms); HttpNorthbounderConfig getConfig(); void setConfig(HttpNorthbounderConfig config);  }