Skip to content

Commit

Permalink
Add the means to extract the contextual properties from HttpChannel, …
Browse files Browse the repository at this point in the history
…TcpCChannel and TrasportChannel without excessive typecasting

Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
reta committed Oct 11, 2023
1 parent 9bcd7ea commit 0eb752b
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ public Channel getNettyChannel() {
return channel;
}

@SuppressWarnings("unchecked")
@Override
public <T> T get(String name, Class<T> clazz) {
Object handler = getNettyChannel().pipeline().get(name);

if (handler == null && inboundPipeline() != null) {
handler = inboundPipeline().get(name);
}

if (handler != null && clazz.isInstance(handler) == true) {
return (T) handler;
}

return null;
}

@Override
public String toString() {
return "Netty4HttpChannel{" + "localAddress=" + getLocalAddress() + ", remoteAddress=" + getRemoteAddress() + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ public void sendMessage(BytesReference reference, ActionListener<Void> listener)
}
}

@SuppressWarnings("unchecked")
@Override
public <T> T get(String name, Class<T> clazz) {
final Object handler = getNettyChannel().pipeline().get(name);

if (handler != null && clazz.isInstance(handler) == true) {
return (T) handler;
}

return null;
}

public Channel getNettyChannel() {
return channel;
}
Expand Down
14 changes: 14 additions & 0 deletions server/src/main/java/org/opensearch/http/HttpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ default void handleException(Exception ex) {}
*/
InetSocketAddress getRemoteAddress();

/**
* Returns the contextual property associated with this specific HTTP channel (the
* implementation of how such properties are managed depends on the the particular
* transport engine).
*
* @param name the name of the property
* @param clazz the expected type of the property
*
* @return the value of the property.
* {@code null} if there's no such property or the expected type is not compatible.
*/
default <T> T get(String name, Class<T> clazz) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ public InetSocketAddress getLocalAddress() {
public InetSocketAddress getRemoteAddress() {
return delegate.getRemoteAddress();
}

@Override
public <T> T get(String name, Class<T> clazz) {
return delegate.get(name, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ public void sendResponse(Exception exception) throws IOException {
public Version getVersion() {
return delegate.getVersion();
}

@Override
public <T> T get(String name, Class<T> clazz) {
return delegate.get(name, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public Version getVersion() {
public TransportChannel getChannel() {
return channel;
}

@Override
public <T> T get(String name, Class<T> clazz) {
return getChannel().get(name, clazz);
}
}
15 changes: 15 additions & 0 deletions server/src/main/java/org/opensearch/transport/TcpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ public interface TcpChannel extends CloseableChannel {
*/
ChannelStats getChannelStats();

/**
* Returns the contextual property associated with this specific TCP channel (the
* implementation of how such properties are managed depends on the the particular
* transport engine).
*
* @param name the name of the property
* @param clazz the expected type of the property
*
* @return the value of the property.
* {@code null} if there's no such property or the expected type is not compatible.
*/
default <T> T get(String name, Class<T> clazz) {
return null;
}

/**
* Channel statistics
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,8 @@ public Version getVersion() {
return version;
}

@Override
public <T> T get(String name, Class<T> clazz) {
return getChannel().get(name, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,19 @@ static void sendErrorResponse(TransportChannel channel, String actionName, Trans
);
}
}

/**
* Returns the contextual property associated with this specific transport channel (the
* implementation of how such properties are managed depends on the the particular
* transport engine).
*
* @param name the name of the property
* @param clazz the expected type of the property
*
* @return the value of the property.
* {@code null} if there's no such property or the expected type is not compatible.
*/
default <T> T get(String name, Class<T> clazz) {
return null;
}
}

0 comments on commit 0eb752b

Please sign in to comment.