Search in sources :

Example 6 with PropertyResolver

use of org.springframework.core.env.PropertyResolver in project spring-boot by spring-projects.

the class GroovyTemplateAvailabilityProvider method isTemplateAvailable.

@Override
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) {
    if (ClassUtils.isPresent("groovy.text.TemplateEngine", classLoader)) {
        PropertyResolver resolver = new RelaxedPropertyResolver(environment, "spring.groovy.template.");
        String loaderPath = resolver.getProperty("resource-loader-path", GroovyTemplateProperties.DEFAULT_RESOURCE_LOADER_PATH);
        String prefix = resolver.getProperty("prefix", GroovyTemplateProperties.DEFAULT_PREFIX);
        String suffix = resolver.getProperty("suffix", GroovyTemplateProperties.DEFAULT_SUFFIX);
        return resourceLoader.getResource(loaderPath + prefix + view + suffix).exists();
    }
    return false;
}
Also used : RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) PropertyResolver(org.springframework.core.env.PropertyResolver)

Example 7 with PropertyResolver

use of org.springframework.core.env.PropertyResolver in project uPortal by Jasig.

the class BasicAuthInterceptorTest method testInterceptorWithAuthCode.

@Test
public void testInterceptorWithAuthCode() throws Exception {
    final String id = "test";
    final String authCode = "c29tZUxvbmdVc2VybmFtZTpzb21lTG9uZ1Bhc3N3b3Jk";
    PropertyResolver resolver = mock(PropertyResolver.class);
    when(resolver.getProperty(eq("org.jasig.rest.interceptor.basic-auth." + id + ".authCode"))).thenReturn(authCode);
    doInterceptorTest(resolver, id, authCode);
}
Also used : PropertyResolver(org.springframework.core.env.PropertyResolver) Test(org.junit.Test)

Example 8 with PropertyResolver

use of org.springframework.core.env.PropertyResolver in project uPortal by Jasig.

the class ZeroLeggedOAuthInterceptorTest method testInterceptor.

@Test
public void testInterceptor() throws Exception {
    final String url = "http://www.test.com/lrs?param1=val1&param2=val2";
    final String data = "test";
    final String id = "test";
    final String realm = "realm";
    final String consumerKey = "consumerKey";
    final String secretKey = "secretKey";
    PropertyResolver resolver = mock(PropertyResolver.class);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".realm"))).thenReturn(realm);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".consumerKey"))).thenReturn(consumerKey);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".secretKey"))).thenReturn(secretKey);
    // holder for the headers...
    HttpHeaders headers = new HttpHeaders();
    // Mock guts of RestTemplate so no need to actually hit the web...
    ClientHttpResponse resp = mock(ClientHttpResponse.class);
    when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(resp.getHeaders()).thenReturn(new HttpHeaders());
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ClientHttpRequest client = mock(ClientHttpRequest.class);
    when(client.getHeaders()).thenReturn(headers);
    when(client.getBody()).thenReturn(buffer);
    when(client.execute()).thenReturn(resp);
    ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class);
    when(factory.createRequest(Matchers.any(URI.class), Matchers.any(HttpMethod.class))).thenReturn(client);
    // add the new interceptor...
    ZeroLeggedOAuthInterceptor interceptor = new ZeroLeggedOAuthInterceptor();
    interceptor.setPropertyResolver(resolver);
    interceptor.setId(id);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add(interceptor);
    RestTemplate rest = new RestTemplate(factory);
    rest.setInterceptors(interceptors);
    rest.postForLocation(url, data, Collections.emptyMap());
    // make sure auth header is correctly set...
    assertThat(headers, hasKey(Headers.Authorization.name()));
    String authHeader = headers.get(Headers.Authorization.name()).get(0);
    assertThat(authHeader, containsString("OAuth realm=\"" + realm + "\""));
    assertThat(authHeader, containsString("oauth_consumer_key=\"" + consumerKey + "\""));
    // for now, only supports HMAC-SHA1.  May have to fix later...
    assertThat(authHeader, containsString("oauth_signature_method=\"HMAC-SHA1\""));
    assertThat(authHeader, containsString("oauth_version=\"1.0\""));
    assertThat(authHeader, containsString("oauth_timestamp="));
    assertThat(authHeader, containsString("oauth_nonce="));
    assertThat(authHeader, containsString("oauth_signature="));
    // oauth lib will create 2 oauth_signature params if you call sign
    // multiple times.  Make sure only get 1.
    assertThat(StringUtils.countMatches(authHeader, "oauth_signature="), is(1));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PropertyResolver(org.springframework.core.env.PropertyResolver) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 9 with PropertyResolver

use of org.springframework.core.env.PropertyResolver in project spring-boot by spring-projects.

the class RelaxedPropertyResolver method ignoringUnresolvableNestedPlaceholders.

/**
	 * Return a property resolver for the environment, preferring one that ignores
	 * unresolvable nested placeholders.
	 * @param environment the source environment
	 * @param prefix the prefix
	 * @return a property resolver for the environment
	 * @since 1.4.3
	 */
public static RelaxedPropertyResolver ignoringUnresolvableNestedPlaceholders(Environment environment, String prefix) {
    Assert.notNull(environment, "Environment must not be null");
    PropertyResolver resolver = environment;
    if (environment instanceof ConfigurableEnvironment) {
        resolver = new PropertySourcesPropertyResolver(((ConfigurableEnvironment) environment).getPropertySources());
        ((PropertySourcesPropertyResolver) resolver).setIgnoreUnresolvableNestedPlaceholders(true);
    }
    return new RelaxedPropertyResolver(resolver, prefix);
}
Also used : PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) PropertyResolver(org.springframework.core.env.PropertyResolver) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver)

Example 10 with PropertyResolver

use of org.springframework.core.env.PropertyResolver in project spring-boot by spring-projects.

the class EnvironmentEndpoint method invoke.

@Override
public Map<String, Object> invoke() {
    Map<String, Object> result = new LinkedHashMap<>();
    result.put("profiles", getEnvironment().getActiveProfiles());
    PropertyResolver resolver = getResolver();
    for (Entry<String, PropertySource<?>> entry : getPropertySourcesAsMap().entrySet()) {
        PropertySource<?> source = entry.getValue();
        String sourceName = entry.getKey();
        if (source instanceof EnumerablePropertySource) {
            EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
            Map<String, Object> properties = new LinkedHashMap<>();
            for (String name : enumerable.getPropertyNames()) {
                properties.put(name, sanitize(name, resolver.getProperty(name)));
            }
            properties = postProcessSourceProperties(sourceName, properties);
            if (properties != null) {
                result.put(sourceName, properties);
            }
        }
    }
    return result;
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) PropertyResolver(org.springframework.core.env.PropertyResolver) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) LinkedHashMap(java.util.LinkedHashMap) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Aggregations

PropertyResolver (org.springframework.core.env.PropertyResolver)15 Test (org.junit.Test)7 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)7 RelaxedPropertyResolver (org.springframework.boot.bind.RelaxedPropertyResolver)5 Properties (java.util.Properties)3 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 CompositePropertySource (org.springframework.core.env.CompositePropertySource)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpMethod (org.springframework.http.HttpMethod)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)1