From 9d631d7dbdc578cbfbe269b9c715e39d56f5867b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 29 Nov 2024 14:37:22 +0100 Subject: [PATCH 01/29] [WEJBHTTP-139] Refactoring - moving static imports to the top of imports section --- .../wildfly/httpclient/ejb/ClientHandlers.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index 29e05684..dac786a2 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -17,6 +17,15 @@ */ package org.wildfly.httpclient.ejb; +import static org.wildfly.httpclient.ejb.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.ejb.Serializer.deserializeObject; +import static org.wildfly.httpclient.ejb.Serializer.deserializeSet; +import static org.wildfly.httpclient.ejb.Serializer.serializeMap; +import static org.wildfly.httpclient.ejb.Serializer.serializeObjectArray; +import static org.wildfly.httpclient.ejb.Serializer.serializeTransaction; +import static org.wildfly.httpclient.ejb.Serializer.deserializeMap; +import static org.xnio.IoUtils.safeClose; + import io.undertow.client.ClientResponse; import org.jboss.ejb.client.EJBClientInvocationContext; import org.jboss.ejb.client.EJBModuleIdentifier; @@ -41,15 +50,6 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Function; -import static org.wildfly.httpclient.ejb.ByteOutputs.byteOutputOf; -import static org.wildfly.httpclient.ejb.Serializer.deserializeObject; -import static org.wildfly.httpclient.ejb.Serializer.deserializeSet; -import static org.wildfly.httpclient.ejb.Serializer.serializeMap; -import static org.wildfly.httpclient.ejb.Serializer.serializeObjectArray; -import static org.wildfly.httpclient.ejb.Serializer.serializeTransaction; -import static org.wildfly.httpclient.ejb.Serializer.deserializeMap; -import static org.xnio.IoUtils.safeClose; - /** * Utility class providing factory methods for creating client-side handlers of Remote EJB over HTTP protocol. * From 682a83f230ac2177d27006bcda19eb6242c461a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 29 Nov 2024 14:43:51 +0100 Subject: [PATCH 02/29] [WEJBHTTP-139] Refactoring - moving ByteOutputs classes to common module --- .../httpclient/common}/ByteOutputs.java | 6 +- .../httpclient/common/HttpServerHelper.java | 19 ++--- .../httpclient/ejb/ClientHandlers.java | 5 +- .../httpclient/ejb/ServerHandlers.java | 5 +- .../httpclient/naming/ByteOutputs.java | 76 ------------------- .../httpclient/naming/ClientHandlers.java | 2 +- .../httpclient/naming/ServerHandlers.java | 4 +- .../httpclient/transaction/ByteOutputs.java | 76 ------------------- .../transaction/ClientHandlers.java | 2 +- .../transaction/ServerHandlers.java | 2 +- 10 files changed, 22 insertions(+), 175 deletions(-) rename {ejb/src/main/java/org/wildfly/httpclient/ejb => common/src/main/java/org/wildfly/httpclient/common}/ByteOutputs.java (93%) delete mode 100644 naming/src/main/java/org/wildfly/httpclient/naming/ByteOutputs.java delete mode 100644 transaction/src/main/java/org/wildfly/httpclient/transaction/ByteOutputs.java diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ByteOutputs.java b/common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java similarity index 93% rename from ejb/src/main/java/org/wildfly/httpclient/ejb/ByteOutputs.java rename to common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java index 661ae874..3418dde5 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ByteOutputs.java +++ b/common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.wildfly.httpclient.ejb; +package org.wildfly.httpclient.common; import org.jboss.marshalling.ByteOutput; @@ -27,13 +27,13 @@ * * @author Richard Opalka */ -final class ByteOutputs { +public final class ByteOutputs { private ByteOutputs() { // forbidden instantiation } - static ByteOutput byteOutputOf(final OutputStream delegate) { + public static ByteOutput byteOutputOf(final OutputStream delegate) { if (delegate == null) throw new IllegalArgumentException(); return new ByteOutputStream(delegate); } diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java index aadb887c..68932d26 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java @@ -18,13 +18,13 @@ package org.wildfly.httpclient.common; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import io.undertow.server.HttpServerExchange; import io.undertow.util.Headers; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.Marshalling; import java.io.OutputStream; @@ -42,14 +42,15 @@ public static void sendException(HttpServerExchange exchange, HttpServiceConfig exchange.setStatusCode(status); putResponseHeader(exchange, Headers.CONTENT_TYPE, "application/x-wf-jbmar-exception;version=1"); final Marshaller marshaller = serviceConfig.getHttpMarshallerFactory(exchange).createMarshaller(); - OutputStream outputStream = exchange.getOutputStream(); - final ByteOutput byteOutput = Marshalling.createByteOutput(outputStream); - // start the marshaller - marshaller.start(byteOutput); - marshaller.writeObject(e); - marshaller.write(0); - marshaller.finish(); - marshaller.flush(); + final OutputStream outputStream = exchange.getOutputStream(); + try (ByteOutput byteOutput = byteOutputOf(outputStream)) { + // start the marshaller + marshaller.start(byteOutput); + marshaller.writeObject(e); + marshaller.write(0); + marshaller.finish(); + marshaller.flush(); + } exchange.endExchange(); } catch (Exception ex) { ex.addSuppressed(e); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index dac786a2..e7e6ab39 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -17,7 +17,7 @@ */ package org.wildfly.httpclient.ejb; -import static org.wildfly.httpclient.ejb.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.ejb.Serializer.deserializeObject; import static org.wildfly.httpclient.ejb.Serializer.deserializeSet; import static org.wildfly.httpclient.ejb.Serializer.serializeMap; @@ -35,7 +35,6 @@ import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.HttpTargetContext; import org.xnio.IoUtils; @@ -108,7 +107,7 @@ private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo @Override public void marshall(final OutputStream httpBodyRequestStream) throws Exception { - try (ByteOutput out = Marshalling.createByteOutput(httpBodyRequestStream)) { + try (ByteOutput out = byteOutputOf(httpBodyRequestStream)) { marshaller.start(out); serializeTransaction(marshaller, txnInfo); serializeObjectArray(marshaller, parameters); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 0dfbfc00..687c4bed 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -17,7 +17,7 @@ */ package org.wildfly.httpclient.ejb; -import static org.wildfly.httpclient.ejb.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.ejb.Constants.EJB_DISCOVERY_RESPONSE; import static org.wildfly.httpclient.ejb.Constants.EJB_RESPONSE_NEW_SESSION; import static org.wildfly.httpclient.ejb.Constants.EJB_SESSION_ID; @@ -62,7 +62,6 @@ import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.SimpleClassResolver; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.ContentType; @@ -421,7 +420,7 @@ public void writeInvocationResult(Object result) { // exchange.setResponseCookie(new CookieImpl("JSESSIONID", output.getSessionAffinity()).setPath(WILDFLY_SERVICES)); // } OutputStream outputStream = exchange.getOutputStream(); - final ByteOutput byteOutput = Marshalling.createByteOutput(outputStream); + final ByteOutput byteOutput = byteOutputOf(outputStream); try (byteOutput) { marshaller.start(byteOutput); serializeObject(marshaller, result); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ByteOutputs.java b/naming/src/main/java/org/wildfly/httpclient/naming/ByteOutputs.java deleted file mode 100644 index 26e986e0..00000000 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ByteOutputs.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2024 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * 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 org.wildfly.httpclient.naming; - -import org.jboss.marshalling.ByteOutput; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Helper class. Provides utility method for transforming OutputStreams to ByteOutputs. - * - * @author Richard Opalka - */ -final class ByteOutputs { - - private ByteOutputs() { - // forbidden instantiation - } - - static ByteOutput byteOutputOf(final OutputStream delegate) { - if (delegate == null) throw new IllegalArgumentException(); - return new ByteOutputStream(delegate); - } - - private static final class ByteOutputStream implements ByteOutput { - - private final OutputStream delegate; - - ByteOutputStream(final OutputStream delegate) { - this.delegate = delegate; - } - - @Override - public void close() throws IOException { - delegate.close(); - } - - @Override - public void flush() throws IOException { - delegate.flush(); - } - - @Override - public void write(final int b) throws IOException { - delegate.write(b); - } - - @Override - public void write(final byte[] b) throws IOException { - delegate.write(b); - } - - @Override - public void write(final byte[] b, final int off, final int len) throws IOException { - delegate.write(b, off, len); - } - - } - -} diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java index 2e1e115d..118a5d1c 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java @@ -17,7 +17,7 @@ */ package org.wildfly.httpclient.naming; -import static org.wildfly.httpclient.naming.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.naming.Serializer.deserializeObject; import static org.wildfly.httpclient.naming.Serializer.serializeObject; import static org.wildfly.httpclient.naming.ClassLoaderUtils.setContextClassLoader; diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index 17391e45..e0f4be5c 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -18,6 +18,7 @@ package org.wildfly.httpclient.naming; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.naming.Constants.NAME_PATH_PARAMETER; import static org.wildfly.httpclient.naming.Constants.NEW_QUERY_PARAMETER; import static org.wildfly.httpclient.naming.Constants.VALUE; @@ -34,7 +35,6 @@ import org.jboss.marshalling.ContextClassResolver; import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.ContentType; import org.wildfly.httpclient.common.HttpMarshallerFactory; @@ -134,7 +134,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, VALUE.toString()); HttpNamingServerObjectResolver resolver = new HttpNamingServerObjectResolver(exchange); Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(resolver); - ByteOutput out = new NoFlushByteOutput(Marshalling.createByteOutput(exchange.getOutputStream())); + ByteOutput out = new NoFlushByteOutput(byteOutputOf(exchange.getOutputStream())); try (out) { marshaller.start(out); serializeObject(marshaller, result); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ByteOutputs.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ByteOutputs.java deleted file mode 100644 index e62813fe..00000000 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ByteOutputs.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2024 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * 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 org.wildfly.httpclient.transaction; - -import org.jboss.marshalling.ByteOutput; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Helper class. Provides utility method for transforming OutputStreams to ByteOutputs. - * - * @author Richard Opalka - */ -final class ByteOutputs { - - private ByteOutputs() { - // forbidden instantiation - } - - static ByteOutput byteOutputOf(final OutputStream delegate) { - if (delegate == null) throw new IllegalArgumentException(); - return new ByteOutputStream(delegate); - } - - private static final class ByteOutputStream implements ByteOutput { - - private final OutputStream delegate; - - ByteOutputStream(final OutputStream delegate) { - this.delegate = delegate; - } - - @Override - public void close() throws IOException { - delegate.close(); - } - - @Override - public void flush() throws IOException { - delegate.flush(); - } - - @Override - public void write(final int b) throws IOException { - delegate.write(b); - } - - @Override - public void write(final byte[] b) throws IOException { - delegate.write(b); - } - - @Override - public void write(final byte[] b, final int off, final int len) throws IOException { - delegate.write(b, off, len); - } - - } - -} diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java index 6ec8719a..e7fe49b5 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java @@ -17,7 +17,7 @@ */ package org.wildfly.httpclient.transaction; -import static org.wildfly.httpclient.transaction.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.transaction.Serializer.deserializeXid; import static org.wildfly.httpclient.transaction.Serializer.deserializeXidArray; import static org.wildfly.httpclient.transaction.Serializer.serializeXid; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index dda23d46..8ddeeb47 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -17,8 +17,8 @@ */ package org.wildfly.httpclient.transaction; +import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; -import static org.wildfly.httpclient.transaction.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_FLAGS; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_PARENT_NAME; From a8be101daf6a959e3f6dd666b1f2cf10f5dae6d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 29 Nov 2024 15:19:48 +0100 Subject: [PATCH 03/29] [WEJBHTTP-139] Refactoring - making all ByteOutputs wrapping OutputStreams unflushable --- .../httpclient/common/ByteOutputs.java | 42 +++++++++++- .../httpclient/common/NoFlushByteOutput.java | 64 ------------------- .../httpclient/naming/ServerHandlers.java | 3 +- .../transaction/ClientHandlers.java | 3 +- .../transaction/ServerHandlers.java | 5 +- 5 files changed, 44 insertions(+), 73 deletions(-) delete mode 100644 common/src/main/java/org/wildfly/httpclient/common/NoFlushByteOutput.java diff --git a/common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java b/common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java index 3418dde5..9bc230d7 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java +++ b/common/src/main/java/org/wildfly/httpclient/common/ByteOutputs.java @@ -23,7 +23,11 @@ import java.io.OutputStream; /** - * Helper class. Provides utility method for transforming OutputStreams to ByteOutputs. + * Helper class. Provides various utility methods for example: + *
    + *
  • transforming OutputStreams to ByteOutputs
  • + *
  • introducing special behaviour to existing byte output instances
  • + *
* * @author Richard Opalka */ @@ -35,7 +39,7 @@ private ByteOutputs() { public static ByteOutput byteOutputOf(final OutputStream delegate) { if (delegate == null) throw new IllegalArgumentException(); - return new ByteOutputStream(delegate); + return new UnflushableByteOutput(new ByteOutputStream(delegate)); } private static final class ByteOutputStream implements ByteOutput { @@ -73,4 +77,38 @@ public void write(final byte[] b, final int off, final int len) throws IOExcepti } + private static final class UnflushableByteOutput implements ByteOutput { + + private final ByteOutput delegate; + + public UnflushableByteOutput(final ByteOutput delegate) { + this.delegate = delegate; + } + + @Override + public void close() throws IOException { + delegate.close(); + } + + @Override + public void flush() throws IOException { + //ignore + } + @Override + public void write(final int b) throws IOException { + delegate.write(b); + } + + @Override + public void write(final byte[] b) throws IOException { + delegate.write(b); + } + + @Override + public void write(final byte[] b, int off, int len) throws IOException { + delegate.write(b, off, len); + } + + } + } diff --git a/common/src/main/java/org/wildfly/httpclient/common/NoFlushByteOutput.java b/common/src/main/java/org/wildfly/httpclient/common/NoFlushByteOutput.java deleted file mode 100644 index f1b12402..00000000 --- a/common/src/main/java/org/wildfly/httpclient/common/NoFlushByteOutput.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2017 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * 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 org.wildfly.httpclient.common; - -import java.io.IOException; - -import org.jboss.marshalling.ByteOutput; - -/** - * An output stream that ignores flushes. The marshsaller will flush when it is done, which - * results in two frames on the wire. By ignoring the flush only one frame is sent for - * each message. - * - * @author Stuart Douglas - */ -public class NoFlushByteOutput implements ByteOutput { - - private final ByteOutput delegate; - - public NoFlushByteOutput(ByteOutput delegate) { - this.delegate = delegate; - } - - @Override - public void write(int b) throws IOException { - delegate.write(b); - } - - @Override - public void write(byte[] b) throws IOException { - delegate.write(b); - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - delegate.write(b, off, len); - } - - @Override - public void close() throws IOException { - delegate.close(); - } - - @Override - public void flush() throws IOException { - //ignore - } -} diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index e0f4be5c..b62fd4eb 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -40,7 +40,6 @@ import org.wildfly.httpclient.common.HttpMarshallerFactory; import org.wildfly.httpclient.common.HttpServerHelper; import org.wildfly.httpclient.common.HttpServiceConfig; -import org.wildfly.httpclient.common.NoFlushByteOutput; import javax.naming.Binding; import javax.naming.Context; @@ -134,7 +133,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, VALUE.toString()); HttpNamingServerObjectResolver resolver = new HttpNamingServerObjectResolver(exchange); Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(resolver); - ByteOutput out = new NoFlushByteOutput(byteOutputOf(exchange.getOutputStream())); + ByteOutput out = byteOutputOf(exchange.getOutputStream()); try (out) { marshaller.start(out); serializeObject(marshaller, result); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java index e7fe49b5..bcde6bf5 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java @@ -29,7 +29,6 @@ import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.HttpTargetContext; -import org.wildfly.httpclient.common.NoFlushByteOutput; import org.xnio.IoUtils; import javax.transaction.xa.Xid; @@ -77,7 +76,7 @@ private XidHttpMarshaller(final Marshaller marshaller, final Xid xid) { @Override public void marshall(final OutputStream httpBodyRequestStream) throws Exception { - try (ByteOutput out = new NoFlushByteOutput(byteOutputOf(httpBodyRequestStream))) { + try (ByteOutput out = byteOutputOf(httpBodyRequestStream)) { marshaller.start(out); serializeXid(marshaller, xid); marshaller.finish(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index 8ddeeb47..f4ea0a40 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -41,7 +41,6 @@ import org.wildfly.httpclient.common.ContentType; import org.wildfly.httpclient.common.HttpMarshallerFactory; import org.wildfly.httpclient.common.HttpServiceConfig; -import org.wildfly.httpclient.common.NoFlushByteOutput; import org.wildfly.transaction.client.ImportResult; import org.wildfly.transaction.client.LocalTransaction; import org.wildfly.transaction.client.LocalTransactionContext; @@ -192,7 +191,7 @@ protected void processRequest(final HttpServerExchange exchange) { final Xid xid = xidResolver.apply(transaction); final ByteArrayOutputStream out = new ByteArrayOutputStream(); - final ByteOutput byteOutput = new NoFlushByteOutput(byteOutputOf(out)); + final ByteOutput byteOutput = byteOutputOf(out); try (byteOutput) { Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(); marshaller.start(byteOutput); @@ -237,7 +236,7 @@ protected void processRequest(HttpServerExchange exchange) { final Xid[] recoveryList = ctx.getRecoveryInterface().recover(flags, parentName); final ByteArrayOutputStream out = new ByteArrayOutputStream(); - final ByteOutput byteOutput = new NoFlushByteOutput(byteOutputOf(out)); + final ByteOutput byteOutput = byteOutputOf(out); try (byteOutput) { Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(); marshaller.start(byteOutput); From 3bfbb0098ef722ace1a7c4585be96f41b5c8c3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 29 Nov 2024 15:32:00 +0100 Subject: [PATCH 04/29] [WEJBHTTP-139] Refactoring - unify OutputStream variable names --- .../org/wildfly/httpclient/ejb/ClientHandlers.java | 8 ++++---- .../org/wildfly/httpclient/ejb/ServerHandlers.java | 7 ++----- .../org/wildfly/httpclient/naming/ClientHandlers.java | 4 ++-- .../httpclient/transaction/ClientHandlers.java | 4 ++-- .../httpclient/transaction/ServerHandlers.java | 11 +++++------ 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index e7e6ab39..35cec2db 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -106,8 +106,8 @@ private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo } @Override - public void marshall(final OutputStream httpBodyRequestStream) throws Exception { - try (ByteOutput out = byteOutputOf(httpBodyRequestStream)) { + public void marshall(final OutputStream os) throws Exception { + try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); serializeTransaction(marshaller, txnInfo); serializeObjectArray(marshaller, parameters); @@ -127,8 +127,8 @@ private CreateSessionHttpMarshaller(final Marshaller marshaller, final Transacti } @Override - public void marshall(final OutputStream httpBodyRequestStream) throws Exception { - try (ByteOutput out = byteOutputOf(httpBodyRequestStream)) { + public void marshall(final OutputStream os) throws Exception { + try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); serializeTransaction(marshaller, txnInfo); marshaller.finish(); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 687c4bed..b3d51854 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -80,7 +80,6 @@ import java.io.InputStream; import java.io.InvalidClassException; import java.io.IOException; -import java.io.OutputStream; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.util.Base64; @@ -419,10 +418,8 @@ public void writeInvocationResult(Object result) { // if (output.getSessionAffinity() != null) { // exchange.setResponseCookie(new CookieImpl("JSESSIONID", output.getSessionAffinity()).setPath(WILDFLY_SERVICES)); // } - OutputStream outputStream = exchange.getOutputStream(); - final ByteOutput byteOutput = byteOutputOf(outputStream); - try (byteOutput) { - marshaller.start(byteOutput); + try (final ByteOutput out = byteOutputOf(exchange.getOutputStream())) { + marshaller.start(out); serializeObject(marshaller, result); serializeMap(marshaller, contextData); marshaller.finish(); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java index 118a5d1c..ef8f8c69 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java @@ -76,8 +76,8 @@ private ObjectHttpMarshaller(final Marshaller marshaller, final Object object) { } @Override - public void marshall(final OutputStream httpBodyRequestStream) throws Exception { - try (ByteOutput out = byteOutputOf(httpBodyRequestStream)) { + public void marshall(final OutputStream os) throws Exception { + try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); serializeObject(marshaller, object); marshaller.finish(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java index bcde6bf5..5cae47e4 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java @@ -75,8 +75,8 @@ private XidHttpMarshaller(final Marshaller marshaller, final Xid xid) { } @Override - public void marshall(final OutputStream httpBodyRequestStream) throws Exception { - try (ByteOutput out = byteOutputOf(httpBodyRequestStream)) { + public void marshall(final OutputStream os) throws Exception { + try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); serializeXid(marshaller, xid); marshaller.finish(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index f4ea0a40..59b1ba23 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -190,15 +190,14 @@ protected void processRequest(final HttpServerExchange exchange) { final LocalTransaction transaction = ctx.beginTransaction(timeout); final Xid xid = xidResolver.apply(transaction); - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - final ByteOutput byteOutput = byteOutputOf(out); - try (byteOutput) { - Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(); - marshaller.start(byteOutput); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(); + try (ByteOutput out = byteOutputOf(baos)) { + marshaller.start(out); serializeXid(marshaller, xid); marshaller.finish(); } - exchange.getResponseSender().send(ByteBuffer.wrap(out.toByteArray())); + exchange.getResponseSender().send(ByteBuffer.wrap(baos.toByteArray())); } catch (Exception e) { sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); } From 357e42034040f1bb9e2a44cae952b42488daa9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 29 Nov 2024 15:43:53 +0100 Subject: [PATCH 05/29] [WEJBHTTP-139] Removing remaining deprecated methods & constructors --- .../java/org/wildfly/httpclient/common/HttpServerHelper.java | 4 ---- .../main/java/org/wildfly/httpclient/ejb/ServerHandlers.java | 5 ----- 2 files changed, 9 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java index 68932d26..744d3985 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java @@ -59,8 +59,4 @@ public static void sendException(HttpServerExchange exchange, HttpServiceConfig } } - @Deprecated - public static void sendException(HttpServerExchange exchange, int status, Throwable e) { - sendException(exchange, HttpServiceConfig.getInstance(), status, e); - } } diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index b3d51854..2c88378d 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -689,11 +689,6 @@ private static final class HttpDiscoveryHandler extends AbstractEjbHandler { private final Set availableModules = new HashSet<>(); private final HttpServiceConfig httpServiceConfig; - @Deprecated - public HttpDiscoveryHandler(ExecutorService executorService, Association association) { - this (executorService, association, HttpServiceConfig.DEFAULT); - } - public HttpDiscoveryHandler(ExecutorService executorService, Association association, HttpServiceConfig httpServiceConfig) { super(executorService); association.registerModuleAvailabilityListener(new ModuleAvailabilityListener() { From 1dde42a358135d8c45a52e359aa50daa39fbb3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 29 Nov 2024 15:53:39 +0100 Subject: [PATCH 06/29] [WEJBHTTP-139] Refactoring - use HttpServerHelper utility method via static imports --- .../httpclient/ejb/ServerHandlers.java | 26 +++++++++---------- .../httpclient/naming/ServerHandlers.java | 4 +-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 2c88378d..527df159 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -18,6 +18,7 @@ package org.wildfly.httpclient.ejb; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.ejb.Constants.EJB_DISCOVERY_RESPONSE; import static org.wildfly.httpclient.ejb.Constants.EJB_RESPONSE_NEW_SESSION; import static org.wildfly.httpclient.ejb.Constants.EJB_SESSION_ID; @@ -67,7 +68,6 @@ import org.wildfly.httpclient.common.ContentType; import org.wildfly.httpclient.common.ElytronIdentityHandler; import org.wildfly.httpclient.common.HttpMarshallerFactory; -import org.wildfly.httpclient.common.HttpServerHelper; import org.wildfly.httpclient.common.HttpServiceConfig; import org.wildfly.common.annotation.NotNull; import org.wildfly.security.auth.server.SecurityIdentity; @@ -267,7 +267,7 @@ public void writeNoSuchMethod() { if(identifier != null) { cancellationFlags.remove(identifier); } - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.noSuchMethod()); + sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.noSuchMethod()); } @Override @@ -275,7 +275,7 @@ public void writeSessionNotActive() { if(identifier != null) { cancellationFlags.remove(identifier); } - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.sessionNotActive()); + sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.sessionNotActive()); } @Override @@ -283,7 +283,7 @@ public void writeWrongViewType() { if(identifier != null) { cancellationFlags.remove(identifier); } - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); + sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); } @Override @@ -316,7 +316,7 @@ public void writeException(@NotNull Exception exception) { if(identifier != null) { cancellationFlags.remove(identifier); } - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, exception); + sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, exception); } @Override @@ -324,7 +324,7 @@ public void writeNoSuchEJB() { if(identifier != null) { cancellationFlags.remove(identifier); } - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, new NoSuchEJBException()); + sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, new NoSuchEJBException()); } @Override @@ -340,7 +340,7 @@ public void writeNotStateful() { if(identifier != null) { cancellationFlags.remove(identifier); } - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); + sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); } @Override @@ -426,7 +426,7 @@ public void writeInvocationResult(Object result) { } exchange.endExchange(); } catch (Exception e) { - HttpServerHelper.sendException(exchange, httpServiceConfig, 500, e); + sendException(exchange, httpServiceConfig, 500, e); } } } @@ -569,7 +569,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { unmarshaller.finish(); } } catch (Exception e) { - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, e); return; } final Transaction transaction; @@ -633,17 +633,17 @@ public SecurityIdentity getSecurityIdentity() { @Override public void writeException(@NotNull Exception exception) { - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, exception); + sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, exception); } @Override public void writeNoSuchEJB() { - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, new NoSuchEJBException()); + sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, new NoSuchEJBException()); } @Override public void writeWrongViewType() { - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); + sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); } @Override @@ -653,7 +653,7 @@ public void writeCancelResponse() { @Override public void writeNotStateful() { - HttpServerHelper.sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); + sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); } @Override diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index b62fd4eb..9334b5be 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -19,6 +19,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.naming.Constants.NAME_PATH_PARAMETER; import static org.wildfly.httpclient.naming.Constants.NEW_QUERY_PARAMETER; import static org.wildfly.httpclient.naming.Constants.VALUE; @@ -38,7 +39,6 @@ import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.ContentType; import org.wildfly.httpclient.common.HttpMarshallerFactory; -import org.wildfly.httpclient.common.HttpServerHelper; import org.wildfly.httpclient.common.HttpServiceConfig; import javax.naming.Binding; @@ -141,7 +141,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { } } } catch (Throwable e) { - HttpServerHelper.sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); } } From 85b5810a1cb7ccab1ba99ab3aea5cd7b5eba1522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 5 Dec 2024 13:13:35 +0100 Subject: [PATCH 07/29] [WEJBHTTP-139] Refactoring - merge transaction/Utils , naming/Utils & ejb/Utils to common/Utils --- .../org/wildfly/httpclient/common}/Utils.java | 29 ++++++++-- .../ejb/HttpEJBDiscoveryProvider.java | 2 +- .../org/wildfly/httpclient/ejb/Utils.java | 56 ------------------- .../httpclient/naming/HttpRootContext.java | 4 +- .../HttpRemoteTransactionHandle.java | 2 +- .../HttpRemoteTransactionPeer.java | 2 +- .../HttpSubordinateTransactionHandle.java | 2 +- .../wildfly/httpclient/transaction/Utils.java | 56 ------------------- 8 files changed, 30 insertions(+), 123 deletions(-) rename {naming/src/main/java/org/wildfly/httpclient/naming => common/src/main/java/org/wildfly/httpclient/common}/Utils.java (59%) delete mode 100644 ejb/src/main/java/org/wildfly/httpclient/ejb/Utils.java delete mode 100644 transaction/src/main/java/org/wildfly/httpclient/transaction/Utils.java diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/Utils.java b/common/src/main/java/org/wildfly/httpclient/common/Utils.java similarity index 59% rename from naming/src/main/java/org/wildfly/httpclient/naming/Utils.java rename to common/src/main/java/org/wildfly/httpclient/common/Utils.java index e0b7ac72..82feef9f 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/Utils.java +++ b/common/src/main/java/org/wildfly/httpclient/common/Utils.java @@ -16,25 +16,24 @@ * limitations under the License. */ -package org.wildfly.httpclient.naming; +package org.wildfly.httpclient.common; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.ObjectResolver; import org.jboss.marshalling.Unmarshaller; -import org.wildfly.httpclient.common.HttpMarshallerFactory; import java.util.concurrent.CompletableFuture; /** * @author Richard Opalka */ -final class Utils { +public final class Utils { private Utils() { // forbidden instantiation } - static Marshaller newMarshaller(final ObjectResolver objectResolver, final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { + public static Marshaller newMarshaller(final ObjectResolver objectResolver, final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { Marshaller marshaller = null; try { marshaller = objectResolver != null ? factory.createMarshaller(objectResolver) : factory.createMarshaller(); @@ -44,7 +43,7 @@ static Marshaller newMarshaller(final ObjectResolver objectResolver, final H return marshaller; } - static Unmarshaller newUnmarshaller(final ObjectResolver objectResolver, final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { + public static Unmarshaller newUnmarshaller(final ObjectResolver objectResolver, final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { Unmarshaller unmarshaller = null; try { unmarshaller = objectResolver != null ? factory.createUnmarshaller(objectResolver) : factory.createUnmarshaller(); @@ -54,4 +53,24 @@ static Unmarshaller newUnmarshaller(final ObjectResolver objectResolver, fin return unmarshaller; } + public static Marshaller newMarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { + Marshaller marshaller = null; + try { + marshaller = factory.createMarshaller(); + } catch (Exception e) { + failureHandler.completeExceptionally(e); + } + return marshaller; + } + + public static Unmarshaller newUnmarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { + Unmarshaller unmarshaller = null; + try { + unmarshaller = factory.createUnmarshaller(); + } catch (Exception e) { + failureHandler.completeExceptionally(e); + } + return unmarshaller; + } + } diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java index 02ac1132..9bb3f8fa 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java @@ -22,7 +22,7 @@ import static org.wildfly.httpclient.ejb.Constants.HTTPS_SCHEME; import static org.wildfly.httpclient.ejb.Constants.HTTP_SCHEME; import static org.wildfly.httpclient.ejb.ClientHandlers.discoveryHttpResultHandler; -import static org.wildfly.httpclient.ejb.Utils.newUnmarshaller; +import static org.wildfly.httpclient.common.Utils.newUnmarshaller; import io.undertow.client.ClientRequest; import org.jboss.ejb.client.EJBClientConnection; diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/Utils.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/Utils.java deleted file mode 100644 index 42d5d911..00000000 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/Utils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2024 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * 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 org.wildfly.httpclient.ejb; - -import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.Unmarshaller; -import org.wildfly.httpclient.common.HttpMarshallerFactory; - -import java.util.concurrent.CompletableFuture; - -/** - * @author Richard Opalka - */ -final class Utils { - - private Utils() { - // forbidden instantiation - } - - static Marshaller newMarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Marshaller marshaller = null; - try { - marshaller = factory.createMarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return marshaller; - } - - static Unmarshaller newUnmarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Unmarshaller unmarshaller = null; - try { - unmarshaller = factory.createUnmarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return unmarshaller; - } - -} diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java index 3c8c03c7..139fdecc 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java @@ -37,8 +37,8 @@ import static org.wildfly.httpclient.naming.RequestType.REBIND; import static org.wildfly.httpclient.naming.RequestType.RENAME; import static org.wildfly.httpclient.naming.RequestType.UNBIND; -import static org.wildfly.httpclient.naming.Utils.newMarshaller; -import static org.wildfly.httpclient.naming.Utils.newUnmarshaller; +import static org.wildfly.httpclient.common.Utils.newMarshaller; +import static org.wildfly.httpclient.common.Utils.newUnmarshaller; import io.undertow.client.ClientRequest; import org.jboss.marshalling.Marshaller; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java index 8f2fb1ff..a41a198b 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java @@ -20,7 +20,7 @@ import static org.wildfly.httpclient.transaction.ClientHandlers.emptyHttpResultHandler; import static org.wildfly.httpclient.transaction.ClientHandlers.xidHttpMarshaller; -import static org.wildfly.httpclient.transaction.Utils.newMarshaller; +import static org.wildfly.httpclient.common.Utils.newMarshaller; import io.undertow.client.ClientRequest; import org.jboss.marshalling.Marshaller; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java index c55adf27..982ac7ee 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java @@ -22,7 +22,7 @@ import static org.wildfly.httpclient.transaction.ClientHandlers.xidArrayHttpResultHandler; import static org.wildfly.httpclient.transaction.ClientHandlers.xidHttpResultHandler; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; -import static org.wildfly.httpclient.transaction.Utils.newUnmarshaller; +import static org.wildfly.httpclient.common.Utils.newUnmarshaller; import io.undertow.client.ClientRequest; import org.jboss.marshalling.Unmarshaller; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java index 32423dc0..be672caa 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java @@ -26,7 +26,7 @@ import static org.wildfly.httpclient.transaction.RequestType.XA_FORGET; import static org.wildfly.httpclient.transaction.RequestType.XA_PREPARE; import static org.wildfly.httpclient.transaction.RequestType.XA_ROLLBACK; -import static org.wildfly.httpclient.transaction.Utils.newMarshaller; +import static org.wildfly.httpclient.common.Utils.newMarshaller; import io.undertow.client.ClientRequest; import io.undertow.client.ClientResponse; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/Utils.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/Utils.java deleted file mode 100644 index fb5b7af8..00000000 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/Utils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2024 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * 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 org.wildfly.httpclient.transaction; - -import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.Unmarshaller; -import org.wildfly.httpclient.common.HttpMarshallerFactory; - -import java.util.concurrent.CompletableFuture; - -/** - * @author Richard Opalka - */ -final class Utils { - - private Utils() { - // forbidden instantiation - } - - static Marshaller newMarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Marshaller marshaller = null; - try { - marshaller = factory.createMarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return marshaller; - } - - static Unmarshaller newUnmarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Unmarshaller unmarshaller = null; - try { - unmarshaller = factory.createUnmarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return unmarshaller; - } - -} From c4208dad9dd140f0e8312517f61cbfa95f6dd528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 5 Dec 2024 13:38:09 +0100 Subject: [PATCH 08/29] [WEJBHTTP-139] Refactoring - merge common/Utils & common/HttpMarshallerFactory --- .../common/HttpMarshallerFactory.java | 67 ++++++++++++++++ .../org/wildfly/httpclient/common/Utils.java | 76 ------------------- .../ejb/HttpEJBDiscoveryProvider.java | 3 +- .../httpclient/naming/HttpRootContext.java | 6 +- .../HttpRemoteTransactionHandle.java | 5 +- .../HttpRemoteTransactionPeer.java | 5 +- .../HttpSubordinateTransactionHandle.java | 3 +- 7 files changed, 75 insertions(+), 90 deletions(-) delete mode 100644 common/src/main/java/org/wildfly/httpclient/common/Utils.java diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpMarshallerFactory.java b/common/src/main/java/org/wildfly/httpclient/common/HttpMarshallerFactory.java index a0de4de2..4c7dd3c8 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpMarshallerFactory.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpMarshallerFactory.java @@ -30,6 +30,7 @@ import org.wildfly.common.annotation.NotNull; import java.io.IOException; +import java.util.concurrent.CompletableFuture; /** * Creates {@link Marshaller} objects for reading and writing requests and responses objects as bytes. @@ -124,6 +125,72 @@ public Marshaller createMarshaller(@NotNull ClassResolver resolver, @NotNull Obj return RIVER_MARSHALLER_FACTORY.createMarshaller(marshallingConfiguration); } + /** + * Creates a {@code Marshaller} configured with an optional object resolver. + * + * @param resolver responsible for substituting objects when marshalling. This parameter can be null. + * @param failureHandler to store exception if an I/O error occurs during marshaller creation + * @return the marshaller or null if exception have been stored in failureHandler + */ + public Marshaller createMarshaller(final ObjectResolver resolver, @NotNull final CompletableFuture failureHandler) { + Marshaller marshaller = null; + try { + marshaller = resolver != null ? createMarshaller(resolver) : createMarshaller(); + } catch (Exception e) { + failureHandler.completeExceptionally(e); + } + return marshaller; + } + + /** + * Creates a {@code Unmarshaller} configured with an optional object resolver. + * + * @param resolver responsible for substituting objects when unmarshalling. This parameter can be null. + * @param failureHandler to store exception if an I/O error occurs during marshaller creation + * @return the unmarshaller or null if exception have been stored in failureHandler + */ + public Unmarshaller createUnmarshaller(final ObjectResolver resolver, @NotNull final CompletableFuture failureHandler) { + Unmarshaller unmarshaller = null; + try { + unmarshaller = resolver != null ? createUnmarshaller(resolver) : createUnmarshaller(); + } catch (Exception e) { + failureHandler.completeExceptionally(e); + } + return unmarshaller; + } + + /** + * Creates a simple {@code Marshaller}. + * + * @param failureHandler to store exception if an I/O error occurs during marshaller creation + * @return the marshaller or null if exception have been stored in failureHandler + */ + public Marshaller createMarshaller(@NotNull final CompletableFuture failureHandler) { + Marshaller marshaller = null; + try { + marshaller = createMarshaller(); + } catch (Exception e) { + failureHandler.completeExceptionally(e); + } + return marshaller; + } + + /** + * Creates a simple {@code Unmarshaller}. + * + * @param failureHandler to store exception if an I/O error occurs during unmarshaller creation + * @return the unmarshaller or null if exception have been stored in failureHandler + */ + public Unmarshaller createUnmarshaller(@NotNull final CompletableFuture failureHandler) { + Unmarshaller unmarshaller = null; + try { + unmarshaller = createUnmarshaller(); + } catch (Exception e) { + failureHandler.completeExceptionally(e); + } + return unmarshaller; + } + /** * Creates a simple {@code Unmarshaller}. * diff --git a/common/src/main/java/org/wildfly/httpclient/common/Utils.java b/common/src/main/java/org/wildfly/httpclient/common/Utils.java deleted file mode 100644 index 82feef9f..00000000 --- a/common/src/main/java/org/wildfly/httpclient/common/Utils.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * JBoss, Home of Professional Open Source. - * Copyright 2024 Red Hat, Inc., and individual contributors - * as indicated by the @author tags. - * - * 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 org.wildfly.httpclient.common; - -import org.jboss.marshalling.Marshaller; -import org.jboss.marshalling.ObjectResolver; -import org.jboss.marshalling.Unmarshaller; - -import java.util.concurrent.CompletableFuture; - -/** - * @author Richard Opalka - */ -public final class Utils { - - private Utils() { - // forbidden instantiation - } - - public static Marshaller newMarshaller(final ObjectResolver objectResolver, final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Marshaller marshaller = null; - try { - marshaller = objectResolver != null ? factory.createMarshaller(objectResolver) : factory.createMarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return marshaller; - } - - public static Unmarshaller newUnmarshaller(final ObjectResolver objectResolver, final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Unmarshaller unmarshaller = null; - try { - unmarshaller = objectResolver != null ? factory.createUnmarshaller(objectResolver) : factory.createUnmarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return unmarshaller; - } - - public static Marshaller newMarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Marshaller marshaller = null; - try { - marshaller = factory.createMarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return marshaller; - } - - public static Unmarshaller newUnmarshaller(final HttpMarshallerFactory factory, final CompletableFuture failureHandler) { - Unmarshaller unmarshaller = null; - try { - unmarshaller = factory.createUnmarshaller(); - } catch (Exception e) { - failureHandler.completeExceptionally(e); - } - return unmarshaller; - } - -} diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java index 9bb3f8fa..eb8c1c1d 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpEJBDiscoveryProvider.java @@ -22,7 +22,6 @@ import static org.wildfly.httpclient.ejb.Constants.HTTPS_SCHEME; import static org.wildfly.httpclient.ejb.Constants.HTTP_SCHEME; import static org.wildfly.httpclient.ejb.ClientHandlers.discoveryHttpResultHandler; -import static org.wildfly.httpclient.common.Utils.newUnmarshaller; import io.undertow.client.ClientRequest; import org.jboss.ejb.client.EJBClientConnection; @@ -159,7 +158,7 @@ private void discoverFromConnection(final EJBClientConnection connection, final final ClientRequest request = builder.createRequest(targetContext.getUri().getPath()); final CompletableFuture> result = new CompletableFuture<>(); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); - final Unmarshaller unmarshaller = newUnmarshaller(marshallerFactory, result); + final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(result); if (unmarshaller != null) { targetContext.sendRequest(request, sslContext, authenticationConfiguration, null, discoveryHttpResultHandler(unmarshaller, result), diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java index 139fdecc..b1ee6b81 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java @@ -37,8 +37,6 @@ import static org.wildfly.httpclient.naming.RequestType.REBIND; import static org.wildfly.httpclient.naming.RequestType.RENAME; import static org.wildfly.httpclient.naming.RequestType.UNBIND; -import static org.wildfly.httpclient.common.Utils.newMarshaller; -import static org.wildfly.httpclient.common.Utils.newUnmarshaller; import io.undertow.client.ClientRequest; import org.jboss.marshalling.Marshaller; @@ -272,7 +270,7 @@ private Object performOperation(Name name, URI providerUri, HttpTargetContext ta final CompletableFuture result = new CompletableFuture<>(); final ObjectResolver objectResolver = getObjectResolver(providerUri); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(clientRequest); - final Unmarshaller unmarshaller = newUnmarshaller(objectResolver, marshallerFactory, result); + final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(objectResolver, result); if (unmarshaller != null) { targetContext.sendRequest(clientRequest, sslContext, authenticationConfiguration, null, optionalObjectHttpResultHandler(unmarshaller, result, httpNamingProvider, getContextClassLoader()), @@ -323,7 +321,7 @@ private void performOperation(URI providerUri, Object object, HttpTargetContext final CompletableFuture result = new CompletableFuture<>(); final ObjectResolver objectResolver = getObjectResolver(providerUri); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(clientRequest); - final Marshaller marshaller = newMarshaller(objectResolver, marshallerFactory, result); + final Marshaller marshaller = marshallerFactory.createMarshaller(objectResolver, result); if (marshaller != null) { targetContext.sendRequest(clientRequest, sslContext, authenticationConfiguration, object != null ? objectHttpMarshaller(marshaller, object) : null, emptyHttpResultHandler(result, null), result::completeExceptionally, null, null); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java index a41a198b..b9a47132 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionHandle.java @@ -20,7 +20,6 @@ import static org.wildfly.httpclient.transaction.ClientHandlers.emptyHttpResultHandler; import static org.wildfly.httpclient.transaction.ClientHandlers.xidHttpMarshaller; -import static org.wildfly.httpclient.common.Utils.newMarshaller; import io.undertow.client.ClientRequest; import org.jboss.marshalling.Marshaller; @@ -86,7 +85,7 @@ public void commit() throws RollbackException, HeuristicMixedException, Heuristi final CompletableFuture result = new CompletableFuture<>(); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); - final Marshaller marshaller = newMarshaller(marshallerFactory, result); + final Marshaller marshaller = marshallerFactory.createMarshaller(result); if (marshaller != null) { targetContext.sendRequest(request, sslContext, authenticationConfiguration, xidHttpMarshaller(marshaller, id), emptyHttpResultHandler(result, null), result::completeExceptionally, null, null); @@ -141,7 +140,7 @@ public void rollback() throws SecurityException, SystemException { final CompletableFuture result = new CompletableFuture<>(); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); - final Marshaller marshaller = newMarshaller(marshallerFactory, result); + final Marshaller marshaller = marshallerFactory.createMarshaller(result); if (marshaller != null) { targetContext.sendRequest(request, sslContext, authenticationConfiguration, xidHttpMarshaller(marshaller, id), emptyHttpResultHandler(result, null), result::completeExceptionally, null, null); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java index 982ac7ee..22211429 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionPeer.java @@ -22,7 +22,6 @@ import static org.wildfly.httpclient.transaction.ClientHandlers.xidArrayHttpResultHandler; import static org.wildfly.httpclient.transaction.ClientHandlers.xidHttpResultHandler; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; -import static org.wildfly.httpclient.common.Utils.newUnmarshaller; import io.undertow.client.ClientRequest; import org.jboss.marshalling.Unmarshaller; @@ -91,7 +90,7 @@ public Xid[] recover(int flag, String parentName) throws XAException { final CompletableFuture result = new CompletableFuture<>(); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); - final Unmarshaller unmarshaller = newUnmarshaller(marshallerFactory, result); + final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(result); if (unmarshaller != null) { targetContext.sendRequest(request, sslContext, authenticationConfiguration, null, xidArrayHttpResultHandler(unmarshaller, result), result::completeExceptionally, NEW_TRANSACTION, null); @@ -126,7 +125,7 @@ public SimpleTransactionControl begin(int timeout) throws SystemException { final CompletableFuture result = new CompletableFuture<>(); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); - final Unmarshaller unmarshaller = newUnmarshaller(marshallerFactory, result); + final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(result); if (unmarshaller != null) { targetContext.sendRequest(request, sslContext, authenticationConfiguration, null, xidHttpResultHandler(unmarshaller, result), result::completeExceptionally, NEW_TRANSACTION, null); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java index be672caa..d6c5f580 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java @@ -26,7 +26,6 @@ import static org.wildfly.httpclient.transaction.RequestType.XA_FORGET; import static org.wildfly.httpclient.transaction.RequestType.XA_PREPARE; import static org.wildfly.httpclient.transaction.RequestType.XA_ROLLBACK; -import static org.wildfly.httpclient.common.Utils.newMarshaller; import io.undertow.client.ClientRequest; import io.undertow.client.ClientResponse; @@ -110,7 +109,7 @@ private T processOperation(RequestType requestType, Function result = new CompletableFuture<>(); final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); - final Marshaller marshaller = newMarshaller(marshallerFactory, result); + final Marshaller marshaller = marshallerFactory.createMarshaller(result); if (marshaller != null) { targetContext.sendRequest(request, sslContext, authenticationConfiguration, xidHttpMarshaller(marshaller, id), emptyHttpResultHandler(result, resultFunction), result::completeExceptionally, null, null); From d2c8137055525f0fb7ece5e3ebd0e67df06b9817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 6 Dec 2024 11:37:25 +0100 Subject: [PATCH 09/29] [WEJBHTTP-139] Refactoring - unify parameter names in Serializers --- .../wildfly/httpclient/ejb/Serializer.java | 32 +++++++++---------- .../wildfly/httpclient/naming/Serializer.java | 4 +-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java index 69ca0b65..823eb534 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java @@ -48,8 +48,8 @@ private Serializer() { // forbidden instantiation } - static void serializeObject(final ObjectOutput out, final Object o) throws IOException { - out.writeObject(o); + static void serializeObject(final ObjectOutput out, final Object object) throws IOException { + out.writeObject(object); } static Object deserializeObject(final ObjectInput in) throws IOException, ClassNotFoundException { @@ -81,18 +81,18 @@ static void serializePackedInteger(final ObjectOutput out, int value) throws IOE } } - static int deserializePackedInteger(final ObjectInput input) throws IOException { - int ret = input.readByte(); + static int deserializePackedInteger(final ObjectInput in) throws IOException { + int ret = in.readByte(); if ((ret & 0x80) == 0x80) { - return deserializePackedInteger(input) << 7 | (ret & 0x7F); + return deserializePackedInteger(in) << 7 | (ret & 0x7F); } return ret; } - static void serializeMap(final ObjectOutput out, final Map contextData) throws IOException { - int size = contextData != null ? contextData.size() : 0; + static void serializeMap(final ObjectOutput out, final Map map) throws IOException { + int size = map != null ? map.size() : 0; serializePackedInteger(out, size); - if (size > 0) for (Map.Entry entry : contextData.entrySet()) { + if (size > 0) for (Map.Entry entry : map.entrySet()) { out.writeObject(entry.getKey()); out.writeObject(entry.getValue()); } @@ -114,10 +114,10 @@ static Map deserializeMap(final ObjectInput in) throws IOExcepti return ret; } - static void serializeSet(final ObjectOutput out, final Set modules) throws IOException { - out.writeInt(modules.size()); - for (EJBModuleIdentifier ejbModuleIdentifier : modules) { - out.writeObject(ejbModuleIdentifier); + static void serializeSet(final ObjectOutput out, final Set set) throws IOException { + out.writeInt(set.size()); + for (EJBModuleIdentifier moduleId : set) { + out.writeObject(moduleId); } } @@ -130,18 +130,18 @@ static Set deserializeSet(final ObjectInput in) throws IOEx return ret; } - static void serializeTransaction(final ObjectOutput out, final TransactionInfo transaction) throws IOException { - final byte transactionType = transaction.getType(); + static void serializeTransaction(final ObjectOutput out, final TransactionInfo txn) throws IOException { + final byte transactionType = txn.getType(); out.writeByte(transactionType); if (transactionType == NULL_TRANSACTION) { return; } - serializeXid(out, transaction.getXid()); + serializeXid(out, txn.getXid()); if (transactionType == REMOTE_TRANSACTION) { return; } if (transactionType == LOCAL_TRANSACTION) { - out.writeInt(transaction.getRemainingTime()); + out.writeInt(txn.getRemainingTime()); } } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java b/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java index f8e3503d..9aa7a4fb 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java @@ -33,8 +33,8 @@ private Serializer() { // forbidden instantiation } - static void serializeObject(final ObjectOutput out, final Object o) throws IOException { - out.writeObject(o); + static void serializeObject(final ObjectOutput out, final Object object) throws IOException { + out.writeObject(object); } static Object deserializeObject(final ObjectInput in) throws IOException, ClassNotFoundException { From 11abf958100478ba5795f4ef92c275244abd63a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 6 Dec 2024 11:51:09 +0100 Subject: [PATCH 10/29] [WEJBHTTP-139] Refactoring - unify parameter names in ClientHandlers --- .../httpclient/ejb/ClientHandlers.java | 80 +++++++++---------- .../httpclient/naming/ClientHandlers.java | 16 ++-- .../transaction/ClientHandlers.java | 14 ++-- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index 35cec2db..55ce58cc 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -60,12 +60,12 @@ private ClientHandlers() { // forbidden instantiation } - static HttpTargetContext.HttpMarshaller invokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo, final Object[] parameters, final Map contextData) { - return new InvokeHttpMarshaller(marshaller, txnInfo, parameters, contextData); + static HttpTargetContext.HttpMarshaller invokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn, final Object[] objects, final Map map) { + return new InvokeHttpMarshaller(marshaller, txn, objects, map); } - static HttpTargetContext.HttpMarshaller createSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo) { - return new CreateSessionHttpMarshaller(marshaller, txnInfo); + static HttpTargetContext.HttpMarshaller createSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn) { + return new CreateSessionHttpMarshaller(marshaller, txn); } static HttpTargetContext.HttpResultHandler emptyHttpResultHandler(final CompletableFuture result, final Function function) { @@ -76,8 +76,8 @@ static HttpTargetContext.HttpResultHandler discoveryHttpResultHandler(final Unma return new DiscoveryHttpResultHandler(unmarshaller, result); } - static HttpTargetContext.HttpResultHandler invokeHttpResultHandler(final Unmarshaller unmarshaller, final EJBReceiverInvocationContext receiverContext, final EJBClientInvocationContext clientInvocationContext) { - return new EjbClassLoaderAwareHttpResultHandler(unmarshaller, receiverContext, clientInvocationContext); + static HttpTargetContext.HttpResultHandler invokeHttpResultHandler(final Unmarshaller unmarshaller, final EJBReceiverInvocationContext receiverCtx, final EJBClientInvocationContext clientCtx) { + return new EjbClassLoaderAwareHttpResultHandler(unmarshaller, receiverCtx, clientCtx); } static Function cancelInvocationResponseFunction() { @@ -94,24 +94,24 @@ private static HttpTargetContext.HttpResultHandler invokeHttpResultHandlerIntern private static final class InvokeHttpMarshaller implements HttpTargetContext.HttpMarshaller { private final Marshaller marshaller; - private final TransactionInfo txnInfo; - private final Object[] parameters; - private final Map contextData; + private final TransactionInfo txn; + private final Object[] objects; + private final Map map; - private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo, final Object[] parameters, final Map contextData) { + private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn, final Object[] objects, final Map map) { this.marshaller = marshaller; - this.txnInfo = txnInfo; - this.parameters = parameters; - this.contextData = contextData; + this.txn = txn; + this.objects = objects; + this.map = map; } @Override public void marshall(final OutputStream os) throws Exception { try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); - serializeTransaction(marshaller, txnInfo); - serializeObjectArray(marshaller, parameters); - serializeMap(marshaller, contextData); + serializeTransaction(marshaller, txn); + serializeObjectArray(marshaller, objects); + serializeMap(marshaller, map); marshaller.finish(); } } @@ -119,18 +119,18 @@ public void marshall(final OutputStream os) throws Exception { private static final class CreateSessionHttpMarshaller implements HttpTargetContext.HttpMarshaller { private final Marshaller marshaller; - private final TransactionInfo txnInfo; + private final TransactionInfo txn; - private CreateSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo) { + private CreateSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn) { this.marshaller = marshaller; - this.txnInfo = txnInfo; + this.txn = txn; } @Override public void marshall(final OutputStream os) throws Exception { try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); - serializeTransaction(marshaller, txnInfo); + serializeTransaction(marshaller, txn); marshaller.finish(); } } @@ -146,7 +146,7 @@ private EmptyHttpResultHandler(final CompletableFuture result, final Function } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { try { result.complete(function != null ? function.apply(httpResponse) : null); } finally { @@ -156,8 +156,8 @@ public void handleResult(final InputStream httpBodyResponseStream, final ClientR } private static final class DiscoveryHttpResultHandler implements HttpTargetContext.HttpResultHandler { - private final CompletableFuture> result; private final Unmarshaller unmarshaller; + private final CompletableFuture> result; private DiscoveryHttpResultHandler(final Unmarshaller unmarshaller, final CompletableFuture> result) { this.unmarshaller = unmarshaller; @@ -165,8 +165,8 @@ private DiscoveryHttpResultHandler(final Unmarshaller unmarshaller, final Comple } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(httpBodyResponseStream)) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + try (ByteInput in = new InputStreamByteInput(is)) { Set modules; unmarshaller.start(in); modules = deserializeSet(unmarshaller); @@ -188,36 +188,36 @@ private static final class EjbClassLoaderAwareHttpResultHandler implements HttpT WELL_KNOWN_KEYS.add("jboss.source.address"); } - private final EJBReceiverInvocationContext receiverContext; - private final EJBClientInvocationContext clientInvocationContext; private final Unmarshaller unmarshaller; + private final EJBReceiverInvocationContext receiverCtx; + private final EJBClientInvocationContext clientCtx; - private EjbClassLoaderAwareHttpResultHandler(final Unmarshaller unmarshaller, final EJBReceiverInvocationContext receiverContext, final EJBClientInvocationContext clientInvocationContext) { + private EjbClassLoaderAwareHttpResultHandler(final Unmarshaller unmarshaller, final EJBReceiverInvocationContext receiverCtx, final EJBClientInvocationContext clientCtx) { this.unmarshaller = unmarshaller; - this.receiverContext = receiverContext; - this.clientInvocationContext = clientInvocationContext; + this.receiverCtx = receiverCtx; + this.clientCtx = clientCtx; } - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { - receiverContext.resultReady(new EJBReceiverInvocationContext.ResultProducer() { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + receiverCtx.resultReady(new EJBReceiverInvocationContext.ResultProducer() { @Override public Object getResult() throws Exception { final CompletableFuture result = new CompletableFuture<>(); - invokeHttpResultHandlerInternal(unmarshaller, result).handleResult(httpBodyResponseStream, httpResponse, doneCallback); + invokeHttpResultHandlerInternal(unmarshaller, result).handleResult(is, httpResponse, doneCallback); // WEJBHTTP-83 - remove jboss.returned.keys values from the local context data, so that after unmarshalling the response, we have the correct ContextData - Set returnedContextDataKeys = (Set) clientInvocationContext.getContextData().get(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY); + Set returnedContextDataKeys = (Set) clientCtx.getContextData().get(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY); if(returnedContextDataKeys != null) { - clientInvocationContext.getContextData().keySet().removeIf(k -> (!k.equals(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY))); + clientCtx.getContextData().keySet().removeIf(k -> (!k.equals(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY))); } - Set returnedKeys = (Set) clientInvocationContext.getContextData().get(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY); + Set returnedKeys = (Set) clientCtx.getContextData().get(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY); // If there are any attachments, add them to the client invocation's context data if (result.get().getAttachments() != null) { for (Map.Entry entry : result.get().getAttachments().entrySet()) { if (entry.getValue() != null && ((returnedKeys != null && returnedKeys.contains(entry.getKey())) || WELL_KNOWN_KEYS.contains(entry.getKey()))) { - clientInvocationContext.getContextData().put(entry.getKey(), entry.getValue()); + clientCtx.getContextData().put(entry.getKey(), entry.getValue()); } } } @@ -227,7 +227,7 @@ public Object getResult() throws Exception { @Override public void discardResult() { IoUtils.safeClose(doneCallback); - IoUtils.safeClose(httpBodyResponseStream); + IoUtils.safeClose(is); } }); @@ -235,8 +235,8 @@ public void discardResult() { } private static final class InvokeHttpResultHandler implements HttpTargetContext.HttpResultHandler { - private final CompletableFuture result; private final Unmarshaller unmarshaller; + private final CompletableFuture result; private InvokeHttpResultHandler(final Unmarshaller unmarshaller, final CompletableFuture result) { this.unmarshaller = unmarshaller; @@ -244,8 +244,8 @@ private InvokeHttpResultHandler(final Unmarshaller unmarshaller, final Completab } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(httpBodyResponseStream)) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + try (ByteInput in = new InputStreamByteInput(is)) { unmarshaller.start(in); final Object returned = deserializeObject(unmarshaller); final Map attachments = deserializeMap(unmarshaller); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java index ef8f8c69..184edb90 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java @@ -95,7 +95,7 @@ private EmptyHttpResultHandler(final CompletableFuture result, final Function } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { try { result.complete(function != null ? function.apply(httpResponse) : null); } finally { @@ -105,8 +105,8 @@ public void handleResult(final InputStream httpBodyResponseStream, final ClientR } private static final class OptionalObjectHttpResultHandler implements HttpTargetContext.HttpResultHandler { - private final CompletableFuture result; private final Unmarshaller unmarshaller; + private final CompletableFuture result; private final NamingProvider namingProvider; private final ClassLoader classLoader; @@ -118,15 +118,15 @@ private OptionalObjectHttpResultHandler(final Unmarshaller unmarshaller, final C } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { try { namingProvider.performExceptionAction((a, b) -> { ClassLoader old = setContextClassLoader(classLoader); try { if (httpResponse.getResponseCode() == StatusCodes.NO_CONTENT) { - emptyHttpResultHandler(result, null).handleResult(httpBodyResponseStream, httpResponse, doneCallback); + emptyHttpResultHandler(result, null).handleResult(is, httpResponse, doneCallback); } else { - objectHttpResultHandler(unmarshaller, result).handleResult(httpBodyResponseStream, httpResponse, doneCallback); + objectHttpResultHandler(unmarshaller, result).handleResult(is, httpResponse, doneCallback); } } finally { setContextClassLoader(old); @@ -140,8 +140,8 @@ public void handleResult(final InputStream httpBodyResponseStream, final ClientR } private static final class ObjectHttpResultHandler implements HttpTargetContext.HttpResultHandler { - private final CompletableFuture result; private final Unmarshaller unmarshaller; + private final CompletableFuture result; private ObjectHttpResultHandler(final Unmarshaller unmarshaller, final CompletableFuture result) { this.unmarshaller = unmarshaller; @@ -149,8 +149,8 @@ private ObjectHttpResultHandler(final Unmarshaller unmarshaller, final Completab } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(httpBodyResponseStream)) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + try (ByteInput in = new InputStreamByteInput(is)) { unmarshaller.start(in); Object object = deserializeObject(unmarshaller); unmarshaller.finish(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java index 5cae47e4..c14132b3 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java @@ -94,7 +94,7 @@ private EmptyHttpResultHandler(final CompletableFuture result, final Function } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { try { result.complete(function != null ? function.apply(httpResponse) : null); } finally { @@ -104,8 +104,8 @@ public void handleResult(final InputStream httpBodyResponseStream, final ClientR } private static final class XidHttpResultHandler implements HttpTargetContext.HttpResultHandler { - private final CompletableFuture result; private final Unmarshaller unmarshaller; + private final CompletableFuture result; private XidHttpResultHandler(final Unmarshaller unmarshaller, final CompletableFuture result) { this.unmarshaller = unmarshaller; @@ -113,8 +113,8 @@ private XidHttpResultHandler(final Unmarshaller unmarshaller, final CompletableF } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(httpBodyResponseStream)) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + try (ByteInput in = new InputStreamByteInput(is)) { unmarshaller.start(in); Xid xid = deserializeXid(unmarshaller); unmarshaller.finish(); @@ -128,8 +128,8 @@ public void handleResult(final InputStream httpBodyResponseStream, final ClientR } private static final class XidArrayHttpResultHandler implements HttpTargetContext.HttpResultHandler { - private final CompletableFuture result; private final Unmarshaller unmarshaller; + private final CompletableFuture result; private XidArrayHttpResultHandler(final Unmarshaller unmarshaller, final CompletableFuture result) { this.unmarshaller = unmarshaller; @@ -137,8 +137,8 @@ private XidArrayHttpResultHandler(final Unmarshaller unmarshaller, final Complet } @Override - public void handleResult(final InputStream httpBodyResponseStream, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(httpBodyResponseStream)) { + public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + try (ByteInput in = new InputStreamByteInput(is)) { unmarshaller.start(in); Xid[] ret = deserializeXidArray(unmarshaller); unmarshaller.finish(); From 424ce0e6fbc78e1e8f5651e21ccb3854723941e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 6 Dec 2024 12:29:22 +0100 Subject: [PATCH 11/29] [WEJBHTTP-139] Refactoring - Introducing ByteInputs utility class --- .../wildfly/httpclient/common/ByteInputs.java | 83 +++++++++++++++++++ .../httpclient/common/HttpTargetContext.java | 4 +- .../httpclient/ejb/ClientHandlers.java | 6 +- .../httpclient/ejb/ServerHandlers.java | 10 +-- .../httpclient/naming/ClientHandlers.java | 4 +- .../httpclient/naming/ServerHandlers.java | 6 +- .../transaction/ClientHandlers.java | 6 +- .../transaction/ServerHandlers.java | 6 +- 8 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 common/src/main/java/org/wildfly/httpclient/common/ByteInputs.java diff --git a/common/src/main/java/org/wildfly/httpclient/common/ByteInputs.java b/common/src/main/java/org/wildfly/httpclient/common/ByteInputs.java new file mode 100644 index 00000000..1eca306e --- /dev/null +++ b/common/src/main/java/org/wildfly/httpclient/common/ByteInputs.java @@ -0,0 +1,83 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2024 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * 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 org.wildfly.httpclient.common; + +import org.jboss.marshalling.ByteInput; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Helper class. Provides various utility methods for example: + *
    + *
  • transforming InputStreams to ByteInputs
  • + *
+ * + * @author Richard Opalka + */ +public final class ByteInputs { + + private ByteInputs() { + // forbidden instantiation + } + + public static ByteInput byteInputOf(final InputStream delegate) { + if (delegate == null) throw new IllegalArgumentException(); + return new ByteInputStream(delegate); + } + + private static final class ByteInputStream implements ByteInput { + + private final InputStream delegate; + + private ByteInputStream(final InputStream delegate) { + this.delegate = delegate; + } + + @Override + public int read() throws IOException { + return delegate.read(); + } + + @Override + public int read(final byte[] b) throws IOException { + return delegate.read(b); + } + + @Override + public int read(final byte[] b, final int off, final int len) throws IOException { + return delegate.read(b, off, len); + } + + @Override + public int available() throws IOException { + return delegate.available(); + } + + @Override + public long skip(final long n) throws IOException { + return delegate.skip(n); + } + + @Override + public void close() throws IOException { + delegate.close(); + } + } + +} diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java index 68ae6192..f0e87809 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java @@ -27,6 +27,7 @@ import static io.undertow.util.Headers.IDENTITY; import static io.undertow.util.Headers.SET_COOKIE; import static io.undertow.util.Headers.TRANSFER_ENCODING; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.HeadersHelper.addRequestHeader; import static org.wildfly.httpclient.common.HeadersHelper.containsRequestHeader; import static org.wildfly.httpclient.common.HeadersHelper.getRequestHeader; @@ -44,7 +45,6 @@ import io.undertow.util.HeaderValues; import io.undertow.util.Methods; import io.undertow.util.StatusCodes; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Unmarshaller; import org.wildfly.security.auth.client.AuthenticationConfiguration; import org.wildfly.security.auth.client.AuthenticationContext; @@ -280,7 +280,7 @@ public void completed(ClientExchange result) { throw HttpClientMessages.MESSAGES.invalidContentEncoding(encoding); } } - unmarshaller.start(new InputStreamByteInput(in)); + unmarshaller.start(byteInputOf(in)); Throwable exception = (Throwable) unmarshaller.readObject(); Map attachments = readAttachments(unmarshaller); int read = in.read(); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index 55ce58cc..37dad0ba 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.ejb; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.ejb.Serializer.deserializeObject; import static org.wildfly.httpclient.ejb.Serializer.deserializeSet; @@ -33,7 +34,6 @@ import org.jboss.ejb.client.SessionID; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.HttpTargetContext; @@ -166,7 +166,7 @@ private DiscoveryHttpResultHandler(final Unmarshaller unmarshaller, final Comple @Override public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(is)) { + try (ByteInput in = byteInputOf(is)) { Set modules; unmarshaller.start(in); modules = deserializeSet(unmarshaller); @@ -245,7 +245,7 @@ private InvokeHttpResultHandler(final Unmarshaller unmarshaller, final Completab @Override public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(is)) { + try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); final Object returned = deserializeObject(unmarshaller); final Map attachments = deserializeMap(unmarshaller); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 527df159..0092f335 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.ejb; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.ejb.Constants.EJB_DISCOVERY_RESPONSE; @@ -61,7 +62,6 @@ import org.jboss.ejb.server.ModuleAvailabilityListener; import org.jboss.ejb.server.SessionOpenRequest; import org.jboss.marshalling.ByteOutput; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.SimpleClassResolver; import org.jboss.marshalling.Unmarshaller; @@ -217,8 +217,8 @@ public Resolved getRequestContent(final ClassLoader classLoader) throws IOExcept final HttpMarshallerFactory unmarshallingFactory = httpServiceConfig.getHttpUnmarshallerFactory(exchange); final Unmarshaller unmarshaller = unmarshallingFactory.createUnmarshaller(new FilteringClassResolver(classLoader, classResolverFilter), HttpProtocolV1ObjectTable.INSTANCE); - try (InputStream inputStream = exchange.getInputStream()) { - unmarshaller.start(new InputStreamByteInput(inputStream)); + try (InputStream is = exchange.getInputStream()) { + unmarshaller.start(byteInputOf(is)); final TransactionInfo txnInfo = deserializeTransaction(unmarshaller); final Object[] methodParams = new Object[parameterTypeNames.length]; deserializeObjectArray(unmarshaller, methodParams); @@ -563,8 +563,8 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { final HttpMarshallerFactory httpUnmarshallerFactory = httpServiceConfig.getHttpUnmarshallerFactory(exchange); final Unmarshaller unmarshaller = httpUnmarshallerFactory.createUnmarshaller(HttpProtocolV1ObjectTable.INSTANCE); - try (InputStream inputStream = exchange.getInputStream()) { - unmarshaller.start(new InputStreamByteInput(inputStream)); + try (InputStream is = exchange.getInputStream()) { + unmarshaller.start(byteInputOf(is)); txnInfo = deserializeTransaction(unmarshaller); unmarshaller.finish(); } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java index 184edb90..4c833164 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.naming; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.naming.Serializer.deserializeObject; import static org.wildfly.httpclient.naming.Serializer.serializeObject; @@ -27,7 +28,6 @@ import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.HttpTargetContext; @@ -150,7 +150,7 @@ private ObjectHttpResultHandler(final Unmarshaller unmarshaller, final Completab @Override public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(is)) { + try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); Object object = deserializeObject(unmarshaller); unmarshaller.finish(); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index 9334b5be..86d5e749 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -18,6 +18,7 @@ package org.wildfly.httpclient.naming; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.naming.Constants.NAME_PATH_PARAMETER; @@ -34,7 +35,6 @@ import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.ContextClassResolver; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.ContentType; @@ -47,6 +47,7 @@ import javax.naming.NamingEnumeration; import javax.naming.NamingException; import java.io.IOException; +import java.io.InputStream; import java.io.InvalidClassException; import java.net.URLDecoder; import java.util.Collections; @@ -286,7 +287,8 @@ protected Object doOperation(HttpServerExchange exchange, String name) throws Na return null; } final HttpMarshallerFactory marshallerFactory = config.getHttpUnmarshallerFactory(exchange); - try (ByteInput in = new InputStreamByteInput(exchange.getInputStream())) { + final InputStream is = exchange.getInputStream(); + try (ByteInput in = byteInputOf(is)) { Unmarshaller unmarshaller = classFilter != null ? marshallerFactory.createUnmarshaller(new FilterClassResolver(classFilter)): marshallerFactory.createUnmarshaller(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java index c14132b3..432874b7 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.transaction; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.transaction.Serializer.deserializeXid; import static org.wildfly.httpclient.transaction.Serializer.deserializeXidArray; @@ -25,7 +26,6 @@ import io.undertow.client.ClientResponse; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Unmarshaller; import org.wildfly.httpclient.common.HttpTargetContext; @@ -114,7 +114,7 @@ private XidHttpResultHandler(final Unmarshaller unmarshaller, final CompletableF @Override public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(is)) { + try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); Xid xid = deserializeXid(unmarshaller); unmarshaller.finish(); @@ -138,7 +138,7 @@ private XidArrayHttpResultHandler(final Unmarshaller unmarshaller, final Complet @Override public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { - try (ByteInput in = new InputStreamByteInput(is)) { + try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); Xid[] ret = deserializeXidArray(unmarshaller); unmarshaller.finish(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index 59b1ba23..027b1b5b 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.transaction; +import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; @@ -34,7 +35,6 @@ import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; -import org.jboss.marshalling.InputStreamByteInput; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.Unmarshaller; import org.wildfly.common.function.ExceptionBiFunction; @@ -47,6 +47,7 @@ import javax.transaction.xa.Xid; import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Deque; import java.util.function.Function; @@ -145,8 +146,9 @@ protected void processRequest(final HttpServerExchange exchange) { try { final HttpMarshallerFactory httpMarshallerFactory = config.getHttpUnmarshallerFactory(exchange); final Unmarshaller unmarshaller = httpMarshallerFactory.createUnmarshaller(); + final InputStream is = exchange.getInputStream(); Xid simpleXid; - try (ByteInput in = new InputStreamByteInput(exchange.getInputStream())) { + try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); simpleXid = deserializeXid(unmarshaller); unmarshaller.finish(); From 4509263fe472da6b8cbf62aba6956f4de1b0ec07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 6 Dec 2024 13:27:11 +0100 Subject: [PATCH 12/29] [WEJBHTTP-139] Refactoring - renaming httpServiceConfig variable to config --- .../httpclient/ejb/HttpRemoteEjbService.java | 10 ++-- .../httpclient/ejb/ServerHandlers.java | 50 +++++++++---------- .../naming/HttpRemoteNamingService.java | 10 ++-- .../HttpRemoteTransactionService.java | 10 ++-- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java index 40cb816e..7569ee36 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java @@ -45,7 +45,7 @@ * @author Richard Opalka */ public class HttpRemoteEjbService { - private final HttpServiceConfig httpServiceConfig; + private final HttpServiceConfig config; private final Map cancellationFlags = new ConcurrentHashMap<>(); private final ServerHandlers serverHandlers; @@ -55,9 +55,9 @@ public HttpRemoteEjbService(Association association, ExecutorService executorSer } private HttpRemoteEjbService(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, - Function classResolverFilter, HttpServiceConfig httpServiceConfig) { - this.httpServiceConfig = httpServiceConfig; - this.serverHandlers = ServerHandlers.newInstance(association, executorService, localTransactionContext, classResolverFilter, httpServiceConfig); + Function classResolverFilter, HttpServiceConfig config) { + this.config = config; + this.serverHandlers = ServerHandlers.newInstance(association, executorService, localTransactionContext, classResolverFilter, config); } public HttpHandler createHttpHandler() { @@ -69,7 +69,7 @@ public HttpHandler createHttpHandler() { EncodingHandler encodingHandler = new EncodingHandler(pathHandler, new ContentEncodingRepository().addEncodingHandler(Headers.GZIP.toString(), new GzipEncodingProvider(), 1)); RequestEncodingHandler requestEncodingHandler = new RequestEncodingHandler(encodingHandler); requestEncodingHandler.addEncoding(Headers.GZIP.toString(), GzipStreamSourceConduit.WRAPPER); - return httpServiceConfig.wrap(requestEncodingHandler); + return config.wrap(requestEncodingHandler); } private void registerHandler(final PathHandler pathHandler, final RequestType requestType) { diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 0092f335..c79f1cbe 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -141,18 +141,18 @@ static final class HttpInvocationHandler extends AbstractEjbHandler { private final LocalTransactionContext localTransactionContext; private final Map cancellationFlags; private final Function classResolverFilter; - private final HttpServiceConfig httpServiceConfig; + private final HttpServiceConfig config; HttpInvocationHandler(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, Map cancellationFlags, Function classResolverFilter, - HttpServiceConfig httpServiceConfig) { + HttpServiceConfig config) { super(executorService); this.association = association; this.executorService = executorService; this.localTransactionContext = localTransactionContext; this.cancellationFlags = cancellationFlags; this.classResolverFilter = classResolverFilter; - this.httpServiceConfig = httpServiceConfig; + this.config = config; } @Override @@ -214,7 +214,7 @@ public SocketAddress getLocalAddress() { @Override public Resolved getRequestContent(final ClassLoader classLoader) throws IOException, ClassNotFoundException { final Class view = Class.forName(viewName, false, classLoader); - final HttpMarshallerFactory unmarshallingFactory = httpServiceConfig.getHttpUnmarshallerFactory(exchange); + final HttpMarshallerFactory unmarshallingFactory = config.getHttpUnmarshallerFactory(exchange); final Unmarshaller unmarshaller = unmarshallingFactory.createUnmarshaller(new FilteringClassResolver(classLoader, classResolverFilter), HttpProtocolV1ObjectTable.INSTANCE); try (InputStream is = exchange.getInputStream()) { @@ -236,7 +236,7 @@ public Resolved getRequestContent(final ClassLoader classLoader) throws IOExcept locator = new StatelessEJBLocator<>(view, app, module, bean, distinct, Affinity.LOCAL); } - final HttpMarshallerFactory marshallerFactory = httpServiceConfig.getHttpMarshallerFactory(exchange); + final HttpMarshallerFactory marshallerFactory = config.getHttpMarshallerFactory(exchange); final Marshaller marshaller = marshallerFactory.createMarshaller(new FilteringClassResolver(classLoader, classResolverFilter), HttpProtocolV1ObjectTable.INSTANCE); final Transaction transaction; if ((txnInfo.getType() == TransactionInfo.NULL_TRANSACTION) || localTransactionContext == null) { //the TX context may be null in unit tests @@ -267,7 +267,7 @@ public void writeNoSuchMethod() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.noSuchMethod()); + sendException(exchange, config, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.noSuchMethod()); } @Override @@ -275,7 +275,7 @@ public void writeSessionNotActive() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.sessionNotActive()); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.sessionNotActive()); } @Override @@ -283,7 +283,7 @@ public void writeWrongViewType() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); + sendException(exchange, config, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); } @Override @@ -316,7 +316,7 @@ public void writeException(@NotNull Exception exception) { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, exception); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, exception); } @Override @@ -324,7 +324,7 @@ public void writeNoSuchEJB() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, new NoSuchEJBException()); + sendException(exchange, config, StatusCodes.NOT_FOUND, new NoSuchEJBException()); } @Override @@ -340,7 +340,7 @@ public void writeNotStateful() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); } @Override @@ -426,7 +426,7 @@ public void writeInvocationResult(Object result) { } exchange.endExchange(); } catch (Exception e) { - sendException(exchange, httpServiceConfig, 500, e); + sendException(exchange, config, 500, e); } } } @@ -517,14 +517,14 @@ private static final class HttpSessionOpenHandler extends AbstractEjbHandler { private final ExecutorService executorService; private final SessionIdGenerator sessionIdGenerator = new SecureRandomSessionIdGenerator(); private final LocalTransactionContext localTransactionContext; - private final HttpServiceConfig httpServiceConfig; + private final HttpServiceConfig config; - HttpSessionOpenHandler(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, HttpServiceConfig httpServiceConfig) { + HttpSessionOpenHandler(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, HttpServiceConfig config) { super(executorService); this.association = association; this.executorService = executorService; this.localTransactionContext = localTransactionContext; - this.httpServiceConfig = httpServiceConfig; + this.config = config; } @Override @@ -560,7 +560,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { exchange.dispatch(executorService, () -> { final TransactionInfo txnInfo; try { - final HttpMarshallerFactory httpUnmarshallerFactory = httpServiceConfig.getHttpUnmarshallerFactory(exchange); + final HttpMarshallerFactory httpUnmarshallerFactory = config.getHttpUnmarshallerFactory(exchange); final Unmarshaller unmarshaller = httpUnmarshallerFactory.createUnmarshaller(HttpProtocolV1ObjectTable.INSTANCE); try (InputStream is = exchange.getInputStream()) { @@ -569,7 +569,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { unmarshaller.finish(); } } catch (Exception e) { - sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); return; } final Transaction transaction; @@ -633,17 +633,17 @@ public SecurityIdentity getSecurityIdentity() { @Override public void writeException(@NotNull Exception exception) { - sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, exception); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, exception); } @Override public void writeNoSuchEJB() { - sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, new NoSuchEJBException()); + sendException(exchange, config, StatusCodes.NOT_FOUND, new NoSuchEJBException()); } @Override public void writeWrongViewType() { - sendException(exchange, httpServiceConfig, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); + sendException(exchange, config, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); } @Override @@ -653,7 +653,7 @@ public void writeCancelResponse() { @Override public void writeNotStateful() { - sendException(exchange, httpServiceConfig, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); + sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); } @Override @@ -687,9 +687,9 @@ public C getProviderInterface(Class providerInterfaceType) { private static final class HttpDiscoveryHandler extends AbstractEjbHandler { private final Set availableModules = new HashSet<>(); - private final HttpServiceConfig httpServiceConfig; + private final HttpServiceConfig config; - public HttpDiscoveryHandler(ExecutorService executorService, Association association, HttpServiceConfig httpServiceConfig) { + public HttpDiscoveryHandler(ExecutorService executorService, Association association, HttpServiceConfig config) { super(executorService); association.registerModuleAvailabilityListener(new ModuleAvailabilityListener() { @Override @@ -702,7 +702,7 @@ public void moduleUnavailable(List modules) { availableModules.removeAll(modules); } }); - this.httpServiceConfig = httpServiceConfig; + this.config = config; } @Override @@ -710,7 +710,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, EJB_DISCOVERY_RESPONSE.toString()); byte[] data; final ByteArrayOutputStream out = new ByteArrayOutputStream(); - Marshaller marshaller = httpServiceConfig.getHttpMarshallerFactory(exchange) + Marshaller marshaller = config.getHttpMarshallerFactory(exchange) .createMarshaller(HttpProtocolV1ObjectTable.INSTANCE); ByteOutput byteOutput = byteOutputOf(out); try (byteOutput) { diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java index f2ddd232..78974b48 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java @@ -36,16 +36,16 @@ * @author Richard Opalka */ public class HttpRemoteNamingService { - private final HttpServiceConfig httpServiceConfig; + private final HttpServiceConfig config; private final ServerHandlers serverHandlers; public HttpRemoteNamingService(final Context localContext, final Function classResolverFilter) { this (localContext, classResolverFilter, HttpServiceConfig.getInstance()); } - private HttpRemoteNamingService(final Context localContext, final Function classResolverFilter, final HttpServiceConfig httpServiceConfig) { - this.httpServiceConfig = httpServiceConfig; - this.serverHandlers = ServerHandlers.newInstance(localContext, classResolverFilter, httpServiceConfig); + private HttpRemoteNamingService(final Context localContext, final Function classResolverFilter, final HttpServiceConfig config) { + this.config = config; + this.serverHandlers = ServerHandlers.newInstance(localContext, classResolverFilter, config); } public HttpHandler createHandler() { @@ -54,7 +54,7 @@ public HttpHandler createHandler() { registerHandler(routingHandler, requestType); } - return httpServiceConfig.wrap(new BlockingHandler(new ElytronIdentityHandler(routingHandler))); + return config.wrap(new BlockingHandler(new ElytronIdentityHandler(routingHandler))); } private void registerHandler(final RoutingHandler routingHandler, final RequestType requestType) { diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java index 6c8de55c..b40870b8 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java @@ -34,16 +34,16 @@ * @author Richard Opalka */ public class HttpRemoteTransactionService { - private final HttpServiceConfig httpServiceConfig; + private final HttpServiceConfig config; private final ServerHandlers serverHandlers; public HttpRemoteTransactionService(final LocalTransactionContext transactionContext, final Function xidResolver) { this(transactionContext, xidResolver, HttpServiceConfig.getInstance()); } - private HttpRemoteTransactionService(final LocalTransactionContext transactionContext, final Function xidResolver, final HttpServiceConfig httpServiceConfig) { - this.httpServiceConfig = httpServiceConfig; - this.serverHandlers = ServerHandlers.newInstance(transactionContext, xidResolver, httpServiceConfig); + private HttpRemoteTransactionService(final LocalTransactionContext transactionContext, final Function xidResolver, final HttpServiceConfig config) { + this.config = config; + this.serverHandlers = ServerHandlers.newInstance(transactionContext, xidResolver, config); } public HttpHandler createHandler() { @@ -52,7 +52,7 @@ public HttpHandler createHandler() { registerHandler(routingHandler, requestType); } - return httpServiceConfig.wrap(new BlockingHandler(new ElytronIdentityHandler(routingHandler))); + return config.wrap(new BlockingHandler(new ElytronIdentityHandler(routingHandler))); } private void registerHandler(final RoutingHandler routingHandler, final RequestType requestType) { From 1585469383091e5872b8abe1f13e0d619bc7c8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 6 Dec 2024 14:56:05 +0100 Subject: [PATCH 13/29] [WEJBHTTP-139] Removing unused HttpRemoteEjbService.cancellationFlags field --- .../java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java index 7569ee36..9aae798b 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java @@ -28,12 +28,9 @@ import io.undertow.server.handlers.encoding.RequestEncodingHandler; import io.undertow.util.Headers; import org.jboss.ejb.server.Association; -import org.jboss.ejb.server.CancelHandle; import org.wildfly.httpclient.common.HttpServiceConfig; import org.wildfly.transaction.client.LocalTransactionContext; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.function.Function; @@ -46,7 +43,6 @@ */ public class HttpRemoteEjbService { private final HttpServiceConfig config; - private final Map cancellationFlags = new ConcurrentHashMap<>(); private final ServerHandlers serverHandlers; public HttpRemoteEjbService(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, From c526cb35529e09d0c6480abb1e89f0be502033d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Fri, 6 Dec 2024 15:07:50 +0100 Subject: [PATCH 14/29] [WEJBHTTP-139] Refactoring - reordering method parameters - let HttpServiceConfig be the first parameter --- .../httpclient/ejb/HttpRemoteEjbService.java | 8 +- .../httpclient/ejb/ServerHandlers.java | 41 ++++--- .../naming/HttpRemoteNamingService.java | 6 +- .../httpclient/naming/ServerHandlers.java | 100 +++++++++--------- .../HttpRemoteTransactionService.java | 6 +- .../transaction/ServerHandlers.java | 92 ++++++++-------- 6 files changed, 126 insertions(+), 127 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java index 9aae798b..421d7628 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java @@ -47,13 +47,13 @@ public class HttpRemoteEjbService { public HttpRemoteEjbService(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, Function classResolverFilter) { - this(association, executorService, localTransactionContext, classResolverFilter, HttpServiceConfig.getInstance()); + this(HttpServiceConfig.getInstance(), association, executorService, localTransactionContext, classResolverFilter); } - private HttpRemoteEjbService(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, - Function classResolverFilter, HttpServiceConfig config) { + private HttpRemoteEjbService(HttpServiceConfig config, Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, + Function classResolverFilter) { this.config = config; - this.serverHandlers = ServerHandlers.newInstance(association, executorService, localTransactionContext, classResolverFilter, config); + this.serverHandlers = ServerHandlers.newInstance(config, association, executorService, localTransactionContext, classResolverFilter); } public HttpHandler createHttpHandler() { diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index c79f1cbe..694346f8 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -99,37 +99,37 @@ */ final class ServerHandlers { + private final HttpServiceConfig config; private final Association association; private final ExecutorService executorService; private final LocalTransactionContext ctx; private final Function classFilter; private final Map cancellationFlags = new ConcurrentHashMap<>(); - private final HttpServiceConfig config; - private ServerHandlers(final Association association, final ExecutorService executorService, final LocalTransactionContext ctx, - final Function classFilter, final HttpServiceConfig config) { + private ServerHandlers(final HttpServiceConfig config, final Association association, final ExecutorService executorService, final LocalTransactionContext ctx, + final Function classFilter) { + this.config = config; this.association = association; this.executorService = executorService; this.ctx = ctx; this.classFilter = classFilter; - this.config = config; } - static ServerHandlers newInstance(final Association association, final ExecutorService executorService, final LocalTransactionContext ctx, - final Function classFilter, final HttpServiceConfig config) { - return new ServerHandlers(association, executorService, ctx, classFilter, config); + static ServerHandlers newInstance(final HttpServiceConfig config, final Association association, final ExecutorService executorService, final LocalTransactionContext ctx, + final Function classFilter) { + return new ServerHandlers(config, association, executorService, ctx, classFilter); } HttpHandler handlerOf(final RequestType requestType) { switch (requestType) { case INVOKE: - return new HttpInvocationHandler(association, executorService, ctx, cancellationFlags, classFilter, config); + return new HttpInvocationHandler(config, association, executorService, ctx, cancellationFlags, classFilter); case CANCEL : - return new HttpCancelHandler(executorService, cancellationFlags); + return new HttpCancelHandler(config, executorService, cancellationFlags); case CREATE_SESSION: - return new HttpSessionOpenHandler(association, executorService, ctx, config); + return new HttpSessionOpenHandler(config, association, executorService, ctx); case DISCOVER: - return new HttpDiscoveryHandler(executorService, association, config); + return new HttpDiscoveryHandler(config, executorService, association); default: throw new IllegalStateException(); } @@ -143,20 +143,19 @@ static final class HttpInvocationHandler extends AbstractEjbHandler { private final Function classResolverFilter; private final HttpServiceConfig config; - HttpInvocationHandler(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, - Map cancellationFlags, Function classResolverFilter, - HttpServiceConfig config) { + HttpInvocationHandler(HttpServiceConfig config, Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, + Map cancellationFlags, Function classResolverFilter) { super(executorService); + this.config = config; this.association = association; this.executorService = executorService; this.localTransactionContext = localTransactionContext; this.cancellationFlags = cancellationFlags; this.classResolverFilter = classResolverFilter; - this.config = config; } @Override - protected void handleInternal(HttpServerExchange exchange) throws Exception { + protected void handleInternal(final HttpServerExchange exchange) throws Exception { String ct = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !INVOCATION.getType().equals(contentType.getType())) { @@ -465,7 +464,7 @@ private static final class HttpCancelHandler extends AbstractEjbHandler { private final Map cancellationFlags; - HttpCancelHandler(ExecutorService executorService, Map cancellationFlags) { + HttpCancelHandler(HttpServiceConfig config, ExecutorService executorService, Map cancellationFlags) { super(executorService); this.cancellationFlags = cancellationFlags; } @@ -519,12 +518,12 @@ private static final class HttpSessionOpenHandler extends AbstractEjbHandler { private final LocalTransactionContext localTransactionContext; private final HttpServiceConfig config; - HttpSessionOpenHandler(Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext, HttpServiceConfig config) { + HttpSessionOpenHandler(HttpServiceConfig config, Association association, ExecutorService executorService, LocalTransactionContext localTransactionContext) { super(executorService); + this.config = config; this.association = association; this.executorService = executorService; this.localTransactionContext = localTransactionContext; - this.config = config; } @Override @@ -689,8 +688,9 @@ private static final class HttpDiscoveryHandler extends AbstractEjbHandler { private final Set availableModules = new HashSet<>(); private final HttpServiceConfig config; - public HttpDiscoveryHandler(ExecutorService executorService, Association association, HttpServiceConfig config) { + public HttpDiscoveryHandler(HttpServiceConfig config, ExecutorService executorService, Association association) { super(executorService); + this.config = config; association.registerModuleAvailabilityListener(new ModuleAvailabilityListener() { @Override public void moduleAvailable(List modules) { @@ -702,7 +702,6 @@ public void moduleUnavailable(List modules) { availableModules.removeAll(modules); } }); - this.config = config; } @Override diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java index 78974b48..31e40a34 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRemoteNamingService.java @@ -40,12 +40,12 @@ public class HttpRemoteNamingService { private final ServerHandlers serverHandlers; public HttpRemoteNamingService(final Context localContext, final Function classResolverFilter) { - this (localContext, classResolverFilter, HttpServiceConfig.getInstance()); + this (HttpServiceConfig.getInstance(), localContext, classResolverFilter); } - private HttpRemoteNamingService(final Context localContext, final Function classResolverFilter, final HttpServiceConfig config) { + private HttpRemoteNamingService(final HttpServiceConfig config, final Context localContext, final Function classResolverFilter) { this.config = config; - this.serverHandlers = ServerHandlers.newInstance(localContext, classResolverFilter, config); + this.serverHandlers = ServerHandlers.newInstance(config, localContext, classResolverFilter); } public HttpHandler createHandler() { diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index 86d5e749..10c97e54 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -65,38 +65,38 @@ final class ServerHandlers { private final Function classFilter; private final HttpServiceConfig config; - private ServerHandlers(final Context ctx, final Function classFilter, final HttpServiceConfig config) { + private ServerHandlers(final HttpServiceConfig config, final Context ctx, final Function classFilter) { + this.config = config; this.ctx = ctx; this.classFilter = classFilter; - this.config = config; } - static ServerHandlers newInstance(final Context ctx, final Function classFilter, final HttpServiceConfig config) { - return new ServerHandlers(ctx, classFilter, config); + static ServerHandlers newInstance(final HttpServiceConfig config, final Context ctx, final Function classFilter) { + return new ServerHandlers(config, ctx, classFilter); } HttpHandler handlerOf(final RequestType requestType) { switch (requestType) { case BIND: - return new BindHandler(ctx, config, classFilter); + return new BindHandler(config, ctx, classFilter); case CREATE_SUBCONTEXT: - return new CreateSubContextHandler(ctx, config); + return new CreateSubContextHandler(config, ctx); case DESTROY_SUBCONTEXT: - return new DestroySubContextHandler(ctx, config); + return new DestroySubContextHandler(config, ctx); case LIST: - return new ListHandler(ctx, config); + return new ListHandler(config, ctx); case LIST_BINDINGS: - return new ListBindingsHandler(ctx, config); + return new ListBindingsHandler(config, ctx); case LOOKUP: - return new LookupHandler(ctx, config); + return new LookupHandler(config, ctx); case LOOKUP_LINK: - return new LookupLinkHandler(ctx, config); + return new LookupLinkHandler(config, ctx); case REBIND: - return new RebindHandler(ctx, config, classFilter); + return new RebindHandler(config, ctx, classFilter); case RENAME: - return new RenameHandler(ctx, config); + return new RenameHandler(config, ctx); case UNBIND: - return new UnbindHandler(ctx, config); + return new UnbindHandler(config, ctx); default: throw new IllegalStateException(); } @@ -107,11 +107,11 @@ private abstract static class AbstractNamingHandler implements HttpHandler { protected final HttpServiceConfig config; protected final Function classFilter; - private AbstractNamingHandler(final Context ctx, final HttpServiceConfig config) { - this(ctx, null, config); + private AbstractNamingHandler(final HttpServiceConfig config, final Context ctx) { + this(config, ctx, null); } - private AbstractNamingHandler(final Context ctx, final Function classFilter, final HttpServiceConfig config) { + private AbstractNamingHandler(final HttpServiceConfig config, final Context ctx, final Function classFilter) { this.ctx = ctx; this.classFilter = classFilter; this.config = config; @@ -150,69 +150,69 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { } private static final class LookupHandler extends AbstractNamingHandler { - private LookupHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private LookupHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { return ctx.lookup(name); } } private static final class LookupLinkHandler extends AbstractNamingHandler { - private LookupLinkHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private LookupLinkHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { return ctx.lookupLink(name); } } private static final class CreateSubContextHandler extends AbstractNamingHandler { - private CreateSubContextHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private CreateSubContextHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { return ctx.createSubcontext(name); } } private static final class UnbindHandler extends AbstractNamingHandler { - private UnbindHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private UnbindHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { ctx.unbind(name); return null; } } private static final class ListBindingsHandler extends AbstractNamingHandler { - private ListBindingsHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private ListBindingsHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { final NamingEnumeration namingEnumeration = ctx.listBindings(name); return Collections.list(namingEnumeration); } } private static final class RenameHandler extends AbstractNamingHandler { - private RenameHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private RenameHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { Deque newName = exchange.getQueryParameters().get(NEW_QUERY_PARAMETER); if (newName == null || newName.isEmpty()) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); @@ -226,60 +226,60 @@ protected Object doOperation(HttpServerExchange exchange, String name) throws Na } private static final class DestroySubContextHandler extends AbstractNamingHandler { - private DestroySubContextHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private DestroySubContextHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { ctx.destroySubcontext(name); return null; } } private static final class ListHandler extends AbstractNamingHandler { - private ListHandler(final Context ctx, final HttpServiceConfig config) { - super(ctx, config); + private ListHandler(final HttpServiceConfig config, final Context ctx) { + super(config, ctx); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { final NamingEnumeration namingEnumeration = ctx.list(name); return Collections.list(namingEnumeration); } } private class RebindHandler extends AbstractClassFilteringNamingHandler { - private RebindHandler(final Context ctx, final HttpServiceConfig config, final Function classFilter) { - super(ctx, config, classFilter); + private RebindHandler(final HttpServiceConfig config, final Context ctx, final Function classFilter) { + super(config, ctx, classFilter); } @Override - protected void doOperation(String name, Object object) throws NamingException { + protected void doOperation(final String name, final Object object) throws NamingException { ctx.rebind(name, object); } } private class BindHandler extends AbstractClassFilteringNamingHandler { - private BindHandler(final Context ctx, final HttpServiceConfig config, final Function classFilter) { - super(ctx, config, classFilter); + private BindHandler(final HttpServiceConfig config, final Context ctx, final Function classFilter) { + super(config, ctx, classFilter); } @Override - protected void doOperation(String name, Object object) throws NamingException { + protected void doOperation(final String name, final Object object) throws NamingException { ctx.bind(name, object); } } private abstract static class AbstractClassFilteringNamingHandler extends AbstractNamingHandler { - private AbstractClassFilteringNamingHandler(final Context ctx, final HttpServiceConfig config, final Function classFilter) { - super(ctx, classFilter, config); + private AbstractClassFilteringNamingHandler(final HttpServiceConfig config, final Context ctx, final Function classFilter) { + super(config, ctx, classFilter); } @Override - protected Object doOperation(HttpServerExchange exchange, String name) throws NamingException { + protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE)); if (contentType == null || !contentType.getType().equals(VALUE.getType()) || contentType.getVersion() != 1) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java index b40870b8..ce92164b 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpRemoteTransactionService.java @@ -38,12 +38,12 @@ public class HttpRemoteTransactionService { private final ServerHandlers serverHandlers; public HttpRemoteTransactionService(final LocalTransactionContext transactionContext, final Function xidResolver) { - this(transactionContext, xidResolver, HttpServiceConfig.getInstance()); + this(HttpServiceConfig.getInstance(), transactionContext, xidResolver); } - private HttpRemoteTransactionService(final LocalTransactionContext transactionContext, final Function xidResolver, final HttpServiceConfig config) { + private HttpRemoteTransactionService(final HttpServiceConfig config, final LocalTransactionContext transactionContext, final Function xidResolver) { this.config = config; - this.serverHandlers = ServerHandlers.newInstance(transactionContext, xidResolver, config); + this.serverHandlers = ServerHandlers.newInstance(config, transactionContext, xidResolver); } public HttpHandler createHandler() { diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index 027b1b5b..b002f808 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -63,36 +63,36 @@ final class ServerHandlers { private final Function xidResolver; private final HttpServiceConfig config; - private ServerHandlers(final LocalTransactionContext ctx, final Function xidResolver, final HttpServiceConfig config) { + private ServerHandlers(final HttpServiceConfig config, final LocalTransactionContext ctx, final Function xidResolver) { + this.config = config; this.ctx = ctx; this.xidResolver = xidResolver; - this.config = config; } - static ServerHandlers newInstance(final LocalTransactionContext ctx, final Function xidResolver, final HttpServiceConfig config) { - return new ServerHandlers(ctx, xidResolver, config); + static ServerHandlers newInstance(final HttpServiceConfig config, final LocalTransactionContext ctx, final Function xidResolver) { + return new ServerHandlers(config, ctx, xidResolver); } HttpHandler handlerOf(final RequestType requestType) { switch (requestType) { case UT_BEGIN: - return new BeginHandler(ctx, config, xidResolver); + return new BeginHandler(config, ctx, xidResolver); case UT_COMMIT: - return new UTCommitHandler(ctx, config); + return new UTCommitHandler(config, ctx); case UT_ROLLBACK: - return new UTRollbackHandler(ctx, config); + return new UTRollbackHandler(config, ctx); case XA_BEFORE_COMPLETION: - return new XABeforeCompletionHandler(ctx, config); + return new XABeforeCompletionHandler(config, ctx); case XA_COMMIT: - return new XACommitHandler(ctx, config); + return new XACommitHandler(config, ctx); case XA_FORGET: - return new XAForgetHandler(ctx, config); + return new XAForgetHandler(config, ctx); case XA_PREPARE: - return new XAPrepHandler(ctx, config); + return new XAPrepHandler(config, ctx); case XA_RECOVER: - return new XARecoveryHandler(ctx, config); + return new XARecoveryHandler(config, ctx); case XA_ROLLBACK: - return new XARollbackHandler(ctx, config); + return new XARollbackHandler(config, ctx); default: throw new IllegalStateException(); } @@ -104,14 +104,14 @@ private abstract static class ValidatingTransactionHandler implements HttpHandle protected final Function xidResolver; protected final HttpServiceConfig config; - private ValidatingTransactionHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - this(ctx, null, config); + private ValidatingTransactionHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + this(config, ctx, null); } - private ValidatingTransactionHandler(final LocalTransactionContext ctx, final Function xidResolver, final HttpServiceConfig config) { + private ValidatingTransactionHandler(final HttpServiceConfig config, final LocalTransactionContext ctx, final Function xidResolver) { + this.config = config; this.ctx = ctx; this.xidResolver = xidResolver; - this.config = config; } protected abstract boolean isValidRequest(HttpServerExchange exchange); @@ -126,8 +126,8 @@ public final void handleRequest(final HttpServerExchange exchange) throws Except } private abstract static class AbstractTransactionHandler extends ValidatingTransactionHandler { - private AbstractTransactionHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private AbstractTransactionHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override @@ -168,8 +168,8 @@ protected void processRequest(final HttpServerExchange exchange) { } private static final class BeginHandler extends ValidatingTransactionHandler { - private BeginHandler(final LocalTransactionContext ctx, final HttpServiceConfig config, final Function xidResolver) { - super(ctx, xidResolver, config); + private BeginHandler(final HttpServiceConfig config, final LocalTransactionContext ctx, final Function xidResolver) { + super(config, ctx, xidResolver); } @Override @@ -207,12 +207,12 @@ protected void processRequest(final HttpServerExchange exchange) { } private static final class XARecoveryHandler extends ValidatingTransactionHandler { - private XARecoveryHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private XARecoveryHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected boolean isValidRequest(HttpServerExchange exchange) { + protected boolean isValidRequest(final HttpServerExchange exchange) { String flagsStringString = exchange.getRequestHeaders().getFirst(RECOVERY_FLAGS); if (flagsStringString == null) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); @@ -229,7 +229,7 @@ protected boolean isValidRequest(HttpServerExchange exchange) { } @Override - protected void processRequest(HttpServerExchange exchange) { + protected void processRequest(final HttpServerExchange exchange) { try { final String flagsStringString = exchange.getRequestHeaders().getFirst(RECOVERY_FLAGS); final int flags = Integer.parseInt(flagsStringString); @@ -252,78 +252,78 @@ protected void processRequest(HttpServerExchange exchange) { } private static final class UTRollbackHandler extends AbstractTransactionHandler { - private UTRollbackHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private UTRollbackHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { transaction.getTransaction().rollback(); } } private static final class UTCommitHandler extends AbstractTransactionHandler { - private UTCommitHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private UTCommitHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { transaction.getTransaction().commit(); } } private static final class XABeforeCompletionHandler extends AbstractTransactionHandler { - private XABeforeCompletionHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private XABeforeCompletionHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { transaction.getControl().beforeCompletion(); } } private static final class XAForgetHandler extends AbstractTransactionHandler { - private XAForgetHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private XAForgetHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { transaction.getControl().forget(); } } private static final class XAPrepHandler extends AbstractTransactionHandler { - private XAPrepHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private XAPrepHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { transaction.getControl().prepare(); } } private static final class XARollbackHandler extends AbstractTransactionHandler { - private XARollbackHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private XARollbackHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { transaction.getControl().rollback(); } } private static final class XACommitHandler extends AbstractTransactionHandler { - private XACommitHandler(final LocalTransactionContext ctx, final HttpServiceConfig config) { - super(ctx, config); + private XACommitHandler(final HttpServiceConfig config, final LocalTransactionContext ctx) { + super(config, ctx); } @Override - protected void handleImpl(HttpServerExchange exchange, ImportResult transaction) throws Exception { + protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { Deque opc = exchange.getQueryParameters().get("opc"); boolean onePhase = false; if (opc != null && !opc.isEmpty()) { From f47e24cf45e8da7f03fb41a475683ed425b6eb9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 18:07:48 +0100 Subject: [PATCH 15/29] [WEJBHTTP-139] Refactoring - be consistent and always use HeaderMap.put() instead of HeaderMap.add() --- .../java/org/wildfly/httpclient/ejb/RequestBuilder.java | 6 +++--- .../org/wildfly/httpclient/transaction/RequestBuilder.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java index 2541ba1e..80b5f7fd 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java @@ -141,7 +141,7 @@ private void setRequestHeaders(final ClientRequest request) { final HeaderMap headers = request.getRequestHeaders(); switch (requestType) { case INVOKE: { - headers.add(ACCEPT, INVOCATION_ACCEPT + "," + EJB_EXCEPTION); + headers.put(ACCEPT, INVOCATION_ACCEPT + "," + EJB_EXCEPTION); headers.put(CONTENT_TYPE, INVOCATION.toString()); if (invocationId != null) { headers.put(INVOCATION_ID, invocationId); @@ -155,11 +155,11 @@ private void setRequestHeaders(final ClientRequest request) { headers.put(TRANSFER_ENCODING, CHUNKED.toString()); } break; case CREATE_SESSION: { - headers.add(ACCEPT, EJB_EXCEPTION.toString()); + headers.put(ACCEPT, EJB_EXCEPTION.toString()); headers.put(CONTENT_TYPE, SESSION_OPEN.toString()); } break; case DISCOVER: { - headers.add(ACCEPT, EJB_DISCOVERY_RESPONSE + "," + EJB_EXCEPTION); + headers.put(ACCEPT, EJB_DISCOVERY_RESPONSE + "," + EJB_EXCEPTION); } break; case CANCEL: { // no headers to be added diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java index 1559ec03..f8a568c3 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java @@ -128,7 +128,7 @@ private void setRequestHeaders(final ClientRequest request) { headers.put(RECOVERY_PARENT_NAME, parentName); headers.put(RECOVERY_FLAGS, Integer.toString(flags)); } else { - headers.add(ACCEPT, EXCEPTION.toString()); + headers.put(ACCEPT, EXCEPTION.toString()); headers.put(CONTENT_TYPE, XID.toString()); } } From 9b3515dd0d424fb44fba1e7ce6961121052c170c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 18:16:16 +0100 Subject: [PATCH 16/29] [WEJBHTTP-139] Refactoring - Use Headers fields via static imports --- .../httpclient/common/HttpServerHelper.java | 4 ++-- .../httpclient/ejb/HttpRemoteEjbService.java | 7 ++++--- .../org/wildfly/httpclient/ejb/ServerHandlers.java | 14 +++++++------- .../httpclient/ejb/AsyncInvocationTestCase.java | 6 +++--- .../httpclient/ejb/SimpleInvocationTestCase.java | 5 +++-- .../wildfly/httpclient/naming/ServerHandlers.java | 6 +++--- .../httpclient/transaction/ServerHandlers.java | 6 +++--- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java index 744d3985..d4783a82 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpServerHelper.java @@ -18,11 +18,11 @@ package org.wildfly.httpclient.common; +import static io.undertow.util.Headers.CONTENT_TYPE; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import io.undertow.server.HttpServerExchange; -import io.undertow.util.Headers; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.Marshaller; @@ -40,7 +40,7 @@ private HttpServerHelper() { public static void sendException(HttpServerExchange exchange, HttpServiceConfig serviceConfig, int status, Throwable e) { try { exchange.setStatusCode(status); - putResponseHeader(exchange, Headers.CONTENT_TYPE, "application/x-wf-jbmar-exception;version=1"); + putResponseHeader(exchange, CONTENT_TYPE, "application/x-wf-jbmar-exception;version=1"); final Marshaller marshaller = serviceConfig.getHttpMarshallerFactory(exchange).createMarshaller(); final OutputStream outputStream = exchange.getOutputStream(); try (ByteOutput byteOutput = byteOutputOf(outputStream)) { diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java index 421d7628..c543896d 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/HttpRemoteEjbService.java @@ -18,6 +18,8 @@ package org.wildfly.httpclient.ejb; +import static io.undertow.util.Headers.GZIP; + import io.undertow.conduits.GzipStreamSourceConduit; import io.undertow.server.HttpHandler; import io.undertow.server.handlers.AllowedMethodsHandler; @@ -26,7 +28,6 @@ import io.undertow.server.handlers.encoding.EncodingHandler; import io.undertow.server.handlers.encoding.GzipEncodingProvider; import io.undertow.server.handlers.encoding.RequestEncodingHandler; -import io.undertow.util.Headers; import org.jboss.ejb.server.Association; import org.wildfly.httpclient.common.HttpServiceConfig; import org.wildfly.transaction.client.LocalTransactionContext; @@ -62,9 +63,9 @@ public HttpHandler createHttpHandler() { registerHandler(pathHandler, requestType); } - EncodingHandler encodingHandler = new EncodingHandler(pathHandler, new ContentEncodingRepository().addEncodingHandler(Headers.GZIP.toString(), new GzipEncodingProvider(), 1)); + EncodingHandler encodingHandler = new EncodingHandler(pathHandler, new ContentEncodingRepository().addEncodingHandler(GZIP.toString(), new GzipEncodingProvider(), 1)); RequestEncodingHandler requestEncodingHandler = new RequestEncodingHandler(encodingHandler); - requestEncodingHandler.addEncoding(Headers.GZIP.toString(), GzipStreamSourceConduit.WRAPPER); + requestEncodingHandler.addEncoding(GZIP.toString(), GzipStreamSourceConduit.WRAPPER); return config.wrap(requestEncodingHandler); } diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 694346f8..8461dad6 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.ejb; +import static io.undertow.util.Headers.CONTENT_TYPE; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; @@ -40,7 +41,6 @@ import io.undertow.server.session.SecureRandomSessionIdGenerator; import io.undertow.server.session.SessionIdGenerator; import io.undertow.util.AttachmentKey; -import io.undertow.util.Headers; import io.undertow.util.StatusCodes; import jakarta.ejb.EJBHome; import jakarta.ejb.NoSuchEJBException; @@ -156,7 +156,7 @@ static final class HttpInvocationHandler extends AbstractEjbHandler { @Override protected void handleInternal(final HttpServerExchange exchange) throws Exception { - String ct = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); + String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !INVOCATION.getType().equals(contentType.getType())) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); @@ -413,7 +413,7 @@ public void writeInvocationResult(Object result) { cancellationFlags.remove(identifier); } try { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, Constants.EJB_RESPONSE.toString()); + exchange.getResponseHeaders().put(CONTENT_TYPE, Constants.EJB_RESPONSE.toString()); // if (output.getSessionAffinity() != null) { // exchange.setResponseCookie(new CookieImpl("JSESSIONID", output.getSessionAffinity()).setPath(WILDFLY_SERVICES)); // } @@ -471,7 +471,7 @@ private static final class HttpCancelHandler extends AbstractEjbHandler { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - String ct = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); + String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType != null) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); @@ -528,7 +528,7 @@ private static final class HttpSessionOpenHandler extends AbstractEjbHandler { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - String ct = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); + String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !SESSION_OPEN.getType().equals(contentType.getType())) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); @@ -668,7 +668,7 @@ public void convertToStateful(@NotNull SessionID sessionId) throws IllegalArgume exchange.setResponseCookie(new CookieImpl(JSESSIONID_COOKIE_NAME, sessionIdGenerator.createSessionId()).setPath(rootPath)); } - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION.toString()); + exchange.getResponseHeaders().put(CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION.toString()); exchange.getResponseHeaders().put(EJB_SESSION_ID, Base64.getUrlEncoder().encodeToString(sessionId.getEncodedForm())); exchange.setStatusCode(StatusCodes.NO_CONTENT); @@ -706,7 +706,7 @@ public void moduleUnavailable(List modules) { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, EJB_DISCOVERY_RESPONSE.toString()); + exchange.getResponseHeaders().put(CONTENT_TYPE, EJB_DISCOVERY_RESPONSE.toString()); byte[] data; final ByteArrayOutputStream out = new ByteArrayOutputStream(); Marshaller marshaller = config.getHttpMarshallerFactory(exchange) diff --git a/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java b/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java index 3e67dbd8..10ce3ad2 100644 --- a/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java +++ b/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java @@ -18,8 +18,9 @@ package org.wildfly.httpclient.ejb; +import static io.undertow.util.Headers.SET_COOKIE; + import java.net.URI; -import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -33,7 +34,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import io.undertow.util.Headers; /** * @author Stuart Douglas @@ -46,7 +46,7 @@ public class AsyncInvocationTestCase { @Before public void before() { - EJBTestServer.registerServicesHandler("common/v1/affinity", httpServerExchange -> httpServerExchange.getResponseHeaders().put(Headers.SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); + EJBTestServer.registerServicesHandler("common/v1/affinity", httpServerExchange -> httpServerExchange.getResponseHeaders().put(SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); } @Test diff --git a/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java b/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java index 07fd211e..7e567fa1 100644 --- a/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java +++ b/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java @@ -18,7 +18,8 @@ package org.wildfly.httpclient.ejb; -import io.undertow.util.Headers; +import static io.undertow.util.Headers.SET_COOKIE; + import org.jboss.ejb.client.EJBClient; import org.jboss.ejb.client.EJBClientContext; import org.jboss.ejb.client.EJBClientInvocationContext; @@ -56,7 +57,7 @@ public class SimpleInvocationTestCase { @Before public void before() { - EJBTestServer.registerServicesHandler("common/v1/affinity", httpServerExchange -> httpServerExchange.getResponseHeaders().put(Headers.SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); + EJBTestServer.registerServicesHandler("common/v1/affinity", httpServerExchange -> httpServerExchange.getResponseHeaders().put(SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; ++i) { sb.append("Hello World "); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index 10c97e54..e28bcf06 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.naming; +import static io.undertow.util.Headers.CONTENT_TYPE; import static java.nio.charset.StandardCharsets.UTF_8; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; @@ -29,7 +30,6 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; -import io.undertow.util.Headers; import io.undertow.util.PathTemplateMatch; import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; @@ -131,7 +131,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { } else if (result instanceof Context) { exchange.setStatusCode(StatusCodes.NO_CONTENT); } else { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, VALUE.toString()); + exchange.getResponseHeaders().put(CONTENT_TYPE, VALUE.toString()); HttpNamingServerObjectResolver resolver = new HttpNamingServerObjectResolver(exchange); Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(resolver); ByteOutput out = byteOutputOf(exchange.getOutputStream()); @@ -280,7 +280,7 @@ private AbstractClassFilteringNamingHandler(final HttpServiceConfig config, fina @Override protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { - ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE)); + ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(CONTENT_TYPE)); if (contentType == null || !contentType.getType().equals(VALUE.getType()) || contentType.getVersion() != 1) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); exchange.endExchange(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index b002f808..de3dab89 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.transaction; +import static io.undertow.util.Headers.CONTENT_TYPE; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; @@ -31,7 +32,6 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; -import io.undertow.util.Headers; import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; @@ -132,7 +132,7 @@ private AbstractTransactionHandler(final HttpServiceConfig config, final LocalTr @Override protected boolean isValidRequest(final HttpServerExchange exchange) { - final ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE)); + final ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(CONTENT_TYPE)); if (contentType == null || contentType.getVersion() != 1 || !contentType.getType().equals(XID.getType())) { exchange.setStatusCode(StatusCodes.BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s has incorrect or missing content type", exchange); @@ -188,7 +188,7 @@ protected void processRequest(final HttpServerExchange exchange) { try { final String timeoutString = exchange.getRequestHeaders().getFirst(TIMEOUT); final Integer timeout = Integer.parseInt(timeoutString); - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, NEW_TRANSACTION.toString()); + exchange.getResponseHeaders().put(CONTENT_TYPE, NEW_TRANSACTION.toString()); final LocalTransaction transaction = ctx.beginTransaction(timeout); final Xid xid = xidResolver.apply(transaction); From b1f577da63d305e494282b350c222cb961753656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 18:27:14 +0100 Subject: [PATCH 17/29] [WEJBHTTP-139] Refactoring - Use StatusCodes fields via static imports --- .../httpclient/common/HttpTargetContext.java | 6 +-- .../common/PoolAuthenticationContext.java | 6 +-- .../httpclient/ejb/ServerHandlers.java | 43 ++++++++++--------- .../httpclient/naming/ClientHandlers.java | 4 +- .../httpclient/naming/ServerHandlers.java | 15 ++++--- .../transaction/ServerHandlers.java | 17 ++++---- 6 files changed, 49 insertions(+), 42 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java index f0e87809..a4b0e0cf 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java @@ -18,6 +18,7 @@ package org.wildfly.httpclient.common; +import static io.undertow.util.StatusCodes.NO_CONTENT; import static io.undertow.util.Headers.CHUNKED; import static io.undertow.util.Headers.COOKIE; import static io.undertow.util.Headers.CONTENT_ENCODING; @@ -44,7 +45,6 @@ import io.undertow.util.Cookies; import io.undertow.util.HeaderValues; import io.undertow.util.Methods; -import io.undertow.util.StatusCodes; import org.jboss.marshalling.Unmarshaller; import org.wildfly.security.auth.client.AuthenticationConfiguration; import org.wildfly.security.auth.client.AuthenticationContext; @@ -237,7 +237,7 @@ public void completed(ClientExchange result) { final boolean ok; final boolean isException; if (type == null) { - ok = expectedResponse == null || (allowNoContent && response.getResponseCode() == StatusCodes.NO_CONTENT); + ok = expectedResponse == null || (allowNoContent && response.getResponseCode() == NO_CONTENT); isException = false; } else { if (type.getType().equals(EXCEPTION_TYPE)) { @@ -310,7 +310,7 @@ public void completed(ClientExchange result) { } connection.done(false); }; - if (response.getResponseCode() == StatusCodes.NO_CONTENT) { + if (response.getResponseCode() == NO_CONTENT) { IoUtils.safeClose(in); httpResultHandler.handleResult(null, response, doneCallback); } else { diff --git a/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java b/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java index 6e16fa5a..5949f1ea 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java @@ -21,6 +21,7 @@ import static io.undertow.util.Headers.AUTHENTICATION_INFO; import static io.undertow.util.Headers.AUTHORIZATION; import static io.undertow.util.Headers.WWW_AUTHENTICATE; +import static io.undertow.util.StatusCodes.UNAUTHORIZED; import static org.wildfly.httpclient.common.HeadersHelper.getResponseHeader; import static org.wildfly.httpclient.common.HeadersHelper.getResponseHeaders; import static org.wildfly.httpclient.common.HeadersHelper.putRequestHeader; @@ -54,7 +55,6 @@ import io.undertow.util.FlexBase64; import io.undertow.util.HeaderValues; import io.undertow.util.HexConverter; -import io.undertow.util.StatusCodes; import io.undertow.util.AttachmentKey; import org.wildfly.security.auth.principal.NamePrincipal; @@ -80,7 +80,7 @@ class PoolAuthenticationContext { private static final SecureRandomSessionIdGenerator cnonceGenerator = new SecureRandomSessionIdGenerator(); boolean handleResponse(ClientResponse response) { - if (response.getResponseCode() != StatusCodes.UNAUTHORIZED) { + if (response.getResponseCode() != UNAUTHORIZED) { return false; } String authenticate = getResponseHeader(response, WWW_AUTHENTICATE); @@ -280,7 +280,7 @@ boolean isStale(ClientExchange exchange) { return false; } ClientResponse response = exchange.getResponse(); - if (response.getResponseCode() != StatusCodes.UNAUTHORIZED) { + if (response.getResponseCode() != UNAUTHORIZED) { DigestImpl digest = exchange.getRequest().getAttachment(DIGEST); if(digest != null) { String authInfo = getResponseHeader(response, AUTHENTICATION_INFO); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 8461dad6..ff66b93c 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -18,6 +18,10 @@ package org.wildfly.httpclient.ejb; import static io.undertow.util.Headers.CONTENT_TYPE; +import static io.undertow.util.StatusCodes.BAD_REQUEST; +import static io.undertow.util.StatusCodes.INTERNAL_SERVER_ERROR; +import static io.undertow.util.StatusCodes.NO_CONTENT; +import static io.undertow.util.StatusCodes.NOT_FOUND; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; @@ -41,7 +45,6 @@ import io.undertow.server.session.SecureRandomSessionIdGenerator; import io.undertow.server.session.SessionIdGenerator; import io.undertow.util.AttachmentKey; -import io.undertow.util.StatusCodes; import jakarta.ejb.EJBHome; import jakarta.ejb.NoSuchEJBException; import jakarta.transaction.SystemException; @@ -159,7 +162,7 @@ protected void handleInternal(final HttpServerExchange exchange) throws Exceptio String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !INVOCATION.getType().equals(contentType.getType())) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); EjbHttpClientMessages.MESSAGES.debugf("Bad content type %s", ct); return; } @@ -170,7 +173,7 @@ protected void handleInternal(final HttpServerExchange exchange) throws Exceptio } String[] parts = relativePath.split("/"); if(parts.length < 7) { - exchange.setStatusCode(StatusCodes.NOT_FOUND); + exchange.setStatusCode(NOT_FOUND); return; } final String app = handleDash(parts[0]); @@ -266,7 +269,7 @@ public void writeNoSuchMethod() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, config, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.noSuchMethod()); + sendException(exchange, config, NOT_FOUND, EjbHttpClientMessages.MESSAGES.noSuchMethod()); } @Override @@ -274,7 +277,7 @@ public void writeSessionNotActive() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.sessionNotActive()); + sendException(exchange, config, INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.sessionNotActive()); } @Override @@ -282,7 +285,7 @@ public void writeWrongViewType() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, config, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); + sendException(exchange, config, NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); } @Override @@ -315,7 +318,7 @@ public void writeException(@NotNull Exception exception) { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, exception); + sendException(exchange, config, INTERNAL_SERVER_ERROR, exception); } @Override @@ -323,7 +326,7 @@ public void writeNoSuchEJB() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, config, StatusCodes.NOT_FOUND, new NoSuchEJBException()); + sendException(exchange, config, NOT_FOUND, new NoSuchEJBException()); } @Override @@ -339,7 +342,7 @@ public void writeNotStateful() { if(identifier != null) { cancellationFlags.remove(identifier); } - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); + sendException(exchange, config, INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); } @Override @@ -474,7 +477,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType != null) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); EjbHttpClientMessages.MESSAGES.debugf("Bad content type %s", ct); return; } @@ -485,7 +488,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { } String[] parts = relativePath.split("/"); if (parts.length != 6) { - exchange.setStatusCode(StatusCodes.NOT_FOUND); + exchange.setStatusCode(NOT_FOUND); return; } final String app = handleDash(parts[0]); @@ -500,7 +503,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { if (invocationId != null && sessionAffinity != null) { identifier = new InvocationIdentifier(invocationId, sessionAffinity); } else { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); EjbHttpClientMessages.MESSAGES.debugf("Exchange %s did not include both session id and invocation id in cancel request", exchange); return; } @@ -531,7 +534,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !SESSION_OPEN.getType().equals(contentType.getType())) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); EjbHttpClientMessages.MESSAGES.debugf("Bad content type %s", ct); return; } @@ -541,7 +544,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { } String[] parts = relativePath.split("/"); if(parts.length != 4) { - exchange.setStatusCode(StatusCodes.NOT_FOUND); + exchange.setStatusCode(NOT_FOUND); return; } final String app = handleDash(parts[0]); @@ -568,7 +571,7 @@ protected void handleInternal(HttpServerExchange exchange) throws Exception { unmarshaller.finish(); } } catch (Exception e) { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, INTERNAL_SERVER_ERROR, e); return; } final Transaction transaction; @@ -632,17 +635,17 @@ public SecurityIdentity getSecurityIdentity() { @Override public void writeException(@NotNull Exception exception) { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, exception); + sendException(exchange, config, INTERNAL_SERVER_ERROR, exception); } @Override public void writeNoSuchEJB() { - sendException(exchange, config, StatusCodes.NOT_FOUND, new NoSuchEJBException()); + sendException(exchange, config, NOT_FOUND, new NoSuchEJBException()); } @Override public void writeWrongViewType() { - sendException(exchange, config, StatusCodes.NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); + sendException(exchange, config, NOT_FOUND, EjbHttpClientMessages.MESSAGES.wrongViewType()); } @Override @@ -652,7 +655,7 @@ public void writeCancelResponse() { @Override public void writeNotStateful() { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); + sendException(exchange, config, INTERNAL_SERVER_ERROR, EjbHttpClientMessages.MESSAGES.notStateful()); } @Override @@ -671,7 +674,7 @@ public void convertToStateful(@NotNull SessionID sessionId) throws IllegalArgume exchange.getResponseHeaders().put(CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION.toString()); exchange.getResponseHeaders().put(EJB_SESSION_ID, Base64.getUrlEncoder().encodeToString(sessionId.getEncodedForm())); - exchange.setStatusCode(StatusCodes.NO_CONTENT); + exchange.setStatusCode(NO_CONTENT); exchange.endExchange(); } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java index 4c833164..f22d71c7 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.naming; +import static io.undertow.util.StatusCodes.NO_CONTENT; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.naming.Serializer.deserializeObject; @@ -25,7 +26,6 @@ import static org.xnio.IoUtils.safeClose; import io.undertow.client.ClientResponse; -import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.Marshaller; @@ -123,7 +123,7 @@ public void handleResult(final InputStream is, final ClientResponse httpResponse namingProvider.performExceptionAction((a, b) -> { ClassLoader old = setContextClassLoader(classLoader); try { - if (httpResponse.getResponseCode() == StatusCodes.NO_CONTENT) { + if (httpResponse.getResponseCode() == NO_CONTENT) { emptyHttpResultHandler(result, null).handleResult(is, httpResponse, doneCallback); } else { objectHttpResultHandler(unmarshaller, result).handleResult(is, httpResponse, doneCallback); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index e28bcf06..dd0ac0b3 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -18,6 +18,10 @@ package org.wildfly.httpclient.naming; import static io.undertow.util.Headers.CONTENT_TYPE; +import static io.undertow.util.StatusCodes.BAD_REQUEST; +import static io.undertow.util.StatusCodes.INTERNAL_SERVER_ERROR; +import static io.undertow.util.StatusCodes.NO_CONTENT; +import static io.undertow.util.StatusCodes.OK; import static java.nio.charset.StandardCharsets.UTF_8; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; @@ -31,7 +35,6 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.util.PathTemplateMatch; -import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.ContextClassResolver; @@ -127,9 +130,9 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { return; } if (result == null) { - exchange.setStatusCode(StatusCodes.OK); + exchange.setStatusCode(OK); } else if (result instanceof Context) { - exchange.setStatusCode(StatusCodes.NO_CONTENT); + exchange.setStatusCode(NO_CONTENT); } else { exchange.getResponseHeaders().put(CONTENT_TYPE, VALUE.toString()); HttpNamingServerObjectResolver resolver = new HttpNamingServerObjectResolver(exchange); @@ -142,7 +145,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { } } } catch (Throwable e) { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, INTERNAL_SERVER_ERROR, e); } } @@ -215,7 +218,7 @@ private RenameHandler(final HttpServiceConfig config, final Context ctx) { protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { Deque newName = exchange.getQueryParameters().get(NEW_QUERY_PARAMETER); if (newName == null || newName.isEmpty()) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); exchange.endExchange(); return null; } @@ -282,7 +285,7 @@ private AbstractClassFilteringNamingHandler(final HttpServiceConfig config, fina protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(CONTENT_TYPE)); if (contentType == null || !contentType.getType().equals(VALUE.getType()) || contentType.getVersion() != 1) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); exchange.endExchange(); return null; } diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index de3dab89..6de81a6d 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -18,6 +18,8 @@ package org.wildfly.httpclient.transaction; import static io.undertow.util.Headers.CONTENT_TYPE; +import static io.undertow.util.StatusCodes.BAD_REQUEST; +import static io.undertow.util.StatusCodes.INTERNAL_SERVER_ERROR; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; @@ -32,7 +34,6 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; -import io.undertow.util.StatusCodes; import org.jboss.marshalling.ByteInput; import org.jboss.marshalling.ByteOutput; import org.jboss.marshalling.Marshaller; @@ -134,7 +135,7 @@ private AbstractTransactionHandler(final HttpServiceConfig config, final LocalTr protected boolean isValidRequest(final HttpServerExchange exchange) { final ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(CONTENT_TYPE)); if (contentType == null || contentType.getVersion() != 1 || !contentType.getType().equals(XID.getType())) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s has incorrect or missing content type", exchange); return false; } @@ -160,7 +161,7 @@ protected void processRequest(final HttpServerExchange exchange) { return null; }, transaction, exchange); } catch (Exception e) { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, INTERNAL_SERVER_ERROR, e); } } @@ -176,7 +177,7 @@ private BeginHandler(final HttpServiceConfig config, final LocalTransactionConte protected boolean isValidRequest(final HttpServerExchange exchange) { final String timeoutString = exchange.getRequestHeaders().getFirst(TIMEOUT); if (timeoutString == null) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s is missing %s header", exchange, TIMEOUT); return false; } @@ -201,7 +202,7 @@ protected void processRequest(final HttpServerExchange exchange) { } exchange.getResponseSender().send(ByteBuffer.wrap(baos.toByteArray())); } catch (Exception e) { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, INTERNAL_SERVER_ERROR, e); } } } @@ -215,13 +216,13 @@ private XARecoveryHandler(final HttpServiceConfig config, final LocalTransaction protected boolean isValidRequest(final HttpServerExchange exchange) { String flagsStringString = exchange.getRequestHeaders().getFirst(RECOVERY_FLAGS); if (flagsStringString == null) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s is missing %s header", exchange, RECOVERY_FLAGS); return false; } String parentName = exchange.getRequestHeaders().getFirst(RECOVERY_PARENT_NAME); if (parentName == null) { - exchange.setStatusCode(StatusCodes.BAD_REQUEST); + exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s is missing %s header", exchange, RECOVERY_PARENT_NAME); return false; } @@ -246,7 +247,7 @@ protected void processRequest(final HttpServerExchange exchange) { } exchange.getResponseSender().send(ByteBuffer.wrap(out.toByteArray())); } catch (Exception e) { - sendException(exchange, config, StatusCodes.INTERNAL_SERVER_ERROR, e); + sendException(exchange, config, INTERNAL_SERVER_ERROR, e); } } } From 35e8968aeb30bf763db4849ac314d0581846d630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 19:50:23 +0100 Subject: [PATCH 18/29] [WEJBHTTP-139] Refactoring - always use HeadersHelper methods for better code readability --- .../httpclient/common/HeadersHelper.java | 35 +++++++++++-------- .../httpclient/ejb/ClientHandlers.java | 3 +- .../httpclient/ejb/RequestBuilder.java | 21 ++++++----- .../httpclient/ejb/ServerHandlers.java | 18 +++++----- .../ejb/AsyncInvocationTestCase.java | 3 +- .../ejb/SimpleInvocationTestCase.java | 3 +- .../httpclient/naming/RequestBuilder.java | 7 ++-- .../httpclient/naming/ServerHandlers.java | 6 ++-- .../HttpSubordinateTransactionHandle.java | 3 +- .../transaction/RequestBuilder.java | 17 +++++---- .../transaction/ServerHandlers.java | 18 +++++----- 11 files changed, 73 insertions(+), 61 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java index cc8ec18c..6231e222 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java @@ -35,31 +35,16 @@ public static void addRequestHeader(final ClientRequest request, final HttpStrin request.getRequestHeaders().add(headerName, headerValue); } - public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { - if (exchange == null || headerName == null) return; - exchange.getResponseHeaders().add(headerName, headerValue); - } - public static boolean containsRequestHeader(final ClientRequest request, final HttpString headerName) { if (request == null || headerName == null) return false; return request.getRequestHeaders().contains(headerName); } - public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final String headerValue) { - if (request == null || headerName == null) return; - request.getRequestHeaders().put(headerName, headerValue); - } - public static String getRequestHeader(final ClientRequest request, final HttpString headerName) { if (request == null || headerName == null) return null; return request.getRequestHeaders().getFirst(headerName); } - public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { - if (exchange == null || headerName == null) return; - exchange.getResponseHeaders().put(headerName, headerValue); - } - public static String getResponseHeader(final ClientResponse response, final HttpString headerName) { if (response == null || headerName == null) return null; return response.getResponseHeaders().getFirst(headerName); @@ -70,9 +55,29 @@ public static HeaderValues getResponseHeaders(final ClientResponse response, fin return response.getResponseHeaders().get(headerName); } + public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final long headerValue) { + if (request == null || headerName == null) return; + request.getRequestHeaders().put(headerName, headerValue); + } + + public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final String headerValue) { + if (request == null || headerName == null) return; + request.getRequestHeaders().put(headerName, headerValue); + } + + public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { + if (exchange == null || headerName == null) return; + exchange.getResponseHeaders().add(headerName, headerValue); + } + public static String getRequestHeader(final HttpServerExchange exchange, final HttpString headerName) { if (exchange == null || headerName == null) return null; return exchange.getRequestHeaders().getFirst(headerName); } + public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { + if (exchange == null || headerName == null) return; + exchange.getResponseHeaders().put(headerName, headerValue); + } + } diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index 37dad0ba..32e7c745 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.ejb; +import static org.wildfly.httpclient.common.HeadersHelper.getResponseHeader; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; import static org.wildfly.httpclient.ejb.Serializer.deserializeObject; @@ -269,7 +270,7 @@ public Boolean apply(final ClientResponse clientResponse) { private static final class CreateSessionResponseFunction implements Function { @Override public SessionID apply(final ClientResponse clientResponse) { - final String sessionId = clientResponse.getResponseHeaders().getFirst(Constants.EJB_SESSION_ID); + final String sessionId = getResponseHeader(clientResponse, Constants.EJB_SESSION_ID); if (sessionId != null) { return SessionID.createSessionID(Base64.getUrlDecoder().decode(sessionId)); } diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java index 80b5f7fd..e6adf2ad 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java @@ -26,6 +26,7 @@ import static io.undertow.util.Headers.GZIP; import static io.undertow.util.Headers.TRANSFER_ENCODING; +import static org.wildfly.httpclient.common.HeadersHelper.putRequestHeader; import static org.wildfly.httpclient.common.Protocol.VERSION_PATH; import static org.wildfly.httpclient.ejb.Constants.EJB_CONTEXT; import static org.wildfly.httpclient.ejb.Constants.EJB_DISCOVERY_RESPONSE; @@ -39,7 +40,6 @@ import static java.net.URLEncoder.encode; import io.undertow.client.ClientRequest; -import io.undertow.util.HeaderMap; import org.jboss.ejb.client.EJBLocator; import org.wildfly.httpclient.common.Protocol; @@ -138,28 +138,27 @@ private void setRequestPath(final ClientRequest request, final String prefix) { } private void setRequestHeaders(final ClientRequest request) { - final HeaderMap headers = request.getRequestHeaders(); switch (requestType) { case INVOKE: { - headers.put(ACCEPT, INVOCATION_ACCEPT + "," + EJB_EXCEPTION); - headers.put(CONTENT_TYPE, INVOCATION.toString()); + putRequestHeader(request, ACCEPT, INVOCATION_ACCEPT + "," + EJB_EXCEPTION); + putRequestHeader(request, CONTENT_TYPE, INVOCATION.toString()); if (invocationId != null) { - headers.put(INVOCATION_ID, invocationId); + putRequestHeader(request, INVOCATION_ID, invocationId); } if (compressRequest) { - headers.put(CONTENT_ENCODING, GZIP.toString()); + putRequestHeader(request, CONTENT_ENCODING, GZIP.toString()); } if (compressResponse) { - headers.put(ACCEPT_ENCODING, GZIP.toString()); + putRequestHeader(request, ACCEPT_ENCODING, GZIP.toString()); } - headers.put(TRANSFER_ENCODING, CHUNKED.toString()); + putRequestHeader(request, TRANSFER_ENCODING, CHUNKED.toString()); } break; case CREATE_SESSION: { - headers.put(ACCEPT, EJB_EXCEPTION.toString()); - headers.put(CONTENT_TYPE, SESSION_OPEN.toString()); + putRequestHeader(request, ACCEPT, EJB_EXCEPTION.toString()); + putRequestHeader(request, CONTENT_TYPE, SESSION_OPEN.toString()); } break; case DISCOVER: { - headers.put(ACCEPT, EJB_DISCOVERY_RESPONSE + "," + EJB_EXCEPTION); + putRequestHeader(request, ACCEPT, EJB_DISCOVERY_RESPONSE + "," + EJB_EXCEPTION); } break; case CANCEL: { // no headers to be added diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index ff66b93c..98df8708 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -24,6 +24,8 @@ import static io.undertow.util.StatusCodes.NOT_FOUND; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.HeadersHelper.getRequestHeader; +import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.ejb.Constants.EJB_DISCOVERY_RESPONSE; import static org.wildfly.httpclient.ejb.Constants.EJB_RESPONSE_NEW_SESSION; @@ -159,7 +161,7 @@ static final class HttpInvocationHandler extends AbstractEjbHandler { @Override protected void handleInternal(final HttpServerExchange exchange) throws Exception { - String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); + String ct = getRequestHeader(exchange, CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !INVOCATION.getType().equals(contentType.getType())) { exchange.setStatusCode(BAD_REQUEST); @@ -192,7 +194,7 @@ protected void handleInternal(final HttpServerExchange exchange) throws Exceptio final String sessionAffinity = cookie != null ? cookie.getValue() : null; final EJBIdentifier ejbIdentifier = new EJBIdentifier(app, module, bean, distinct); - final String cancellationId = exchange.getRequestHeaders().getFirst(Constants.INVOCATION_ID); + final String cancellationId = getRequestHeader(exchange, Constants.INVOCATION_ID); final InvocationIdentifier identifier; if(cancellationId != null && sessionAffinity != null) { identifier = new InvocationIdentifier(cancellationId, sessionAffinity); @@ -416,7 +418,7 @@ public void writeInvocationResult(Object result) { cancellationFlags.remove(identifier); } try { - exchange.getResponseHeaders().put(CONTENT_TYPE, Constants.EJB_RESPONSE.toString()); + putResponseHeader(exchange, CONTENT_TYPE, Constants.EJB_RESPONSE.toString()); // if (output.getSessionAffinity() != null) { // exchange.setResponseCookie(new CookieImpl("JSESSIONID", output.getSessionAffinity()).setPath(WILDFLY_SERVICES)); // } @@ -474,7 +476,7 @@ private static final class HttpCancelHandler extends AbstractEjbHandler { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); + String ct = getRequestHeader(exchange, CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType != null) { exchange.setStatusCode(BAD_REQUEST); @@ -531,7 +533,7 @@ private static final class HttpSessionOpenHandler extends AbstractEjbHandler { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - String ct = exchange.getRequestHeaders().getFirst(CONTENT_TYPE); + String ct = getRequestHeader(exchange, CONTENT_TYPE); ContentType contentType = ContentType.parse(ct); if (contentType == null || contentType.getVersion() != 1 || !SESSION_OPEN.getType().equals(contentType.getType())) { exchange.setStatusCode(BAD_REQUEST); @@ -671,8 +673,8 @@ public void convertToStateful(@NotNull SessionID sessionId) throws IllegalArgume exchange.setResponseCookie(new CookieImpl(JSESSIONID_COOKIE_NAME, sessionIdGenerator.createSessionId()).setPath(rootPath)); } - exchange.getResponseHeaders().put(CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION.toString()); - exchange.getResponseHeaders().put(EJB_SESSION_ID, Base64.getUrlEncoder().encodeToString(sessionId.getEncodedForm())); + putResponseHeader(exchange, CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION.toString()); + putResponseHeader(exchange, EJB_SESSION_ID, Base64.getUrlEncoder().encodeToString(sessionId.getEncodedForm())); exchange.setStatusCode(NO_CONTENT); exchange.endExchange(); @@ -709,7 +711,7 @@ public void moduleUnavailable(List modules) { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - exchange.getResponseHeaders().put(CONTENT_TYPE, EJB_DISCOVERY_RESPONSE.toString()); + putResponseHeader(exchange, CONTENT_TYPE, EJB_DISCOVERY_RESPONSE.toString()); byte[] data; final ByteArrayOutputStream out = new ByteArrayOutputStream(); Marshaller marshaller = config.getHttpMarshallerFactory(exchange) diff --git a/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java b/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java index 10ce3ad2..b9219b7c 100644 --- a/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java +++ b/ejb/src/test/java/org/wildfly/httpclient/ejb/AsyncInvocationTestCase.java @@ -19,6 +19,7 @@ package org.wildfly.httpclient.ejb; import static io.undertow.util.Headers.SET_COOKIE; +import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import java.net.URI; import java.util.concurrent.CompletableFuture; @@ -46,7 +47,7 @@ public class AsyncInvocationTestCase { @Before public void before() { - EJBTestServer.registerServicesHandler("common/v1/affinity", httpServerExchange -> httpServerExchange.getResponseHeaders().put(SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); + EJBTestServer.registerServicesHandler("common/v1/affinity", exchange -> putResponseHeader(exchange, SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); } @Test diff --git a/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java b/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java index 7e567fa1..d734ba47 100644 --- a/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java +++ b/ejb/src/test/java/org/wildfly/httpclient/ejb/SimpleInvocationTestCase.java @@ -19,6 +19,7 @@ package org.wildfly.httpclient.ejb; import static io.undertow.util.Headers.SET_COOKIE; +import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import org.jboss.ejb.client.EJBClient; import org.jboss.ejb.client.EJBClientContext; @@ -57,7 +58,7 @@ public class SimpleInvocationTestCase { @Before public void before() { - EJBTestServer.registerServicesHandler("common/v1/affinity", httpServerExchange -> httpServerExchange.getResponseHeaders().put(SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); + EJBTestServer.registerServicesHandler("common/v1/affinity", exchange -> putResponseHeader(exchange, SET_COOKIE, "JSESSIONID=" + EJBTestServer.INITIAL_SESSION_AFFINITY)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; ++i) { sb.append("Hello World "); diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java index 12645602..44e0c135 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java @@ -22,13 +22,13 @@ import static io.undertow.util.Headers.CONTENT_TYPE; import static java.net.URLEncoder.encode; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.wildfly.httpclient.common.HeadersHelper.putRequestHeader; import static org.wildfly.httpclient.common.Protocol.VERSION_PATH; import static org.wildfly.httpclient.naming.Constants.EXCEPTION; import static org.wildfly.httpclient.naming.Constants.NAMING_CONTEXT; import static org.wildfly.httpclient.naming.Constants.VALUE; import io.undertow.client.ClientRequest; -import io.undertow.util.HeaderMap; import org.wildfly.httpclient.common.Protocol; import javax.naming.Name; @@ -106,10 +106,9 @@ private void setRequestPath(final ClientRequest request, final String prefix) { } private void setRequestHeaders(final ClientRequest request) { - final HeaderMap headers = request.getRequestHeaders(); - headers.put(ACCEPT, VALUE + "," + EXCEPTION); + putRequestHeader(request, ACCEPT, VALUE + "," + EXCEPTION); if (object != null) { - headers.put(CONTENT_TYPE, VALUE.toString()); + putRequestHeader(request, CONTENT_TYPE, VALUE.toString()); } } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index dd0ac0b3..30432e65 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -25,6 +25,8 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.HeadersHelper.getRequestHeader; +import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.naming.Constants.NAME_PATH_PARAMETER; import static org.wildfly.httpclient.naming.Constants.NEW_QUERY_PARAMETER; @@ -134,7 +136,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { } else if (result instanceof Context) { exchange.setStatusCode(NO_CONTENT); } else { - exchange.getResponseHeaders().put(CONTENT_TYPE, VALUE.toString()); + putResponseHeader(exchange, CONTENT_TYPE, VALUE.toString()); HttpNamingServerObjectResolver resolver = new HttpNamingServerObjectResolver(exchange); Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(resolver); ByteOutput out = byteOutputOf(exchange.getOutputStream()); @@ -283,7 +285,7 @@ private AbstractClassFilteringNamingHandler(final HttpServiceConfig config, fina @Override protected Object doOperation(final HttpServerExchange exchange, final String name) throws NamingException { - ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(CONTENT_TYPE)); + ContentType contentType = ContentType.parse(getRequestHeader(exchange, CONTENT_TYPE)); if (contentType == null || !contentType.getType().equals(VALUE.getType()) || contentType.getVersion() != 1) { exchange.setStatusCode(BAD_REQUEST); exchange.endExchange(); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java index d6c5f580..c067fe21 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java @@ -18,6 +18,7 @@ package org.wildfly.httpclient.transaction; +import static org.wildfly.httpclient.common.HeadersHelper.getResponseHeader; import static org.wildfly.httpclient.transaction.ClientHandlers.xidHttpMarshaller; import static org.wildfly.httpclient.transaction.ClientHandlers.emptyHttpResultHandler; import static org.wildfly.httpclient.transaction.Constants.READ_ONLY; @@ -89,7 +90,7 @@ public void beforeCompletion() throws XAException { @Override public int prepare() throws XAException { boolean readOnly = processOperation(XA_PREPARE, (result) -> { - String header = result.getResponseHeaders().getFirst(READ_ONLY); + String header = getResponseHeader(result, READ_ONLY); return header != null && Boolean.parseBoolean(header); }, null); return readOnly ? XAResource.XA_RDONLY : XAResource.XA_OK; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java index f8a568c3..403f2e35 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java @@ -22,6 +22,7 @@ import static io.undertow.util.Headers.CONTENT_TYPE; import static java.net.URLEncoder.encode; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.wildfly.httpclient.common.HeadersHelper.putRequestHeader; import static org.wildfly.httpclient.common.Protocol.VERSION_PATH; import static org.wildfly.httpclient.transaction.Constants.EXCEPTION; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; @@ -36,7 +37,6 @@ import static org.wildfly.httpclient.transaction.RequestType.XA_RECOVER; import io.undertow.client.ClientRequest; -import io.undertow.util.HeaderMap; import org.wildfly.httpclient.common.Protocol; /** @@ -119,17 +119,16 @@ private void setRequestPath(final ClientRequest request, final String prefix) { private void setRequestHeaders(final ClientRequest request) { - final HeaderMap headers = request.getRequestHeaders(); if (requestType == UT_BEGIN) { - headers.put(ACCEPT, EXCEPTION + "," + NEW_TRANSACTION); - headers.put(TIMEOUT, timeout); + putRequestHeader(request, ACCEPT, EXCEPTION + "," + NEW_TRANSACTION); + putRequestHeader(request, TIMEOUT, timeout); } else if (requestType == XA_RECOVER) { - headers.put(ACCEPT, XID_LIST + "," + NEW_TRANSACTION); - headers.put(RECOVERY_PARENT_NAME, parentName); - headers.put(RECOVERY_FLAGS, Integer.toString(flags)); + putRequestHeader(request, ACCEPT, XID_LIST + "," + NEW_TRANSACTION); + putRequestHeader(request, RECOVERY_PARENT_NAME, parentName); + putRequestHeader(request, RECOVERY_FLAGS, Integer.toString(flags)); } else { - headers.put(ACCEPT, EXCEPTION.toString()); - headers.put(CONTENT_TYPE, XID.toString()); + putRequestHeader(request, ACCEPT, EXCEPTION.toString()); + putRequestHeader(request, CONTENT_TYPE, XID.toString()); } } diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index 6de81a6d..29bc617e 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -22,6 +22,8 @@ import static io.undertow.util.StatusCodes.INTERNAL_SERVER_ERROR; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.common.HeadersHelper.getRequestHeader; +import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_FLAGS; @@ -133,7 +135,7 @@ private AbstractTransactionHandler(final HttpServiceConfig config, final LocalTr @Override protected boolean isValidRequest(final HttpServerExchange exchange) { - final ContentType contentType = ContentType.parse(exchange.getRequestHeaders().getFirst(CONTENT_TYPE)); + final ContentType contentType = ContentType.parse(getRequestHeader(exchange, CONTENT_TYPE)); if (contentType == null || contentType.getVersion() != 1 || !contentType.getType().equals(XID.getType())) { exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s has incorrect or missing content type", exchange); @@ -175,7 +177,7 @@ private BeginHandler(final HttpServiceConfig config, final LocalTransactionConte @Override protected boolean isValidRequest(final HttpServerExchange exchange) { - final String timeoutString = exchange.getRequestHeaders().getFirst(TIMEOUT); + final String timeoutString = getRequestHeader(exchange, TIMEOUT); if (timeoutString == null) { exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s is missing %s header", exchange, TIMEOUT); @@ -187,9 +189,9 @@ protected boolean isValidRequest(final HttpServerExchange exchange) { @Override protected void processRequest(final HttpServerExchange exchange) { try { - final String timeoutString = exchange.getRequestHeaders().getFirst(TIMEOUT); + final String timeoutString = getRequestHeader(exchange, TIMEOUT); final Integer timeout = Integer.parseInt(timeoutString); - exchange.getResponseHeaders().put(CONTENT_TYPE, NEW_TRANSACTION.toString()); + putResponseHeader(exchange, CONTENT_TYPE, NEW_TRANSACTION.toString()); final LocalTransaction transaction = ctx.beginTransaction(timeout); final Xid xid = xidResolver.apply(transaction); @@ -214,13 +216,13 @@ private XARecoveryHandler(final HttpServiceConfig config, final LocalTransaction @Override protected boolean isValidRequest(final HttpServerExchange exchange) { - String flagsStringString = exchange.getRequestHeaders().getFirst(RECOVERY_FLAGS); + String flagsStringString = getRequestHeader(exchange, RECOVERY_FLAGS); if (flagsStringString == null) { exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s is missing %s header", exchange, RECOVERY_FLAGS); return false; } - String parentName = exchange.getRequestHeaders().getFirst(RECOVERY_PARENT_NAME); + String parentName = getRequestHeader(exchange, RECOVERY_PARENT_NAME); if (parentName == null) { exchange.setStatusCode(BAD_REQUEST); HttpRemoteTransactionMessages.MESSAGES.debugf("Exchange %s is missing %s header", exchange, RECOVERY_PARENT_NAME); @@ -232,9 +234,9 @@ protected boolean isValidRequest(final HttpServerExchange exchange) { @Override protected void processRequest(final HttpServerExchange exchange) { try { - final String flagsStringString = exchange.getRequestHeaders().getFirst(RECOVERY_FLAGS); + final String flagsStringString = getRequestHeader(exchange, RECOVERY_FLAGS); final int flags = Integer.parseInt(flagsStringString); - final String parentName = exchange.getRequestHeaders().getFirst(RECOVERY_PARENT_NAME); + final String parentName = getRequestHeader(exchange, RECOVERY_PARENT_NAME); final Xid[] recoveryList = ctx.getRecoveryInterface().recover(flags, parentName); final ByteArrayOutputStream out = new ByteArrayOutputStream(); From 3a7d2b88b09fe9a1f69dd6dd5921ff639301afa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 20:03:00 +0100 Subject: [PATCH 19/29] [WEJBHTTP-139] Refactoring - Enhance HeadersHelper methods to enhance code readability --- .../wildfly/httpclient/common/HeadersHelper.java | 15 +++++++++------ .../httpclient/common/HttpTargetContext.java | 2 +- .../common/PoolAuthenticationContext.java | 2 +- .../wildfly/httpclient/ejb/RequestBuilder.java | 12 ++++++------ .../wildfly/httpclient/ejb/ServerHandlers.java | 6 +++--- .../wildfly/httpclient/naming/RequestBuilder.java | 2 +- .../wildfly/httpclient/naming/ServerHandlers.java | 2 +- .../httpclient/transaction/RequestBuilder.java | 6 +++--- .../httpclient/transaction/ServerHandlers.java | 2 +- 9 files changed, 26 insertions(+), 23 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java index 6231e222..14942ef2 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java @@ -60,14 +60,16 @@ public static void putRequestHeader(final ClientRequest request, final HttpStrin request.getRequestHeaders().put(headerName, headerValue); } - public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final String headerValue) { + public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final Object headerValue) { if (request == null || headerName == null) return; - request.getRequestHeaders().put(headerName, headerValue); + if (headerValue == null) throw new IllegalArgumentException(); + request.getRequestHeaders().put(headerName, headerValue.toString()); } - public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { + public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final Object headerValue) { if (exchange == null || headerName == null) return; - exchange.getResponseHeaders().add(headerName, headerValue); + if (headerValue == null) throw new IllegalArgumentException(); + exchange.getResponseHeaders().add(headerName, headerValue.toString()); } public static String getRequestHeader(final HttpServerExchange exchange, final HttpString headerName) { @@ -75,9 +77,10 @@ public static String getRequestHeader(final HttpServerExchange exchange, final H return exchange.getRequestHeaders().getFirst(headerName); } - public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { + public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final Object headerValue) { if (exchange == null || headerName == null) return; - exchange.getResponseHeaders().put(headerName, headerValue); + if (headerValue == null) throw new IllegalArgumentException(); + exchange.getResponseHeaders().put(headerName, headerValue.toString()); } } diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java index a4b0e0cf..ede72d73 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java @@ -194,7 +194,7 @@ private void sendRequestInternal(final HttpConnectionPool.ConnectionHandle conne : authenticationConfiguration; if (containsRequestHeader(request, CONTENT_TYPE)) { - putRequestHeader(request, TRANSFER_ENCODING, CHUNKED.toString()); + putRequestHeader(request, TRANSFER_ENCODING, CHUNKED); } final boolean authAdded = retry || connection.getAuthenticationContext().prepareRequest(connection.getUri(), request, authenticationConfiguration); connection.sendRequest(request, new ClientCallback() { diff --git a/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java b/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java index 5949f1ea..1e8192ab 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java @@ -266,7 +266,7 @@ boolean prepareRequest(URI uri, ClientRequest request, AuthenticationConfigurati sb.append(", response=\""); sb.append(HexConverter.convertToHexString(digest.digest())); sb.append("\""); - putRequestHeader(request, AUTHORIZATION, sb.toString()); + putRequestHeader(request, AUTHORIZATION, sb); return true; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java index e6adf2ad..1e38f98f 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java @@ -141,21 +141,21 @@ private void setRequestHeaders(final ClientRequest request) { switch (requestType) { case INVOKE: { putRequestHeader(request, ACCEPT, INVOCATION_ACCEPT + "," + EJB_EXCEPTION); - putRequestHeader(request, CONTENT_TYPE, INVOCATION.toString()); + putRequestHeader(request, CONTENT_TYPE, INVOCATION); if (invocationId != null) { putRequestHeader(request, INVOCATION_ID, invocationId); } if (compressRequest) { - putRequestHeader(request, CONTENT_ENCODING, GZIP.toString()); + putRequestHeader(request, CONTENT_ENCODING, GZIP); } if (compressResponse) { - putRequestHeader(request, ACCEPT_ENCODING, GZIP.toString()); + putRequestHeader(request, ACCEPT_ENCODING, GZIP); } - putRequestHeader(request, TRANSFER_ENCODING, CHUNKED.toString()); + putRequestHeader(request, TRANSFER_ENCODING, CHUNKED); } break; case CREATE_SESSION: { - putRequestHeader(request, ACCEPT, EJB_EXCEPTION.toString()); - putRequestHeader(request, CONTENT_TYPE, SESSION_OPEN.toString()); + putRequestHeader(request, ACCEPT, EJB_EXCEPTION); + putRequestHeader(request, CONTENT_TYPE, SESSION_OPEN); } break; case DISCOVER: { putRequestHeader(request, ACCEPT, EJB_DISCOVERY_RESPONSE + "," + EJB_EXCEPTION); diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java index 98df8708..229c700d 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ServerHandlers.java @@ -418,7 +418,7 @@ public void writeInvocationResult(Object result) { cancellationFlags.remove(identifier); } try { - putResponseHeader(exchange, CONTENT_TYPE, Constants.EJB_RESPONSE.toString()); + putResponseHeader(exchange, CONTENT_TYPE, Constants.EJB_RESPONSE); // if (output.getSessionAffinity() != null) { // exchange.setResponseCookie(new CookieImpl("JSESSIONID", output.getSessionAffinity()).setPath(WILDFLY_SERVICES)); // } @@ -673,7 +673,7 @@ public void convertToStateful(@NotNull SessionID sessionId) throws IllegalArgume exchange.setResponseCookie(new CookieImpl(JSESSIONID_COOKIE_NAME, sessionIdGenerator.createSessionId()).setPath(rootPath)); } - putResponseHeader(exchange, CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION.toString()); + putResponseHeader(exchange, CONTENT_TYPE, EJB_RESPONSE_NEW_SESSION); putResponseHeader(exchange, EJB_SESSION_ID, Base64.getUrlEncoder().encodeToString(sessionId.getEncodedForm())); exchange.setStatusCode(NO_CONTENT); @@ -711,7 +711,7 @@ public void moduleUnavailable(List modules) { @Override protected void handleInternal(HttpServerExchange exchange) throws Exception { - putResponseHeader(exchange, CONTENT_TYPE, EJB_DISCOVERY_RESPONSE.toString()); + putResponseHeader(exchange, CONTENT_TYPE, EJB_DISCOVERY_RESPONSE); byte[] data; final ByteArrayOutputStream out = new ByteArrayOutputStream(); Marshaller marshaller = config.getHttpMarshallerFactory(exchange) diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java index 44e0c135..bbb9162a 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java @@ -108,7 +108,7 @@ private void setRequestPath(final ClientRequest request, final String prefix) { private void setRequestHeaders(final ClientRequest request) { putRequestHeader(request, ACCEPT, VALUE + "," + EXCEPTION); if (object != null) { - putRequestHeader(request, CONTENT_TYPE, VALUE.toString()); + putRequestHeader(request, CONTENT_TYPE, VALUE); } } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java index 30432e65..98a64aac 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ServerHandlers.java @@ -136,7 +136,7 @@ public final void handleRequest(HttpServerExchange exchange) throws Exception { } else if (result instanceof Context) { exchange.setStatusCode(NO_CONTENT); } else { - putResponseHeader(exchange, CONTENT_TYPE, VALUE.toString()); + putResponseHeader(exchange, CONTENT_TYPE, VALUE); HttpNamingServerObjectResolver resolver = new HttpNamingServerObjectResolver(exchange); Marshaller marshaller = config.getHttpMarshallerFactory(exchange).createMarshaller(resolver); ByteOutput out = byteOutputOf(exchange.getOutputStream()); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java index 403f2e35..fb8af8cd 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java @@ -125,10 +125,10 @@ private void setRequestHeaders(final ClientRequest request) { } else if (requestType == XA_RECOVER) { putRequestHeader(request, ACCEPT, XID_LIST + "," + NEW_TRANSACTION); putRequestHeader(request, RECOVERY_PARENT_NAME, parentName); - putRequestHeader(request, RECOVERY_FLAGS, Integer.toString(flags)); + putRequestHeader(request, RECOVERY_FLAGS, flags); } else { - putRequestHeader(request, ACCEPT, EXCEPTION.toString()); - putRequestHeader(request, CONTENT_TYPE, XID.toString()); + putRequestHeader(request, ACCEPT, EXCEPTION); + putRequestHeader(request, CONTENT_TYPE, XID); } } diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index 29bc617e..766b4b01 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -191,7 +191,7 @@ protected void processRequest(final HttpServerExchange exchange) { try { final String timeoutString = getRequestHeader(exchange, TIMEOUT); final Integer timeout = Integer.parseInt(timeoutString); - putResponseHeader(exchange, CONTENT_TYPE, NEW_TRANSACTION.toString()); + putResponseHeader(exchange, CONTENT_TYPE, NEW_TRANSACTION); final LocalTransaction transaction = ctx.beginTransaction(timeout); final Xid xid = xidResolver.apply(transaction); From 50ba2940f08a3d57954bc61cf67ae550fb9886fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 23:19:20 +0100 Subject: [PATCH 20/29] [WEJBHTTP-139] Refactoring - use parseBoolean via static import --- .../transaction/HttpSubordinateTransactionHandle.java | 6 ++++-- .../org/wildfly/httpclient/transaction/ServerHandlers.java | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java index c067fe21..a90c23d9 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/HttpSubordinateTransactionHandle.java @@ -18,6 +18,8 @@ package org.wildfly.httpclient.transaction; +import static java.lang.Boolean.TRUE; +import static java.lang.Boolean.parseBoolean; import static org.wildfly.httpclient.common.HeadersHelper.getResponseHeader; import static org.wildfly.httpclient.transaction.ClientHandlers.xidHttpMarshaller; import static org.wildfly.httpclient.transaction.ClientHandlers.emptyHttpResultHandler; @@ -69,7 +71,7 @@ Xid getId() { @Override public void commit(boolean onePhase) throws XAException { - processOperation(XA_COMMIT, null, onePhase ? Boolean.TRUE : null); + processOperation(XA_COMMIT, null, onePhase ? TRUE : null); } @Override @@ -91,7 +93,7 @@ public void beforeCompletion() throws XAException { public int prepare() throws XAException { boolean readOnly = processOperation(XA_PREPARE, (result) -> { String header = getResponseHeader(result, READ_ONLY); - return header != null && Boolean.parseBoolean(header); + return parseBoolean(header); }, null); return readOnly ? XAResource.XA_RDONLY : XAResource.XA_OK; } diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index 766b4b01..e040e6ff 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -17,6 +17,7 @@ */ package org.wildfly.httpclient.transaction; +import static java.lang.Boolean.parseBoolean; import static io.undertow.util.Headers.CONTENT_TYPE; import static io.undertow.util.StatusCodes.BAD_REQUEST; import static io.undertow.util.StatusCodes.INTERNAL_SERVER_ERROR; @@ -330,7 +331,7 @@ protected void handleImpl(final HttpServerExchange exchange, final ImportResult< Deque opc = exchange.getQueryParameters().get("opc"); boolean onePhase = false; if (opc != null && !opc.isEmpty()) { - onePhase = Boolean.parseBoolean(opc.poll()); + onePhase = parseBoolean(opc.poll()); } transaction.getControl().commit(onePhase); } From 928e1110f71a84df79bcb991c80f5e9f997fea2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 23:24:29 +0100 Subject: [PATCH 21/29] [WEJBHTTP-139] Refactoring - introducing OPC query parameter constant --- .../java/org/wildfly/httpclient/transaction/Constants.java | 3 +++ .../org/wildfly/httpclient/transaction/RequestBuilder.java | 4 +++- .../org/wildfly/httpclient/transaction/ServerHandlers.java | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/Constants.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/Constants.java index 164d8660..07da79f1 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/Constants.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/Constants.java @@ -42,6 +42,9 @@ final class Constants { // context path static final String TXN_CONTEXT = "/txn"; + // params + static final String OPC_QUERY_PARAMETER = "opc"; + // protocols static final String HTTP_SCHEME = "http"; static final String HTTPS_SCHEME = "https"; diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java index fb8af8cd..bbfb0896 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java @@ -18,6 +18,7 @@ package org.wildfly.httpclient.transaction; +import static java.lang.Boolean.TRUE; import static io.undertow.util.Headers.ACCEPT; import static io.undertow.util.Headers.CONTENT_TYPE; import static java.net.URLEncoder.encode; @@ -26,6 +27,7 @@ import static org.wildfly.httpclient.common.Protocol.VERSION_PATH; import static org.wildfly.httpclient.transaction.Constants.EXCEPTION; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; +import static org.wildfly.httpclient.transaction.Constants.OPC_QUERY_PARAMETER; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_FLAGS; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_PARENT_NAME; import static org.wildfly.httpclient.transaction.Constants.TIMEOUT; @@ -110,7 +112,7 @@ private void setRequestPath(final ClientRequest request, final String prefix) { appendPath(sb, VERSION_PATH + version, false); appendPath(sb, requestType.getPath(), false); if (requestType == XA_COMMIT) { - sb.append(onePhase != null && onePhase ? "?opc=true" : ""); + sb.append(onePhase != null && onePhase ? "?" + OPC_QUERY_PARAMETER + "=" + TRUE : ""); } else if (requestType == XA_RECOVER) { appendPath(sb, parentName, false); } diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java index e040e6ff..8f41cc55 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ServerHandlers.java @@ -27,6 +27,7 @@ import static org.wildfly.httpclient.common.HeadersHelper.putResponseHeader; import static org.wildfly.httpclient.common.HttpServerHelper.sendException; import static org.wildfly.httpclient.transaction.Constants.NEW_TRANSACTION; +import static org.wildfly.httpclient.transaction.Constants.OPC_QUERY_PARAMETER; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_FLAGS; import static org.wildfly.httpclient.transaction.Constants.RECOVERY_PARENT_NAME; import static org.wildfly.httpclient.transaction.Constants.TIMEOUT; @@ -328,7 +329,7 @@ private XACommitHandler(final HttpServiceConfig config, final LocalTransactionCo @Override protected void handleImpl(final HttpServerExchange exchange, final ImportResult transaction) throws Exception { - Deque opc = exchange.getQueryParameters().get("opc"); + Deque opc = exchange.getQueryParameters().get(OPC_QUERY_PARAMETER); boolean onePhase = false; if (opc != null && !opc.isEmpty()) { onePhase = parseBoolean(opc.poll()); From 0d91d2731f0a7eee83403c5d6eea66ef4d93b3cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 23:28:30 +0100 Subject: [PATCH 22/29] [WEJBHTTP-139] Refactoring - use NEW_QUERY_PARAMETER constant --- .../java/org/wildfly/httpclient/naming/RequestBuilder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java index bbb9162a..49838e1a 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java @@ -26,6 +26,7 @@ import static org.wildfly.httpclient.common.Protocol.VERSION_PATH; import static org.wildfly.httpclient.naming.Constants.EXCEPTION; import static org.wildfly.httpclient.naming.Constants.NAMING_CONTEXT; +import static org.wildfly.httpclient.naming.Constants.NEW_QUERY_PARAMETER; import static org.wildfly.httpclient.naming.Constants.VALUE; import io.undertow.client.ClientRequest; @@ -99,7 +100,7 @@ private void setRequestPath(final ClientRequest request, final String prefix) { appendPath(sb, requestType.getPath(), false); appendPath(sb, name.toString(), true); if (newName != null) { - sb.append("?new="); + sb.append("?" + NEW_QUERY_PARAMETER + "="); sb.append(encode(newName.toString(), UTF_8)); } request.setPath(sb.toString()); From 1b4f6658ed03b41f6771067616dfd233cd091e72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 23:38:10 +0100 Subject: [PATCH 23/29] [WEJBHTTP-139] Refactoring - unify ClientRequest variable names --- .../httpclient/common/HttpTargetContext.java | 12 ++++++------ .../common/PoolAuthenticationContextTestCase.java | 14 +++++++------- .../org/wildfly/httpclient/ejb/RequestBuilder.java | 10 +++++----- .../wildfly/httpclient/naming/HttpRootContext.java | 12 ++++++------ .../wildfly/httpclient/naming/RequestBuilder.java | 10 +++++----- .../httpclient/transaction/RequestBuilder.java | 10 +++++----- 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java index ede72d73..a73e0f7a 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HttpTargetContext.java @@ -144,9 +144,9 @@ private void acquireAffinitiy(AuthenticationConfiguration authenticationConfigur private void acquireSessionAffinity(CountDownLatch latch, AuthenticationConfiguration authenticationConfiguration) { - ClientRequest clientRequest = new ClientRequest(); - clientRequest.setMethod(Methods.GET); - clientRequest.setPath(uri.getPath() + "/common/v1/affinity"); + ClientRequest request = new ClientRequest(); + request.setMethod(Methods.GET); + request.setPath(uri.getPath() + "/common/v1/affinity"); AuthenticationContext context = AuthenticationContext.captureCurrent(); SSLContext sslContext; try { @@ -156,7 +156,7 @@ private void acquireSessionAffinity(CountDownLatch latch, AuthenticationConfigur HttpClientMessages.MESSAGES.failedToAcquireSession(e); return; } - sendRequest(clientRequest, sslContext, authenticationConfiguration, null, null, (e) -> { + sendRequest(request, sslContext, authenticationConfiguration, null, null, (e) -> { latch.countDown(); HttpClientMessages.MESSAGES.failedToAcquireSession(e); }, null, latch::countDown); @@ -439,8 +439,8 @@ private static Map readAttachments(final ObjectInput input) thro return attachments; } - public HttpMarshallerFactory getHttpMarshallerFactory(ClientRequest clientRequest) { - return this.httpMarshallerFactoryProvider.getMarshallerFactory(clientRequest); + public HttpMarshallerFactory getHttpMarshallerFactory(ClientRequest request) { + return this.httpMarshallerFactoryProvider.getMarshallerFactory(request); } public HttpConnectionPool getConnectionPool() { diff --git a/common/src/test/java/org/wildfly/httpclient/common/PoolAuthenticationContextTestCase.java b/common/src/test/java/org/wildfly/httpclient/common/PoolAuthenticationContextTestCase.java index 9aa091f3..1da27a2c 100644 --- a/common/src/test/java/org/wildfly/httpclient/common/PoolAuthenticationContextTestCase.java +++ b/common/src/test/java/org/wildfly/httpclient/common/PoolAuthenticationContextTestCase.java @@ -32,19 +32,19 @@ public class PoolAuthenticationContextTestCase { @Test public void testCreateTargetUriWithParams() throws URISyntaxException { - ClientRequest req = new ClientRequest(); - req.setPath("/te%2Bst/test.html?param1=value1¶m2=val%20ue2"); + ClientRequest request = new ClientRequest(); + request.setPath("/te%2Bst/test.html?param1=value1¶m2=val%20ue2"); Assert.assertEquals("http://localhost:8080/te%2Bst/test.html?param1=value1¶m2=val%20ue2", - PoolAuthenticationContext.createTargetUri(new URI("http://localhost:8080"), req)); + PoolAuthenticationContext.createTargetUri(new URI("http://localhost:8080"), request)); Assert.assertEquals("http://localhost/te%2Bst/test.html?param1=value1¶m2=val%20ue2", - PoolAuthenticationContext.createTargetUri(new URI("http://localhost"), req)); + PoolAuthenticationContext.createTargetUri(new URI("http://localhost"), request)); Assert.assertEquals("http://localhost/te%2Bst/test.html?param1=value1¶m2=val%20ue2", - PoolAuthenticationContext.createTargetUri(new URI("http://localhost:80"), req)); + PoolAuthenticationContext.createTargetUri(new URI("http://localhost:80"), request)); Assert.assertEquals("https://localhost/te%2Bst/test.html?param1=value1¶m2=val%20ue2", - PoolAuthenticationContext.createTargetUri(new URI("https://localhost"), req)); + PoolAuthenticationContext.createTargetUri(new URI("https://localhost"), request)); Assert.assertEquals("https://localhost/te%2Bst/test.html?param1=value1¶m2=val%20ue2", - PoolAuthenticationContext.createTargetUri(new URI("https://localhost:443"), req)); + PoolAuthenticationContext.createTargetUri(new URI("https://localhost:443"), request)); } @Test diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java index 1e38f98f..8d0d910a 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java @@ -116,11 +116,11 @@ RequestBuilder setCancelIfRunning(final boolean cancelIfRunning) { } ClientRequest createRequest(final String prefix) { - final ClientRequest clientRequest = new ClientRequest(); - setRequestMethod(clientRequest); - setRequestPath(clientRequest, prefix); - setRequestHeaders(clientRequest); - return clientRequest; + final ClientRequest request = new ClientRequest(); + setRequestMethod(request); + setRequestPath(request, prefix); + setRequestHeaders(request); + return request; } private void setRequestMethod(final ClientRequest request) { diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java index b1ee6b81..bf43cbf5 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/HttpRootContext.java @@ -252,7 +252,7 @@ private Object processInvocation(RequestType requestType, Name name, Name newNam }, environment, context, name, object); } - private Object performOperation(Name name, URI providerUri, HttpTargetContext targetContext, ClientRequest clientRequest) throws NamingException { + private Object performOperation(Name name, URI providerUri, HttpTargetContext targetContext, ClientRequest request) throws NamingException { final ProviderEnvironment providerEnvironment = httpNamingProvider.getProviderEnvironment(); final AuthenticationContext context = providerEnvironment.getAuthenticationContextSupplier().get(); AuthenticationContextConfigurationClient client = CLIENT; @@ -269,10 +269,10 @@ private Object performOperation(Name name, URI providerUri, HttpTargetContext ta final CompletableFuture result = new CompletableFuture<>(); final ObjectResolver objectResolver = getObjectResolver(providerUri); - final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(clientRequest); + final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(objectResolver, result); if (unmarshaller != null) { - targetContext.sendRequest(clientRequest, sslContext, authenticationConfiguration, null, + targetContext.sendRequest(request, sslContext, authenticationConfiguration, null, optionalObjectHttpResultHandler(unmarshaller, result, httpNamingProvider, getContextClassLoader()), result::completeExceptionally, VALUE, null, true); } @@ -303,7 +303,7 @@ private boolean canRetry(ProviderEnvironment environment) { return environment.getProviderUris().size() > 1; } - private void performOperation(URI providerUri, Object object, HttpTargetContext targetContext, ClientRequest clientRequest) throws NamingException { + private void performOperation(URI providerUri, Object object, HttpTargetContext targetContext, ClientRequest request) throws NamingException { final ProviderEnvironment providerEnvironment = httpNamingProvider.getProviderEnvironment(); final AuthenticationContext context = providerEnvironment.getAuthenticationContextSupplier().get(); AuthenticationContextConfigurationClient client = CLIENT; @@ -320,10 +320,10 @@ private void performOperation(URI providerUri, Object object, HttpTargetContext final CompletableFuture result = new CompletableFuture<>(); final ObjectResolver objectResolver = getObjectResolver(providerUri); - final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(clientRequest); + final HttpMarshallerFactory marshallerFactory = targetContext.getHttpMarshallerFactory(request); final Marshaller marshaller = marshallerFactory.createMarshaller(objectResolver, result); if (marshaller != null) { - targetContext.sendRequest(clientRequest, sslContext, authenticationConfiguration, + targetContext.sendRequest(request, sslContext, authenticationConfiguration, object != null ? objectHttpMarshaller(marshaller, object) : null, emptyHttpResultHandler(result, null), result::completeExceptionally, null, null); } try { diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java index 49838e1a..9283e12a 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/RequestBuilder.java @@ -79,11 +79,11 @@ RequestBuilder setVersion(final int version) { // helper methods ClientRequest createRequest(final String prefix) { - final ClientRequest clientRequest = new ClientRequest(); - setRequestMethod(clientRequest); - setRequestPath(clientRequest, prefix); - setRequestHeaders(clientRequest); - return clientRequest; + final ClientRequest request = new ClientRequest(); + setRequestMethod(request); + setRequestPath(request, prefix); + setRequestHeaders(request); + return request; } private void setRequestMethod(final ClientRequest request) { diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java index bbfb0896..93e25d70 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java @@ -92,11 +92,11 @@ RequestBuilder setParent(final String parentName) { // helper methods ClientRequest createRequest(final String prefix) { - final ClientRequest clientRequest = new ClientRequest(); - setRequestMethod(clientRequest); - setRequestPath(clientRequest, prefix); - setRequestHeaders(clientRequest); - return clientRequest; + final ClientRequest request = new ClientRequest(); + setRequestMethod(request); + setRequestPath(request, prefix); + setRequestHeaders(request); + return request; } private void setRequestMethod(final ClientRequest request) { From b1e7ff3f7e7ebb3e69d246e8851eb4589b8c54d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 23:45:09 +0100 Subject: [PATCH 24/29] [WEJBHTTP-139] Refactoring - unify ClientResponse variable names --- .../wildfly/httpclient/ejb/ClientHandlers.java | 18 +++++++++--------- .../httpclient/naming/ClientHandlers.java | 14 +++++++------- .../httpclient/transaction/ClientHandlers.java | 8 ++++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index 32e7c745..d7107521 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -147,9 +147,9 @@ private EmptyHttpResultHandler(final CompletableFuture result, final Function } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try { - result.complete(function != null ? function.apply(httpResponse) : null); + result.complete(function != null ? function.apply(response) : null); } finally { IoUtils.safeClose(doneCallback); } @@ -166,7 +166,7 @@ private DiscoveryHttpResultHandler(final Unmarshaller unmarshaller, final Comple } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try (ByteInput in = byteInputOf(is)) { Set modules; unmarshaller.start(in); @@ -199,12 +199,12 @@ private EjbClassLoaderAwareHttpResultHandler(final Unmarshaller unmarshaller, fi this.clientCtx = clientCtx; } - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { receiverCtx.resultReady(new EJBReceiverInvocationContext.ResultProducer() { @Override public Object getResult() throws Exception { final CompletableFuture result = new CompletableFuture<>(); - invokeHttpResultHandlerInternal(unmarshaller, result).handleResult(is, httpResponse, doneCallback); + invokeHttpResultHandlerInternal(unmarshaller, result).handleResult(is, response, doneCallback); // WEJBHTTP-83 - remove jboss.returned.keys values from the local context data, so that after unmarshalling the response, we have the correct ContextData Set returnedContextDataKeys = (Set) clientCtx.getContextData().get(EJBClientInvocationContext.RETURNED_CONTEXT_DATA_KEY); @@ -245,7 +245,7 @@ private InvokeHttpResultHandler(final Unmarshaller unmarshaller, final Completab } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); final Object returned = deserializeObject(unmarshaller); @@ -262,15 +262,15 @@ public void handleResult(final InputStream is, final ClientResponse httpResponse private static final class CancelInvocationResponseFunction implements Function { @Override - public Boolean apply(final ClientResponse clientResponse) { + public Boolean apply(final ClientResponse response) { return true; } } private static final class CreateSessionResponseFunction implements Function { @Override - public SessionID apply(final ClientResponse clientResponse) { - final String sessionId = getResponseHeader(clientResponse, Constants.EJB_SESSION_ID); + public SessionID apply(final ClientResponse response) { + final String sessionId = getResponseHeader(response, Constants.EJB_SESSION_ID); if (sessionId != null) { return SessionID.createSessionID(Base64.getUrlDecoder().decode(sessionId)); } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java index f22d71c7..f24fd34f 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/ClientHandlers.java @@ -95,9 +95,9 @@ private EmptyHttpResultHandler(final CompletableFuture result, final Function } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try { - result.complete(function != null ? function.apply(httpResponse) : null); + result.complete(function != null ? function.apply(response) : null); } finally { safeClose(doneCallback); } @@ -118,15 +118,15 @@ private OptionalObjectHttpResultHandler(final Unmarshaller unmarshaller, final C } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try { namingProvider.performExceptionAction((a, b) -> { ClassLoader old = setContextClassLoader(classLoader); try { - if (httpResponse.getResponseCode() == NO_CONTENT) { - emptyHttpResultHandler(result, null).handleResult(is, httpResponse, doneCallback); + if (response.getResponseCode() == NO_CONTENT) { + emptyHttpResultHandler(result, null).handleResult(is, response, doneCallback); } else { - objectHttpResultHandler(unmarshaller, result).handleResult(is, httpResponse, doneCallback); + objectHttpResultHandler(unmarshaller, result).handleResult(is, response, doneCallback); } } finally { setContextClassLoader(old); @@ -149,7 +149,7 @@ private ObjectHttpResultHandler(final Unmarshaller unmarshaller, final Completab } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); Object object = deserializeObject(unmarshaller); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java index 432874b7..2a687184 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/ClientHandlers.java @@ -94,9 +94,9 @@ private EmptyHttpResultHandler(final CompletableFuture result, final Function } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try { - result.complete(function != null ? function.apply(httpResponse) : null); + result.complete(function != null ? function.apply(response) : null); } finally { IoUtils.safeClose(doneCallback); } @@ -113,7 +113,7 @@ private XidHttpResultHandler(final Unmarshaller unmarshaller, final CompletableF } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); Xid xid = deserializeXid(unmarshaller); @@ -137,7 +137,7 @@ private XidArrayHttpResultHandler(final Unmarshaller unmarshaller, final Complet } @Override - public void handleResult(final InputStream is, final ClientResponse httpResponse, final Closeable doneCallback) { + public void handleResult(final InputStream is, final ClientResponse response, final Closeable doneCallback) { try (ByteInput in = byteInputOf(is)) { unmarshaller.start(in); Xid[] ret = deserializeXidArray(unmarshaller); From bd13773b863e8b3d679009bf2c48f6179fd199fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Tue, 10 Dec 2024 23:49:25 +0100 Subject: [PATCH 25/29] [WEJBHTTP-139] Refactoring - use EJB_SESSION_ID via static import --- .../main/java/org/wildfly/httpclient/ejb/ClientHandlers.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index d7107521..6e150b50 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -20,6 +20,7 @@ import static org.wildfly.httpclient.common.HeadersHelper.getResponseHeader; import static org.wildfly.httpclient.common.ByteInputs.byteInputOf; import static org.wildfly.httpclient.common.ByteOutputs.byteOutputOf; +import static org.wildfly.httpclient.ejb.Constants.EJB_SESSION_ID; import static org.wildfly.httpclient.ejb.Serializer.deserializeObject; import static org.wildfly.httpclient.ejb.Serializer.deserializeSet; import static org.wildfly.httpclient.ejb.Serializer.serializeMap; @@ -270,7 +271,7 @@ public Boolean apply(final ClientResponse response) { private static final class CreateSessionResponseFunction implements Function { @Override public SessionID apply(final ClientResponse response) { - final String sessionId = getResponseHeader(response, Constants.EJB_SESSION_ID); + final String sessionId = getResponseHeader(response, EJB_SESSION_ID); if (sessionId != null) { return SessionID.createSessionID(Base64.getUrlDecoder().decode(sessionId)); } From 43d0acd0c39f726f2d54f20723e7f9d6fd4ce635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 16 Jan 2025 13:18:54 +0100 Subject: [PATCH 26/29] [WEJBHTTP-139] Enhancement according to peer review - always throw IllegalArgumentException if HeadersHelper method parameter is null --- .../httpclient/common/HeadersHelper.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java index 14942ef2..0bc9496d 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java @@ -36,50 +36,47 @@ public static void addRequestHeader(final ClientRequest request, final HttpStrin } public static boolean containsRequestHeader(final ClientRequest request, final HttpString headerName) { - if (request == null || headerName == null) return false; + if (request == null || headerName == null) throw new IllegalArgumentException(); return request.getRequestHeaders().contains(headerName); } public static String getRequestHeader(final ClientRequest request, final HttpString headerName) { - if (request == null || headerName == null) return null; + if (request == null || headerName == null) throw new IllegalArgumentException(); return request.getRequestHeaders().getFirst(headerName); } public static String getResponseHeader(final ClientResponse response, final HttpString headerName) { - if (response == null || headerName == null) return null; + if (response == null || headerName == null) throw new IllegalArgumentException(); return response.getResponseHeaders().getFirst(headerName); } public static HeaderValues getResponseHeaders(final ClientResponse response, final HttpString headerName) { - if (response == null || headerName == null) return null; + if (response == null || headerName == null) throw new IllegalArgumentException(); return response.getResponseHeaders().get(headerName); } public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final long headerValue) { - if (request == null || headerName == null) return; + if (request == null || headerName == null) throw new IllegalArgumentException(); request.getRequestHeaders().put(headerName, headerValue); } public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final Object headerValue) { - if (request == null || headerName == null) return; - if (headerValue == null) throw new IllegalArgumentException(); + if (request == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); request.getRequestHeaders().put(headerName, headerValue.toString()); } public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final Object headerValue) { - if (exchange == null || headerName == null) return; - if (headerValue == null) throw new IllegalArgumentException(); + if (exchange == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); exchange.getResponseHeaders().add(headerName, headerValue.toString()); } public static String getRequestHeader(final HttpServerExchange exchange, final HttpString headerName) { - if (exchange == null || headerName == null) return null; + if (exchange == null || headerName == null) throw new IllegalArgumentException(); return exchange.getRequestHeaders().getFirst(headerName); } public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final Object headerValue) { - if (exchange == null || headerName == null) return; - if (headerValue == null) throw new IllegalArgumentException(); + if (exchange == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); exchange.getResponseHeaders().put(headerName, headerValue.toString()); } From 32c4a0ed5ac673839161d7ea9ab090f2cc71ff40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 16 Jan 2025 13:42:00 +0100 Subject: [PATCH 27/29] [WEJBHTTP-139] Enhancement according to peer review - use 'input' and 'output' variable names for ObjectInput and ObjectOutput --- .../wildfly/httpclient/ejb/Serializer.java | 98 +++++++++---------- .../wildfly/httpclient/naming/Serializer.java | 8 +- .../httpclient/transaction/Serializer.java | 36 +++---- 3 files changed, 71 insertions(+), 71 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java index 823eb534..6facae96 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java @@ -48,58 +48,58 @@ private Serializer() { // forbidden instantiation } - static void serializeObject(final ObjectOutput out, final Object object) throws IOException { - out.writeObject(object); + static void serializeObject(final ObjectOutput output, final Object object) throws IOException { + output.writeObject(object); } - static Object deserializeObject(final ObjectInput in) throws IOException, ClassNotFoundException { - return in.readObject(); + static Object deserializeObject(final ObjectInput input) throws IOException, ClassNotFoundException { + return input.readObject(); } - static void serializeObjectArray(final ObjectOutput out, final Object[] objects) throws IOException { + static void serializeObjectArray(final ObjectOutput output, final Object[] objects) throws IOException { if (objects == null) return; for (final Object object : objects) { - out.writeObject(object); + output.writeObject(object); } } - static void deserializeObjectArray(final ObjectInput in, final Object[] objects) throws IOException, ClassNotFoundException { + static void deserializeObjectArray(final ObjectInput input, final Object[] objects) throws IOException, ClassNotFoundException { if (objects == null) return; for (int i = 0; i < objects.length; i++) { - objects[i] = deserializeObject(in); + objects[i] = deserializeObject(input); } } - static void serializePackedInteger(final ObjectOutput out, int value) throws IOException { + static void serializePackedInteger(final ObjectOutput output, int value) throws IOException { if (value < 0) throw new IllegalArgumentException(); if (value > 127) { - out.writeByte(value & 0x7F | 0x80); - serializePackedInteger(out, value >> 7); + output.writeByte(value & 0x7F | 0x80); + serializePackedInteger(output, value >> 7); } else { - out.writeByte(value & 0xFF); + output.writeByte(value & 0xFF); } } - static int deserializePackedInteger(final ObjectInput in) throws IOException { - int ret = in.readByte(); + static int deserializePackedInteger(final ObjectInput input) throws IOException { + int ret = input.readByte(); if ((ret & 0x80) == 0x80) { - return deserializePackedInteger(in) << 7 | (ret & 0x7F); + return deserializePackedInteger(input) << 7 | (ret & 0x7F); } return ret; } - static void serializeMap(final ObjectOutput out, final Map map) throws IOException { + static void serializeMap(final ObjectOutput output, final Map map) throws IOException { int size = map != null ? map.size() : 0; - serializePackedInteger(out, size); + serializePackedInteger(output, size); if (size > 0) for (Map.Entry entry : map.entrySet()) { - out.writeObject(entry.getKey()); - out.writeObject(entry.getValue()); + output.writeObject(entry.getKey()); + output.writeObject(entry.getValue()); } } - static Map deserializeMap(final ObjectInput in) throws IOException, ClassNotFoundException { - final int contextDataSize = deserializePackedInteger(in); + static Map deserializeMap(final ObjectInput input) throws IOException, ClassNotFoundException { + final int contextDataSize = deserializePackedInteger(input); if (contextDataSize == 0) { return new HashMap<>(); } @@ -107,71 +107,71 @@ static Map deserializeMap(final ObjectInput in) throws IOExcepti String key; Object value; for (int i = 0; i < contextDataSize; i++) { - key = (String) in.readObject(); - value = in.readObject(); + key = (String) input.readObject(); + value = input.readObject(); ret.put(key, value); } return ret; } - static void serializeSet(final ObjectOutput out, final Set set) throws IOException { - out.writeInt(set.size()); + static void serializeSet(final ObjectOutput output, final Set set) throws IOException { + output.writeInt(set.size()); for (EJBModuleIdentifier moduleId : set) { - out.writeObject(moduleId); + output.writeObject(moduleId); } } - static Set deserializeSet(final ObjectInput in) throws IOException, ClassNotFoundException { - int size = in.readInt(); + static Set deserializeSet(final ObjectInput input) throws IOException, ClassNotFoundException { + int size = input.readInt(); Set ret = new HashSet<>(size); for (int i = 0; i < size; i++) { - ret.add((EJBModuleIdentifier) in.readObject()); + ret.add((EJBModuleIdentifier) input.readObject()); } return ret; } - static void serializeTransaction(final ObjectOutput out, final TransactionInfo txn) throws IOException { + static void serializeTransaction(final ObjectOutput output, final TransactionInfo txn) throws IOException { final byte transactionType = txn.getType(); - out.writeByte(transactionType); + output.writeByte(transactionType); if (transactionType == NULL_TRANSACTION) { return; } - serializeXid(out, txn.getXid()); + serializeXid(output, txn.getXid()); if (transactionType == REMOTE_TRANSACTION) { return; } if (transactionType == LOCAL_TRANSACTION) { - out.writeInt(txn.getRemainingTime()); + output.writeInt(txn.getRemainingTime()); } } - static TransactionInfo deserializeTransaction(final ObjectInput in) throws IOException { - final int txnType = in.readByte(); + static TransactionInfo deserializeTransaction(final ObjectInput input) throws IOException { + final int txnType = input.readByte(); if (txnType == NULL_TRANSACTION) { return nullTransaction(); } else if (txnType == REMOTE_TRANSACTION || txnType == LOCAL_TRANSACTION) { - final Xid xid = deserializeXid(in); - return txnType == REMOTE_TRANSACTION ? remoteTransaction(xid) : localTransaction(xid, in.readInt()); + final Xid xid = deserializeXid(input); + return txnType == REMOTE_TRANSACTION ? remoteTransaction(xid) : localTransaction(xid, input.readInt()); } throw EjbHttpClientMessages.MESSAGES.invalidTransactionType(txnType); } - static void serializeXid(final ObjectOutput out, final Xid xid) throws IOException { - out.writeInt(xid.getFormatId()); - out.writeInt(xid.getGlobalTransactionId().length); - out.write(xid.getGlobalTransactionId()); - out.writeInt(xid.getBranchQualifier().length); - out.write(xid.getBranchQualifier()); + static void serializeXid(final ObjectOutput output, final Xid xid) throws IOException { + output.writeInt(xid.getFormatId()); + output.writeInt(xid.getGlobalTransactionId().length); + output.write(xid.getGlobalTransactionId()); + output.writeInt(xid.getBranchQualifier().length); + output.write(xid.getBranchQualifier()); } - static Xid deserializeXid(final ObjectInput in) throws IOException { - int formatId = in.readInt(); - int length = in.readInt(); + static Xid deserializeXid(final ObjectInput input) throws IOException { + int formatId = input.readInt(); + int length = input.readInt(); byte[] globalId = new byte[length]; - in.readFully(globalId); - length = in.readInt(); + input.readFully(globalId); + length = input.readInt(); byte[] branchId = new byte[length]; - in.readFully(branchId); + input.readFully(branchId); return new SimpleXid(formatId, globalId, branchId); } diff --git a/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java b/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java index 9aa7a4fb..f616f794 100644 --- a/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java +++ b/naming/src/main/java/org/wildfly/httpclient/naming/Serializer.java @@ -33,12 +33,12 @@ private Serializer() { // forbidden instantiation } - static void serializeObject(final ObjectOutput out, final Object object) throws IOException { - out.writeObject(object); + static void serializeObject(final ObjectOutput output, final Object object) throws IOException { + output.writeObject(object); } - static Object deserializeObject(final ObjectInput in) throws IOException, ClassNotFoundException { - return in.readObject(); + static Object deserializeObject(final ObjectInput input) throws IOException, ClassNotFoundException { + return input.readObject(); } } diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/Serializer.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/Serializer.java index cde6b9d8..ed2d62cd 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/Serializer.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/Serializer.java @@ -36,37 +36,37 @@ private Serializer() { // forbidden instantiation } - static void serializeXid(final ObjectOutput out, final Xid xid) throws IOException { - out.writeInt(xid.getFormatId()); - out.writeInt(xid.getGlobalTransactionId().length); - out.write(xid.getGlobalTransactionId()); - out.writeInt(xid.getBranchQualifier().length); - out.write(xid.getBranchQualifier()); + static void serializeXid(final ObjectOutput output, final Xid xid) throws IOException { + output.writeInt(xid.getFormatId()); + output.writeInt(xid.getGlobalTransactionId().length); + output.write(xid.getGlobalTransactionId()); + output.writeInt(xid.getBranchQualifier().length); + output.write(xid.getBranchQualifier()); } - static Xid deserializeXid(final ObjectInput in) throws IOException { - int formatId = in.readInt(); - int length = in.readInt(); + static Xid deserializeXid(final ObjectInput input) throws IOException { + int formatId = input.readInt(); + int length = input.readInt(); byte[] globalId = new byte[length]; - in.readFully(globalId); - length = in.readInt(); + input.readFully(globalId); + length = input.readInt(); byte[] branchId = new byte[length]; - in.readFully(branchId); + input.readFully(branchId); return new SimpleXid(formatId, globalId, branchId); } - static void serializeXidArray(final ObjectOutput out, final Xid[] xids) throws IOException { - out.writeInt(xids.length); + static void serializeXidArray(final ObjectOutput output, final Xid[] xids) throws IOException { + output.writeInt(xids.length); for (Xid xid : xids) { - serializeXid(out, xid); + serializeXid(output, xid); } } - static Xid[] deserializeXidArray(final ObjectInput in) throws IOException { - int length = in.readInt(); + static Xid[] deserializeXidArray(final ObjectInput input) throws IOException { + int length = input.readInt(); Xid[] ret = new Xid[length]; for (int i = 0; i < length; ++i) { - ret[i] = deserializeXid(in); + ret[i] = deserializeXid(input); } return ret; } From b79131a342e3b568b8d7405201295c2c9bc6ec58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 16 Jan 2025 13:55:08 +0100 Subject: [PATCH 28/29] [WEJBHTTP-139] Enhancement according to peer review - don't use Object and long as method parameters in HeadersHelper --- .../httpclient/common/HeadersHelper.java | 24 +++++++++++++------ .../common/PoolAuthenticationContext.java | 2 +- .../transaction/RequestBuilder.java | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java index 0bc9496d..36b676b5 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java +++ b/common/src/main/java/org/wildfly/httpclient/common/HeadersHelper.java @@ -55,19 +55,24 @@ public static HeaderValues getResponseHeaders(final ClientResponse response, fin return response.getResponseHeaders().get(headerName); } - public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final long headerValue) { - if (request == null || headerName == null) throw new IllegalArgumentException(); - request.getRequestHeaders().put(headerName, headerValue); + public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final ContentType headerValue) { + if (request == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); + request.getRequestHeaders().put(headerName, headerValue.toString()); } - public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final Object headerValue) { + public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final HttpString headerValue) { if (request == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); request.getRequestHeaders().put(headerName, headerValue.toString()); } - public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final Object headerValue) { + public static void putRequestHeader(final ClientRequest request, final HttpString headerName, final String headerValue) { + if (request == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); + request.getRequestHeaders().put(headerName, headerValue); + } + + public static void addResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { if (exchange == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); - exchange.getResponseHeaders().add(headerName, headerValue.toString()); + exchange.getResponseHeaders().add(headerName, headerValue); } public static String getRequestHeader(final HttpServerExchange exchange, final HttpString headerName) { @@ -75,7 +80,12 @@ public static String getRequestHeader(final HttpServerExchange exchange, final H return exchange.getRequestHeaders().getFirst(headerName); } - public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final Object headerValue) { + public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final String headerValue) { + if (exchange == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); + exchange.getResponseHeaders().put(headerName, headerValue); + } + + public static void putResponseHeader(final HttpServerExchange exchange, final HttpString headerName, final ContentType headerValue) { if (exchange == null || headerName == null || headerValue == null) throw new IllegalArgumentException(); exchange.getResponseHeaders().put(headerName, headerValue.toString()); } diff --git a/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java b/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java index 1e8192ab..5949f1ea 100644 --- a/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java +++ b/common/src/main/java/org/wildfly/httpclient/common/PoolAuthenticationContext.java @@ -266,7 +266,7 @@ boolean prepareRequest(URI uri, ClientRequest request, AuthenticationConfigurati sb.append(", response=\""); sb.append(HexConverter.convertToHexString(digest.digest())); sb.append("\""); - putRequestHeader(request, AUTHORIZATION, sb); + putRequestHeader(request, AUTHORIZATION, sb.toString()); return true; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); diff --git a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java index 93e25d70..0103b09e 100644 --- a/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java +++ b/transaction/src/main/java/org/wildfly/httpclient/transaction/RequestBuilder.java @@ -123,11 +123,11 @@ private void setRequestPath(final ClientRequest request, final String prefix) { private void setRequestHeaders(final ClientRequest request) { if (requestType == UT_BEGIN) { putRequestHeader(request, ACCEPT, EXCEPTION + "," + NEW_TRANSACTION); - putRequestHeader(request, TIMEOUT, timeout); + putRequestHeader(request, TIMEOUT, String.valueOf(timeout)); } else if (requestType == XA_RECOVER) { putRequestHeader(request, ACCEPT, XID_LIST + "," + NEW_TRANSACTION); putRequestHeader(request, RECOVERY_PARENT_NAME, parentName); - putRequestHeader(request, RECOVERY_FLAGS, flags); + putRequestHeader(request, RECOVERY_FLAGS, String.valueOf(flags)); } else { putRequestHeader(request, ACCEPT, EXCEPTION); putRequestHeader(request, CONTENT_TYPE, XID); From 41b44a2a88953233b5ff19f41a2bb485f38b2af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Op=C3=A1lka?= Date: Thu, 16 Jan 2025 14:04:06 +0100 Subject: [PATCH 29/29] [WEJBHTTP-139] Enhancement according to peer review - use txnInfo instead of txn for TransactionInfo parameter names --- .../httpclient/ejb/ClientHandlers.java | 24 +++++++++---------- .../wildfly/httpclient/ejb/Serializer.java | 8 +++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java index 6e150b50..265997b7 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/ClientHandlers.java @@ -62,12 +62,12 @@ private ClientHandlers() { // forbidden instantiation } - static HttpTargetContext.HttpMarshaller invokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn, final Object[] objects, final Map map) { - return new InvokeHttpMarshaller(marshaller, txn, objects, map); + static HttpTargetContext.HttpMarshaller invokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo, final Object[] objects, final Map map) { + return new InvokeHttpMarshaller(marshaller, txnInfo, objects, map); } - static HttpTargetContext.HttpMarshaller createSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn) { - return new CreateSessionHttpMarshaller(marshaller, txn); + static HttpTargetContext.HttpMarshaller createSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo) { + return new CreateSessionHttpMarshaller(marshaller, txnInfo); } static HttpTargetContext.HttpResultHandler emptyHttpResultHandler(final CompletableFuture result, final Function function) { @@ -96,13 +96,13 @@ private static HttpTargetContext.HttpResultHandler invokeHttpResultHandlerIntern private static final class InvokeHttpMarshaller implements HttpTargetContext.HttpMarshaller { private final Marshaller marshaller; - private final TransactionInfo txn; + private final TransactionInfo txnInfo; private final Object[] objects; private final Map map; - private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn, final Object[] objects, final Map map) { + private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo, final Object[] objects, final Map map) { this.marshaller = marshaller; - this.txn = txn; + this.txnInfo = txnInfo; this.objects = objects; this.map = map; } @@ -111,7 +111,7 @@ private InvokeHttpMarshaller(final Marshaller marshaller, final TransactionInfo public void marshall(final OutputStream os) throws Exception { try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); - serializeTransaction(marshaller, txn); + serializeTransaction(marshaller, txnInfo); serializeObjectArray(marshaller, objects); serializeMap(marshaller, map); marshaller.finish(); @@ -121,18 +121,18 @@ public void marshall(final OutputStream os) throws Exception { private static final class CreateSessionHttpMarshaller implements HttpTargetContext.HttpMarshaller { private final Marshaller marshaller; - private final TransactionInfo txn; + private final TransactionInfo txnInfo; - private CreateSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txn) { + private CreateSessionHttpMarshaller(final Marshaller marshaller, final TransactionInfo txnInfo) { this.marshaller = marshaller; - this.txn = txn; + this.txnInfo = txnInfo; } @Override public void marshall(final OutputStream os) throws Exception { try (ByteOutput out = byteOutputOf(os)) { marshaller.start(out); - serializeTransaction(marshaller, txn); + serializeTransaction(marshaller, txnInfo); marshaller.finish(); } } diff --git a/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java b/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java index 6facae96..f222f2d9 100644 --- a/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java +++ b/ejb/src/main/java/org/wildfly/httpclient/ejb/Serializer.java @@ -130,18 +130,18 @@ static Set deserializeSet(final ObjectInput input) throws I return ret; } - static void serializeTransaction(final ObjectOutput output, final TransactionInfo txn) throws IOException { - final byte transactionType = txn.getType(); + static void serializeTransaction(final ObjectOutput output, final TransactionInfo txnInfo) throws IOException { + final byte transactionType = txnInfo.getType(); output.writeByte(transactionType); if (transactionType == NULL_TRANSACTION) { return; } - serializeXid(output, txn.getXid()); + serializeXid(output, txnInfo.getXid()); if (transactionType == REMOTE_TRANSACTION) { return; } if (transactionType == LOCAL_TRANSACTION) { - output.writeInt(txn.getRemainingTime()); + output.writeInt(txnInfo.getRemainingTime()); } }