2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.web
.mvc
;
19 import java
.io
.PrintWriter
;
20 import java
.io
.StringWriter
;
22 import javax
.servlet
.http
.HttpServletRequest
;
23 import javax
.servlet
.http
.HttpServletResponse
;
25 import org
.argeo
.slc
.msg
.ExecutionAnswer
;
26 import org
.springframework
.web
.servlet
.ModelAndView
;
27 import org
.springframework
.web
.servlet
.mvc
.AbstractController
;
29 public abstract class AbstractServiceController
extends AbstractController
{
30 public final static String KEY_ANSWER
= "__answer";
32 private String viewName
= null;
35 protected final ModelAndView
handleRequestInternal(
36 HttpServletRequest request
, HttpServletResponse response
)
38 ModelAndView mv
= new ModelAndView();
39 mv
.setViewName(getViewName());
41 handleServiceRequest(request
, response
, mv
);
42 } catch (Exception e
) {
44 StringWriter writer
= new StringWriter();
45 e
.printStackTrace(new PrintWriter(writer
));
46 ExecutionAnswer answer
= ExecutionAnswer
.error(writer
.toString());
47 ModelAndView errorMv
= new ModelAndView();
48 errorMv
.addObject(KEY_ANSWER
, answer
);
49 errorMv
.setViewName(KEY_ANSWER
);
53 if (mv
.getModel().size() == 0) {
54 mv
.addObject(KEY_ANSWER
, ExecutionAnswer
55 .ok("Execution completed properly"));
58 if (mv
.getViewName() == null && mv
.getModel().size() == 1)
59 mv
.setViewName(mv
.getModel().keySet().iterator().next().toString());
64 protected abstract void handleServiceRequest(HttpServletRequest request
,
65 HttpServletResponse response
, ModelAndView modelAndView
)
68 public void setViewName(String viewName
) {
69 this.viewName
= viewName
;
72 public String
getViewName() {