com.devops4j.embedded.servlet.EmbeddedHttpServletResponse Maven / Gradle / Ivy
package com.devops4j.embedded.servlet;
import com.devops4j.embedded.buffer.ByteBuf;
import com.devops4j.embedded.httpserver.Headers;
import com.devops4j.embedded.httpserver.HttpExchange;
import lombok.Data;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by devops4j on 2018/1/20.
*/
@Data
public class EmbeddedHttpServletResponse implements HttpServletResponse {
//---------------------------------------------------------------------
// ServletResponse properties
//---------------------------------------------------------------------
private boolean outputStreamAccessAllowed = true;
private boolean writerAccessAllowed = true;
private String characterEncoding = "UTF-8";
private boolean charset = false;
private ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
private long contentLength = 0;
private String contentType;
private int bufferSize = 4096;
private boolean committed;
private Locale locale = Locale.getDefault();
HttpExchange exchange;
//---------------------------------------------------------------------
// HttpServletResponse properties
//---------------------------------------------------------------------
private final List cookies = new ArrayList();
private final Map> headers = new ConcurrentHashMap();
private int status = HttpServletResponse.SC_OK;
private String errorMessage;
private String forwardedUrl;
private final List includedUrls = new ArrayList();
ByteBuf byteBuf;
public EmbeddedHttpServletResponse(HttpExchange exchange) {
this.byteBuf = ByteBuf.allocate(1024).autoExpand(true);
this.exchange = exchange;
// 指定允许其他域名访问
setHeader("Access-Control-Allow-Origin", "*");
// 响应类型 响应方法
setHeader("Access-Control-Allow-Methods", "GET,POST");
// 响应头设置
setHeader("Access-Control-Allow-Headers", "POWERED-BY-FANTONG");
setHeader("Access-Control-Max-Age", "30");
setHeader("Access-Control-Max-Age", "30");
setHeader("Content-Type", "text/html; charset=UTF-8");
setStatus(200);
}
public void write(String msg) {
this.byteBuf.put("UTF-8", msg);
}
public void write(byte[] data) {
this.byteBuf.put(data);
}
@Override
public void addCookie(Cookie cookie) {
cookies.add(cookie);
}
@Override
public boolean containsHeader(String name) {
return headers.containsKey(name);
}
@Override
public String encodeURL(String url) {
return null;
}
@Override
public String encodeRedirectURL(String url) {
return null;
}
@Override
public String encodeUrl(String url) {
return null;
}
@Override
public String encodeRedirectUrl(String url) {
return null;
}
@Override
public void sendError(int sc, String msg) throws IOException {
setStatus(sc);
}
@Override
public void sendError(int sc) throws IOException {
setStatus(sc);
}
@Override
public void sendRedirect(String location) throws IOException {
//通过301进行重定向
setStatus(301);
setHeader("Location", location);
}
@Override
public void setDateHeader(String name, long date) {
}
@Override
public void addDateHeader(String name, long date) {
}
@Override
public void setHeader(String name, String value) {
addHeader(name, value);
}
@Override
public void addHeader(String name, String value) {
if ("Content-Type".equalsIgnoreCase(name)) {
setContentType(value);
} else {
doAddHeaderValue(name, value);
}
Headers headers = exchange.getResponseHeaders();
headers.set(name, value);
}
private void doAddHeaderValue(String name, Object value) {
List