Skip to content
Browse files

Ajout du support de jsonp via le paramètre jsonp dans n'importe quell…

…e requête, exemple :

	http://localhost:8080/GexServer/rest/node/list/vivop/?jsonp=callback
renvoie :
	callback(
{"node":[{"id":"vio","label":"Vio","parentId":"vivop","project":"true"},{"id":"siclop","label":"Siclop","parentId":"vivop","project":"true"}]});
  • Loading branch information...
1 parent 34aa388 commit 271bad9c10ef2c1c5d57deb63d02c0d689bd5d9f sdaclin committed
View
56 src/main/java/fr/astek/gex/server/filter/JSONPRequestFilter.java
@@ -0,0 +1,56 @@
+package fr.astek.gex.server.filter;
+
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author sdaclin
+ * Adapted from http://solutionsfit.com/blog/2010/04/18/serving-up-jsonp-from-your-jax-rs-web-services/
+ */
+public class JSONPRequestFilter implements Filter {
+
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+ if (!(request instanceof HttpServletRequest)) {
+ throw new ServletException("This filter can only process HttpServletRequest requests");
+ }
+
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+
+ if (isJSONPRequest(httpRequest)) {
+ ServletOutputStream out = response.getOutputStream();
+
+ out.println(getCallbackMethod(httpRequest) + "(");
+ chain.doFilter(request, response);
+ out.println(");");
+
+ response.setContentType("text/javascript");
+ } else {
+ chain.doFilter(request, response);
+ }
+ }
+
+ private String getCallbackMethod(HttpServletRequest httpRequest) {
+ return httpRequest.getParameter("jsonp");
+ }
+
+ private boolean isJSONPRequest(HttpServletRequest httpRequest) {
+ String callbackMethod = getCallbackMethod(httpRequest);
+ return (callbackMethod != null && callbackMethod.length() > 0);
+ }
+
+ public void init(FilterConfig fc) throws ServletException {
+ //throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public void destroy() {
+ //throw new UnsupportedOperationException("Not supported yet.");
+ }
+}
View
4 src/main/java/fr/astek/gex/server/service/AgexService.java
@@ -10,7 +10,7 @@
* @author sdaclin
*/
public class AgexService {
- private static Map<String,Node> nodes = new HashMap<String, Node>();
+ private static final Map<String,Node> nodes = new HashMap<String, Node>();
static {
Node vivop = new Node();
vivop.setId("vivop");
@@ -70,4 +70,4 @@ public static void addNode(Node node) {
nodes.put(node.getId().toLowerCase(), node);
}
-}
+}
View
13 src/main/webapp/WEB-INF/web.xml
@@ -2,7 +2,7 @@
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
+ <!-- Prise en compte JAX-RS -->
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
@@ -17,4 +17,15 @@
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
+
+ <!-- Prise en compte jsonp -->
+ <filter>
+ <filter-class>fr.astek.gex.server.filter.JSONPRequestFilter</filter-class>
+ <filter-name>JSONPRequestFilter</filter-name>
+ </filter>
+
+ <filter-mapping>
+ <filter-name>JSONPRequestFilter</filter-name>
+ <url-pattern>/rest/*</url-pattern>
+ </filter-mapping>
</web-app>

0 comments on commit 271bad9

Please sign in to comment.
Something went wrong with that request. Please try again.