Skip to content

Commit

Permalink
fix: Pass null for Context method arguments in JaxRsRequestHandlerPro…
Browse files Browse the repository at this point in the history
…xy (#4935)

Signed-off-by: Nicola Timeus <[email protected]>
  • Loading branch information
nicolatimeus authored Nov 1, 2023
1 parent 17538d5 commit 7fe29dc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ Import-Package: com.google.gson,
org.eclipse.kura.cloudconnection.request;version="[1.0,2.0)",
org.eclipse.kura.message;version="[1.4,2.0)",
org.slf4j;version="1.7.25"
Export-Package: org.eclipse.kura.request.handler.jaxrs;version="1.0.0",
Export-Package: org.eclipse.kura.request.handler.jaxrs;version="1.1.0",
org.eclipse.kura.request.handler.jaxrs.annotation;version="1.0.0",
org.eclipse.kura.request.handler.jaxrs.consumer;version="1.0.0"
org.eclipse.kura.request.handler.jaxrs.consumer;version="1.1.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
* Copyright (c) 2021, 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -18,6 +18,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +32,7 @@
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.eclipse.kura.KuraErrorCode;
Expand All @@ -40,6 +43,7 @@
import org.eclipse.kura.message.KuraPayload;
import org.eclipse.kura.message.KuraResponsePayload;
import org.eclipse.kura.request.handler.jaxrs.annotation.EXEC;
import org.eclipse.kura.request.handler.jaxrs.consumer.RequestArgumentHandler;
import org.eclipse.kura.request.handler.jaxrs.consumer.RequestParameterHandler;
import org.eclipse.kura.request.handler.jaxrs.consumer.ResponseBodyHandler;
import org.slf4j.Logger;
Expand Down Expand Up @@ -151,25 +155,25 @@ protected Optional<MethodProxy> buildMethodProxy(final Method method) {

final Parameter[] parameters = method.getParameters();

final RequestParameterHandler parameterHandler;
final List<RequestArgumentHandler<?>> handlers = new ArrayList<>();

for (final Parameter parameter : parameters) {

if (parameters.length == 0) {
parameterHandler = RequestParameterHandlers.noArgsHandler();
} else if (parameters.length == 1) {
final Parameter parameter = parameters[0];
final Class<?> type = parameter.getType();

if (type == InputStream.class) {
parameterHandler = RequestParameterHandlers.inputStreamHandler();
if (Arrays.asList(parameter.getAnnotations()).stream().map(a -> a.annotationType())
.anyMatch(Context.class::equals)) {
handlers.add(RequestParameterHandlers.nullArgumentHandler());
} else if (type == InputStream.class) {
handlers.add(RequestParameterHandlers.inputStreamArgumentHandler());
} else {
parameterHandler = RequestParameterHandlers.gsonHandler(type, gson);
handlers.add(RequestParameterHandlers.gsonArgumentHandler(type, gson));
}

} else {
logger.debug("method {} has more than one parameter, ignoring", method);
return Optional.empty();
}

final RequestParameterHandler parameterHandler = RequestParameterHandlers.fromArgumentHandlers(handlers);

final ResponseBodyHandler bodyHandler;

final Class<?> returnType = method.getReturnType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
* Copyright (c) 2021, 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -13,13 +13,16 @@
package org.eclipse.kura.request.handler.jaxrs;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.message.KuraPayload;
import org.eclipse.kura.request.handler.jaxrs.consumer.RequestArgumentHandler;
import org.eclipse.kura.request.handler.jaxrs.consumer.RequestParameterHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,7 +42,22 @@ public static RequestParameterHandler noArgsHandler() {
return m -> EMPTY_PARAMETERS;
}

public static RequestParameterHandler inputStreamHandler() {
public static RequestParameterHandler fromArgumentHandlers(final List<RequestArgumentHandler<?>> handlers) {
return m -> {
final Object[] result = new Object[handlers.size()];

for (int i = 0; i < handlers.size(); i++) {
result[i] = handlers.get(i).buildParameter(m);
}
return result;
};
}

public static <T> RequestArgumentHandler<T> nullArgumentHandler() {
return m -> null;
}

public static RequestArgumentHandler<InputStream> inputStreamArgumentHandler() {
return m -> {
final KuraPayload payload = m.getPayload();
final byte[] body = payload.getBody();
Expand All @@ -48,12 +66,19 @@ public static RequestParameterHandler inputStreamHandler() {
return null;
}

return new Object[] { new ByteArrayInputStream(body) };
return new ByteArrayInputStream(body);
};
}

public static RequestParameterHandler gsonHandler(final Class<?> type, final Gson gson) {
public static RequestParameterHandler inputStreamHandler() {
return m -> {
final RequestArgumentHandler<InputStream> handler = inputStreamArgumentHandler();

return new Object[] { handler.buildParameter(m) };
};
}

public static <T> RequestArgumentHandler<T> gsonArgumentHandler(final Class<T> type, final Gson gson) {
return m -> {
final KuraPayload payload = m.getPayload();
final byte[] body = payload.getBody();
Expand All @@ -73,7 +98,7 @@ public static RequestParameterHandler gsonHandler(final Class<?> type, final Gso
throw new KuraException(KuraErrorCode.BAD_REQUEST);
}

final Object result;
final T result;

try {
result = gson.fromJson(asString, type);
Expand All @@ -82,7 +107,17 @@ public static RequestParameterHandler gsonHandler(final Class<?> type, final Gso
throw new KuraException(KuraErrorCode.BAD_REQUEST);
}

return new Object[] { result };
return result;
};
}

public static RequestParameterHandler gsonHandler(final Class<?> type, final Gson gson) {

return m -> {

final RequestArgumentHandler<?> handler = gsonArgumentHandler(type, gson);

return new Object[] { handler.buildParameter(m) };
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech
*******************************************************************************/
package org.eclipse.kura.request.handler.jaxrs.consumer;

import org.eclipse.kura.KuraException;
import org.eclipse.kura.cloudconnection.message.KuraMessage;

public interface RequestArgumentHandler<T> {

T buildParameter(final KuraMessage request) throws KuraException;
}
Loading

0 comments on commit 7fe29dc

Please sign in to comment.