public
Last active

policy file fix for GWT app behind reverse proxy

  • Download Gist
ContextRemoteServiceServlet.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
 
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.gwt.user.server.rpc.SerializationPolicy;
import com.google.gwt.user.server.rpc.SerializationPolicyLoader;
 
/**
* ContextRemoteServiceServlet is an extension of RemoteServiceServlet that
* loads the RPC serialization policy based on the context name rather than URL if it is
* not successfully loaded using the GWT's default method. This is useful if
* you're proxying requests to RPC servlets from one server to another and the
* paths to the servlets don't match perfectly (causing the RPC policy file to fail to load).
*
*/
public class ContextRemoteServiceServlet extends RemoteServiceServlet {
 
private static final long serialVersionUID = -4332306688541651819L;
 
/**
* Attempt to load the RPC serialization policy normally. If it isn't found,
* try loading it using the context path instead of the URL.
*/
@Override
protected SerializationPolicy doGetSerializationPolicy(HttpServletRequest request, String moduleBaseURL, String strongName)
{
SerializationPolicy policy = super.doGetSerializationPolicy(request, moduleBaseURL, strongName);
if(policy == null) {
return ContextRemoteServiceServlet.loadSerializationPolicy(this, request, moduleBaseURL, strongName);
} else {
return policy;
}
}
 
/**
* Load the RPC serialization policy via the context path.
*/
static SerializationPolicy loadSerializationPolicy(HttpServlet servlet,
HttpServletRequest request, String moduleBaseURL, String strongName) {
// The serialization policy path depends only by context path
String contextPath = request.getContextPath();
SerializationPolicy serializationPolicy = null;
String contextRelativePath = contextPath + "/";
String serializationPolicyFilePath = SerializationPolicyLoader.getSerializationPolicyFileName(contextRelativePath
+ strongName);
 
// Open the RPC resource file and read its contents.
InputStream is = servlet.getServletContext().getResourceAsStream(serializationPolicyFilePath);
try {
if (is != null) {
try {
serializationPolicy = SerializationPolicyLoader.loadFromStream(is, null);
} catch (ParseException e) {
servlet.log("ERROR: Failed to parse the policy file '" + serializationPolicyFilePath + "'", e);
} catch (IOException e) {
servlet.log("ERROR: Could not read the policy file '" + serializationPolicyFilePath + "'", e);
}
} else {
String message = "ERROR: The serialization policy file '" + serializationPolicyFilePath +
"' was not found; did you forget to include it in this deployment?";
servlet.log(message);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Ignore this error
}
}
}
 
return serializationPolicy;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.