1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ws; |
17 | |
|
18 | |
import java.util.Enumeration; |
19 | |
import java.util.Properties; |
20 | |
|
21 | |
import javax.servlet.Servlet; |
22 | |
import javax.servlet.ServletConfig; |
23 | |
import javax.servlet.ServletContext; |
24 | |
import javax.servlet.http.HttpServletRequest; |
25 | |
import javax.servlet.http.HttpServletResponse; |
26 | |
|
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.springframework.beans.factory.BeanNameAware; |
29 | |
import org.springframework.beans.factory.DisposableBean; |
30 | |
import org.springframework.beans.factory.InitializingBean; |
31 | |
import org.springframework.web.servlet.ModelAndView; |
32 | |
import org.springframework.web.servlet.mvc.AbstractController; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class ServletWrappingController extends AbstractController implements |
41 | |
BeanNameAware, InitializingBean, DisposableBean { |
42 | |
private Class<? extends Servlet> servletClass; |
43 | |
private String servletName; |
44 | 0 | private Properties initParameters = new Properties(); |
45 | |
private String beanName; |
46 | |
private Servlet servletInstance; |
47 | |
|
48 | 0 | private static org.apache.log4j.Logger log = Logger.getLogger(ServletWrappingController.class); |
49 | |
|
50 | |
|
51 | |
public void setServletClass(Class<? extends Servlet> servletClass) { |
52 | 0 | log.info("setServletClass : " + servletClass); |
53 | 0 | this.servletClass = servletClass; |
54 | 0 | } |
55 | |
|
56 | |
public void setServletName(String servletName) { |
57 | 0 | log.info("setServletName : " + servletName); |
58 | 0 | this.servletName = servletName; |
59 | 0 | } |
60 | |
|
61 | |
public void setInitParameters(Properties initParameters) { |
62 | 0 | log.info("setInitParameters : " + initParameters); |
63 | 0 | this.initParameters = initParameters; |
64 | 0 | } |
65 | |
|
66 | |
public void setBeanName(String name) { |
67 | 0 | log.info("setBeanName : " + name); |
68 | 0 | this.beanName = name; |
69 | 0 | } |
70 | |
|
71 | |
public void setServletInstance(Servlet servletInstance) { |
72 | 0 | log.info("setServletInstance : " + servletInstance); |
73 | 0 | this.servletInstance = servletInstance; |
74 | 0 | } |
75 | |
|
76 | |
public void afterPropertiesSet() throws Exception { |
77 | 0 | log.info("afterPropertiesSet"); |
78 | 0 | if (this.servletInstance == null) { |
79 | 0 | throw new IllegalArgumentException("servletInstance is required"); |
80 | |
} |
81 | 0 | if (!Servlet.class.isAssignableFrom(servletInstance.getClass())) { |
82 | 0 | throw new IllegalArgumentException("servletInstance [" |
83 | |
+ this.servletClass.getName() |
84 | |
+ "] needs to implement interface [javax.servlet.Servlet]"); |
85 | |
} |
86 | 0 | if (this.servletName == null) { |
87 | 0 | this.servletName = this.beanName; |
88 | |
} |
89 | 0 | this.servletInstance.init(new DelegatingServletConfig()); |
90 | 0 | } |
91 | |
|
92 | |
protected ModelAndView handleRequestInternal(HttpServletRequest request, |
93 | |
HttpServletResponse response) throws Exception { |
94 | 0 | log.info("handleRequestInternal : " + servletName); |
95 | |
try{ |
96 | 0 | this.servletInstance.service(request, response); |
97 | 0 | }catch(Exception e){ |
98 | 0 | log.error(e.getMessage()); |
99 | 0 | throw e; |
100 | 0 | } |
101 | |
|
102 | 0 | return null; |
103 | |
} |
104 | |
|
105 | |
public void destroy() { |
106 | 0 | log.info("destroy : " + servletName); |
107 | 0 | this.servletInstance.destroy(); |
108 | 0 | } |
109 | |
|
110 | 0 | private class DelegatingServletConfig implements ServletConfig { |
111 | |
public String getServletName() { |
112 | 0 | return servletName; |
113 | |
} |
114 | |
|
115 | |
public ServletContext getServletContext() { |
116 | 0 | return getWebApplicationContext().getServletContext(); |
117 | |
} |
118 | |
|
119 | |
public String getInitParameter(String paramName) { |
120 | 0 | return initParameters.getProperty(paramName); |
121 | |
|
122 | |
} |
123 | |
|
124 | |
public Enumeration<?> getInitParameterNames() { |
125 | 0 | return initParameters.keys(); |
126 | |
} |
127 | |
} |
128 | |
} |