GH-1172 Ensure content-type set in a message is honored on the output
Also, ensures that the proper content type is set in GCF FunctionInvoker Resolves #1172
This commit is contained in:
@@ -24,7 +24,10 @@ import java.util.GregorianCalendar;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class PetData {
|
public final class PetData {
|
||||||
|
private PetData() {
|
||||||
|
|
||||||
|
}
|
||||||
private static List<String> breeds = new ArrayList<>();
|
private static List<String> breeds = new ArrayList<>();
|
||||||
static {
|
static {
|
||||||
breeds.add("Afghan Hound");
|
breeds.add("Afghan Hound");
|
||||||
|
|||||||
@@ -121,7 +121,15 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
|
|||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
MessageHeaders headers = result.getHeaders();
|
MessageHeaders headers = result.getHeaders();
|
||||||
httpResponse.setContentType(result.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString());
|
if (result.getHeaders().containsKey(MessageHeaders.CONTENT_TYPE)) {
|
||||||
|
httpResponse.setContentType(result.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString());
|
||||||
|
}
|
||||||
|
else if (result.getHeaders().containsKey("Content-Type")) {
|
||||||
|
httpResponse.setContentType(result.getHeaders().get("Content-Type").toString());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType));
|
||||||
|
}
|
||||||
httpResponse.getWriter().write(new String(result.getPayload(), StandardCharsets.UTF_8));
|
httpResponse.getWriter().write(new String(result.getPayload(), StandardCharsets.UTF_8));
|
||||||
for (Entry<String, Object> header : headers.entrySet()) {
|
for (Entry<String, Object> header : headers.entrySet()) {
|
||||||
Object values = header.getValue();
|
Object values = header.getValue();
|
||||||
@@ -133,7 +141,6 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
|
|||||||
httpResponse.appendHeader(header.getKey(), header.getValue().toString());
|
httpResponse.appendHeader(header.getKey(), header.getValue().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType));
|
|
||||||
|
|
||||||
if (headers.containsKey(HTTP_STATUS_CODE)) {
|
if (headers.containsKey(HTTP_STATUS_CODE)) {
|
||||||
if (headers.get(HTTP_STATUS_CODE) instanceof Integer) {
|
if (headers.get(HTTP_STATUS_CODE) instanceof Integer) {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
|
import org.springframework.messaging.MessageHeaders;
|
||||||
import org.springframework.messaging.support.MessageBuilder;
|
import org.springframework.messaging.support.MessageBuilder;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
@@ -165,7 +166,7 @@ public class FunctionInvokerHttpTests {
|
|||||||
bufferedWriter.close();
|
bufferedWriter.close();
|
||||||
|
|
||||||
verify(response).setStatusCode(404);
|
verify(response).setStatusCode(404);
|
||||||
|
verify(response).setContentType("text/plain");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -200,7 +201,7 @@ public class FunctionInvokerHttpTests {
|
|||||||
|
|
||||||
String payload = "hello";
|
String payload = "hello";
|
||||||
|
|
||||||
Message<String> msg = MessageBuilder.withPayload(payload).setHeader("statusCode", 404)
|
Message<String> msg = MessageBuilder.withPayload(payload).setHeader("statusCode", 404).setHeader(MessageHeaders.CONTENT_TYPE, "text/plain")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return x -> msg;
|
return x -> msg;
|
||||||
|
|||||||
@@ -45,6 +45,16 @@
|
|||||||
</extensions>
|
</extensions>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>checkstyle-validation</id>
|
||||||
|
<phase>none</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
<groupId>org.xolstice.maven.plugins</groupId>
|
<groupId>org.xolstice.maven.plugins</groupId>
|
||||||
<artifactId>protobuf-maven-plugin</artifactId>
|
<artifactId>protobuf-maven-plugin</artifactId>
|
||||||
<version>0.6.1</version>
|
<version>0.6.1</version>
|
||||||
|
|||||||
@@ -24,8 +24,13 @@ import java.util.GregorianCalendar;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class PetData {
|
public final class PetData {
|
||||||
private static List<String> breeds = new ArrayList<>();
|
private static List<String> breeds = new ArrayList<>();
|
||||||
|
|
||||||
|
private PetData() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
breeds.add("Afghan Hound");
|
breeds.add("Afghan Hound");
|
||||||
breeds.add("Beagle");
|
breeds.add("Beagle");
|
||||||
|
|||||||
@@ -129,6 +129,9 @@ public class PetStoreSpringAppConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class SimpleFilter extends OncePerRequestFilter {
|
public static class SimpleFilter extends OncePerRequestFilter {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public boolean invoked;
|
public boolean invoked;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -146,6 +149,10 @@ public class PetStoreSpringAppConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class AnotherFilter extends OncePerRequestFilter {
|
public static class AnotherFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public boolean invoked;
|
public boolean invoked;
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
|||||||
@@ -1438,9 +1438,15 @@ public class SimpleFunctionRegistry implements FunctionRegistry {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Object convertOutputMessageIfNecessary(Object output, String expectedOutputContetntType) {
|
private Object convertOutputMessageIfNecessary(Object output, String expectedOutputContetntType) {
|
||||||
String contentType = ((Message) output).getHeaders().containsKey(FunctionProperties.EXPECT_CONTENT_TYPE_HEADER)
|
String contentType;
|
||||||
? (String) ((Message) output).getHeaders().get(FunctionProperties.EXPECT_CONTENT_TYPE_HEADER)
|
if (((Message) output).getHeaders().containsKey(MessageHeaders.CONTENT_TYPE)) {
|
||||||
: expectedOutputContetntType;
|
contentType = ((Message) output).getHeaders().get(MessageHeaders.CONTENT_TYPE).toString();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
contentType = ((Message) output).getHeaders().containsKey(FunctionProperties.EXPECT_CONTENT_TYPE_HEADER)
|
||||||
|
? (String) ((Message) output).getHeaders().get(FunctionProperties.EXPECT_CONTENT_TYPE_HEADER)
|
||||||
|
: expectedOutputContetntType;
|
||||||
|
}
|
||||||
|
|
||||||
if (StringUtils.hasText(contentType)) {
|
if (StringUtils.hasText(contentType)) {
|
||||||
Map<String, Object> headersMap = new HashMap(((Message) output).getHeaders());
|
Map<String, Object> headersMap = new HashMap(((Message) output).getHeaders());
|
||||||
|
|||||||
Reference in New Issue
Block a user