@Override public DataAndUrl get(List<String> urlStrings) { for (String url : urlStrings) { byte[] data = get(url); if (data != null) { return new DataAndUrl(data, url); } } return null; }
@Override public void setContentType(String contentType) { dataLoader.setContentType(contentType); } }
@Override public byte[] get(String url) { return get(url, false); }
@Override public byte[] get(final String urlString) { 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 FILE, HTTP, HTTPS, FTP and LDAP Urls."); } return httpGet(urlString); }
@Test public void testFileGet() { NativeHTTPDataLoader dataLoader = new NativeHTTPDataLoader(); byte[] bytesArray = dataLoader.get(FILE_URL_TO_LOAD); CertificateToken certificate = DSSUtils.loadCertificate(bytesArray); assertNotNull(certificate); }
@Test(expected=DSSException.class) public void testGetBiggerThanMaxSize() { NativeHTTPDataLoader dataLoader = new NativeHTTPDataLoader(); dataLoader.setMaxInputSize(1); dataLoader.get(FILE_URL_TO_LOAD); }
@Test(expected=DSSException.class) public void testGetTimeout() { NativeHTTPDataLoader dataLoader = new NativeHTTPDataLoader(); dataLoader.setTimeout(1); dataLoader.get(HTTP_URL_TO_LOAD); } }
private DataLoader createDataLoader() { if (Protocol.isHttpUrl(this.configuration.getTslLocation())) { CachingDataLoader dataLoader = new CachingDataLoader(this.configuration); dataLoader.setTimeoutConnection(this.configuration.getConnectionTimeout()); dataLoader.setTimeoutSocket(this.configuration.getSocketTimeout()); dataLoader.setCacheExpirationTime(this.configuration.getTslCacheExpirationTime()); dataLoader.setFileCacheDirectory(this.fileCacheDirectory); logger.debug("Using file cache directory for storing TSL: {}", this.fileCacheDirectory); return dataLoader; } else { return new CommonsDataLoader(); } }
@Override public DataAndUrl get(List<String> urlStrings) { for (final String urlString : urlStrings) { try { final byte[] bytes = get(urlString); if (bytes != null) { return new DataAndUrl(bytes, urlString); } } catch (Exception e) { LOGGER.warn("Impossible to obtain data using {}", urlString, e); } } throw new DSSException(String.format("Impossible to obtain data using with given urls %s", urlStrings)); }
@Test public void testWithNativeHTTPDataLoader() { OnlineTSPSource tspSource = new OnlineTSPSource(TSA_URL); tspSource.setDataLoader(new NativeHTTPDataLoader()); byte[] digest = DSSUtils.digest(DigestAlgorithm.SHA1, "Hello world".getBytes()); TimeStampToken timeStampResponse = tspSource.getTimeStampResponse(DigestAlgorithm.SHA1, digest); assertNotNull(timeStampResponse); }
/** * This method try to retrieve the protocol indicated by the given URL string. * * @param urlString to be analysed * @return found {@code Protocol} or {@code null} */ public static Protocol getFrom(final String urlString) { if (HTTP.isTheSame(urlString)) { return HTTP; } else if (HTTPS.isTheSame(urlString)) { return HTTPS; } else if (LDAP.isTheSame(urlString)) { return LDAP; } else if (FTP.isTheSame(urlString)) { return FTP; } else if (FILE.isTheSame(urlString)) { return FILE; } return null; }
/** * Download a CRL from any location with any protocol. * * @param downloadUrls * the {@code List} of urls to be used to obtain the revocation data through the CRL canal. * @return {@code X509CRL} or null if it was not possible to download the CRL */ private DataLoader.DataAndUrl downloadCrl(final List<String> downloadUrls) { try { return dataLoader.get(downloadUrls); } catch (DSSException e) { LOG.warn("Unable to download CRL from URLs {}", downloadUrls, e); return null; } }
public byte[] call() { OutputStream out = null; InputStream inputStream = null; byte[] result = null; try { URLConnection connection = createConnection(); connection.setUseCaches(useCaches); connection.setDoInput(true); if (content != null) { connection.setDoOutput(true); out = connection.getOutputStream(); Utils.write(content, out); } inputStream = connection.getInputStream(); result = Utils.toByteArray(maxInputSize > 0? new MaxSizeInputStream(inputStream, maxInputSize, url): inputStream); } catch (IOException e) { throw new DSSException(String.format(ERROR_MESSAGE, url, e.getMessage()), e); } finally { Utils.closeQuietly(out); Utils.closeQuietly(inputStream); } return result; }
@Override public byte[] get(String url, boolean refresh) { return request(url, HttpMethod.GET, null, !refresh); }
@Override public byte[] post(String url, byte[] content) { return get(url); }
@Test public void testHttpGet() { NativeHTTPDataLoader dataLoader = new NativeHTTPDataLoader(); byte[] bytesArray = dataLoader.get(HTTP_URL_TO_LOAD); CertificateToken certificate = DSSUtils.loadCertificate(bytesArray); assertNotNull(certificate); }
/** * if {@code preferredProtocol} is set then the list of urls is prioritize. * NOTE: This is not standard conformant! However in the major number of cases LDAP is much slower then HTTP! * * @param urls * {@code List} of urls to prioritize */ private void prioritize(final List<String> urls) { if (preferredProtocol != null) { final List<String> priorityUrls = new ArrayList<String>(); for (final String url : urls) { if (preferredProtocol.isTheSame(url)) { priorityUrls.add(url); } } urls.removeAll(priorityUrls); for (int ii = priorityUrls.size() - 1; ii >= 0; ii--) { urls.add(0, priorityUrls.get(ii)); } } }
@Override public byte[] post(String url, byte[] content) { return request(url, HttpMethod.POST, content, false); }
@Override public byte[] get(String url, boolean refresh) { return get(url); }