Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[WEJBHTTP-139] Final round of cleanup refactorings accross all modules #259

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9d631d7
[WEJBHTTP-139] Refactoring - moving static imports to the top of impo…
ropalka Nov 29, 2024
682a83f
[WEJBHTTP-139] Refactoring - moving ByteOutputs classes to common module
ropalka Nov 29, 2024
a8be101
[WEJBHTTP-139] Refactoring - making all ByteOutputs wrapping OutputSt…
ropalka Nov 29, 2024
3bfbb00
[WEJBHTTP-139] Refactoring - unify OutputStream variable names
ropalka Nov 29, 2024
357e420
[WEJBHTTP-139] Removing remaining deprecated methods & constructors
ropalka Nov 29, 2024
1dde42a
[WEJBHTTP-139] Refactoring - use HttpServerHelper utility method via …
ropalka Nov 29, 2024
85b5810
[WEJBHTTP-139] Refactoring - merge transaction/Utils , naming/Utils &…
ropalka Dec 5, 2024
c4208da
[WEJBHTTP-139] Refactoring - merge common/Utils & common/HttpMarshall…
ropalka Dec 5, 2024
d2c8137
[WEJBHTTP-139] Refactoring - unify parameter names in Serializers
ropalka Dec 6, 2024
11abf95
[WEJBHTTP-139] Refactoring - unify parameter names in ClientHandlers
ropalka Dec 6, 2024
424ce0e
[WEJBHTTP-139] Refactoring - Introducing ByteInputs utility class
ropalka Dec 6, 2024
4509263
[WEJBHTTP-139] Refactoring - renaming httpServiceConfig variable to c…
ropalka Dec 6, 2024
1585469
[WEJBHTTP-139] Removing unused HttpRemoteEjbService.cancellationFlags…
ropalka Dec 6, 2024
c526cb3
[WEJBHTTP-139] Refactoring - reordering method parameters - let HttpS…
ropalka Dec 6, 2024
f47e24c
[WEJBHTTP-139] Refactoring - be consistent and always use HeaderMap.p…
ropalka Dec 10, 2024
9b3515d
[WEJBHTTP-139] Refactoring - Use Headers fields via static imports
ropalka Dec 10, 2024
b1f577d
[WEJBHTTP-139] Refactoring - Use StatusCodes fields via static imports
ropalka Dec 10, 2024
35e8968
[WEJBHTTP-139] Refactoring - always use HeadersHelper methods for bet…
ropalka Dec 10, 2024
3a7d2b8
[WEJBHTTP-139] Refactoring - Enhance HeadersHelper methods to enhance…
ropalka Dec 10, 2024
50ba294
[WEJBHTTP-139] Refactoring - use parseBoolean via static import
ropalka Dec 10, 2024
928e111
[WEJBHTTP-139] Refactoring - introducing OPC query parameter constant
ropalka Dec 10, 2024
0d91d27
[WEJBHTTP-139] Refactoring - use NEW_QUERY_PARAMETER constant
ropalka Dec 10, 2024
1b4f665
[WEJBHTTP-139] Refactoring - unify ClientRequest variable names
ropalka Dec 10, 2024
b1e7ff3
[WEJBHTTP-139] Refactoring - unify ClientResponse variable names
ropalka Dec 10, 2024
bd13773
[WEJBHTTP-139] Refactoring - use EJB_SESSION_ID via static import
ropalka Dec 10, 2024
43d0acd
[WEJBHTTP-139] Enhancement according to peer review - always throw Il…
ropalka Jan 16, 2025
32c4a0e
[WEJBHTTP-139] Enhancement according to peer review - use 'input' and…
ropalka Jan 16, 2025
b79131a
[WEJBHTTP-139] Enhancement according to peer review - don't use Objec…
ropalka Jan 16, 2025
41b44a2
[WEJBHTTP-139] Enhancement according to peer review - use txnInfo ins…
ropalka Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
ropalka marked this conversation as resolved.
Show resolved Hide resolved
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;
ropalka marked this conversation as resolved.
Show resolved Hide resolved
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -269,7 +270,7 @@ public Boolean apply(final ClientResponse clientResponse) {
private static final class CreateSessionResponseFunction implements Function<ClientResponse, SessionID> {
@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));
}
Expand Down
21 changes: 10 additions & 11 deletions ejb/src/main/java/org/wildfly/httpclient/ejb/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
// }
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -709,7 +711,7 @@ public void moduleUnavailable(List<EJBModuleIdentifier> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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());
}
}

Expand Down
Loading