-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
29 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...es/src/main/java/com/flipkart/gjex/examples/helloworld/filter/CustomHeaderHttpFilter.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,41 @@ | ||
package com.flipkart.gjex.examples.helloworld.filter; | ||
|
||
import com.flipkart.gjex.core.filter.RequestParams; | ||
import com.flipkart.gjex.core.filter.http.HttpFilter; | ||
|
||
import javax.inject.Named; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.Map; | ||
|
||
@Named("CustomHeaderHttpFilter") | ||
public class CustomHeaderHttpFilter extends HttpFilter { | ||
|
||
@Override | ||
public void doProcessRequest(ServletRequest servletRequest, RequestParams<Map<String, String>> requestParams) { | ||
super.doProcessRequest(servletRequest, requestParams); | ||
} | ||
|
||
@Override | ||
public void doProcessResponseHeaders(Map<String, String> responseHeaders) { | ||
super.doProcessResponseHeaders(responseHeaders); | ||
} | ||
|
||
@Override | ||
public void doProcessResponse(ServletResponse response) { | ||
super.doProcessResponse(response); | ||
HttpServletResponse httpServletResponse = (HttpServletResponse) response; | ||
httpServletResponse.addHeader("x-custom-header", "custom-header-value"); | ||
} | ||
|
||
@Override | ||
public void doHandleException(Exception e) { | ||
super.doHandleException(e); | ||
} | ||
|
||
@Override | ||
public HttpFilter getInstance() { | ||
return new CustomHeaderHttpFilter(); | ||
} | ||
} |
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
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
69 changes: 69 additions & 0 deletions
69
guice/src/main/java/com/flipkart/gjex/http/interceptor/FilterServletResponseWrapper.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,69 @@ | ||
package com.flipkart.gjex.http.interceptor; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.WriteListener; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpServletResponseWrapper; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
public class FilterServletResponseWrapper extends HttpServletResponseWrapper { | ||
|
||
ServletOutputStreamWrapper stream = new ServletOutputStreamWrapper(); | ||
|
||
/** | ||
* Constructs a request object wrapping the given request. | ||
* | ||
* @param request | ||
* @throws IllegalArgumentException if the request is null | ||
*/ | ||
public FilterServletResponseWrapper(HttpServletResponse request) { | ||
super(request); | ||
} | ||
|
||
public ServletOutputStream getOutputStream() throws IOException | ||
{ | ||
return stream; | ||
} | ||
|
||
public PrintWriter getWriter() throws IOException | ||
{ | ||
return new PrintWriter(stream); | ||
} | ||
|
||
public byte[] getWrapperBytes() | ||
{ | ||
return stream.getBytes(); | ||
} | ||
|
||
|
||
static class ServletOutputStreamWrapper extends ServletOutputStream { | ||
|
||
private final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
|
||
public void write(int b) throws IOException { | ||
out.write(b); | ||
} | ||
|
||
public byte[] getBytes() { | ||
return out.toByteArray(); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setWriteListener(WriteListener writeListener) { | ||
try { | ||
writeListener.onWritePossible(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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