forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxisServlet.java
337 lines (263 loc) · 8.27 KB
/
AxisServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.util.axis;
import com.liferay.portal.kernel.exception.LoggedExceptionInInitializerError;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ReflectionUtil;
import com.liferay.portal.kernel.util.ServerDetector;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.xml.Document;
import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
import java.io.IOException;
import java.lang.reflect.Field;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.axis.utils.cache.MethodCache;
/**
* @author Brian Wing Shun Chan
*/
public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
@Override
public void destroy() {
if (ServerDetector.isWebLogic()) {
doDestroy();
}
else {
DestroyThread destroyThread = new DestroyThread();
destroyThread.start();
try {
destroyThread.join();
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
Exception e = destroyThread.getException();
if (e != null) {
if (e instanceof RuntimeException) {
throw (RuntimeException)e;
}
else {
throw new RuntimeException(e);
}
}
}
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
_servletConfig = servletConfig;
StringBundler sb = new StringBundler(9);
sb.append("<complexType abstract=\"true\" name=\"OrderByComparator\">");
sb.append("<sequence><element name=\"ascending\" ");
sb.append("type=\"xsd:boolean\"/><element name=\"orderBy\" ");
sb.append("nillable=\"true\" type=\"soapenc:string\"/><element ");
sb.append("name=\"orderByConditionFields\" nillable=\"true\" ");
sb.append("type=\"impl:ArrayOf_xsd_string\"/><element ");
sb.append("name=\"orderByFields\" nillable=\"true\" ");
sb.append("type=\"impl:ArrayOf_xsd_string\"/></sequence>");
sb.append("</complexType>");
_correctOrderByComparator = sb.toString();
sb = new StringBundler(5);
sb.append("<complexType name=\"ArrayOf_xsd_long\"><complexContent>");
sb.append("<restriction base=\"soapenc:Array\"><attribute ");
sb.append("ref=\"soapenc:arrayType\" ");
sb.append("wsdl:arrayType=\"soapenc:long[]\"/></restriction>");
sb.append("</complexContent></complexType>");
_correctLongArray = sb.toString();
sb = new StringBundler(5);
sb.append("<complexType name=\"ArrayOf_xsd_string\"><complexContent>");
sb.append("<restriction base=\"soapenc:Array\"><attribute ");
sb.append("ref=\"soapenc:arrayType\" ");
sb.append("wsdl:arrayType=\"soapenc:string[]\"/></restriction>");
sb.append("</complexContent></complexType>");
_correctStringArray = sb.toString();
sb = new StringBundler(2);
sb.append("<complexType name=\"OrderByComparator\"><simpleContent>");
sb.append("<extension/></simpleContent></complexType>");
_incorrectOrderByComparator = sb.toString();
sb = new StringBundler(2);
sb.append("<complexType name=\"ArrayOf_xsd_long\"><simpleContent>");
sb.append("<extension/></simpleContent></complexType>");
_incorrectLongArray = sb.toString();
sb = new StringBundler(2);
sb.append("<complexType name=\"ArrayOf_xsd_string\"><simpleContent>");
sb.append("<extension/></simpleContent></complexType>");
_incorrectStringArray = sb.toString();
if (ServerDetector.isResin() || ServerDetector.isWebLogic()) {
doInit();
}
else {
InitThread initThread = new InitThread();
initThread.start();
try {
initThread.join();
}
catch (InterruptedException ie) {
throw new ServletException(ie);
}
Exception e = initThread.getException();
if (e != null) {
if (e instanceof ServletException) {
throw (ServletException)e;
}
else {
throw new ServletException(e);
}
}
}
}
@Override
public void service(
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
if (!_ready) {
return;
}
BufferCacheServletResponse bufferCacheServletResponse =
new BufferCacheServletResponse(response);
super.service(request, bufferCacheServletResponse);
String contentType = bufferCacheServletResponse.getContentType();
response.setContentType(contentType);
String content = bufferCacheServletResponse.getString();
if (_fixContent) {
if (contentType.contains(ContentTypes.TEXT_HTML)) {
content = _HTML_TOP_WRAPPER.concat(content).concat(
_HTML_BOTTOM_WRAPPER);
}
else if (contentType.contains(ContentTypes.TEXT_XML)) {
content = fixXml(content);
}
}
ServletResponseUtil.write(
new UncommittedServletResponse(response),
content.getBytes(StringPool.UTF8));
}
catch (IOException ioe) {
throw ioe;
}
catch (ServletException se) {
throw se;
}
catch (Exception e) {
throw new ServletException(e);
}
finally {
try {
ThreadLocal<?> cache = (ThreadLocal<?>)_CACHE_FIELD.get(null);
if (cache != null) {
cache.remove();
}
}
catch (Exception e) {
_log.error(e, e);
}
}
}
protected void doDestroy() {
_ready = false;
super.destroy();
}
protected void doInit() throws ServletException {
super.init(_servletConfig);
_fixContent = GetterUtil.getBoolean(
_servletConfig.getInitParameter("fix-content"), true);
_ready = true;
}
protected String fixXml(String xml) throws Exception {
if (!xml.contains("<wsdl:definitions")) {
return xml;
}
xml = StringUtil.replace(
xml,
new String[] {
"\r\n", "\n", " ", "> <", _incorrectOrderByComparator,
_incorrectLongArray, _incorrectStringArray
},
new String[] {
StringPool.BLANK, StringPool.BLANK, StringPool.BLANK, "><",
_correctOrderByComparator, _correctLongArray,
_correctStringArray
});
Document document = UnsecureSAXReaderUtil.read(xml);
return document.formattedString();
}
private static final Field _CACHE_FIELD;
private static final String _HTML_BOTTOM_WRAPPER = "</body></html>";
private static final String _HTML_TOP_WRAPPER = "<html><body>";
private static final Log _log = LogFactoryUtil.getLog(AxisServlet.class);
static {
try {
_CACHE_FIELD = ReflectionUtil.getDeclaredField(
MethodCache.class, "cache");
}
catch (Exception e) {
throw new LoggedExceptionInInitializerError(e);
}
}
private String _correctLongArray;
private String _correctOrderByComparator;
private String _correctStringArray;
private boolean _fixContent;
private String _incorrectLongArray;
private String _incorrectOrderByComparator;
private String _incorrectStringArray;
private boolean _ready;
private ServletConfig _servletConfig;
private class DestroyThread extends Thread {
public DestroyThread() {
setDaemon(true);
}
public Exception getException() {
return _exception;
}
@Override
public void run() {
try {
doDestroy();
}
catch (Exception e) {
_exception = e;
}
}
private Exception _exception;
}
private class InitThread extends Thread {
public InitThread() {
setDaemon(true);
}
public Exception getException() {
return _exception;
}
@Override
public void run() {
try {
doInit();
}
catch (Exception e) {
_exception = e;
}
}
private Exception _exception;
}
}