Esempio n. 1
0
  PwmHttpClientResponse makeApiRequest(
      final HttpMethod method, final String urlPart, final Serializable body)
      throws PwmUnrecoverableException {
    final Map<String, String> headers = new HashMap<>();
    headers.put(PwmConstants.HttpHeader.Content_Type.getHttpName(), "application/json");
    if (locale != null) {
      headers.put(PwmConstants.HttpHeader.Accept_Language.getHttpName(), locale.toLanguageTag());
    }

    final PwmHttpClientRequest pwmHttpClientRequest =
        new PwmHttpClientRequest(
            method, getEndpointURL() + urlPart, JsonUtil.serialize(body), headers);
    return pwmHttpClient.makeRequest(pwmHttpClientRequest);
  }
Esempio n. 2
0
 public static String makeOutboundRestWSCall(
     final PwmApplication pwmApplication,
     final Locale locale,
     final String url,
     final String jsonRequestBody)
     throws PwmOperationalException, PwmUnrecoverableException {
   final HttpPost httpPost = new HttpPost(url);
   httpPost.setHeader("Accept", PwmConstants.AcceptValue.json.getHeaderValue());
   if (locale != null) {
     httpPost.setHeader("Accept-Locale", locale.toString());
   }
   httpPost.setHeader("Content-Type", PwmConstants.ContentTypeValue.json.getHeaderValue());
   final HttpResponse httpResponse;
   try {
     final StringEntity stringEntity = new StringEntity(jsonRequestBody);
     stringEntity.setContentType(PwmConstants.AcceptValue.json.getHeaderValue());
     httpPost.setEntity(stringEntity);
     LOGGER.debug(
         "beginning external rest call to: " + httpPost.toString() + ", body: " + jsonRequestBody);
     httpResponse = PwmHttpClient.getHttpClient(pwmApplication.getConfig()).execute(httpPost);
     final String responseBody = EntityUtils.toString(httpResponse.getEntity());
     LOGGER.trace(
         "external rest call returned: "
             + httpResponse.getStatusLine().toString()
             + ", body: "
             + responseBody);
     if (httpResponse.getStatusLine().getStatusCode() != 200) {
       final String errorMsg =
           "received non-200 response code ("
               + httpResponse.getStatusLine().getStatusCode()
               + ") when executing web-service";
       LOGGER.error(errorMsg);
       throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg));
     }
     return responseBody;
   } catch (IOException e) {
     final String errorMsg =
         "http response error while executing external rest call, error: " + e.getMessage();
     LOGGER.error(errorMsg);
     throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg), e);
   }
 }
Esempio n. 3
0
  private void publishStatisticsToCloud()
      throws URISyntaxException, IOException, PwmUnrecoverableException {
    final StatsPublishBean statsPublishData;
    {
      final StatisticsBundle bundle = getStatBundleForKey(KEY_CUMULATIVE);
      final Map<String, String> statData = new HashMap<>();
      for (final Statistic loopStat : Statistic.values()) {
        statData.put(loopStat.getKey(), bundle.getStatistic(loopStat));
      }
      final Configuration config = pwmApplication.getConfig();
      final List<String> configuredSettings = new ArrayList<>();
      for (final PwmSetting pwmSetting : config.nonDefaultSettings()) {
        if (!pwmSetting.getCategory().hasProfiles() && !config.isDefaultValue(pwmSetting)) {
          configuredSettings.add(pwmSetting.getKey());
        }
      }
      final Map<String, String> otherData = new HashMap<>();
      otherData.put(
          StatsPublishBean.KEYS.SITE_URL.toString(),
          config.readSettingAsString(PwmSetting.PWM_SITE_URL));
      otherData.put(
          StatsPublishBean.KEYS.SITE_DESCRIPTION.toString(),
          config.readSettingAsString(PwmSetting.PUBLISH_STATS_SITE_DESCRIPTION));
      otherData.put(
          StatsPublishBean.KEYS.INSTALL_DATE.toString(),
          PwmConstants.DEFAULT_DATETIME_FORMAT.format(pwmApplication.getInstallTime()));

      try {
        otherData.put(
            StatsPublishBean.KEYS.LDAP_VENDOR.toString(),
            pwmApplication
                .getProxyChaiProvider(config.getDefaultLdapProfile().getIdentifier())
                .getDirectoryVendor()
                .toString());
      } catch (Exception e) {
        LOGGER.trace("unable to read ldap vendor type for stats publication: " + e.getMessage());
      }

      statsPublishData =
          new StatsPublishBean(
              pwmApplication.getInstanceID(),
              new Date(),
              statData,
              configuredSettings,
              PwmConstants.BUILD_NUMBER,
              PwmConstants.BUILD_VERSION,
              otherData);
    }
    final URI requestURI = new URI(PwmConstants.PWM_URL_CLOUD + "/rest/pwm/statistics");
    final HttpPost httpPost = new HttpPost(requestURI.toString());
    final String jsonDataString = JsonUtil.serialize(statsPublishData);
    httpPost.setEntity(new StringEntity(jsonDataString));
    httpPost.setHeader("Accept", PwmConstants.AcceptValue.json.getHeaderValue());
    httpPost.setHeader("Content-Type", PwmConstants.ContentTypeValue.json.getHeaderValue());
    LOGGER.debug(
        "preparing to send anonymous statistics to "
            + requestURI.toString()
            + ", data to send: "
            + jsonDataString);
    final HttpResponse httpResponse =
        PwmHttpClient.getHttpClient(pwmApplication.getConfig()).execute(httpPost);
    if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
      throw new IOException(
          "http response error code: " + httpResponse.getStatusLine().getStatusCode());
    }
    LOGGER.info("published anonymous statistics to " + requestURI.toString());
    try {
      localDB.put(
          LocalDB.DB.PWM_STATS,
          KEY_CLOUD_PUBLISH_TIMESTAMP,
          String.valueOf(System.currentTimeMillis()));
    } catch (LocalDBException e) {
      LOGGER.error(
          "unexpected error trying to save last statistics published time to LocalDB: "
              + e.getMessage());
    }
  }