/** * Does resource exist? * * @param resource the resource * @param resourceLoader the resource loader * @return the boolean */ public static boolean doesResourceExist(final String resource, final ResourceLoader resourceLoader) { try { if (StringUtils.isNotBlank(resource)) { val res = resourceLoader.getResource(resource); return doesResourceExist(res); } } catch (final Exception e) { LOGGER.warn(e.getMessage(), e); } return false; }
/** * Does resource exist? * * @param location the resource * @return the boolean */ public static boolean doesResourceExist(final String location) { try { return getResourceFrom(location) != null; } catch (final Exception e) { LOGGER.trace(e.getMessage(), e); } return false; }
/** * Gets resource from a String location. * * @param location the metadata location * @return the resource from * @throws IOException the exception */ public static AbstractResource getResourceFrom(final String location) throws IOException { val metadataLocationResource = getRawResourceFrom(location); if (!metadataLocationResource.exists() || !metadataLocationResource.isReadable()) { throw new FileNotFoundException("Resource " + location + " does not exist or is unreadable"); } return metadataLocationResource; }
/** * Prepare classpath resource if needed file. * * @param resource the resource * @return the file */ public static Resource prepareClasspathResourceIfNeeded(final Resource resource) { if (resource == null) { LOGGER.debug("No resource defined to prepare. Returning null"); return null; } return prepareClasspathResourceIfNeeded(resource, false, resource.getFilename()); }
public AbstractResourceBasedServiceRegistry(final Resource configDirectory, final Collection<StringSerializer<RegisteredService>> serializers, final boolean enableWatcher, final ApplicationEventPublisher eventPublisher, final RegisteredServiceReplicationStrategy registeredServiceReplicationStrategy, final RegisteredServiceResourceNamingStrategy resourceNamingStrategy) throws Exception { super(eventPublisher); LOGGER.trace("Provided service registry directory is specified at [{}]", configDirectory); val pattern = String.join("|", getExtensions()); val servicesDirectory = ResourceUtils.prepareClasspathResourceIfNeeded(configDirectory, true, pattern); if (servicesDirectory == null) { throw new IllegalArgumentException("Could not determine the services configuration directory from " + configDirectory); } val file = servicesDirectory.getFile(); LOGGER.trace("Prepared service registry directory is specified at [{}]", file); initializeRegistry(Paths.get(file.getCanonicalPath()), serializers, enableWatcher, registeredServiceReplicationStrategy, resourceNamingStrategy); }
@SneakyThrows private static Map<String, String> getDevicesFromJsonResource(final Resource jsonResource) { if (!ResourceUtils.doesResourceExist(jsonResource)) { val res = jsonResource.getFile().createNewFile(); if (res) { LOGGER.debug("Created JSON resource @ [{}]", jsonResource); } } if (ResourceUtils.doesResourceExist(jsonResource)) { val file = jsonResource.getFile(); if (file.canRead() && file.length() > 0) { return MAPPER.readValue(file, Map.class); } } else { LOGGER.warn("JSON resource @ [{}] does not exist", jsonResource); } return new HashMap<>(0); }
@SneakyThrows private void buildGroovyAccessStrategyInstanceIfNeeded() { if (this.groovyStrategyInstance == null) { val groovyResource = ResourceUtils.getResourceFrom(this.groovyScript); this.groovyStrategyInstance = ScriptingUtils.getObjectInstanceFromGroovyResource(groovyResource, RegisteredServiceAccessStrategy.class); } } }
LOGGER.debug("Service [{}] is configured to use a custom theme [{}]", rService, rService.getTheme()); val resource = ResourceUtils.getRawResourceFrom(rService.getTheme()); if (resource instanceof FileSystemResource && resource.exists()) { LOGGER.debug("Executing groovy script to determine theme for [{}]", service.getId());
@SneakyThrows private void readResourceForInterrupts() { this.interrupts = new LinkedHashMap<>(); if (ResourceUtils.doesResourceExist(resource)) { try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { final TypeReference<Map<String, InterruptResponse>> personList = new TypeReference<>() { }; this.interrupts = MAPPER.readValue(JsonValue.readHjson(reader).toString(), personList); } } } }
@Override public boolean supports(final SamlRegisteredService service) { try { val metadataLocation = service.getMetadataLocation(); val metadataResource = ResourceUtils.getResourceFrom(metadataLocation); return metadataResource instanceof ClassPathResource; } catch (final Exception e) { LOGGER.trace(e.getMessage(), e); } return false; }
@SneakyThrows private Set<ConsentDecision> readDecisionsFromJsonResource() { if (ResourceUtils.doesResourceExist(jsonResource)) { try (Reader reader = new InputStreamReader(jsonResource.getInputStream(), StandardCharsets.UTF_8)) { final TypeReference<Set<ConsentDecision>> personList = new TypeReference<>() { }; return MAPPER.readValue(JsonValue.readHjson(reader).toString(), personList); } } return new LinkedHashSet<>(0); }
@SneakyThrows private void buildGroovyMultifactorPolicyInstanceIfNeeded() { if (this.groovyPolicyInstance == null) { val groovyResource = ResourceUtils.getResourceFrom(this.groovyScript); this.groovyPolicyInstance = ScriptingUtils.getObjectInstanceFromGroovyResource(groovyResource, RegisteredServiceMultifactorPolicy.class); } }
@SneakyThrows private void readTrustedRecordsFromResource() { this.storage = new LinkedHashMap<>(); if (ResourceUtils.doesResourceExist(location)) { try (Reader reader = new InputStreamReader(location.getInputStream(), StandardCharsets.UTF_8)) { val personList = new TypeReference<Map<String, MultifactorAuthenticationTrustRecord>>() { }; this.storage = MAPPER.readValue(JsonValue.readHjson(reader).toString(), personList); } } }
@Override public boolean supports(final SamlRegisteredService service) { try { val metadataLocation = service.getMetadataLocation(); val metadataResource = ResourceUtils.getResourceFrom(metadataLocation); return metadataResource instanceof FileSystemResource; } catch (final Exception e) { LOGGER.trace(e.getMessage(), e); } return false; }
public CasSpringSecurityAuthorizationGenerator(final Resource usersFile) { val properties = new Properties(); try { if (ResourceUtils.doesResourceExist(usersFile)) { properties.load(usersFile.getInputStream()); } } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } this.generator = new SpringSecurityPropertiesAuthorizationGenerator(properties); watchResource(usersFile); }
@Override public Map<String, Object> getAttributesInternal(final Principal principal, final Map<String, Object> attributes, final RegisteredService service) { try { final Object[] args = {attributes, LOGGER, principal, service}; val resource = ResourceUtils.getResourceFrom(this.groovyScript); return ScriptingUtils.executeGroovyScript(resource, args, Map.class, true); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } LOGGER.warn("Groovy script [{}] does not exist or cannot be loaded", groovyScript); return new HashMap<>(0); } }
@SneakyThrows public UmaRequestingPartyTokenSigningService(final Resource jwksFile, final String issuer) { super(issuer); if (ResourceUtils.doesResourceExist(jwksFile)) { val json = IOUtils.toString(jwksFile.getInputStream(), StandardCharsets.UTF_8); val jsonWebKeySet = new JsonWebKeySet(json); val keys = jsonWebKeySet.getJsonWebKeys(); if (keys.isEmpty()) { throw new IllegalArgumentException("No JSON web keys are found in the JWKS keystore " + jwksFile); } this.jsonWebKey = RsaJsonWebKey.class.cast(jsonWebKeySet.getJsonWebKeys().get(0)); } else { LOGGER.warn("JWKS file for UMA RPT tokens is undefined or cannot be located. Tokens will not be signed"); this.jsonWebKey = null; } }
@Override protected Map<String, Object> getAttributesForSamlRegisteredService(final Map<String, Object> attributes, final SamlRegisteredService service, final ApplicationContext applicationContext, final SamlRegisteredServiceCachingMetadataResolver resolver, final SamlRegisteredServiceServiceProviderMetadataFacade facade, final EntityDescriptor entityDescriptor) { try { final Object[] args = {attributes, service, resolver, facade, entityDescriptor, applicationContext, LOGGER}; val resource = ResourceUtils.getResourceFrom(this.groovyScript); return ScriptingUtils.executeGroovyScript(resource, args, Map.class, true); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } LOGGER.warn("Groovy script [{}] does not exist or cannot be loaded", groovyScript); return new HashMap<>(0); } }
@Override public boolean isAvailable(final SamlRegisteredService service) { if (supports(service)) { val metadataLocation = service.getMetadataLocation(); return ResourceUtils.doesResourceExist(metadataLocation); } return false; } }
private String resolveUsernameFromExternalGroovyScript(final Principal principal, final Service service, final String scriptFile) { try { LOGGER.debug("Found groovy script to execute"); val resourceFrom = ResourceUtils.getResourceFrom(scriptFile); val script = IOUtils.toString(resourceFrom.getInputStream(), StandardCharsets.UTF_8); val result = getGroovyAttributeValue(principal, script); if (result != null) { LOGGER.debug("Found username [{}] from script [{}]", result, scriptFile); return result.toString(); } } catch (final IOException e) { LOGGER.error(e.getMessage(), e); } LOGGER.warn("Groovy script [{}] returned no value for username attribute. Fallback to default [{}]", this.groovyScript, principal.getId()); return principal.getId(); }