Search in sources :

Example 86 with UrlResource

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());
}
Also used : lombok.val(lombok.val) UrlResource(org.springframework.core.io.UrlResource) URL(java.net.URL) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 87 with UrlResource

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);
}
Also used : lombok.val(lombok.val) UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) SamlException(org.apereo.cas.support.saml.SamlException) SamlException(org.apereo.cas.support.saml.SamlException) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) IOException(java.io.IOException)

Example 88 with UrlResource

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();
}
Also used : lombok.val(lombok.val) UrlResource(org.springframework.core.io.UrlResource) HttpResponse(org.apache.http.HttpResponse) FileSystemResource(org.springframework.core.io.FileSystemResource)

Example 89 with UrlResource

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UrlResource(org.springframework.core.io.UrlResource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ArrayList(java.util.ArrayList) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BeansException(org.springframework.beans.BeansException) BeansException(org.springframework.beans.BeansException)

Example 90 with UrlResource

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);
}
Also used : MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) UrlResource(org.springframework.core.io.UrlResource) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BeansException(org.springframework.beans.BeansException) BeansException(org.springframework.beans.BeansException)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)100 Resource (org.springframework.core.io.Resource)41 URL (java.net.URL)33 Test (org.junit.jupiter.api.Test)30 Test (org.junit.Test)24 ClassPathResource (org.springframework.core.io.ClassPathResource)19 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 FileSystemResource (org.springframework.core.io.FileSystemResource)13 MalformedURLException (java.net.MalformedURLException)11 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 lombok.val (lombok.val)9 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)9 GenericBean (org.springframework.beans.testfixture.beans.GenericBean)9 File (java.io.File)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)8 InputStream (java.io.InputStream)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 Properties (java.util.Properties)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5