use of org.springframework.core.io.UrlResource in project cas by apereo.
the class JsonGoogleAuthenticatorTokenCredentialRepositoryTests method verifyBadResource.
@Test
public void verifyBadResource() throws Exception {
val repo = new JsonGoogleAuthenticatorTokenCredentialRepository(new UrlResource(new URL("https://httpbin.org/get")), googleAuthenticatorInstance, CipherExecutor.noOpOfStringToString());
assertTrue(repo.get("casuser").isEmpty());
}
use of org.springframework.core.io.UrlResource in project cas by apereo.
the class UrlResourceMetadataResolver method resolve.
@Override
public Collection<? extends MetadataResolver> resolve(final SamlRegisteredService service, final CriteriaSet criteriaSet) {
HttpResponse response = null;
try {
RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(service);
val metadataLocation = getMetadataLocationForService(service, criteriaSet);
LOGGER.info("Loading SAML metadata from [{}]", metadataLocation);
val metadataResource = new UrlResource(metadataLocation);
val backupFile = getMetadataBackupFile(metadataResource, service);
if (backupFile.exists() && samlIdPProperties.getMetadata().getHttp().isForceMetadataRefresh()) {
cleanUpExpiredBackupMetadataFilesFor(metadataResource, service);
}
val canonicalPath = backupFile.getCanonicalPath();
LOGGER.debug("Metadata backup file will be at [{}]", canonicalPath);
FileUtils.forceMkdirParent(backupFile);
response = fetchMetadata(service, metadataLocation, criteriaSet, backupFile);
val status = HttpStatus.valueOf(response.getStatusLine().getStatusCode());
if (shouldHttpResponseStatusBeProcessed(status)) {
val metadataProvider = getMetadataResolverFromResponse(response, backupFile);
configureAndInitializeSingleMetadataResolver(metadataProvider, service);
return CollectionUtils.wrap(metadataProvider);
}
} catch (final UnauthorizedServiceException e) {
LoggingUtils.error(LOGGER, e);
throw new SamlException(e.getMessage(), e);
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
} finally {
HttpUtils.close(response);
}
return new ArrayList<>(0);
}
use of org.springframework.core.io.UrlResource in project cas by apereo.
the class RegisteredServiceThemeResolver method determineThemeNameToChoose.
/**
* Determine theme name to choose.
*
* @param request the request
* @param service the service
* @param rService the r service
* @return the string
*/
protected String determineThemeNameToChoose(final HttpServletRequest request, final Service service, final RegisteredService rService) {
HttpResponse response = null;
try {
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());
val result = ScriptingUtils.executeGroovyScript(resource, new Object[] { service, rService, request.getQueryString(), HttpRequestUtils.getRequestHeaders(request), LOGGER }, String.class, true);
return StringUtils.defaultIfBlank(result, getDefaultThemeName());
}
if (resource instanceof UrlResource) {
val url = resource.getURL().toExternalForm();
LOGGER.debug("Executing URL [{}] to determine theme for [{}]", url, service.getId());
val exec = HttpUtils.HttpExecutionRequest.builder().parameters(CollectionUtils.wrap("service", service.getId())).url(url).method(HttpMethod.GET).build();
response = HttpUtils.execute(exec);
if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
val result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
return StringUtils.defaultIfBlank(result, getDefaultThemeName());
}
}
val messageSource = new CasThemeResourceBundleMessageSource();
val theme = SpringExpressionLanguageValueResolver.getInstance().resolve(rService.getTheme());
messageSource.setBasename(theme);
if (messageSource.doGetBundle(theme, request.getLocale()) != null) {
LOGGER.trace("Found custom theme [{}] for service [{}]", theme, rService);
return theme;
}
LOGGER.warn("Custom theme [{}] for service [{}] cannot be located. Falling back to default theme...", rService.getTheme(), rService);
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
} finally {
HttpUtils.close(response);
}
return getDefaultThemeName();
}
use of org.springframework.core.io.UrlResource in project ignite by apache.
the class CacheJdbcStoreAbstractMultithreadedSelfTest method beforeTestsStarted.
/**
* {@inheritDoc}
*/
@Override
protected void beforeTestsStarted() throws Exception {
store = store();
URL cfgUrl;
try {
cfgUrl = new URL(DFLT_MAPPING_CONFIG);
} catch (MalformedURLException ignore) {
cfgUrl = U.resolveIgniteUrl(DFLT_MAPPING_CONFIG);
}
if (cfgUrl == null)
throw new Exception("Failed to resolve metadata path: " + DFLT_MAPPING_CONFIG);
try {
GenericApplicationContext springCtx = new GenericApplicationContext();
new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(cfgUrl));
springCtx.refresh();
Collection<JdbcType> types = new ArrayList<>(springCtx.getBeansOfType(JdbcType.class).values());
store.setTypes(types.toArray(new JdbcType[types.size()]));
} catch (BeansException e) {
if (X.hasCause(e, ClassNotFoundException.class))
throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) " + "[springUrl=" + cfgUrl + ']', e);
else
throw new IgniteCheckedException("Failed to instantiate Spring XML application context [springUrl=" + cfgUrl + ", err=" + e.getMessage() + ']', e);
}
}
use of org.springframework.core.io.UrlResource in project ignite by apache.
the class IgniteThinClient method loadConfiguration.
/**
* @param springCfgPath Spring configuration file path.
* @return Tuple with grid configuration and Spring application context.
* @throws Exception If failed.
*/
private static IgniteBiTuple<IgniteConfiguration, ? extends ApplicationContext> loadConfiguration(String springCfgPath) throws Exception {
URL url;
try {
url = new URL(springCfgPath);
} catch (MalformedURLException e) {
url = IgniteUtils.resolveIgniteUrl(springCfgPath);
if (url == null) {
throw new IgniteCheckedException("Spring XML configuration path is invalid: " + springCfgPath + ". Note that this path should be either absolute or a relative local file system path, " + "relative to META-INF in classpath or valid URL to IGNITE_HOME.", e);
}
}
GenericApplicationContext springCtx;
try {
springCtx = new GenericApplicationContext();
new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));
springCtx.refresh();
} catch (BeansException e) {
throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e);
}
Map<String, IgniteConfiguration> cfgMap;
try {
cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class);
} catch (BeansException e) {
throw new Exception("Failed to instantiate bean [type=" + IgniteConfiguration.class + ", err=" + e.getMessage() + ']', e);
}
if (cfgMap == null || cfgMap.isEmpty())
throw new Exception("Failed to find ignite configuration in: " + url);
return new IgniteBiTuple<>(cfgMap.values().iterator().next(), springCtx);
}
Aggregations