Ejemplo n.º 1
0
  /**
   * Configure the proxy with the required credential if needed
   *
   * @param httpClientBuilder
   * @param credsProvider
   * @param url
   * @return
   * @throws java.net.MalformedURLException
   */
  private HttpClientBuilder configureProxy(
      HttpClientBuilder httpClientBuilder, CredentialsProvider credsProvider, String url)
      throws DSSException {

    try {

      if (proxyPreferenceManager == null) {
        return httpClientBuilder;
      }
      final String protocol = new URL(url).getProtocol();

      final boolean proxyHTTPS =
          Protocol.isHttps(protocol) && proxyPreferenceManager.isHttpsEnabled();
      final boolean proxyHTTP = Protocol.isHttp(protocol) && proxyPreferenceManager.isHttpEnabled();

      if (!proxyHTTPS && !proxyHTTP) {
        return httpClientBuilder;
      }

      String proxyHost = null;
      int proxyPort = 0;
      String proxyUser = null;
      String proxyPassword = null;

      if (proxyHTTPS) {
        LOG.debug("Use proxy https parameters");
        final Long port = proxyPreferenceManager.getHttpsPort();
        proxyPort = port != null ? port.intValue() : 0;
        proxyHost = proxyPreferenceManager.getHttpsHost();
        proxyUser = proxyPreferenceManager.getHttpsUser();
        proxyPassword = proxyPreferenceManager.getHttpsPassword();
      } else // noinspection ConstantConditions
      if (proxyHTTP) {
        LOG.debug("Use proxy http parameters");
        final Long port = proxyPreferenceManager.getHttpPort();
        proxyPort = port != null ? port.intValue() : 0;
        proxyHost = proxyPreferenceManager.getHttpHost();
        proxyUser = proxyPreferenceManager.getHttpUser();
        proxyPassword = proxyPreferenceManager.getHttpPassword();
      }

      if (DSSUtils.isNotEmpty(proxyUser) && DSSUtils.isNotEmpty(proxyPassword)) {
        LOG.debug("proxy user: "******":" + proxyPassword);
        AuthScope proxyAuth = new AuthScope(proxyHost, proxyPort);
        UsernamePasswordCredentials proxyCredentials =
            new UsernamePasswordCredentials(proxyUser, proxyPassword);
        credsProvider.setCredentials(proxyAuth, proxyCredentials);
      }

      LOG.debug("proxy host/port: " + proxyHost + ":" + proxyPort);
      // TODO SSL peer shut down incorrectly when protocol is https
      HttpHost proxy = new HttpHost(proxyHost, proxyPort, Protocol.HTTP.getName());
      return httpClientBuilder.setProxy(proxy);
    } catch (MalformedURLException e) {
      throw new DSSException(e);
    }
  }
Ejemplo n.º 2
0
  /**
   * Loads TSL certificates If configuration mode is TEST then TSL signature is not checked.
   *
   * @return TSL source
   */
  public TSLCertificateSource getTSL() {
    logger.debug("");
    if (tslCertificateSource != null) {
      logger.debug("Using TSL cached copy");
      return tslCertificateSource;
    }

    tslCertificateSource = new TSLCertificateSource();
    tslCertificateSource.setTslRefreshPolicy(TSLRefreshPolicy.WHEN_NECESSARY);

    String tslLocation = getTslLocation();
    if (Protocol.isHttpUrl(tslLocation)) {
      FileCacheDataLoader dataLoader = new FileCacheDataLoader();
      dataLoader.setConnectTimeout(getConnectionTimeout());
      dataLoader.setFileCacheDirectory(TSLCertificateSource.fileCacheDirectory);
      tslCertificateSource.setTslRefreshPolicy(TSLRefreshPolicy.NEVER);
      tslCertificateSource.setDataLoader(dataLoader);
    } else {
      tslCertificateSource.setDataLoader(new CommonsDataLoader());
    }

    tslCertificateSource.setLotlUrl(tslLocation);

    tslCertificateSource.setCheckSignature(false);

    try {
      tslCertificateSource.init();
    } catch (DSSException e) {
      logger.error(e.getMessage());
      throw new DigiDoc4JException(e.getMessage());
    }

    return tslCertificateSource;
  }
Ejemplo n.º 3
0
  @Override
  public byte[] get(final String urlString) throws DSSCannotFetchDataException {

    if (Protocol.isFileUrl(urlString)) {
      return fileGet(urlString);
    } else if (Protocol.isHttpUrl(urlString)) {
      return httpGet(urlString);
    } else if (Protocol.isFtpUrl(urlString)) {
      return ftpGet(urlString);
    } else if (Protocol.isLdapUrl(urlString)) {
      return ldapGet(urlString);
    } else {
      LOG.warn("DSS framework only supports HTTP, HTTPS, FTP and LDAP CRL's urlString.");
    }

    return httpGet(urlString);
  }
Ejemplo n.º 4
0
 String getTslLocation() {
   logger.debug("");
   String urlString = getConfigurationParameter("tslLocation");
   if (!Protocol.isFileUrl(urlString)) return urlString;
   try {
     String filePath = new URL(urlString).getPath();
     if (!new File(filePath).exists()) {
       URL resource = getClass().getClassLoader().getResource(filePath);
       if (resource != null) urlString = resource.toString();
     }
   } catch (MalformedURLException e) {
     logger.warn(e.getMessage());
   }
   return urlString;
 }