forked from apache/atlas
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLT-2568 brotli compression for all endpoints
- Loading branch information
1 parent
e928462
commit 305a385
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
webapp/src/main/java/org/apache/atlas/web/filters/BrotliCompressionFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.apache.atlas.web.filters; | ||
|
||
import com.aayushatharva.brotli4j.Brotli4jLoader; | ||
import com.aayushatharva.brotli4j.encoder.Encoder; | ||
import com.aayushatharva.brotli4j.encoder.Encoder.Parameters; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class BrotliCompressionFilter implements Filter { | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) { | ||
Brotli4jLoader.ensureAvailability(); | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
|
||
// Ensure request and response are HttpServletRequest and HttpServletResponse | ||
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { | ||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
|
||
String acceptEncoding = httpRequest.getHeader("Accept-Encoding"); | ||
|
||
// Check if the client supports Brotli compression | ||
if (acceptEncoding != null && acceptEncoding.contains("br")) { | ||
// Wrap the response with a Brotli compression wrapper | ||
BrotliResponseWrapper responseWrapper = new BrotliResponseWrapper(httpResponse); | ||
chain.doFilter(request, responseWrapper); | ||
|
||
// Compress the response content with Brotli | ||
byte[] uncompressedData = responseWrapper.getOutputStreamData(); | ||
Parameters params = new Parameters().setQuality(6); // Set Brotli quality level | ||
byte[] compressedOutput = Encoder.compress(uncompressedData, params); | ||
|
||
// Write Brotli-compressed data to the actual response | ||
httpResponse.setHeader("Content-Encoding", "br"); | ||
httpResponse.setContentLength(compressedOutput.length); | ||
httpResponse.getOutputStream().write(compressedOutput); | ||
} else { | ||
// Proceed without compression | ||
chain.doFilter(request, response); | ||
} | ||
} else { | ||
// Proceed without compression if not HTTP | ||
chain.doFilter(request, response); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
// Optional: Add cleanup logic here if needed | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
webapp/src/main/java/org/apache/atlas/web/filters/BrotliResponseWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.apache.atlas.web.filters; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpServletResponseWrapper; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
public class BrotliResponseWrapper extends HttpServletResponseWrapper { | ||
|
||
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
private ServletOutputStream servletOutputStream; | ||
private PrintWriter writer; | ||
|
||
public BrotliResponseWrapper(HttpServletResponse response) { | ||
super(response); | ||
} | ||
|
||
@Override | ||
public ServletOutputStream getOutputStream() throws IOException { | ||
if (servletOutputStream == null) { | ||
servletOutputStream = new ServletOutputStream() { | ||
@Override | ||
public void write(int b) { | ||
outputStream.write(b); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setWriteListener(javax.servlet.WriteListener listener) { | ||
// No-op for this example | ||
} | ||
}; | ||
} | ||
return servletOutputStream; | ||
} | ||
|
||
@Override | ||
public PrintWriter getWriter() throws IOException { | ||
if (writer == null) { | ||
writer = new PrintWriter(outputStream); | ||
} | ||
return writer; | ||
} | ||
|
||
public byte[] getOutputStreamData() { | ||
return outputStream.toByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters