]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/json/mvc/JsonView.java
Prepare next development cycle
[lgpl/argeo-commons.git] / server / json / mvc / JsonView.java
1 package org.argeo.slc.web.mvc;
2
3 import java.util.Map;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.springframework.web.servlet.view.AbstractView;
9
10 /** Marshal one of the object of the map to the output. */
11 public class JsonView extends AbstractView {
12 private String modelKey = null;
13
14 public JsonView() {
15 }
16
17 @Override
18 @SuppressWarnings(value = { "unchecked" })
19 protected void renderMergedOutputModel(Map model,
20 HttpServletRequest request, HttpServletResponse response)
21 throws Exception {
22 final Object answer;
23 if (modelKey != null) {
24 if (!model.containsKey(modelKey))
25 throw new SlcException("Key " + modelKey
26 + " not found in model.");
27 answer = model.get(modelKey);
28 } else {
29 if (model.size() != 1)
30 throw new SlcException(
31 "Model has a size different from 1. Specify a modelKey.");
32 answer = model.values().iterator().next();
33 }
34
35 if (answer instanceof JSONObject) {
36 ((JSONObject) answer).write(response.getWriter());
37 } else {
38 JSONObject jsonObject = new JSONObject(answer);
39 jsonObject.write(response.getWriter());
40 }
41 }
42
43 public void setModelKey(String modelKey) {
44 this.modelKey = modelKey;
45 }
46
47 }