Skip to content

Commit

Permalink
Support user-defined reason phrase
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Jan 5, 2024
1 parent 3a3cbb2 commit dcf9286
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 18 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ for easy development of WebSocket applications.Eclipse Tyrus is also
a Jakarta WebSocket 2.0 compatible implementation.

WebSocket protocol defined by IETF
provides bi-directional communication between the server and the remote host. The
provides bidirectional communication between the server and the remote host. The
pros are mainly the ability to communicate both ways, low latency and small
communication overhead. Therefore Tyrus and WebSocket in general are suitable for web
communication overhead. Therefore, Tyrus and WebSocket in general are suitable for web
applications that require sending a huge volume of relatively small messages like
online games or market ticker broadcasting.

Expand All @@ -20,6 +20,15 @@ online games or market ticker broadcasting.
Building Tyrus can be done using `mvn clean install`, but sometimes (such as for building 2.x from a tag)
`mvn clean install -Pstaging` would be required.

## Tyrus Git Branches

| branch | Jakarta Version | Tyrus Version |
|--------|---------------------------------|---------------|
| master | Java EE 8 / Jakarta EE 8 branch | Tyrus 1.x |
| 2.0.x | Jakarta EE 9 branch | Tyrus 2.0.x |
| 2.1.x | Jakarta EE 10 branch | Tyrus 2.1.x |
| 2.x | Jakarta EE 11 branch | Tyrus 2.2.x |

## Licensing

- [Eclipse Public License 2.0](https://projects.eclipse.org/license/epl-2.0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -443,6 +443,11 @@ public void setReasonPhrase(String reason) {

}

@Override
public String getReasonPhrase() {
return null;
}

@Override
public Map<String, List<String>> getHeaders() {
headers.put(HandshakeResponse.SEC_WEBSOCKET_ACCEPT, Collections.singletonList(serverKey));
Expand All @@ -468,6 +473,11 @@ public void setReasonPhrase(String reason) {

}

@Override
public String getReasonPhrase() {
return null;
}

@Override
public Map<String, List<String>> getHeaders() {
return headers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -396,6 +396,7 @@ private static UpgradeResponse getUpgradeResponse(HttpResponsePacket httpRespons
}

tyrusUpgradeResponse.setStatus(httpResponsePacket.getStatus());
tyrusUpgradeResponse.setReasonPhrase(httpResponsePacket.getReasonPhrase());

return tyrusUpgradeResponse;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -281,6 +281,9 @@ private void write(FilterChainContext ctx, UpgradeResponse response) {
((HttpRequestPacket) ((HttpContent) ctx.getMessage()).getHttpHeader()).getResponse();
responsePacket.setProtocol(Protocol.HTTP_1_1);
responsePacket.setStatus(response.getStatus());
if (response.getReasonPhrase() != null) {
responsePacket.setReasonPhrase(response.getReasonPhrase());
}

// TODO
// responsePacket.setReasonPhrase(response.getReasonPhrase());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -52,10 +52,13 @@ public int getStatus() {

/**
* Get HTTP reason phrase.
* <p>
* Warning: The Reason Phrase is removed from HTTP/2 and from Servlet 6.
* </p>
*
* @return reason phrase.
*/
// @Override
@Override
public String getReasonPhrase() {
return reasonPhrase;
}
Expand Down
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Copyright (c) 2011, 2023 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -574,6 +574,22 @@
<module>bundles</module>
</modules>
</profile>
<profile>
<id>JDK_8_for_IDE</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<reporting>
Expand Down
17 changes: 12 additions & 5 deletions spi/src/main/java/org/glassfish/tyrus/spi/UpgradeResponse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -64,15 +64,22 @@ public abstract class UpgradeResponse implements HandshakeResponse {
public abstract void setStatus(int status);

/**
* Get HTTP reason phrase.
* Set HTTP reason phrase.
* <p>
* TODO remove ?? we are using only for "Switching Protocols" and that is
* TODO standard status code 101
*
* Warning: The Reason Phrase is removed from HTTP/2 and from Servlet 6.
* </p>
* @param reason reason phrase to be set.
*/
public abstract void setReasonPhrase(String reason);

/**
* Get HTTP reason phrase.
* <p>
* Warning: The Reason Phrase is removed from HTTP/2 and from Servlet 6.
* </p>
*/
public abstract String getReasonPhrase();

/**
* Gets the value of the response header with the given name.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -60,6 +60,10 @@ public class HandshakeTest extends TestContainer {
private String receivedMessage;

private static final String SENT_MESSAGE = "hello";
private static final int STATUS = 499;
private static final String HEADER = "TEST_HEADER";
private static final String TEST_REASON = "Any Reason";


public static class MyClientEndpointConfigurator extends ClientEndpointConfig.Configurator {

Expand Down Expand Up @@ -129,10 +133,6 @@ public void onOpen(Session session) {
}
}


static final int STATUS = 499;
static final String HEADER = "TEST_HEADER";

public static class StatusSetterConfiguration extends ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
Expand Down Expand Up @@ -201,6 +201,7 @@ public static class Status401SetterConfiguration extends ServerEndpointConfig.Co
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
((UpgradeResponse) response).setStatus(401);
((UpgradeResponse) response).setReasonPhrase(TEST_REASON);
response.getHeaders().put(HEADER, Collections.singletonList(HEADER));
}
}
Expand All @@ -217,13 +218,15 @@ public void onMessage(String message) {

final AtomicReference<Integer> status = new AtomicReference<>();
final AtomicReference<String> header = new AtomicReference<>();
final AtomicReference<String> reasonPhrase = new AtomicReference<>();

Server server = startServer(Status401SetterEndpoint.class);

ClientEndpointConfig.Configurator cecc = new ClientEndpointConfig.Configurator() {
@Override
public void afterResponse(HandshakeResponse hr) {
status.set(((UpgradeResponse) hr).getStatus());
reasonPhrase.set(((UpgradeResponse) hr).getReasonPhrase());
header.set(((UpgradeResponse) hr).getFirstHeaderValue(HEADER));
}
};
Expand Down Expand Up @@ -257,6 +260,7 @@ public EndpointConfig getEndpointConfig() {
Assert.assertEquals(401, status.get().intValue());
Assert.assertEquals(status.get().intValue(), de.getHttpStatusCode());
Assert.assertEquals(HEADER, header.get());
Assert.assertEquals(TEST_REASON, reasonPhrase.get());
} finally {
server.stop();
}
Expand Down

0 comments on commit dcf9286

Please sign in to comment.