| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ApiPage |
|
| 1.5714285714285714;1.571 | ||||
| ApiPage$ApiResult |
|
| 1.5714285714285714;1.571 |
| 1 | package jp.co.y_net.amm.api; | |
| 2 | ||
| 3 | import org.apache.wicket.markup.html.WebPage; | |
| 4 | import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler; | |
| 5 | import org.apache.wicket.request.http.WebResponse; | |
| 6 | import org.apache.wicket.util.resource.StringResourceStream; | |
| 7 | import org.apache.wicket.util.time.Time; | |
| 8 | ||
| 9 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 10 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 11 | ||
| 12 | /** | |
| 13 | * @author k_inaba | |
| 14 | */ | |
| 15 | 0 | public abstract class ApiPage extends WebPage{ |
| 16 | ||
| 17 | @Override | |
| 18 | protected void configureResponse(WebResponse response){ | |
| 19 | 0 | // response.setContentType("application/json; charset=utf-8"); |
| 20 | 0 | response.setContentType("application/json; utf-8"); |
| 21 | ||
| 22 | /* ブラウザキャッシュ無効化 */ | |
| 23 | 0 | java.util.Calendar objCal1=java.util.Calendar.getInstance(); |
| 24 | 0 | java.util.Calendar objCal2=java.util.Calendar.getInstance(); |
| 25 | 0 | objCal2.set(1970,0,1,0,0,0); |
| 26 | ||
| 27 | 0 | response.setDateHeader("Last-Modified",Time.valueOf(objCal1.getTime())); |
| 28 | 0 | response.setDateHeader("Expires",Time.valueOf(objCal2.getTime())); |
| 29 | 0 | response.setHeader("progma","no-cache"); |
| 30 | 0 | response.setHeader("Cache-Control","no-cache"); |
| 31 | 0 | |
| 32 | 0 | |
| 33 | 0 | super.configureResponse(response); |
| 34 | 0 | } |
| 35 | ||
| 36 | 0 | public void setResponse(Object result) { |
| 37 | 0 | ObjectMapper objectMapper = new ObjectMapper(); |
| 38 | String json; | |
| 39 | 0 | try { |
| 40 | 0 | json = objectMapper.writeValueAsString(result); |
| 41 | 0 | } catch (JsonProcessingException e) { |
| 42 | 0 | throw new RuntimeException(e); |
| 43 | } | |
| 44 | 0 | |
| 45 | 0 | getRequestCycle().scheduleRequestHandlerAfterCurrent( |
| 46 | 0 | new ResourceStreamRequestHandler(new StringResourceStream(json)) |
| 47 | 0 | ); |
| 48 | 0 | } |
| 49 | // /* ---------------------------------------------- | |
| 50 | // * パラメータ取得ユーティリティ | |
| 51 | // */ | |
| 52 | // protected String getParam(String key) { | |
| 53 | // if (getPageParameters() == null) { | |
| 54 | // return ""; | |
| 55 | // } else { | |
| 56 | // return getPageParameters().get(key).toString(""); | |
| 57 | // } | |
| 58 | // } | |
| 59 | ||
| 60 | /* ---------------------------------------------- | |
| 61 | * 返却値の共通実装 | |
| 62 | */ | |
| 63 | public static class ApiResult{ | |
| 64 | 0 | |
| 65 | 0 | public boolean r = true; |
| 66 | 0 | public String rmsg = null; |
| 67 | 0 | |
| 68 | 0 | public ApiResult() { |
| 69 | 0 | /* デフォルトコンストラクタ */ |
| 70 | 0 | } |
| 71 | 0 | public ApiResult(String rmsg) { |
| 72 | 0 | this.r = false; |
| 73 | 0 | this.rmsg = rmsg; |
| 74 | 0 | } |
| 75 | 0 | public ApiResult(Exception e) { |
| 76 | 0 | this.rmsg = e.getMessage(); |
| 77 | 0 | if (rmsg == null) { |
| 78 | 0 | this.rmsg = e.getClass().getSimpleName(); |
| 79 | 0 | } |
| 80 | 0 | } |
| 81 | 0 | public ApiResult err(String rmsg) { |
| 82 | 0 | this.r = false; |
| 83 | 0 | this.rmsg = rmsg; |
| 84 | 0 | return this; |
| 85 | } | |
| 86 | 0 | public ApiResult err(Exception e) { |
| 87 | 0 | this.rmsg = e.getMessage(); |
| 88 | 0 | if (rmsg == null) { |
| 89 | 0 | this.rmsg = e.getClass().getSimpleName(); |
| 90 | 0 | } |
| 91 | 0 | return this; |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | } | |
| 96 | //// String text = "{\"a\":123,\"b\":true,\"c\":\"あいう\"}"; | |
| 97 | //String text = readTextAsString(file.getAbsolutePath()); | |
| 98 | //ObjectMapper mapper = new ObjectMapper(); | |
| 99 | //T bean; | |
| 100 | //try { | |
| 101 | // // JsonNode root = mapper.readTree(text); | |
| 102 | // // String c = root.get("c").asText(); | |
| 103 | // bean = mapper.readValue(text, clz); | |
| 104 | //} catch (JsonProcessingException e) { | |
| 105 | // throw new RuntimeException(e); | |
| 106 | //} catch (IOException e) { | |
| 107 | // throw new RuntimeException(e); | |
| 108 | //} | |
| 109 | ||
| 110 | ||
| 111 | ///** | |
| 112 | //* 保存する | |
| 113 | //* @param bean 保存するデータ | |
| 114 | //* @return id 保存したときに付与されたid | |
| 115 | //*/ | |
| 116 | //public int insert(Object bean) { | |
| 117 | // String tablename = bean.getClass().getName(); | |
| 118 | // int maxId = -1; | |
| 119 | // File[] existfiles = getDbFiles(tablename); | |
| 120 | // for (File existfile : existfiles) { | |
| 121 | // int id = toId(existfile.getName(), tablename); | |
| 122 | // maxId = Math.max(maxId, id); | |
| 123 | // } | |
| 124 | // int insertId = maxId + 1; | |
| 125 | // ObjectMapper objectMapper = new ObjectMapper(); | |
| 126 | // try { | |
| 127 | // String json = objectMapper.writeValueAsString(bean); | |
| 128 | // File f = toFile(bean.getClass(), (insertId)); | |
| 129 | // writeText(f.getAbsolutePath(), json); | |
| 130 | // } catch (JsonProcessingException e) { | |
| 131 | // throw new RuntimeException(e); | |
| 132 | // } | |
| 133 | // return insertId; | |
| 134 | //} | |
| 135 |