Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the default context root for Websocket endpoints #1624

Merged
merged 4 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions microprofile/websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

<description>Helidon MP integration with Tyrus</description>

<properties>
<surefire.argLine>-Dmp.initializer.allow=true</surefire.argLine>
</properties>

<dependencies>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
public class WebSocketCdiExtension implements Extension {
private static final Logger LOGGER = Logger.getLogger(WebSocketCdiExtension.class.getName());

private static final String DEFAULT_WEBSOCKET_PATH = "/websocket";
private static final String DEFAULT_WEBSOCKET_PATH = "/";

static {
HelidonFeatures.register(HelidonFlavor.MP, "WebSocket");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,12 @@

package io.helidon.microprofile.tyrus;

import javax.enterprise.context.Dependent;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.spi.CDI;
import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
Expand All @@ -26,71 +31,38 @@
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Logger;

import io.helidon.microprofile.server.RoutingPath;
import io.helidon.microprofile.server.Server;
import io.helidon.microprofile.server.ServerCdiExtension;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* Class WebSocketAppTest.
* Class WebSocketBaseTest.
*/
public class WebSocketAppTest {

private static Server server;
public abstract class WebSocketBaseTest {

@BeforeAll
static void initClass() {
Server.Builder builder = Server.builder();
server = builder.build();
server.start();
}
static SeContainer container;

@AfterAll
static void destroyClass() {
server.stop();
container.close();
}

@Test
public void testEchoAnnot() throws Exception {
URI echoUri = URI.create("ws://localhost:" + server.port() + "/web/echoAnnot");
EchoClient echoClient = new EchoClient(echoUri);
echoClient.echo("hi", "how are you?");
public abstract String context();

public int port() {
ServerCdiExtension cdiExtension = CDI.current().getBeanManager().getExtension(ServerCdiExtension.class);
return cdiExtension.port();
}

@Test
public void testEchoProg() throws Exception {
URI echoUri = URI.create("ws://localhost:" + server.port() + "/web/echoProg");
public void testEchoAnnot() throws Exception {
URI echoUri = URI.create("ws://localhost:" + port() + context() + "/echoAnnot");
EchoClient echoClient = new EchoClient(echoUri);
echoClient.echo("hi", "how are you?");
}

@Dependent
@RoutingPath("/web")
public static class EndpointApplication implements ServerApplicationConfig {
@Override
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpoints) {
ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(
EchoEndpointProg.class, "/echoProg");
return Collections.singleton(builder.build());
}

@Override
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> endpoints) {
return Collections.singleton(EchoEndpointAnnot.class);
}
}

@ServerEndpoint("/echoAnnot")
public static class EchoEndpointAnnot {
private static final Logger LOGGER = Logger.getLogger(EchoEndpointAnnot.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.microprofile.tyrus;

import java.net.URI;
import java.util.Collections;
import java.util.Set;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.enterprise.inject.spi.CDI;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;

import io.helidon.microprofile.server.RoutingPath;

import io.helidon.microprofile.server.ServerCdiExtension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* A test that uses an {@code EndpointApplication} subclass annotated with
* {@code @RoutingPath} to register Websocket endpoints. The context for
* Websocket endpoints is defined by the value of {@code @RoutingPath}.
*/
public class WebSocketEndpointAppTest extends WebSocketBaseTest {

@BeforeAll
static void initClass() {
container = SeContainerInitializer.newInstance()
.addBeanClasses(EndpointApplication.class)
.initialize();
}

@Override
public String context() {
return "/web";
}

@Test
public void testEchoProg() throws Exception {
URI echoUri = URI.create("ws://localhost:" + port() + context() + "/echoProg");
EchoClient echoClient = new EchoClient(echoUri);
echoClient.echo("hi", "how are you?");
}

@Dependent
@RoutingPath("/web")
public static class EndpointApplication implements ServerApplicationConfig {
@Override
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpoints) {
ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(
EchoEndpointProg.class, "/echoProg");
return Collections.singleton(builder.build());
}

@Override
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> endpoints) {
return Collections.singleton(EchoEndpointAnnot.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.microprofile.tyrus;

import javax.enterprise.inject.se.SeContainerInitializer;

import org.junit.jupiter.api.BeforeAll;

/**
* A test that registers a single annotated endpoint on the default WebSocket
* context. See {@code WebSocketCdiExtension#DEFAULT_WEBSOCKET_PATH}.
*/
public class WebSocketEndpointTest extends WebSocketBaseTest {

@BeforeAll
static void initClass() {
container = SeContainerInitializer.newInstance()
.addBeanClasses(EchoEndpointAnnot.class)
.initialize();
}

@Override
public String context() {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.microprofile.tyrus;

import javax.enterprise.inject.se.SeContainerInitializer;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static javax.ws.rs.client.Entity.text;
import static org.hamcrest.CoreMatchers.is;

/**
* A test that mixes Websocket endpoints and REST resources in the same
* application.
*/
public class WebSocketRestEndpointTest extends WebSocketBaseTest {

private static Client client;

@BeforeAll
static void initClass() {
client = ClientBuilder.newClient();
container = SeContainerInitializer.newInstance()
.addBeanClasses(EchoEndpointAnnot.class, EchoResource.class)
.initialize();
}

@Override
public String context() {
return "";
}

@Test
public void testEchoRest() {
String echo = client.target("http://localhost:" + port() + "/echoRest")
.request("text/plain")
.post(text("echo"), String.class);
MatcherAssert.assertThat(echo, is("echo"));
}

@Path("/echoRest")
public static class EchoResource {
@POST
@Produces("text/plain")
@Consumes("text/plain")
public String echo(String s) {
return s;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0"
bean-discovery-mode="annotated">
bean-discovery-mode="none">
</beans>