jersey.jsp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <%@page contentType="text/html;charset=UTF-8"%>
  2. <%@page import="java.lang.annotation.*"%>
  3. <%@page import="java.lang.reflect.*"%>
  4. <%@page import="java.util.*"%>
  5. <%@page import="javax.annotation.*"%>
  6. <%@page import="javax.ws.rs.*"%>
  7. <%@page import="org.springframework.context.ApplicationContext"%>
  8. <%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
  9. <%@page import="com.mossle.core.util.ReflectUtils"%>
  10. <%!
  11. String getRequestMethodColor(String methodName) {
  12. if ("POST".equals(methodName)) {
  13. return "#10A54A";
  14. } else if ("GET".equals(methodName)) {
  15. return "#0f6ab4";
  16. } else if ("DELETE".equals(methodName)) {
  17. return "#a41e22";
  18. } else if ("PUT".equals(methodName)) {
  19. return "#faf5ee";
  20. } else if ("PATCH".equals(methodName)) {
  21. return "#D38042";
  22. } else {
  23. return "black";
  24. }
  25. }
  26. String getRequestMethod(Method method) {
  27. if (method.isAnnotationPresent(Resource.class)) {
  28. return null;
  29. }
  30. if (method.isAnnotationPresent(POST.class)) {
  31. return "POST";
  32. } else if (method.isAnnotationPresent(GET.class)) {
  33. return "GET";
  34. } else if (method.isAnnotationPresent(DELETE.class)) {
  35. return "DELETE";
  36. } else {
  37. System.out.println("UNKNOW : " + method);
  38. return null;
  39. }
  40. }
  41. String getPath(Path subPath, String rootPathValue) {
  42. String subPathValue = null;
  43. if (subPath != null) {
  44. subPathValue = subPath.value();
  45. if (subPathValue.charAt(0) != '/') {
  46. subPathValue = "/" + subPathValue;
  47. }
  48. subPathValue = rootPathValue + subPathValue;
  49. } else {
  50. subPathValue = rootPathValue;
  51. }
  52. return subPathValue;
  53. }
  54. %>
  55. <%
  56. ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
  57. Map<String,Object> map = ctx.getBeansWithAnnotation(Path.class);
  58. %>
  59. <html>
  60. <head>
  61. <meta charset="utf-8">
  62. <title>jersey</title>
  63. <style>
  64. tbody tr:nth-child(odd) td,
  65. tbody tr:nth-child(odd) th {
  66. background-color: #f9f9f9;
  67. }
  68. </style>
  69. <script type="text/javascript">
  70. var data = {
  71. <%
  72. int classIndex = 0;
  73. for (Object item : map.values()) {
  74. Class clz = item.getClass();
  75. Path rootPath = (Path) clz.getAnnotation(Path.class);
  76. String rootPathValue = rootPath.value();
  77. if (rootPathValue.charAt(0) != '/') {
  78. rootPathValue = "/" + rootPathValue;
  79. }
  80. %>
  81. '<%=clz.getName()%>': {
  82. <%
  83. int methodIndex = 0;
  84. for (Method method : clz.getDeclaredMethods()) {
  85. String subMethodValue = getRequestMethod(method);
  86. if (subMethodValue == null) {
  87. continue;
  88. }
  89. Path subPath = (Path) method.getAnnotation(Path.class);
  90. %>
  91. '<%=method.getName()%>': {
  92. path: '<%=getPath(subPath, rootPathValue)%>',
  93. method: '<%=subMethodValue%>',
  94. params: [
  95. <%
  96. Annotation[][] annotationArray = method.getParameterAnnotations();
  97. for (int i = 0; i < annotationArray.length; i++) {
  98. Annotation[] annotations = annotationArray[i];
  99. String name = null;
  100. String type = null;
  101. for (Annotation annotation : annotations) {
  102. if (annotation instanceof PathParam) {
  103. name = ((PathParam) annotation).value();
  104. type = "path";
  105. } else if (annotation instanceof QueryParam) {
  106. name = ((QueryParam) annotation).value();
  107. type = "query";
  108. } else if (annotation instanceof FormParam) {
  109. name = ((FormParam) annotation).value();
  110. type = "form";
  111. } else if (annotation instanceof HeaderParam) {
  112. name = ((HeaderParam) annotation).value();
  113. type = "header";
  114. }
  115. }
  116. if (name == null) {
  117. continue;
  118. }
  119. %>
  120. {
  121. name: '<%=name%>',
  122. type: '<%=type%>'
  123. }<%=(i == annotationArray.length - 1 ? "" : ",")%>
  124. <%
  125. }
  126. %>
  127. ]
  128. }<%=(methodIndex == clz.getDeclaredMethods().length - 1 ? "" : ",")%>
  129. <%
  130. methodIndex++;
  131. }
  132. %>
  133. }<%=(classIndex == map.values().size() - 1 ? "" : ",")%>
  134. <%
  135. classIndex++;
  136. }
  137. %>
  138. };
  139. function toggle(id) {
  140. var el = document.getElementById(id);
  141. if (el.style.display == 'none') {
  142. el.style.display = '';
  143. } else {
  144. el.style.display = 'none';
  145. }
  146. }
  147. function doTest(className, methodName) {
  148. var o = data[className][methodName];
  149. var id = className + '-' + methodName;
  150. var url = '<%=request.getContextPath()%>/rs' + o.path;
  151. var xmlhttp;
  152. if (window.XMLHttpRequest) {
  153. // code for IE7+, Firefox, Chrome, Opera, Safari
  154. xmlhttp = new XMLHttpRequest();
  155. } else {
  156. // code for IE6, IE5
  157. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  158. }
  159. var headerParams = {};
  160. var queryParams = '';
  161. var formParams = '';
  162. for (var i = 0; i < o.params.length; i++) {
  163. var item = o.params[i];
  164. if (item.type == 'path') {
  165. url = url.replace('{' + item.name + '}', document.getElementById(id + '-' + item.name).value);
  166. } else if (item.type == 'query') {
  167. if (queryParams != '') {
  168. queryParams += '&';
  169. }
  170. queryParams += item.name + '=' + encodeURIComponent(document.getElementById(id + '-' + item.name).value);
  171. } else if (item.type == 'form') {
  172. if (formParams != '') {
  173. formParams += '&';
  174. }
  175. formParams += item.name + '=' + document.getElementById(id + '-' + item.name).value;
  176. } else if (item.type == 'header') {
  177. headerParams[item.name] = document.getElementById(id + '-' + item.name).value;
  178. }
  179. }
  180. xmlhttp.onreadystatechange = function() {
  181. if (xmlhttp.readyState == 1) {
  182. for (var key in headerParams) {
  183. var value = headerParams[key];
  184. xmlhttp.setRequestHeader(key, value);
  185. }
  186. }
  187. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  188. document.getElementById(id + '-result').innerHTML = xmlhttp.responseText;
  189. document.getElementById(id + '-result').style.display = '';
  190. }
  191. }
  192. if (queryParams != '') {
  193. if (url.indexOf('?') == -1) {
  194. url += '?' + queryParams;
  195. } else {
  196. url += '&' + queryParams;
  197. }
  198. }
  199. xmlhttp.open(o.method, url, true);
  200. if (o.method == 'GET') {
  201. xmlhttp.send();
  202. } else {
  203. xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  204. xmlhttp.send(formParams);
  205. }
  206. }
  207. </script>
  208. </head>
  209. <body>
  210. <%
  211. for (Object item : map.values()) {
  212. Class clz = item.getClass();
  213. Path rootPath = (Path) clz.getAnnotation(Path.class);
  214. String rootPathValue = rootPath.value();
  215. if (rootPathValue.charAt(0) != '/') {
  216. rootPathValue = "/" + rootPathValue;
  217. }
  218. %>
  219. <div style="background-color:black;color:white;cursor:pointer;padding:5px;border:1px dotted gray;" onclick="toggle('<%=clz.getName()%>')">
  220. <%=rootPathValue%>
  221. </div>
  222. <div id="<%=clz.getName()%>" style="display:none;">
  223. <%
  224. for (Method method : clz.getDeclaredMethods()) {
  225. String subMethodValue = getRequestMethod(method);
  226. if (subMethodValue == null) {
  227. continue;
  228. }
  229. Path subPath = (Path) method.getAnnotation(Path.class);
  230. String subPathValue = getPath(subPath, rootPathValue);
  231. Produces produces = method.getAnnotation(Produces.class);
  232. %>
  233. <div style="border:1px solid gray;margin:5px;padding:5px;cursor:pointer;" onclick="toggle('<%=clz.getName()%>-<%=method.getName()%>')">
  234. <span style="background-color:<%=getRequestMethodColor(subMethodValue)%>;color:white;display:inline-block;width:60px;text-align:center;"><%=subMethodValue%></span>
  235. &nbsp;
  236. <span style="display:inline-block;width:100px;"><%=subPathValue%></span>
  237. &nbsp;
  238. <span style="display:inline-block;float:right;"><%=produces == null ? "" : Arrays.asList(produces.value())%></span>
  239. </div>
  240. <div id="<%=clz.getName()%>-<%=method.getName()%>" style="display:none;border:1px solid gray;padding:5px;margin:5px;">
  241. <%
  242. Annotation[][] annotationArray = method.getParameterAnnotations();
  243. Class[] parameterTypeArray = method.getParameterTypes();
  244. for (int i = 0; i < annotationArray.length; i++) {
  245. Annotation[] annotations = annotationArray[i];
  246. Class parameterType = parameterTypeArray[i];
  247. String name = null;
  248. String type = null;
  249. for (Annotation annotation : annotations) {
  250. if (annotation instanceof PathParam) {
  251. name = ((PathParam) annotation).value();
  252. type = "path";
  253. } else if (annotation instanceof QueryParam) {
  254. name = ((QueryParam) annotation).value();
  255. type = "query";
  256. } else if (annotation instanceof FormParam) {
  257. name = ((FormParam) annotation).value();
  258. type = "form";
  259. } else if (annotation instanceof HeaderParam) {
  260. name = ((HeaderParam) annotation).value();
  261. type = "header";
  262. }
  263. }
  264. if (name == null) {
  265. continue;
  266. }
  267. %>
  268. <div>
  269. <label for="<%=clz.getName()%>-<%=method.getName()%>-<%=name%>"><%=name%>:</label>
  270. <input type="text" id="<%=clz.getName()%>-<%=method.getName()%>-<%=name%>" class="<%=type%>">
  271. </div>
  272. <%
  273. }
  274. %>
  275. <div>
  276. <div id="<%=clz.getName()%>-<%=method.getName()%>-result" style="border:1px solid gray;margin:5px;padding:5px;display:none;"></div>
  277. <button onclick="doTest('<%=clz.getName()%>','<%=method.getName()%>')">test</button>
  278. </div>
  279. </div>
  280. <%
  281. }
  282. %>
  283. </div>
  284. <%
  285. }
  286. %>
  287. </body>
  288. </html>