2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.server
.mvc
;
18 import java
.util
.Locale
;
21 import javax
.servlet
.http
.HttpServletRequest
;
22 import javax
.servlet
.http
.HttpServletResponse
;
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
;
32 * Can be used as a standalone {@link View} or using
33 * {@link SerializingViewResolver}
35 public class SerializingView
extends AbstractView
implements MvcConstants
{
36 private final String viewName
;
37 private final Locale locale
;
39 private ServerSerializer serializer
;
41 public SerializingView() {
43 this.locale
= Locale
.getDefault();
46 public SerializingView(String viewName
, Locale locale
,
47 ServerSerializer serializer
) {
48 this.viewName
= viewName
;
50 this.serializer
= serializer
;
53 @SuppressWarnings( { "restriction", "rawtypes" })
55 protected void renderMergedOutputModel(Map model
,
56 HttpServletRequest request
, HttpServletResponse response
)
58 Boolean serverAnswersAsHtml
= model
59 .containsKey(ANSWER_MODEL_KEY_AS_HTML
);
61 final Object answer
= findAnswerInModel(model
);
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>");
70 serializer
.serialize(answer
, request
, response
);
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;
88 if (otherIsBindingResult
)
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
);
99 if (model
.size() == 0)
100 throw new ArgeoException("Model is empty.");
102 throw new ArgeoException(
103 "Model has a size different from 1. Specify a modelKey.");
107 public String
getViewName() {
111 public Locale
getLocale() {
115 public void setSerializer(ServerSerializer serializer
) {
116 this.serializer
= serializer
;