Java Code Examples for org.springframework.web.context.support.AnnotationConfigWebApplicationContext#setEnvironment()

The following examples show how to use org.springframework.web.context.support.AnnotationConfigWebApplicationContext#setEnvironment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void annotationConfigWebApplicationContext() {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.setEnvironment(prodWebEnv);
	ctx.setConfigLocation(EnvironmentAwareBean.class.getName());
	ctx.refresh();

	assertHasEnvironment(ctx, prodWebEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
 
Example 2
Source File: WebSocketDocsTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtobufMessagesEnvelope() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	
	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);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));

		ResponseEntity<String> response = httpClient.getForEntity(
				new URI("http://localhost:" + properties.get("admin.port") + "/schema/envelope/protobuf"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("message Envelope"));
	} finally {
		context.close();
	}
}
 
Example 3
Source File: WebSocketDocsTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtobufMessagesSchema() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	
	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);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));

		ResponseEntity<String> response = httpClient.getForEntity(
				new URI("http://localhost:" + properties.get("admin.port") + "/schema/messages/protobuf"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("message error"));
	} finally {
		context.close();
	}
}
 
Example 4
Source File: ManagementApiServer.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
public void attachHandlers() {

        // Create the servlet context
        final ServletContextHandler context = new ServletContextHandler(this.server, entrypoint, ServletContextHandler.SESSIONS);

        // REST configuration
        final ServletHolder servletHolder = new ServletHolder(ServletContainer.class);
        servletHolder.setInitParameter("javax.ws.rs.Application", ManagementApplication.class.getName());
        servletHolder.setInitOrder(1);
        servletHolder.setAsyncSupported(true);

        AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
        webApplicationContext.setEnvironment((ConfigurableEnvironment) applicationContext.getEnvironment());
        webApplicationContext.setParent(applicationContext);
        webApplicationContext.setServletContext(context.getServletContext());

        webApplicationContext.register(ManagementConfiguration.class);

        context.addEventListener(new ContextLoaderListener(webApplicationContext));
        context.addServlet(servletHolder, "/*");
        context.addServlet(new ServletHolder(new DispatcherServlet(webApplicationContext)), "/auth/*");

        // X-Forwarded-* support
        context.addFilter(ForwardedHeaderFilter.class, "/*", EnumSet.allOf(DispatcherType.class));

        // Spring Security filter
        context.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")), "/*", EnumSet.allOf(DispatcherType.class));
    }
 
Example 5
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void annotationConfigWebApplicationContext() {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.setEnvironment(prodWebEnv);
	ctx.setConfigLocation(EnvironmentAwareBean.class.getName());
	ctx.refresh();

	assertHasEnvironment(ctx, prodWebEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
 
Example 6
Source File: ExternalizedConfigurationWebApplicationBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistrationBean(ConfigurableEnvironment environment) {
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
    // 构建 DispatcherServlet 应用上下文
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    // 复用当前 ApplicationContext 的 ConfigurableEnvironment 对象
    context.setEnvironment(environment);
    // 设置 DispatcherServlet
    servletRegistrationBean.setServlet(new DispatcherServlet(context));
    // 设置 ServletConfig 初始化参数
    servletRegistrationBean.addInitParameter("my-servlet-name", "My DispatcherServlet");
    return servletRegistrationBean;
}
 
Example 7
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void annotationConfigWebApplicationContext() {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.setEnvironment(prodWebEnv);
	ctx.setConfigLocation(EnvironmentAwareBean.class.getName());
	ctx.refresh();

	assertHasEnvironment(ctx, prodWebEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
 
Example 8
Source File: HybridServiceTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testHybridService() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(TestCombinedService.class);

	WebSocketClient wsClient = new WebSocketClient();

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		final MessageSerDe serDe = context.getBean(ProtobufMessageSerDe.class);

		final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
		
		messageRegistry.registerType("stuff", TestObject.class);
		
		wsClient.start();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);
		
		QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, null);

		Session session = wsClient.connect(webSocket, new URI("ws://localhost:" +  properties.get("websocket.port") + "/protobuf")).get(5000, TimeUnit.MILLISECONDS);

		Envelope envelope = new Envelope("getStuff", null, null, null);
		
		session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
		
		TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("stuff", response.value);

		byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));
		
		envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
		
		session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
		
		response = webSocket.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("stuff", response.value);

		envelope = new Envelope("getStuff", null, null, null);
		
		session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
		
		response = webSocket.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("more stuff", response.value);
		
		response = httpClient.getForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), 
				TestObject.class);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("more stuff", response.value);

		response = httpClient.postForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), 
				new TestObject("even more stuff"),
				TestObject.class);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("more stuff", response.value);
		
		response = httpClient.getForObject(new URI("http://localhost:" + properties.get("http.port") + "/stuff/"), 
				TestObject.class);
		
		Assert.assertNotNull(response);
		Assert.assertEquals("even more stuff", response.value);
	} finally {
		try {
			wsClient.stop();
		} finally {
			context.close();
		}
	}
}
 
Example 9
Source File: SharedTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassPath() throws Exception {

       Map<String, Object> properties = new HashMap<String, Object>();
       properties.put("admin.enabled", "true");
       properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
       properties.put("admin.hostname", "localhost");

       properties.put("websocket.enabled", "true");
       properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
       properties.put("websocket.hostname", "localhost");

       properties.put("http.enabled", "true");
       properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
       properties.put("http.hostname", "localhost");

       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(MetricsConfiguration.class);
	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("admin.port") + "/admin/classpath"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().length() > 0);
	} finally {
		context.close();
	}
}
 
Example 10
Source File: SharedTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperties() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(MetricsConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("admin.port") + "/admin/properties"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("user.dir=" + System.getProperty("user.dir")));
	} finally {
		context.close();
	}
}
 
Example 11
Source File: SharedTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testHealthcheck() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(MetricsConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("admin.port") + "/healthcheck"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertEquals("OK", response.getBody());
	} finally {
		context.close();
	}
}
 
Example 12
Source File: SharedTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSwagger() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(MetricsConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("http.port") + "/swagger/index.html"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("<title>Swagger UI</title>"));
	} finally {
		context.close();
	}
}
 
Example 13
Source File: SharedTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testAdminLinks() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(MetricsConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("admin.port") + "/admin/index.html"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("<a href=\"/metrics/ping\">Ping</a>"));
		Assert.assertTrue(response.getBody().contains("<a href=\"/healthcheck\">Healthcheck</a>"));
		Assert.assertTrue(response.getBody().contains("<a href=\"/metrics/metrics?pretty=true\">Metrics</a>"));
		Assert.assertTrue(response.getBody().contains("<a href=\"/admin/properties\">Properties</a>"));
		Assert.assertTrue(response.getBody().contains("<a href=\"/metrics/threads\">Threads</a>"));
		Assert.assertTrue(response.getBody().contains("<a href=\"/admin/classpath\">Classpath</a>"));
	} finally {
		context.close();
	}
}
 
Example 14
Source File: MetricsTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetricsPing() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(MetricsConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe serDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(serDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("admin.port") + "/metrics/ping"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertEquals("pong".trim(), response.getBody().trim());
	} finally {
		context.close();
	}
}
 
Example 15
Source File: WebSocketTransportTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmptyWebSocketFrameUsingBinary() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(TestWebSocketService.class);

	WebSocketClient wsClient = new WebSocketClient();
	
	try {
		//start server
		context.refresh();

		// start client
		wsClient.start();

		final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);

		final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
		
		QueuingWebSocketListener listener = new QueuingWebSocketListener(serDe, messageRegistry, null);
		
		WebSocketSession session = (WebSocketSession)wsClient.connect(listener, new URI("ws://localhost:" +  properties.get("websocket.port") + "/" + serDe.getMessageFormatName()))
				.get(5000, TimeUnit.MILLISECONDS);
		
		session.getRemote().sendBytes(ByteBuffer.wrap(new byte[0]));
		
		ServiceError error = listener.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(error);
		Assert.assertEquals("EMPTY_ENVELOPE", error.code);
		Assert.assertEquals("STOPPED", session.getState());
	} finally {
		try {
			wsClient.stop();
		} finally {
			context.close();
		}
	}
}
 
Example 16
Source File: WebSocketTransportTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmptyWebSocketFrameUsingText() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	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(TestWebSocketService.class);

	WebSocketClient wsClient = new WebSocketClient();
	
	try {
		//start server
		context.refresh();

		// start client
		wsClient.start();

		final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);

		final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
		
		QueuingWebSocketListener listener = new QueuingWebSocketListener(serDe, messageRegistry, null);
		
		WebSocketSession session = (WebSocketSession)wsClient.connect(listener, new URI("ws://localhost:" +  properties.get("websocket.port") + "/" + serDe.getMessageFormatName()))
				.get(5000, TimeUnit.MILLISECONDS);
		
		session.getRemote().sendString("");
		
		ServiceError error = listener.getResponse(5, TimeUnit.SECONDS);
		
		Assert.assertNotNull(error);
		Assert.assertEquals("EMPTY_ENVELOPE", error.code);
		Assert.assertEquals("STOPPED", session.getState());
	} finally {
		try {
			wsClient.stop();
		} finally {
			context.close();
		}
	}
}
 
Example 17
Source File: JettyEmbeddedContainer.java    From gravitee-management-rest-api with Apache License 2.0 3 votes vote down vote up
protected ServletContextHandler configureAPI(String apiContextPath, String applicationName, Class<? extends GlobalAuthenticationConfigurerAdapter> securityConfigurationClass) {
    final ServletContextHandler childContext = new ServletContextHandler(server, apiContextPath, ServletContextHandler.SESSIONS);

    final ServletHolder servletHolder = new ServletHolder(ServletContainer.class);
    servletHolder.setInitParameter("javax.ws.rs.Application", applicationName);
    servletHolder.setInitOrder(0);
    childContext.addServlet(servletHolder, "/*");

    AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
    webApplicationContext.register(securityConfigurationClass);

    webApplicationContext.setEnvironment((ConfigurableEnvironment) applicationContext.getEnvironment());
    webApplicationContext.setParent(applicationContext);

    childContext.addEventListener(new ContextLoaderListener(webApplicationContext));

    // Spring Security filter
    childContext.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")),"/*", EnumSet.allOf(DispatcherType.class));
    return childContext;


}