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;
}
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);
}
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¶m2=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));
}
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);
}
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;
}
Aggregations