Thiết Kế Database Trong PostgreSQL Cho Hệ Thống Dịch Vụ Doanh Nghiệp
// Lấy token từ context hiện tại String token = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString(); if (token != null) { template.header("Authorization", "Bearer " + token); }
public class CachedBodyHttpServletRequest extends HttpServletRequestWrapper { private byte[] cachedBody;
public CachedBodyHttpServletRequest(HttpServletRequest request) throws IOException {
super(request);
InputStream requestInputStream = request.getInputStream();
this.cachedBody = StreamUtils.copyToByteArray(requestInputStream);
}
@Override
public ServletInputStream getInputStream() {
return new CachedBodyServletInputStream(this.cachedBody);
}
@Override
public BufferedReader getReader() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedBody);
return new BufferedReader(new InputStreamReader(byteArrayInputStream));
}
public byte[] getCachedBody() {
return cachedBody;
}
}
package m41.td.blcore.wrapper;
import jakarta.servlet.ServletOutputStream; import jakarta.servlet.WriteListener; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponseWrapper; import org.springframework.util.FastByteArrayOutputStream;
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter;
public class CachedBodyHttpServletResponse extends HttpServletResponseWrapper { private final FastByteArrayOutputStream content; private ServletOutputStream outputStream; private PrintWriter writer;
public CachedBodyHttpServletResponse(HttpServletResponse response) {
super(response);
this.content = new FastByteArrayOutputStream(1024);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException("getWriter() has already been called on this response");
}
if (outputStream == null) {
outputStream = new CachedBodyServletOutputStream(content);
}
return outputStream;
}
@Override
public PrintWriter getWriter() throws IOException {
if (outputStream != null) {
throw new IllegalStateException("getOutputStream() has already been called on this response");
}
if (writer == null) {
writer = new PrintWriter(new OutputStreamWriter(content, getCharacterEncoding()));
}
return writer;
}
@Override
public void flushBuffer() throws IOException {
if (writer != null) {
writer.flush();
} else if (outputStream != null) {
outputStream.flush();
}
}
public byte[] getContentAsByteArray() {
flushContent();
return content.toByteArray();
}
private void flushContent() {
try {
if (writer != null) {
writer.flush();
} else if (outputStream != null) {
outputStream.flush();
}
} catch (IOException ex) {
throw new RuntimeException("Failed to flush response content", ex);
}
}
private static class CachedBodyServletOutputStream extends ServletOutputStream {
private final FastByteArrayOutputStream content;
public CachedBodyServletOutputStream(FastByteArrayOutputStream content) {
this.content = content;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
throw new UnsupportedOperationException("setWriteListener is not supported");
}
@Override
public void write(int b) throws IOException {
content.write(b);
}
@Override
public void write(byte[] b) throws IOException {
content.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
content.write(b, off, len);
}
}
}
package m41.td.blcore.wrapper;
import jakarta.servlet.ReadListener; import jakarta.servlet.ServletInputStream;
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream;
public class CachedBodyServletInputStream extends ServletInputStream { private final InputStream cachedBodyInputStream;
public CachedBodyServletInputStream(byte[] cachedBody) {
this.cachedBodyInputStream = new ByteArrayInputStream(cachedBody);
}
@Override
public boolean isFinished() {
try {
return cachedBodyInputStream.available() == 0;
} catch (IOException e) {
return true;
}
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
throw new UnsupportedOperationException("setReadListener is not supported");
}
@Override
public int read() throws IOException {
return cachedBodyInputStream.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return cachedBodyInputStream.read(b, off, len);
}
@Override
public void close() throws IOException {
cachedBodyInputStream.close();
}
}
All Rights Reserved