]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/ajaxplorer/mvc/AjxpDriverServlet.java
Update headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / ajaxplorer / mvc / AjxpDriverServlet.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.slc.web.ajaxplorer.mvc;
17
18 import java.io.IOException;
19 import java.util.Enumeration;
20 import java.util.Map;
21
22 import javax.servlet.ServletConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
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;
37
38 public class AjxpDriverServlet extends HttpServletBean {
39 static final long serialVersionUID = 1l;
40
41 protected final Log log = LogFactory.getLog(getClass());
42 private String driverBean;
43 private AjxpDriver driver;
44
45 @Override
46 public void init(ServletConfig sc) throws ServletException {
47 super.init(sc);
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");
54 }
55
56 logger.info("Loading driver " + driverBean);
57 driver = (AjxpDriver) context.getBean(driverBean);
58
59 // overrideBeanProperties(sc);
60 }
61
62 @Override
63 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
64 throws ServletException, IOException {
65 processRequest("GET", req, resp);
66 }
67
68 @Override
69 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
70 throws ServletException, IOException {
71 processRequest("POST", req, resp);
72 }
73
74 protected void processRequest(String method, HttpServletRequest req,
75 HttpServletResponse resp) throws ServletException, IOException {
76 long id = System.currentTimeMillis();
77 try {
78 if (log.isDebugEnabled())
79 logRequest(id, method, req);
80
81 AjxpAnswer answer = driver.executeAction(req);
82 answer.updateResponse(resp);
83
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);
89 }
90
91 }
92
93 public void setDriverBean(String driverName) {
94 this.driverBean = driverName;
95 }
96
97 protected void logRequest(long id, String method, HttpServletRequest req) {
98 if (log.isDebugEnabled()) {
99 StringBuffer buf = new StringBuffer(id + " Received " + method
100 + ": ");
101 buf.append('{');
102 Map<String, String[]> params = req.getParameterMap();
103 int count1 = 0;
104 for (Map.Entry<String, String[]> entry : params.entrySet()) {
105 if (count1 != 0)
106 buf.append(", ");
107 buf.append(entry.getKey()).append("={");
108 int count2 = 0;
109 for (String value : entry.getValue()) {
110 if (count2 != 0)
111 buf.append(',');
112 buf.append(value);
113 count2++;
114 }
115 buf.append('}');
116 count1++;
117 }
118 buf.append('}');
119 log.debug(buf.toString());
120 }
121 }
122
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);
133 if (value != null) {
134 try {
135 wrapper.setPropertyValue(propertyName, value);
136 } catch (BeansException e) {
137 throw new ServletException("Cannot set property "
138 + propertyName + " of bean " + driverBean, e);
139 }
140 }
141 }
142 }
143 }
144 }