public static InputStream openInputStream(CamelContext camelContext, String uri) throws Exception { if (camelContext != null) { uri = camelContext.resolvePropertyPlaceholders(uri); return ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uri); } return Thread.currentThread().getContextClassLoader().getResourceAsStream(uri); } }
private static InputStream determineKeyRingInputStream(CamelContext context, String filename, byte[] keyRing, boolean forEncryption) throws IOException { if (filename != null && keyRing != null) { String encryptionOrSignature; if (forEncryption) { encryptionOrSignature = "encryption"; } else { encryptionOrSignature = "signature"; } throw new IllegalStateException(String.format("Either specify %s file name or key ring byte array. You can not specify both.", encryptionOrSignature)); } InputStream is; if (keyRing != null) { is = new ByteArrayInputStream(keyRing); } else { is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, filename); } return is; }
/** * Loads the given resource. * * @param uri uri of the resource. * @return the loaded resource * @throws IOException is thrown if resource is not found or cannot be loaded */ protected InputStream loadResource(String uri) throws IOException { return ResourceHelper.resolveMandatoryResourceAsInputStream(currentExchange.get().getContext(), uri); }
public SSLContext createSSLContext(ClassResolver classResolver, String keyStoreFormat, String securityProvider, String keyStoreResource, String trustStoreResource, char[] passphrase) throws Exception { SSLContext answer; KeyStore ks = KeyStore.getInstance(keyStoreFormat); InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, keyStoreResource); try { ks.load(is, passphrase); } finally { IOHelper.close(is); } KeyManagerFactory kmf = KeyManagerFactory.getInstance(securityProvider); kmf.init(ks, passphrase); answer = SSLContext.getInstance(SSL_PROTOCOL); if (trustStoreResource != null) { KeyStore ts = KeyStore.getInstance(keyStoreFormat); is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, trustStoreResource); try { ts.load(is, passphrase); } finally { IOHelper.close(is); } TrustManagerFactory tmf = TrustManagerFactory.getInstance(securityProvider); tmf.init(ts); answer.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); } else { answer.init(kmf.getKeyManagers(), null, null); } return answer; }
public SSLContext createSSLContext(ClassResolver classResolver, String keyStoreFormat, String securityProvider, String keyStoreResource, String trustStoreResource, char[] passphrase) throws Exception { SSLContext answer; KeyStore ks = KeyStore.getInstance(keyStoreFormat); InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, keyStoreResource); try { ks.load(is, passphrase); } finally { IOHelper.close(is); } KeyManagerFactory kmf = KeyManagerFactory.getInstance(securityProvider); kmf.init(ks, passphrase); answer = SSLContext.getInstance(SSL_PROTOCOL); if (trustStoreResource != null) { KeyStore ts = KeyStore.getInstance(keyStoreFormat); is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, trustStoreResource); try { ts.load(is, passphrase); } finally { IOHelper.close(is); } TrustManagerFactory tmf = TrustManagerFactory.getInstance(securityProvider); tmf.init(ts); answer.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); } else { answer.init(kmf.getKeyManagers(), null, null); } return answer; }
/** * Loads a private key from a PKCS#8 file. */ public PrivateKey loadPrivateKey() throws Exception { InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), keyLocation); String str; try { str = getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, is); } finally { IOHelper.close(is); } // replace line feeds from windows to unix style if (!System.lineSeparator().equals("\n")) { str = str.replaceAll(System.lineSeparator(), "\n"); } if (str.contains(BEGIN) && str.contains(END)) { str = str.substring(BEGIN.length(), str.lastIndexOf(END)); } byte[] decoded = Base64.decode(str); KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(new PKCS8EncodedKeySpec(decoded)); }
@Override public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) { log.debug("Trying to find known_hosts file %s", knownHostsResource); InputStream knownHostsInputStream = null; try { knownHostsInputStream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, knownHostsResource); List<String> possibleTokens = getKnownHostsFileTokensForSocketAddress(remoteAddress); log.debug("Trying to mach PublicKey against provided known_hosts file"); PublicKey matchingKey = findKeyForServerToken(knownHostsInputStream, possibleTokens); if (matchingKey != null) { log.debug("Found PublicKey match for server"); boolean match = Arrays.areEqual(matchingKey.getEncoded(), serverKey.getEncoded()); return match; } } catch (IOException ioException) { log.debug(String.format("Could not find known_hosts file %s", knownHostsResource), ioException); } finally { IOHelper.close(knownHostsInputStream); } if (failOnUnknownHost) { log.warn("Could not find matching key for client session, connection will fail due to configuration"); return false; } else { log.warn( "Could not find matching key for client session, connection will continue anyway due to configuration"); return true; } }
/** * Resolve the query by loading the query from the classpath or file resource if needed. */ public static String resolveQuery(CamelContext camelContext, String query, String placeholder) throws NoTypeConversionAvailableException, IOException { String answer = query; if (ResourceHelper.hasScheme(query)) { InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, query); answer = camelContext.getTypeConverter().mandatoryConvertTo(String.class, is); if (placeholder != null) { answer = answer.replaceAll(placeholder, "?"); } } return answer; } }
private Object mandatoryLoadResource(CamelContext context, String resource) { Object instance = null; if (resource.startsWith("classpath:")) { try (InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, resource)) { instance = ModelHelper.loadRoutesDefinition(context, is); } catch (Exception e) { throw new IllegalArgumentException(e); } } else if (resource.startsWith("class:")) { Class<?> type = context.getClassResolver().resolveClass(resource.substring("class:".length())); instance = context.getInjector().newInstance(type); } else if (resource.startsWith("bean:")) { instance = context.getRegistry().lookupByName(resource.substring("bean:".length())); } if (instance == null) { throw new IllegalArgumentException("Unable to resolve resource: " + resource); } return instance; }
/** * Loads a private key from a Java keystore depending on this loader's properties. */ public PrivateKey loadPrivateKey() throws Exception { InputStream input = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), keyStoreLocation); try { return loadPrivateKey(input); } finally { IOHelper.close(input); } }
InputStream reader = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getPropertiesFile()); try { properties.load(reader);
/** * Loads the Swagger definition model from the given path. Tries to resolve * the resource using Camel's resource loading support, if it fails uses * Swagger's resource loading support instead. * * @param uri URI of the specification * @param camelContext context to use * @return the specification * @throws IOException */ static Swagger loadSpecificationFrom(final CamelContext camelContext, final URI uri) throws IOException { final ObjectMapper mapper = Json.mapper(); final SwaggerParser swaggerParser = new SwaggerParser(); final String uriAsString = uri.toString(); try (InputStream stream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uriAsString)) { final JsonNode node = mapper.readTree(stream); return swaggerParser.read(node); } catch (final IOException e) { // try Swaggers loader final Swagger swagger = swaggerParser.read(uriAsString); if (swagger != null) { return swagger; } throw new IllegalArgumentException("The given Swagger specification could not be loaded from `" + uri + "`. Tried loading using Camel's resource resolution and using Swagger's own resource resolution." + " Swagger tends to swallow exceptions while parsing, try specifying Java system property `debugParser`" + " (e.g. `-DdebugParser=true`), the exception that occured when loading using Camel's resource" + " loader follows", e); } }
private synchronized AtlasContextFactory getOrCreateAtlasContextFactory() throws Exception { if (atlasContextFactory == null) { Properties properties = new Properties(); // load the properties from property file which may overrides the default ones if (ObjectHelper.isNotEmpty(getPropertiesFile())) { InputStream reader = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getPropertiesFile()); try { properties.load(reader); log.info("Loaded the Atlas properties file " + getPropertiesFile()); } finally { IOHelper.close(reader, getPropertiesFile(), log); } log.debug("Initializing AtlasContextFactory with properties {}", properties); atlasContextFactory = new DefaultAtlasContextFactory(properties); } else { atlasContextFactory = DefaultAtlasContextFactory.getInstance(); } } return atlasContextFactory; }
@Override protected void doStart() throws Exception { super.doStart(); if (transformerFactory == null) { createTransformerFactory(); } if (rules == null) { try { // Attempt to read the schematron rules from the class path first. LOG.debug("Reading schematron rules from class path {}", path); InputStream schRules = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), path); rules = TemplatesFactory.newInstance().getTemplates(schRules, transformerFactory); } catch (Exception classPathException) { // Attempts from the file system. LOG.debug("Error loading schematron rules from class path, attempting file system {}", path); try { InputStream schRules = FileUtils.openInputStream(new File(path)); rules = TemplatesFactory.newInstance().getTemplates(schRules, transformerFactory); } catch (FileNotFoundException e) { LOG.debug("Schematron rules not found in the file system {}", path); throw classPathException; // Can be more meaningful, for example, xslt compilation error. } } // rules not found in class path nor in file system. if (rules == null) { LOG.error("Failed to load schematron rules {}", path); throw new SchematronConfigException("Failed to load schematron rules: " + path); } } }
protected Properties loadProperties() throws SchedulerException { Properties answer = getProperties(); if (answer == null && getPropertiesFile() != null) { LOG.info("Loading Quartz properties file from: {}", getPropertiesFile()); InputStream is = null; try { is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getPropertiesFile()); answer = new Properties(); answer.load(is); } catch (IOException e) { throw new SchedulerException("Error loading Quartz properties file: " + getPropertiesFile(), e); } finally { IOHelper.close(is); } } return answer; }
private Properties loadProperties() throws SchedulerException { Properties answer = getProperties(); if (answer == null && getPropertiesFile() != null) { LOG.info("Loading Quartz properties file from: {}", getPropertiesFile()); InputStream is = null; try { is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), getPropertiesFile()); answer = new Properties(); answer.load(is); } catch (IOException e) { throw new SchedulerException("Error loading Quartz properties file: " + getPropertiesFile(), e); } finally { IOHelper.close(is); } } return answer; }
@Override protected void doStart() throws Exception { super.doStart(); if (cacheManagerFactory == null) { if (configurationFile != null) { InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), configurationFile); cacheManagerFactory = new DefaultCacheManagerFactory(is, configurationFile); } else { cacheManagerFactory = new DefaultCacheManagerFactory(); } } ServiceHelper.startService(cacheManagerFactory); }
InputStream is = null; try { is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, resource); isr = new InputStreamReader(is); r = new PEMParser(isr);
if (ObjectHelper.isNotEmpty(uri)) { uri = camelContext.resolvePropertyPlaceholders(uri); try (InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, uri)) { Properties properties = new Properties(); properties.load(is);
private void configureApnsCertificate(ApnsServiceBuilder builder) throws IOException, GeneralSecurityException { if (getSslContextParameters() != null) { builder.withSSLContext(getSslContextParameters().createSSLContext(getCamelContext())); return; } ObjectHelper.notNull(getCamelContext(), "camelContext"); StringHelper.notEmpty(getCertificatePath(), "certificatePath"); StringHelper.notEmpty(getCertificatePassword(), "certificatePassword"); InputStream certificateInputStream = null; try { certificateInputStream = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, getCertificatePath()); builder.withCert(certificateInputStream, getCertificatePassword()); } finally { ResourceUtils.close(certificateInputStream); } }