Пример #1
0
 @Override
 public boolean accepts(NorthboundAlarm alarm) {
   if (m_config.getAcceptableUeis() == null
       || m_config.getAcceptableUeis().contains(alarm.getUei())) {
     return true;
   }
   return false;
 }
Пример #2
0
  @Override
  public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException {

    LOG.info("Forwarding {} alarms", alarms.size());

    // Need a configuration bean for these

    int connectionTimeout = 3000;
    int socketTimeout = 3000;
    Integer retryCount = Integer.valueOf(3);

    URI uri = m_config.getURI();

    final HttpClientWrapper clientWrapper =
        HttpClientWrapper.create()
            .setConnectionTimeout(connectionTimeout)
            .setSocketTimeout(socketTimeout)
            .setRetries(retryCount)
            .useBrowserCompatibleCookies();

    if (m_config.getVirtualHost() != null && !m_config.getVirtualHost().trim().isEmpty()) {
      clientWrapper.setVirtualHost(m_config.getVirtualHost());
    }
    if (m_config.getUserAgent() != null && !m_config.getUserAgent().trim().isEmpty()) {
      clientWrapper.setUserAgent(m_config.getUserAgent());
    }

    if ("https".equals(uri.getScheme())) {
      try {
        clientWrapper.useRelaxedSSL("https");
      } catch (final GeneralSecurityException e) {
        throw new NorthbounderException(
            "Failed to configure HTTP northbounder for relaxed SSL.", e);
      }
    }

    HttpUriRequest method = null;

    if (HttpMethod.POST == (m_config.getMethod())) {
      HttpPost postMethod = new HttpPost(uri);

      // TODO: need to configure these
      List<NameValuePair> postParms = new ArrayList<NameValuePair>();

      // FIXME:do this for now
      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 {
        // I have no idea what I'm doing here ;)
        entity = new StringEntity("XML HERE");
      } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      postMethod.setEntity(entity);

      method = postMethod;
    } else if (HttpMethod.GET == m_config.getMethod()) {

      // TODO: need to configure these
      // List<NameValuePair> getParms = null;
      method = new HttpGet(uri);
    }
    HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion());
    clientWrapper.setVersion(httpVersion);

    HttpResponse response = null;
    try {
      response = clientWrapper.execute(method);

      int code = response.getStatusLine().getStatusCode();
      HttpResponseRange range = new HttpResponseRange("200-399");
      if (!range.contains(code)) {
        LOG.debug(
            "response code out of range for uri:{}.  Expected {} but received {}",
            uri,
            range,
            code);
        throw new NorthbounderException(
            "response code out of range for uri:"
                + uri
                + ".  Expected "
                + range
                + " but received "
                + code);
      }
      LOG.debug(
          "HTTP Northbounder received response: {}", response.getStatusLine().getReasonPhrase());
    } catch (final ClientProtocolException e) {
      throw new NorthbounderException(e);
    } catch (final IOException e) {
      throw new NorthbounderException(e);
    } finally {
      IOUtils.closeQuietly(clientWrapper);
    }
  }