GH-266 Consolidated Adapter infrastructure
- Moved common logic into a new AbstractSpringFunctionAdapterInitializer - Modified Azure and AWS request handlers to extend from it - Deprecated both AzureSpringFunctionInitializer and SpringFunctionInitializer(AWS) Resolves #266
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2017-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.
|
||||
@@ -19,9 +19,7 @@ package org.springframework.cloud.function.adapter.aws;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||
@@ -30,12 +28,14 @@ import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
public class SpringBootStreamHandler extends AbstractSpringFunctionAdapterInitializer<Context>
|
||||
implements RequestStreamHandler {
|
||||
|
||||
@Autowired(required = false)
|
||||
@@ -50,8 +50,8 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initialize() {
|
||||
super.initialize();
|
||||
protected void initialize(Context context) {
|
||||
super.initialize(context);
|
||||
if (this.mapper == null) {
|
||||
this.mapper = new ObjectMapper();
|
||||
}
|
||||
@@ -60,27 +60,12 @@ public class SpringBootStreamHandler extends SpringFunctionInitializer
|
||||
@Override
|
||||
public void handleRequest(InputStream input, OutputStream output, Context context)
|
||||
throws IOException {
|
||||
initialize();
|
||||
initialize(context);
|
||||
Object value = convertStream(input);
|
||||
Publisher<?> flux = apply(extract(value));
|
||||
this.mapper.writeValue(output, result(value, flux));
|
||||
}
|
||||
|
||||
private Object result(Object input, Publisher<?> flux) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object value : Flux.from(flux).toIterable()) {
|
||||
result.add(value);
|
||||
}
|
||||
if (isSingleValue(input) && result.size() == 1) {
|
||||
return result.get(0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean isSingleValue(Object input) {
|
||||
return !(input instanceof Collection);
|
||||
}
|
||||
|
||||
private Flux<?> extract(Object input) {
|
||||
if (input instanceof Collection) {
|
||||
return Flux.fromIterable((Iterable<?>) input);
|
||||
|
||||
@@ -36,6 +36,7 @@ import reactor.core.publisher.Flux;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer;
|
||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -44,7 +45,10 @@ import org.springframework.util.ClassUtils;
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Semyon Fishman
|
||||
* @deprecated as of 2.1 in favor of {@link AbstractSpringFunctionAdapterInitializer}.
|
||||
* It is no longer used by the framework and only exists for avoiding potential regressions.
|
||||
*/
|
||||
@Deprecated
|
||||
public class SpringFunctionInitializer implements Closeable {
|
||||
|
||||
private static Log logger = LogFactory.getLog(SpringFunctionInitializer.class);
|
||||
|
||||
Reference in New Issue
Block a user