]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.core/src/main/java/org/argeo/server/mvc/SerializingView.java
Make properties writable
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.core / src / main / java / org / argeo / server / mvc / SerializingView.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.server.mvc;
17
18 import java.util.Locale;
19 import java.util.Map;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.argeo.ArgeoException;
25 import org.argeo.server.ServerAnswer;
26 import org.argeo.server.ServerSerializer;
27 import org.springframework.validation.BindingResult;
28 import org.springframework.web.servlet.View;
29 import org.springframework.web.servlet.view.AbstractView;
30
31 /**
32 * Can be used as a standalone {@link View} or using
33 * {@link SerializingViewResolver}
34 */
35 public class SerializingView extends AbstractView implements MvcConstants {
36 private final String viewName;
37 private final Locale locale;
38
39 private ServerSerializer serializer;
40
41 public SerializingView() {
42 this.viewName = null;
43 this.locale = Locale.getDefault();
44 }
45
46 public SerializingView(String viewName, Locale locale,
47 ServerSerializer serializer) {
48 this.viewName = viewName;
49 this.locale = locale;
50 this.serializer = serializer;
51 }
52
53 @SuppressWarnings({ "rawtypes" })
54 @Override
55 protected void renderMergedOutputModel(Map model,
56 HttpServletRequest request, HttpServletResponse response)
57 throws Exception {
58 Boolean serverAnswersAsHtml = model
59 .containsKey(ANSWER_MODEL_KEY_AS_HTML);
60
61 final Object answer = findAnswerInModel(model);
62
63 if ((answer instanceof ServerAnswer) && serverAnswersAsHtml) {
64 response.setContentType("text/html");
65 ServerAnswer serverAnswer = (ServerAnswer) answer;
66 response.getWriter().append("<pre>");
67 response.getWriter().append(serverAnswer.getMessage());
68 response.getWriter().append("</pre>");
69 } else {
70 serializer.serialize(answer, request, response);
71 }
72 }
73
74 @SuppressWarnings("rawtypes")
75 protected Object findAnswerInModel(Map model) {
76 if (model.size() == 1) {
77 return model.values().iterator().next();
78 } else if (model.size() == 2) {
79 boolean otherIsBindingResult = false;
80 Object answerValue = null;
81 for (Object value : model.values()) {
82 if (value instanceof BindingResult)
83 otherIsBindingResult = true;
84 else
85 answerValue = value;
86 }
87
88 if (otherIsBindingResult)
89 return answerValue;
90 }
91
92 if (model.containsKey(ANSWER_MODEL_KEY)) {
93 return model.get(ANSWER_MODEL_KEY);
94 } else if (model.containsKey(ANSWER_MODEL_KEY_AS_HTML)) {
95 return model.get(ANSWER_MODEL_KEY_AS_HTML);
96 } else if (viewName != null && model.containsKey(viewName)) {
97 return model.get(viewName);
98 } else {
99 if (model.size() == 0)
100 throw new ArgeoException("Model is empty.");
101 else
102 throw new ArgeoException(
103 "Model has a size different from 1. Specify a modelKey.");
104 }
105 }
106
107 public String getViewName() {
108 return viewName;
109 }
110
111 public Locale getLocale() {
112 return locale;
113 }
114
115 public void setSerializer(ServerSerializer serializer) {
116 this.serializer = serializer;
117 }
118
119 }