|
@@ -0,0 +1,106 @@
|
|
|
|
+package cn.com.qmth.examcloud.web.support;
|
|
|
|
+
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+
|
|
|
|
+import com.google.common.collect.Maps;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.ResourceLoader;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 测试桩
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @date 2018年8月23日
|
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
|
+ */
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("mock")
|
|
|
|
+public class MockController {
|
|
|
|
+
|
|
|
|
+ protected static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(MockController.class);
|
|
|
|
+
|
|
|
|
+ @Naked
|
|
|
|
+ @RequestMapping(value = {"/{templateId}/**"}, method = RequestMethod.GET)
|
|
|
|
+ public String get(@PathVariable String templateId, HttpServletRequest request) {
|
|
|
|
+ return buildRespBody(templateId, request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Naked
|
|
|
|
+ @RequestMapping(value = {"/{templateId}/**"}, method = RequestMethod.HEAD)
|
|
|
|
+ public ResponseEntity<?> head(@PathVariable String templateId, HttpServletRequest request) {
|
|
|
|
+ return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Naked
|
|
|
|
+ @RequestMapping(value = {"/{templateId}/**"}, method = RequestMethod.OPTIONS)
|
|
|
|
+ public HttpEntity<?> options(@PathVariable String templateId, HttpServletRequest request) {
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.setAllow(Collections.singleton(HttpMethod.GET));
|
|
|
|
+ return new ResponseEntity<Object>(headers, HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Naked
|
|
|
|
+ @RequestMapping(value = {"/{templateId}/**"}, method = RequestMethod.POST)
|
|
|
|
+ public String post(@PathVariable String templateId, HttpServletRequest request) {
|
|
|
|
+ return buildRespBody(templateId, request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Naked
|
|
|
|
+ @RequestMapping(value = {"/{templateId}/**"}, method = RequestMethod.PUT)
|
|
|
|
+ public String put(@PathVariable String templateId, HttpServletRequest request) {
|
|
|
|
+ return buildRespBody(templateId, request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Naked
|
|
|
|
+ @RequestMapping(value = {"/{templateId}/**"}, method = RequestMethod.DELETE)
|
|
|
|
+ public String delete(@PathVariable String templateId, HttpServletRequest request) {
|
|
|
|
+ return buildRespBody(templateId, request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构建响应体
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param id
|
|
|
|
+ * @param request
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String buildRespBody(String id, HttpServletRequest request) {
|
|
|
|
+ String requestURI = request.getRequestURI();
|
|
|
|
+ String uri = requestURI.substring("mock".length() + 2);
|
|
|
|
+ String[] pathValues = uri.split("/");
|
|
|
|
+ Map<String, Object> data = Maps.newHashMap();
|
|
|
|
+ data.put("pathValues", pathValues);
|
|
|
|
+ String template = null;
|
|
|
|
+ try {
|
|
|
|
+ template = ResourceLoader.getResource("mock/" + id);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("XXX", "未定义模板");
|
|
|
|
+ }
|
|
|
|
+ String respBody = null;
|
|
|
|
+ try {
|
|
|
|
+ respBody = FreeMarkerUtil.process(template, data);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new StatusException("XXX", "模板解析失败");
|
|
|
|
+ }
|
|
|
|
+ return respBody;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|