Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 45 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<wrapper.version>1.0.31.RELEASE</wrapper.version>
<pmd.version>7.26.0</pmd.version>
<docs.main>spring-cloud-function</docs.main>
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError>
<maven-checkstyle-plugin.failsOnViolation>true
Expand Down Expand Up @@ -225,6 +226,50 @@
<module>spring-cloud-function-web</module>
</modules>
</profile>
<profile>
<id>pmd</id>
<!-- Report-only PMD 7 analysis with modern Java syntax rules.
Runs at verify only when the profile is activated. Sample apps
that inherit spring-boot-starter-parent are not covered. -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.28.0</version>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmd.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmd.version}</version>
</dependency>
</dependencies>
<configuration>
<rulesets>
<ruleset>${maven.multiModuleProjectDirectory}/src/checkstyle/pmd-modern-java.xml</ruleset>
</rulesets>
<failOnViolation>false</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<includeTests>true</includeTests>
<targetJdk>17</targetJdk>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>spring</id>
<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public static byte[] generateOutput(Message requestMessage, Message<?> responseM

byte[] responseBytes = responseMessage == null ? "\"OK\"".getBytes() : extractPayload((Message<Object>) responseMessage, objectMapper);
if (requestMessage.getHeaders().containsKey(AWS_API_GATEWAY) && ((boolean) requestMessage.getHeaders().get(AWS_API_GATEWAY))) {
Map<String, Object> response = new HashMap<String, Object>();
Map<String, Object> response = new HashMap<>();
response.put(IS_BASE64_ENCODED, responseMessage != null && responseMessage.getHeaders().containsKey(IS_BASE64_ENCODED)
? responseMessage.getHeaders().get(IS_BASE64_ENCODED) : false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public CustomRuntimeEventLoop(ConfigurableApplicationContext applicationContext)

public void run() {
this.running = true;
this.executor.execute(() -> {
eventLoop(this.applicationContext);
});
this.executor.execute(() -> eventLoop(this.applicationContext));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void start() {
if (this.jsonMapper instanceof JacksonMapper) {
((JacksonMapper) this.jsonMapper).configureObjectMapper(objectMapper -> {
if (!objectMapper.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)) {
MapperBuilder builder = objectMapper.rebuild();
MapperBuilder<?, ?> builder = objectMapper.rebuild();
builder.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
objectMapper = builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public String destination(Supplier<?> supplier, String name, Object value) {
logger.debug("Lambda incoming value: " + value);
}
String destination = "unknown";
if (value instanceof Message) {
Message<?> message = (Message<?>) value;
if (value instanceof Message<?> message) {
MessageHeaders headers = message.getHeaders();
if (headers.containsKey("lambda-runtime-aws-request-id")) {
destination = (String) headers.get("lambda-runtime-aws-request-id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ public Function<Person, Person> uppercasePerson() {

@Bean
public Function<Flux<GeoLocation>, Flux<GeoLocation>> echoFlux() {
return flux -> flux.map(g -> {
return new GeoLocation(g.longitude(), g.latitude());
});
return flux -> flux.map(g -> new GeoLocation(g.longitude(), g.latitude()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ public void testLBEventStringInOut() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);

Map result = mapper.readValue(output.toByteArray(), Map.class);
Map<String, Object> result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result.get("body")).isEqualTo("\"Hello from ELB\"");
}

Expand Down Expand Up @@ -1137,7 +1137,7 @@ public void testLBEvent() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);

Map result = mapper.readValue(output.toByteArray(), Map.class);
Map<String, Object> result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result.get("body")).isEqualTo("\"Hello from ELB\"");
}

Expand All @@ -1151,7 +1151,7 @@ public void testLBEventAsMessage() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, Mockito.mock(Context.class));

Map result = mapper.readValue(output.toByteArray(), Map.class);
Map<String, Object> result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result.get("body")).isEqualTo("\"Hello from ELB\"");
}

Expand All @@ -1171,7 +1171,7 @@ public void handleRequest(InputStream input, OutputStream output, Context contex
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, new TestContext());

Map result = mapper.readValue(output.toByteArray(), Map.class);
Map<String, Object> result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result.get("body")).isEqualTo("Hello from ELB");
}

Expand Down Expand Up @@ -1343,7 +1343,7 @@ public void testResponseBase64Encoded() throws Exception {
JsonMapper mapper = new JacksonMapper(new ObjectMapper());

String result = new String(output.toByteArray(), StandardCharsets.UTF_8);
Map resultMap = mapper.fromJson(result, Map.class);
Map<String, Object> resultMap = mapper.fromJson(result, Map.class);
assertThat((boolean) resultMap.get(AWSLambdaUtils.IS_BASE64_ENCODED)).isTrue();
assertThat((int) resultMap.get(AWSLambdaUtils.STATUS_CODE)).isEqualTo(201);
String body = new String(Base64.getDecoder().decode((String) resultMap.get(AWSLambdaUtils.BODY)), StandardCharsets.UTF_8);
Expand Down Expand Up @@ -1476,7 +1476,7 @@ public void testShouldNotWrapIamPolicyResponse() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);

Map result = mapper.readValue(output.toByteArray(), Map.class);
Map<String, Object> result = mapper.readValue(output.toByteArray(), Map.class);
assertThat(result.get("body")).isNull();
assertThat(result.get("principalId")).isNotNull();
}
Expand Down Expand Up @@ -1563,9 +1563,7 @@ public void testPrimitiveMessage() throws Exception {
public static class BasicConfiguration {
@Bean
public Function<Message<String>, Message<String>> uppercase() {
return v -> {
return MessageBuilder.withPayload(v.getPayload().toUpperCase(Locale.ROOT)).build();
};
return v -> MessageBuilder.withPayload(v.getPayload().toUpperCase(Locale.ROOT)).build();
}
}

Expand All @@ -1574,7 +1572,7 @@ public Function<Message<String>, Message<String>> uppercase() {
public static class AuthorizerConfiguration {
@Bean
public Function<APIGatewayCustomAuthorizerEvent, String> acceptAuthorizerEvent() {
return v -> v.toString();
return Object::toString;
}
}

Expand Down Expand Up @@ -1753,9 +1751,7 @@ public static class S3Configuration {

@Bean
public Function<S3Event, S3Event> outputS3Event() {
return v -> {
return v;
};
return v -> v;
}
@Bean
public Function<String, String> echoString() {
Expand Down Expand Up @@ -1858,7 +1854,7 @@ public Function<Message<String>, Message<String>> echoStringMessage() {

@Bean
public Consumer<String> consume() {
return v -> System.out.println(v);
return System.out::println;
}

@Bean
Expand All @@ -1873,9 +1869,7 @@ public Function<Mono<String>, Mono<Void>> reactiveWithVoidReturn() {

@Bean
public Function<Person, String> uppercasePojo() {
return v -> {
return v.getName().toUpperCase(Locale.ROOT);
};
return v -> v.getName().toUpperCase(Locale.ROOT);
}

@Bean
Expand All @@ -1898,9 +1892,7 @@ public Function<Flux<Person>, Flux<Person>> uppercasePojoReturnPojoReactive() {

@Bean
public Function<APIGatewayProxyRequestEvent, String> inputApiEvent() {
return v -> {
return v.getBody();
};
return APIGatewayProxyRequestEvent::getBody;
}

@Bean
Expand Down Expand Up @@ -1962,16 +1954,12 @@ public Function<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> inputOutputApiE

@Bean
public Function<APIGatewayV2HTTPEvent, String> inputApiV2Event() {
return v -> {
return v.getBody();
};
return APIGatewayV2HTTPEvent::getBody;
}

@Bean
public Function<Message<APIGatewayProxyRequestEvent>, String> inputApiEventAsMessage() {
return v -> {
return v.getPayload().getBody();
};
return v -> v.getPayload().getBody();
}

@Bean
Expand Down Expand Up @@ -2006,9 +1994,7 @@ public Function<Mono<String>, Mono<IamPolicyResponse>> outputPolicyResponse() {
public static class PrimitiveConfiguration {
@Bean
public Function<Message<byte[]>, byte[]> returnByteArrayAsMessage() {
return v -> {
return v.getPayload();
};
return Message::getPayload;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void test() throws Exception {
AzureWebProxyInvoker proxyInvoker = new AzureWebProxyInvoker();
AzureWebProxyInvoker instance = proxyInvoker.getInstance(AzureWebProxyInvoker.class);

HttpRequestMessageStub<Optional<String>> request = new HttpRequestMessageStub<Optional<String>>();
HttpRequestMessageStub<Optional<String>> request = new HttpRequestMessageStub<>();

request.setHttpMethod(HttpMethod.GET);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@

package org.springframework.cloud.function.adapter.azure.web;

import java.io.IOException;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -57,13 +51,9 @@ public HandlerAdapter handlerAdapter() {

@Bean
public Filter filter() {
return new Filter() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("FILTER ===> Hello from: " + request.getLocalAddr());
chain.doFilter(request, response);
}
return (request, response, chain) -> {
System.out.println("FILTER ===> Hello from: " + request.getLocalAddr());
chain.doFilter(request, response);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ else if (input instanceof Message) {
.setHeaderIfAbsent(EXECUTION_CONTEXT, executionContext).build();
}
else if (input instanceof Iterable) {
return Flux.fromIterable((Iterable) input).map(item -> {
return constructInputMessageFromItem(item, executionContext);
});
return Flux.fromIterable((Iterable) input).map(item -> constructInputMessageFromItem(item, executionContext));
}
return constructInputMessageFromItem(input, executionContext);
}
Expand All @@ -95,7 +93,7 @@ private static <I> Message<?> constructInputMessageFromItem(Object input, Execut
}

private static <I> MessageHeaders getHeaders(HttpRequestMessage<I> event) {
Map<String, Object> headers = new HashMap<String, Object>();
Map<String, Object> headers = new HashMap<>();

if (event.getHeaders() != null) {
headers.putAll(event.getHeaders());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testFunctionInjector() throws Exception {

Assertions.assertThat(azureFunction).isNotNull();

HttpRequestMessageStub<Optional<String>> requestStub = new HttpRequestMessageStub<Optional<String>>();
HttpRequestMessageStub<Optional<String>> requestStub = new HttpRequestMessageStub<>();

requestStub.setBody(Optional.of("payload"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testFunctionInjector() throws Exception {

MyAzureTestFunction functionInstance = injector.getInstance(MyAzureTestFunction.class);

HttpRequestMessageStub<Optional<String>> request = new HttpRequestMessageStub<Optional<String>>();
HttpRequestMessageStub<Optional<String>> request = new HttpRequestMessageStub<>();

request.setBody(Optional.of("test"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ else if (result.getHeaders().containsKey("Content-Type")) {
httpResponse.setContentType(result.getHeaders().get("Content-Type").toString());
}
else {
httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType));
httpRequest.getContentType().ifPresent(httpResponse::setContentType);
}
String content = result.getPayload() instanceof String strPayload ? strPayload
: new String((byte[]) result.getPayload(), StandardCharsets.UTF_8);
httpResponse.getWriter().write(content);
for (Entry<String, Object> header : headers.entrySet()) {
Object values = header.getValue();
if (values instanceof Collection<?>) {
String headerValue = ((Collection<?>) values).stream().map(item -> item.toString())
String headerValue = ((Collection<?>) values).stream().map(Object::toString)
.collect(Collectors.joining(","));
httpResponse.appendHeader(header.getKey(), headerValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected static class MultiValueHeaderSupplier {
public Function<String, Message<String>> function() {

String payload = "hello";
List<Object> li = new ArrayList<Object>(asList(123, "headerThing"));
List<Object> li = new ArrayList<>(asList(123, "headerThing"));

Message<String> msg = MessageBuilder.withPayload(payload).setHeader("multiValueHeader", li)
.build();
Expand Down Expand Up @@ -298,11 +298,9 @@ protected static class JsonInputOutputFunction {

@Bean
public Function<IncomingRequest, Message<OutgoingResponse>> function() {
return (in) -> {
return MessageBuilder
return (in) -> MessageBuilder
.withPayload(new OutgoingResponse("Thank you for sending the message: " + in.message))
.setHeader("foo", "bar").build();
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testErrorResponse() {

HttpHeaders headers = new HttpHeaders();
ResponseEntity<String> response = testRestTemplate.postForEntity(
"http://localhost:" + serverProcess.getPort(), new HttpEntity<>("test", headers),
"http://localhost:" + serverProcess.port(), new HttpEntity<>("test", headers),
String.class);

assertThat(response.getStatusCode().is5xxServerError()).isTrue();
Expand Down
Loading
Loading