-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from donbeave/master
Added ability to cache the whole graphql response
- Loading branch information
Showing
13 changed files
with
482 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ target/ | |
.project | ||
.settings | ||
bin | ||
.DS_Store |
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
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
9 changes: 5 additions & 4 deletions
9
graphql-java-servlet/src/main/java/graphql/kickstart/servlet/QueryResponseWriter.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
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
142 changes: 142 additions & 0 deletions
142
...va-servlet/src/main/java/graphql/kickstart/servlet/cache/BufferedHttpServletResponse.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,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]; | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
graphql-java-servlet/src/main/java/graphql/kickstart/servlet/cache/CacheReader.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,57 @@ | ||
package graphql.kickstart.servlet.cache; | ||
|
||
import graphql.kickstart.execution.input.GraphQLInvocationInput; | ||
import graphql.kickstart.servlet.HttpRequestHandler; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Slf4j | ||
public final class CacheReader { | ||
|
||
private CacheReader() { | ||
} | ||
|
||
/** | ||
* Response from cache if possible, if nothing in cache will not produce any response | ||
* | ||
* @return {@literal true} if response was fulfilled from cache, {@literal false} is cache not found or an error | ||
* occurred while reading value from cache | ||
* @throws IOException if can not read value from the cache | ||
*/ | ||
public static boolean responseFromCache(GraphQLInvocationInput invocationInput, | ||
HttpServletRequest request, | ||
HttpServletResponse response, | ||
GraphQLResponseCacheManager cacheManager) throws IOException { | ||
CachedResponse cachedResponse = null; | ||
try { | ||
cachedResponse = cacheManager.get(request, invocationInput); | ||
} catch (Throwable t) { | ||
log.warn("Ignore read from cache, unexpected error happened", t); | ||
} | ||
|
||
if (cachedResponse != null) { | ||
write(response, cachedResponse); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static void write(HttpServletResponse response, CachedResponse cachedResponse) | ||
throws IOException { | ||
if (cachedResponse.isError()) { | ||
response.sendError(cachedResponse.getErrorStatusCode(), cachedResponse.getErrorMessage()); | ||
} else { | ||
response.setContentType(HttpRequestHandler.APPLICATION_JSON_UTF8); | ||
response.setStatus(HttpRequestHandler.STATUS_OK); | ||
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); | ||
response.setContentLength(cachedResponse.getContentBytes().length); | ||
response.getOutputStream().write(cachedResponse.getContentBytes()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.