-
Notifications
You must be signed in to change notification settings - Fork 112
Added ability to cache the whole graphql response #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4d4020d
Added ability to cache the whole graphql response.
donbeave b66190e
Fixed SingleQueryResponseWriterTest.
donbeave 8836fab
Pass HttpServletRequest to GraphQLResponseCache methods.
donbeave c52e45b
Added comments.
donbeave 2322c25
Pass request to cacheResponse method.
donbeave 9153aa5
Added method which check is this request must be cached.
donbeave 86cc378
Fixed HttpRequestHandlerImpl.
donbeave 1cf9e73
Merge branch 'master' of https://github.com/graphql-java-kickstart/gr…
donbeave 6c95ef3
Merge branch 'graphql-java-kickstart-master'
donbeave 4b4edfe
Code review changes.
donbeave aa936f9
Fixed tests.
donbeave 4d25d64
Fixed tests.
donbeave 2316944
Code review fixes.
donbeave ac6fc42
Moved createCacheWriter from QueryResponseWriter to CachingQueryRespo…
donbeave 315b77f
Added Override annotation.
donbeave bc93099
Merge branch 'master' of https://github.com/graphql-java-kickstart/gr…
donbeave 2902ee7
Merge branch 'graphql-java-kickstart-master'
donbeave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
9 changes: 5 additions & 4 deletions
9
graphql-java-servlet/src/main/java/graphql/kickstart/servlet/QueryResponseWriter.java
This file contains hidden or 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 hidden or 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
142 changes: 142 additions & 0 deletions
142
...va-servlet/src/main/java/graphql/kickstart/servlet/cache/BufferedHttpServletResponse.java
This file contains hidden or 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,142 @@ | ||
package graphql.kickstart.servlet.cache; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
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.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.PrintWriter; | ||
|
||
@Slf4j | ||
public class BufferedHttpServletResponse extends HttpServletResponseWrapper { | ||
|
||
private static final class BufferedOutputStream extends ServletOutputStream { | ||
|
||
private final OutputStream delegate; | ||
private final ByteArrayOutputStream buf = new ByteArrayOutputStream(); | ||
|
||
public BufferedOutputStream(OutputStream delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public void write(int b) throws IOException { | ||
buf.write(b); | ||
delegate.write(b); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
buf.flush(); | ||
delegate.flush(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
buf.close(); | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setWriteListener(WriteListener writeListener) { | ||
} | ||
|
||
public byte[] toByteArray() { | ||
return buf.toByteArray(); | ||
} | ||
|
||
} | ||
|
||
private BufferedOutputStream copier; | ||
|
||
private ServletOutputStream outputStream; | ||
private PrintWriter writer; | ||
private String errorMessage; | ||
|
||
public BufferedHttpServletResponse(HttpServletResponse response) { | ||
super(response); | ||
} | ||
|
||
@Override | ||
public void sendError(int sc, String msg) throws IOException { | ||
errorMessage = msg; | ||
super.sendError(sc, msg); | ||
} | ||
|
||
@Override | ||
public void sendError(int sc) throws IOException { | ||
sendError(sc, null); | ||
} | ||
|
||
@Override | ||
public ServletOutputStream getOutputStream() throws IOException { | ||
if (writer != null) { | ||
throw new IllegalStateException("getWriter() has already been called on this response."); | ||
} | ||
|
||
if (outputStream == null) { | ||
outputStream = getResponse().getOutputStream(); | ||
copier = new BufferedOutputStream(outputStream); | ||
} | ||
|
||
return copier; | ||
} | ||
|
||
@Override | ||
public PrintWriter getWriter() throws IOException { | ||
if (outputStream != null) { | ||
throw new IllegalStateException("getOutputStream() has already been called on this response."); | ||
} | ||
|
||
if (writer == null) { | ||
copier = new BufferedOutputStream(getResponse().getOutputStream()); | ||
writer = new PrintWriter(new OutputStreamWriter(copier, getResponse().getCharacterEncoding()), true); | ||
} | ||
|
||
return writer; | ||
} | ||
|
||
@Override | ||
public void flushBuffer() throws IOException { | ||
if (writer != null) { | ||
writer.flush(); | ||
} else if (copier != null) { | ||
copier.flush(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCommitted() { | ||
return false; | ||
} | ||
|
||
public void close() throws IOException { | ||
if (writer != null) { | ||
writer.close(); | ||
} else if (copier != null) { | ||
copier.close(); | ||
} | ||
} | ||
|
||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
public byte[] getContentAsByteArray() { | ||
if (copier != null) { | ||
return copier.toByteArray(); | ||
} else { | ||
return new byte[0]; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.