-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2749 from michalszynkiewicz/ws-encoders-registration
register websocket encoders and decoders for reflection
- Loading branch information
Showing
10 changed files
with
426 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
integration-tests/main/src/main/java/io/quarkus/it/websocket/ClientCodingResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.websocket.ContainerProvider; | ||
import javax.websocket.DeploymentException; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.QueryParam; | ||
|
||
@Path("/ws-client-coding-test") | ||
public class ClientCodingResource { | ||
|
||
@GET | ||
public String codeBothWays(@QueryParam("echoServerUri") String echoServerUri) throws IOException, DeploymentException { | ||
ContainerProvider.getWebSocketContainer() | ||
.connectToServer(CodingClient.class, URI.create(echoServerUri)); | ||
|
||
try { | ||
Dto response = CodingClient.messageQueue.poll(20, TimeUnit.SECONDS); | ||
return response.getContent(); | ||
} catch (InterruptedException e) { | ||
CodingClient.close(); | ||
return "Failed to get response in time"; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
integration-tests/main/src/main/java/io/quarkus/it/websocket/ClientDtoDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
import javax.websocket.Decoder; | ||
import javax.websocket.EndpointConfig; | ||
|
||
public class ClientDtoDecoder implements Decoder.TextStream<Dto> { | ||
@Override | ||
public Dto decode(Reader reader) throws IOException { | ||
try (BufferedReader bufferedReader = new BufferedReader(reader)) { | ||
String input = bufferedReader.readLine(); // expecting one line input | ||
Dto result = new Dto(); | ||
result.setContent("[decoded]" + input); | ||
return result; | ||
} | ||
} | ||
|
||
@Override | ||
public void init(EndpointConfig config) { | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
integration-tests/main/src/main/java/io/quarkus/it/websocket/ClientDtoEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
import javax.websocket.Encoder; | ||
import javax.websocket.EndpointConfig; | ||
|
||
public class ClientDtoEncoder implements Encoder.TextStream<Dto> { | ||
@Override | ||
public void encode(Dto object, Writer writer) throws IOException { | ||
writer.append("[encoded]" + object.getContent()); | ||
} | ||
|
||
@Override | ||
public void init(EndpointConfig config) { | ||
|
||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
integration-tests/main/src/main/java/io/quarkus/it/websocket/CodingClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
import javax.websocket.ClientEndpoint; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
|
||
@ClientEndpoint(decoders = ClientDtoDecoder.class, encoders = ClientDtoEncoder.class) | ||
public class CodingClient { | ||
private static List<Session> sessions = Collections.synchronizedList(new ArrayList<>()); | ||
|
||
static LinkedBlockingDeque<Dto> messageQueue = new LinkedBlockingDeque<>(); | ||
|
||
@OnOpen | ||
public void onOpen(Session session) { | ||
sessions.add(session); | ||
|
||
Dto data = new Dto(); | ||
data.setContent("initial data"); | ||
session.getAsyncRemote().sendObject(data); | ||
} | ||
|
||
@OnMessage | ||
public void onMessage(Dto message) { | ||
messageQueue.add(message); | ||
close(); | ||
} | ||
|
||
static void close() { | ||
for (Session session : sessions) { | ||
try { | ||
session.close(); | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
integration-tests/main/src/main/java/io/quarkus/it/websocket/Dto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
public class Dto { | ||
private String content; | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Dto{" + | ||
"content='" + content + '\'' + | ||
'}'; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
integration-tests/main/src/main/java/io/quarkus/it/websocket/RecodingSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
import javax.websocket.OnMessage; | ||
import javax.websocket.server.ServerEndpoint; | ||
|
||
@ServerEndpoint(value = "/recoder", encoders = ServerDtoEncoder.class, decoders = ServerDtoDecoder.class) | ||
public class RecodingSocket { | ||
@OnMessage | ||
public Dto recode(Dto input) { | ||
Dto result = new Dto(); | ||
result.setContent("[recoded]" + input.getContent()); | ||
return result; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
integration-tests/main/src/main/java/io/quarkus/it/websocket/ServerDtoDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. | ||
* | ||
* 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.quarkus.it.websocket; | ||
|
||
import java.io.Reader; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
import javax.websocket.Decoder; | ||
import javax.websocket.EndpointConfig; | ||
|
||
public class ServerDtoDecoder implements Decoder.TextStream<Dto> { | ||
@Override | ||
public Dto decode(Reader reader) { | ||
JsonObject jsonObject = Json.createReader(reader).readObject(); | ||
Dto result = new Dto(); | ||
result.setContent(jsonObject.getString("content")); | ||
return result; | ||
} | ||
|
||
@Override | ||
public void init(EndpointConfig config) { | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
} |
Oops, something went wrong.