Turned on checkstyle
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,7 +32,6 @@ import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
@@ -66,12 +65,12 @@ public class SpringBootApiGatewayRequestHandler extends
|
||||
}
|
||||
|
||||
private boolean functionAcceptsMessage() {
|
||||
return inspector.isMessage(function());
|
||||
return this.inspector.isMessage(function());
|
||||
}
|
||||
|
||||
private Object deserializeBody(String json) {
|
||||
try {
|
||||
return mapper.readValue(json, getInputType());
|
||||
return this.mapper.readValue(json, getInputType());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot convert event", e);
|
||||
@@ -91,13 +90,15 @@ public class SpringBootApiGatewayRequestHandler extends
|
||||
protected APIGatewayProxyResponseEvent convertOutput(Object output) {
|
||||
if (functionReturnsMessage(output)) {
|
||||
Message<?> message = (Message<?>) output;
|
||||
return new APIGatewayProxyResponseEvent().withStatusCode(
|
||||
(Integer) message.getHeaders().getOrDefault("statuscode", HttpStatus.OK.value()))
|
||||
return new APIGatewayProxyResponseEvent()
|
||||
.withStatusCode((Integer) message.getHeaders()
|
||||
.getOrDefault("statuscode", HttpStatus.OK.value()))
|
||||
.withHeaders(toResponseHeaders(message.getHeaders()))
|
||||
.withBody(serializeBody(message.getPayload()));
|
||||
}
|
||||
else {
|
||||
return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatus.OK.value())
|
||||
return new APIGatewayProxyResponseEvent()
|
||||
.withStatusCode(HttpStatus.OK.value())
|
||||
.withBody(serializeBody(output));
|
||||
|
||||
}
|
||||
@@ -116,7 +117,7 @@ public class SpringBootApiGatewayRequestHandler extends
|
||||
|
||||
private String serializeBody(Object body) {
|
||||
try {
|
||||
return mapper.writeValueAsString(body);
|
||||
return this.mapper.writeValueAsString(body);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Cannot convert output", e);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
* 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,
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.function.adapter.aws;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -32,7 +30,11 @@ import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
/**
|
||||
* @param <E> payload type
|
||||
* @param <O> response type
|
||||
* @author Mark Fisher
|
||||
* @author Halvdan Hoem Grelland
|
||||
*/
|
||||
@@ -65,28 +67,26 @@ public class SpringBootKinesisEventHandler<E, O>
|
||||
|
||||
if (functionAcceptsMessage()) {
|
||||
return wrapInMessages(payloads);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return payloads;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Message<E>> wrapInMessages(List<E> payloads) {
|
||||
return payloads.stream()
|
||||
.map(GenericMessage::new)
|
||||
.collect(Collectors.toList());
|
||||
return payloads.stream().map(GenericMessage::new).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<E> deserializePayloads(List<KinesisEvent.KinesisEventRecord> records) {
|
||||
return RecordDeaggregator.deaggregate(records).stream()
|
||||
.map(this::deserializeUserRecord)
|
||||
.collect(toList());
|
||||
.map(this::deserializeUserRecord).collect(toList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private E deserializeUserRecord(UserRecord userRecord) {
|
||||
try {
|
||||
byte[] jsonBytes = userRecord.getData().array();
|
||||
return (E) mapper.readValue(jsonBytes, getInputType());
|
||||
return (E) this.mapper.readValue(jsonBytes, getInputType());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot convert event", e);
|
||||
@@ -94,6 +94,7 @@ public class SpringBootKinesisEventHandler<E, O>
|
||||
}
|
||||
|
||||
private boolean functionAcceptsMessage() {
|
||||
return inspector.isMessage(function());
|
||||
return this.inspector.isMessage(function());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
* 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,
|
||||
@@ -22,12 +22,12 @@ import java.util.List;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @param <E> event type
|
||||
* @param <O> result types
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class SpringBootRequestHandler<E, O> extends SpringFunctionInitializer
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright 2017-1018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
* 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,
|
||||
@@ -26,12 +26,11 @@ import java.util.List;
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -39,7 +38,7 @@ import reactor.core.publisher.Flux;
|
||||
public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
implements RequestStreamHandler {
|
||||
|
||||
@Autowired(required=false)
|
||||
@Autowired(required = false)
|
||||
private ObjectMapper mapper;
|
||||
|
||||
public SpringBootStreamHandler() {
|
||||
@@ -64,7 +63,7 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
initialize();
|
||||
Object value = convertStream(input);
|
||||
Publisher<?> flux = apply(extract(value));
|
||||
mapper.writeValue(output, result(value, flux));
|
||||
this.mapper.writeValue(output, result(value, flux));
|
||||
}
|
||||
|
||||
private Object result(Object input, Publisher<?> flux) {
|
||||
@@ -72,7 +71,7 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
for (Object value : Flux.from(flux).toIterable()) {
|
||||
result.add(value);
|
||||
}
|
||||
if (isSingleValue(input) && result.size()==1) {
|
||||
if (isSingleValue(input) && result.size() == 1) {
|
||||
return result.get(0);
|
||||
}
|
||||
return result;
|
||||
@@ -91,10 +90,11 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
|
||||
private Object convertStream(InputStream input) {
|
||||
try {
|
||||
return mapper.readValue(input, getInputType());
|
||||
return this.mapper.readValue(input, getInputType());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot convert event", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
* 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,
|
||||
@@ -29,6 +29,7 @@ import java.util.jar.Manifest;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -38,8 +39,6 @@ import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@@ -69,71 +68,6 @@ public class SpringFunctionInitializer implements Closeable {
|
||||
this(getStartClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void initialize() {
|
||||
if (!this.initialized.compareAndSet(false, true)) {
|
||||
return;
|
||||
}
|
||||
logger.info("Initializing: " + configurationClass);
|
||||
SpringApplication builder = springApplication();
|
||||
ConfigurableApplicationContext context = builder.run();
|
||||
context.getAutowireCapableBeanFactory().autowireBean(this);
|
||||
String name = context.getEnvironment().getProperty("function.name");
|
||||
if (name == null) {
|
||||
name = "function";
|
||||
}
|
||||
if (this.catalog == null) {
|
||||
if (context.containsBean(name)) {
|
||||
this.function = context.getBean(name, Function.class);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Set<String> functionNames = this.catalog.getNames(Function.class);
|
||||
if (functionNames.size() == 1) {
|
||||
this.function = this.catalog.lookup(Function.class,
|
||||
functionNames.iterator().next());
|
||||
}
|
||||
else {
|
||||
this.function = this.catalog.lookup(Function.class, name);
|
||||
}
|
||||
}
|
||||
this.context = context;
|
||||
|
||||
}
|
||||
|
||||
private SpringApplication springApplication() {
|
||||
Class<?> sourceClass = configurationClass;
|
||||
SpringApplication application = new org.springframework.cloud.function.context.FunctionalSpringApplication(
|
||||
sourceClass);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
return application;
|
||||
}
|
||||
|
||||
protected Class<?> getInputType() {
|
||||
if (inspector != null) {
|
||||
return inspector.getInputType(function());
|
||||
}
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
protected Object function() {
|
||||
return this.function;
|
||||
}
|
||||
|
||||
protected Publisher<?> apply(Publisher<?> input) {
|
||||
if (this.function != null) {
|
||||
return Flux.from(function.apply(input));
|
||||
}
|
||||
throw new IllegalStateException("No function defined");
|
||||
}
|
||||
|
||||
private static Class<?> getStartClass() {
|
||||
ClassLoader classLoader = SpringFunctionInitializer.class.getClassLoader();
|
||||
if (System.getenv("MAIN_CLASS") != null) {
|
||||
@@ -180,4 +114,69 @@ public class SpringFunctionInitializer implements Closeable {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void initialize() {
|
||||
if (!this.initialized.compareAndSet(false, true)) {
|
||||
return;
|
||||
}
|
||||
logger.info("Initializing: " + this.configurationClass);
|
||||
SpringApplication builder = springApplication();
|
||||
ConfigurableApplicationContext context = builder.run();
|
||||
context.getAutowireCapableBeanFactory().autowireBean(this);
|
||||
String name = context.getEnvironment().getProperty("function.name");
|
||||
if (name == null) {
|
||||
name = "function";
|
||||
}
|
||||
if (this.catalog == null) {
|
||||
if (context.containsBean(name)) {
|
||||
this.function = context.getBean(name, Function.class);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Set<String> functionNames = this.catalog.getNames(Function.class);
|
||||
if (functionNames.size() == 1) {
|
||||
this.function = this.catalog.lookup(Function.class,
|
||||
functionNames.iterator().next());
|
||||
}
|
||||
else {
|
||||
this.function = this.catalog.lookup(Function.class, name);
|
||||
}
|
||||
}
|
||||
this.context = context;
|
||||
|
||||
}
|
||||
|
||||
private SpringApplication springApplication() {
|
||||
Class<?> sourceClass = this.configurationClass;
|
||||
SpringApplication application = new org.springframework.cloud.function.context.FunctionalSpringApplication(
|
||||
sourceClass);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
return application;
|
||||
}
|
||||
|
||||
protected Class<?> getInputType() {
|
||||
if (this.inspector != null) {
|
||||
return this.inspector.getInputType(function());
|
||||
}
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
protected Object function() {
|
||||
return this.function;
|
||||
}
|
||||
|
||||
protected Publisher<?> apply(Publisher<?> input) {
|
||||
if (this.function != null) {
|
||||
return Flux.from(this.function.apply(input));
|
||||
}
|
||||
throw new IllegalStateException("No function defined");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user