Created
July 14, 2010 22:09
-
-
Save BinaryMuse/476175 to your computer and use it in GitHub Desktop.
policy file fix for GWT app behind reverse proxy
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment