1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.ajax; |
18 |
|
|
19 |
|
import java.io.IOException; |
20 |
|
import java.io.InputStream; |
21 |
|
import java.io.InputStreamReader; |
22 |
|
import java.io.Reader; |
23 |
|
import java.io.StringWriter; |
24 |
|
import java.lang.reflect.Method; |
25 |
|
import java.util.Iterator; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
import java.util.Properties; |
29 |
|
|
30 |
|
import javax.servlet.ServletOutputStream; |
31 |
|
import javax.servlet.http.HttpServletResponse; |
32 |
|
|
33 |
|
import org.apache.velocity.VelocityContext; |
34 |
|
import org.apache.velocity.app.VelocityEngine; |
35 |
|
import org.apache.velocity.context.Context; |
36 |
|
import org.springframework.beans.BeansException; |
37 |
|
import org.springframework.beans.factory.BeanFactory; |
38 |
|
import org.springframework.beans.factory.BeanFactoryAware; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
public class AJAXServiceImpl implements AJAXService, BeanFactoryAware |
48 |
|
{ |
49 |
|
private Map serviceToBeans; |
50 |
|
private Map serviceToTemplates; |
51 |
|
|
52 |
|
private BeanFactory beanFactory; |
53 |
|
private VelocityEngine engine; |
54 |
|
|
55 |
|
public AJAXServiceImpl(Map serviceToBeans) |
56 |
0 |
{ |
57 |
0 |
this.serviceToBeans = serviceToBeans; |
58 |
0 |
} |
59 |
|
|
60 |
|
public AJAXServiceImpl(Map serviceToBeans, VelocityEngine engine, Map serviceToTemplates) |
61 |
0 |
{ |
62 |
0 |
this.serviceToBeans = serviceToBeans; |
63 |
0 |
this.engine = engine; |
64 |
0 |
this.serviceToTemplates = serviceToTemplates; |
65 |
0 |
} |
66 |
|
|
67 |
|
public AJAXResponse processRequest(AJAXRequest request) |
68 |
|
throws AJAXException |
69 |
|
{ |
70 |
0 |
final String serviceName = request.getServiceName(); |
71 |
0 |
final String methodName = request.getMethodName(); |
72 |
|
|
73 |
|
|
74 |
0 |
final String mappedServiceName = (serviceName+"."+methodName).trim(); |
75 |
|
try |
76 |
|
{ |
77 |
0 |
if(engine == null) |
78 |
|
{ |
79 |
0 |
engine = new VelocityEngine(); |
80 |
0 |
Properties props = new Properties(); |
81 |
0 |
props.load(request.getContext().getResourceAsStream("/WEB-INF/velocity.properties")); |
82 |
0 |
props.setProperty("file.resource.loader.path", request.getContext().getRealPath("/")); |
83 |
0 |
engine.init(); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
0 |
if(!serviceToBeans.containsKey(mappedServiceName)) |
88 |
|
{ |
89 |
0 |
throw new AJAXException("There is no AJAX service named '"+mappedServiceName+"' defined. "+ |
90 |
|
"Please make sure that your ajax.xml is set up correctly."); |
91 |
|
} |
92 |
|
|
93 |
0 |
String beanId = ((String)serviceToBeans.get(mappedServiceName)).trim(); |
94 |
0 |
Object targetService = beanFactory.getBean(beanId); |
95 |
0 |
final List parameters = request.getParameters(); |
96 |
0 |
Method method = targetService.getClass().getMethod(methodName, getTypes(parameters)); |
97 |
0 |
Object result = method.invoke(targetService, getValues(parameters)); |
98 |
0 |
Context context = new VelocityContext(); |
99 |
0 |
context.put("ajaxRequest", request); |
100 |
0 |
context.put("result", result); |
101 |
|
|
102 |
0 |
String templateName = ((String)serviceToTemplates.get(mappedServiceName)).trim(); |
103 |
0 |
final InputStream templateResource = request.getContext().getResourceAsStream(templateName); |
104 |
|
|
105 |
0 |
if(templateResource == null) |
106 |
|
{ |
107 |
0 |
request.getServletResponse().sendError(404, templateName+" ajax template could not be found."); |
108 |
0 |
throw new IOException(templateName+" does not exist"); |
109 |
|
} |
110 |
0 |
Reader template = new InputStreamReader(templateResource); |
111 |
|
|
112 |
0 |
StringWriter stringWriter = new StringWriter(); |
113 |
|
|
114 |
0 |
AJAXResponse ajaxResponse = new AJAXResponseImpl(context, engine, template, stringWriter); |
115 |
0 |
ajaxResponse.complete(); |
116 |
|
|
117 |
0 |
String buffer = stringWriter.getBuffer().toString(); |
118 |
|
|
119 |
|
|
120 |
0 |
HttpServletResponse response = request.getServletResponse(); |
121 |
0 |
ServletOutputStream sos = response.getOutputStream(); |
122 |
0 |
sos.print(buffer); |
123 |
0 |
sos.flush(); |
124 |
0 |
return ajaxResponse; |
125 |
|
} |
126 |
0 |
catch(AJAXException ae) |
127 |
|
{ |
128 |
0 |
throw ae; |
129 |
|
} |
130 |
0 |
catch (Exception e) |
131 |
|
{ |
132 |
0 |
throw new AJAXException("Unable to process service" + mappedServiceName + ": " + e.getMessage(), e); |
133 |
|
} |
134 |
|
|
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException |
143 |
|
{ |
144 |
0 |
this.beanFactory = beanFactory; |
145 |
0 |
} |
146 |
|
|
147 |
|
protected Class[] getTypes(List objects) |
148 |
|
{ |
149 |
0 |
Class[] args = new Class[objects.size()]; |
150 |
0 |
Iterator itr = objects.iterator(); |
151 |
0 |
int i = 0; |
152 |
0 |
while (itr.hasNext()) |
153 |
|
{ |
154 |
0 |
args[i] = ((AJAXRequestImpl.AJAXParameter)itr.next()).getValue().getClass(); |
155 |
0 |
i++; |
156 |
|
} |
157 |
0 |
return args; |
158 |
|
} |
159 |
|
|
160 |
|
protected Object[] getValues(List objects) |
161 |
|
{ |
162 |
0 |
Object[] args = new Object[objects.size()]; |
163 |
0 |
Iterator itr = objects.iterator(); |
164 |
0 |
int i = 0; |
165 |
0 |
while (itr.hasNext()) |
166 |
|
{ |
167 |
0 |
args[i] = ((AJAXRequestImpl.AJAXParameter)itr.next()).getValue(); |
168 |
0 |
i++; |
169 |
|
} |
170 |
0 |
return args; |
171 |
|
} |
172 |
|
|
173 |
|
} |