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
.slc
.web
.ajaxplorer
.mvc
;
18 import java
.io
.IOException
;
19 import java
.util
.Enumeration
;
22 import javax
.servlet
.ServletConfig
;
23 import javax
.servlet
.ServletException
;
24 import javax
.servlet
.http
.HttpServletRequest
;
25 import javax
.servlet
.http
.HttpServletResponse
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.slc
.web
.ajaxplorer
.AjxpAnswer
;
30 import org
.argeo
.slc
.web
.ajaxplorer
.AjxpDriver
;
31 import org
.springframework
.beans
.BeanWrapper
;
32 import org
.springframework
.beans
.BeanWrapperImpl
;
33 import org
.springframework
.beans
.BeansException
;
34 import org
.springframework
.web
.context
.WebApplicationContext
;
35 import org
.springframework
.web
.context
.support
.WebApplicationContextUtils
;
36 import org
.springframework
.web
.servlet
.HttpServletBean
;
38 public class AjxpDriverServlet
extends HttpServletBean
{
39 static final long serialVersionUID
= 1l;
41 protected final Log log
= LogFactory
.getLog(getClass());
42 private String driverBean
;
43 private AjxpDriver driver
;
46 public void init(ServletConfig sc
) throws ServletException
{
48 WebApplicationContext context
= WebApplicationContextUtils
49 .getRequiredWebApplicationContext(sc
.getServletContext());
50 driverBean
= sc
.getInitParameter("driverBean");
51 if (driverBean
== null) {
52 throw new ServletException(
53 "No driver found, please set the driverBean property");
56 logger
.info("Loading driver " + driverBean
);
57 driver
= (AjxpDriver
) context
.getBean(driverBean
);
59 // overrideBeanProperties(sc);
63 protected void doGet(HttpServletRequest req
, HttpServletResponse resp
)
64 throws ServletException
, IOException
{
65 processRequest("GET", req
, resp
);
69 protected void doPost(HttpServletRequest req
, HttpServletResponse resp
)
70 throws ServletException
, IOException
{
71 processRequest("POST", req
, resp
);
74 protected void processRequest(String method
, HttpServletRequest req
,
75 HttpServletResponse resp
) throws ServletException
, IOException
{
76 long id
= System
.currentTimeMillis();
78 if (log
.isDebugEnabled())
79 logRequest(id
, method
, req
);
81 AjxpAnswer answer
= driver
.executeAction(req
);
82 answer
.updateResponse(resp
);
84 if (log
.isDebugEnabled())
85 log
.debug(id
+ " " + method
+ " completed");
86 } catch (Exception e
) {
87 log
.error(id
+ " Cannot process request.", e
);
88 throw new ServletException("Cannot process request " + id
, e
);
93 public void setDriverBean(String driverName
) {
94 this.driverBean
= driverName
;
97 protected void logRequest(long id
, String method
, HttpServletRequest req
) {
98 if (log
.isDebugEnabled()) {
99 StringBuffer buf
= new StringBuffer(id
+ " Received " + method
102 Map
<String
, String
[]> params
= req
.getParameterMap();
104 for (Map
.Entry
<String
, String
[]> entry
: params
.entrySet()) {
107 buf
.append(entry
.getKey()).append("={");
109 for (String value
: entry
.getValue()) {
119 log
.debug(buf
.toString());
123 protected void overrideBeanProperties(ServletConfig sc
)
124 throws ServletException
{
125 BeanWrapper wrapper
= new BeanWrapperImpl(driver
);
126 Enumeration
<String
> en
= sc
.getInitParameterNames();
127 while (en
.hasMoreElements()) {
128 String name
= en
.nextElement();
129 if (name
.indexOf(driverBean
+ '.') == 0
130 && name
.length() > (driverBean
.length() + 1)) {
131 String propertyName
= name
.substring(driverBean
.length() + 1);
132 String value
= sc
.getInitParameter(name
);
135 wrapper
.setPropertyValue(propertyName
, value
);
136 } catch (BeansException e
) {
137 throw new ServletException("Cannot set property "
138 + propertyName
+ " of bean " + driverBean
, e
);