Add Content-Type header to AWScJson response and writer for servlet response

This commit is contained in:
Oleg Zhurakousky
2023-05-23 12:01:58 +02:00
parent 571d00c107
commit aa90d256ec
3 changed files with 70 additions and 13 deletions

View File

@@ -18,8 +18,10 @@ package org.springframework.cloud.function.serverless.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -69,6 +71,8 @@ public class ProxyHttpServletResponse implements HttpServletResponse {
private int status = HttpServletResponse.SC_OK;
private ResponsePrintWriter writer;
@Nullable
private String errorMessage;
@@ -89,7 +93,11 @@ public class ProxyHttpServletResponse implements HttpServletResponse {
@Override
public PrintWriter getWriter() throws UnsupportedEncodingException {
throw new UnsupportedOperationException();
if (this.writer == null) {
Writer targetWriter = new OutputStreamWriter(this.content, getCharacterEncoding());
this.writer = new ResponsePrintWriter(targetWriter);
}
return this.writer;
}
public byte[] getContentAsByteArray() {
@@ -163,7 +171,7 @@ public class ProxyHttpServletResponse implements HttpServletResponse {
@Override
public boolean isCommitted() {
return false;
return this.writer == null ? false : this.writer.commited;
}
@Override
@@ -425,4 +433,47 @@ public class ProxyHttpServletResponse implements HttpServletResponse {
}
}
private class ResponsePrintWriter extends PrintWriter {
private boolean commited;
ResponsePrintWriter(Writer out) {
super(out, true);
}
@Override
public void write(char[] buf, int off, int len) {
super.write(buf, off, len);
super.flush();
this.commited = true;
}
@Override
public void write(String s, int off, int len) {
super.write(s, off, len);
super.flush();
this.commited = true;
}
@Override
public void write(int c) {
super.write(c);
super.flush();
this.commited = true;
}
@Override
public void flush() {
super.flush();
this.commited = true;
}
@Override
public void close() {
super.flush();
super.close();
this.commited = true;
}
}
}

View File

@@ -41,14 +41,13 @@ import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
@@ -85,11 +84,8 @@ public class ProxyMvc {
}
public static ProxyMvc INSTANCE(Class<?>... componentClasses) {
GenericWebApplicationContext applpicationContext = new GenericWebApplicationContext();
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(applpicationContext);
if (!ObjectUtils.isEmpty(componentClasses)) {
reader.register(componentClasses);
}
AnnotationConfigServletWebApplicationContext applpicationContext = new AnnotationConfigServletWebApplicationContext();
applpicationContext.scan(componentClasses[0].getPackageName());
return INSTANCE(applpicationContext);
}
@@ -108,10 +104,17 @@ public class ProxyMvc {
reg.setLoadOnStartup(1);
this.servletContext = applicationContext.getServletContext();
try {
this.dispatcher.init(new ProxyServletConfig(this.servletContext));
try {
this.service(new ProxyHttpServletRequest(servletContext, "INFO", "/"), new ProxyHttpServletResponse());
}
catch (Exception e) {
//ignore as this is just a pre-warming attempt
}
}
catch (Exception e) {
throw new IllegalStateException(e);
throw new IllegalStateException("Faild to create Spring MVC DispatcherServlet proxy", e);
}
}
@@ -162,7 +165,7 @@ public class ProxyMvc {
*
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
* @since 3.2
* @since 4.0.x
*/
ProxyFilterChain(DispatcherServlet servlet) {
List<Filter> filters = new ArrayList<>();