use of org.springframework.core.env.StandardEnvironment in project spring-cloud-config by spring-cloud.
the class EnvironmentPropertySource method prepareEnvironment.
public static StandardEnvironment prepareEnvironment(Environment environment) {
StandardEnvironment standardEnvironment = new StandardEnvironment();
standardEnvironment.getPropertySources().remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
standardEnvironment.getPropertySources().remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
standardEnvironment.getPropertySources().addFirst(new EnvironmentPropertySource(environment));
return standardEnvironment;
}
use of org.springframework.core.env.StandardEnvironment in project spring-cloud-config by spring-cloud.
the class NativeEnvironmentRepository method getEnvironment.
private ConfigurableEnvironment getEnvironment(String profile) {
ConfigurableEnvironment environment = new StandardEnvironment();
Map<String, Object> map = new HashMap<>();
map.put("spring.profiles.active", profile);
map.put("spring.main.web-application-type", "none");
environment.getPropertySources().addFirst(new MapPropertySource("profiles", map));
return environment;
}
use of org.springframework.core.env.StandardEnvironment in project spring-integration-samples by spring-projects.
the class DynamicFtpChannelResolver method setEnvironmentForCustomer.
/**
* Use Spring 3.1. environment support to set properties for the
* customer-specific application context.
*
* @param ctx
* @param customer
*/
private void setEnvironmentForCustomer(ConfigurableApplicationContext ctx, String customer) {
StandardEnvironment env = new StandardEnvironment();
Properties props = new Properties();
// populate properties for customer
props.setProperty("host", "host.for." + customer);
props.setProperty("user", "user");
props.setProperty("password", "password");
props.setProperty("remote.directory", "/tmp");
PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
env.getPropertySources().addLast(pps);
ctx.setEnvironment(env);
}
use of org.springframework.core.env.StandardEnvironment in project spring-integration by spring-projects.
the class UdpUnicastEndToEndTests method createContext.
private ClassPathXmlApplicationContext createContext(UdpUnicastEndToEndTests launcher, String location) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
applicationContext.setConfigLocation(location);
StandardEnvironment env = new StandardEnvironment();
Properties props = new Properties();
props.setProperty("port", Integer.toString(launcher.getReceiverPort()));
PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
env.getPropertySources().addLast(pps);
applicationContext.setEnvironment(env);
applicationContext.refresh();
return applicationContext;
}
use of org.springframework.core.env.StandardEnvironment in project chassis by Kixeye.
the class HttpTransportTest method testHttpServiceWithJsonWithHTTPS.
@Test
public void testHttpServiceWithJsonWithHTTPS() throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("https.enabled", "true");
properties.put("https.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("https.hostname", "localhost");
properties.put("https.selfSigned", "true");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
context.setEnvironment(environment);
context.register(PropertySourcesPlaceholderConfigurer.class);
context.register(TransportConfiguration.class);
context.register(TestRestService.class);
try {
context.refresh();
final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);
SSLContextBuilder builder = SSLContexts.custom();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
SSLContext sslContext = builder.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
@Override
public void verify(String host, SSLSocket ssl) throws IOException {
}
@Override
public void verify(String host, X509Certificate cert) throws SSLException {
}
@Override
public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
}
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(HttpClients.custom().setConnectionManager(cm).build());
RestTemplate httpClient = new RestTemplate(requestFactory);
httpClient.setErrorHandler(new ResponseErrorHandler() {
public boolean hasError(ClientHttpResponse response) throws IOException {
return response.getRawStatusCode() == HttpStatus.OK.value();
}
public void handleError(ClientHttpResponse response) throws IOException {
}
});
httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
httpClient.setMessageConverters(new ArrayList<HttpMessageConverter<?>>(Lists.newArrayList(new SerDeHttpMessageConverter(serDe))));
TestObject response = httpClient.getForObject(new URI("https://localhost:" + properties.get("https.port") + "/stuff/"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
response = httpClient.postForObject(new URI("https://localhost:" + properties.get("https.port") + "/stuff/"), new TestObject("more stuff"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
response = httpClient.getForObject(new URI("https://localhost:" + properties.get("https.port") + "/stuff/"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("more stuff", response.value);
response = httpClient.getForObject(new URI("https://localhost:" + properties.get("https.port") + "/stuff/getFuture"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("more stuff", response.value);
response = httpClient.getForObject(new URI("https://localhost:" + properties.get("https.port") + "/stuff/getObservable"), TestObject.class);
Assert.assertNotNull(response);
Assert.assertEquals("more stuff", response.value);
ResponseEntity<ServiceError> error = httpClient.postForEntity(new URI("https://localhost:" + properties.get("https.port") + "/stuff/"), new TestObject(RandomStringUtils.randomAlphabetic(100)), ServiceError.class);
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.BAD_REQUEST, error.getStatusCode());
Assert.assertEquals(ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE, error.getBody().code);
error = httpClient.getForEntity(new URI("https://localhost:" + properties.get("https.port") + "/stuff/expectedError"), ServiceError.class);
Assert.assertNotNull(response);
Assert.assertEquals(TestRestService.EXPECTED_EXCEPTION_HTTP_CODE, error.getStatusCode());
Assert.assertEquals(TestRestService.EXPECTED_EXCEPTION.code, error.getBody().code);
Assert.assertEquals(TestRestService.EXPECTED_EXCEPTION.description, error.getBody().description);
error = httpClient.getForEntity(new URI("https://localhost:" + properties.get("https.port") + "/stuff/unexpectedError"), ServiceError.class);
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, error.getStatusCode());
Assert.assertEquals(ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE, error.getBody().code);
} finally {
context.close();
}
}
Aggregations