001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.servlet.PortletServlet;
020 import com.liferay.portal.kernel.servlet.StringServletResponse;
021 import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
022 import com.liferay.portal.kernel.util.ContentTypes;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.model.User;
027 import com.liferay.portal.security.auth.PrincipalThreadLocal;
028 import com.liferay.portal.security.permission.PermissionChecker;
029 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
030 import com.liferay.portal.security.permission.PermissionThreadLocal;
031 import com.liferay.portal.service.UserLocalServiceUtil;
032 import com.liferay.portal.util.PortalInstances;
033 import com.liferay.util.servlet.ServletResponseUtil;
034 import com.liferay.util.xml.XMLFormatter;
035
036 import javax.servlet.ServletConfig;
037 import javax.servlet.ServletContext;
038 import javax.servlet.ServletException;
039 import javax.servlet.http.HttpServletRequest;
040 import javax.servlet.http.HttpServletResponse;
041
042
045 public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
046
047 public void init(ServletConfig servletConfig) throws ServletException {
048 ServletContext servletContext = servletConfig.getServletContext();
049
050 _portletClassLoader = (ClassLoader)servletContext.getAttribute(
051 PortletServlet.PORTLET_CLASS_LOADER);
052
053 if (_portletClassLoader == null) {
054 super.init(servletConfig);
055 }
056 else {
057 Thread currentThread = Thread.currentThread();
058
059 ClassLoader contextClassLoader =
060 currentThread.getContextClassLoader();
061
062 try {
063 currentThread.setContextClassLoader(_portletClassLoader);
064
065 super.init(servletConfig);
066 }
067 finally {
068 currentThread.setContextClassLoader(contextClassLoader);
069 }
070 }
071 }
072
073 public void service(
074 HttpServletRequest request, HttpServletResponse response) {
075
076 try {
077 PortalInstances.getCompanyId(request);
078
079 String remoteUser = request.getRemoteUser();
080
081 if (_log.isDebugEnabled()) {
082 _log.debug("Remote user " + remoteUser);
083 }
084
085 if (remoteUser != null) {
086 PrincipalThreadLocal.setName(remoteUser);
087
088 long userId = GetterUtil.getLong(remoteUser);
089
090 User user = UserLocalServiceUtil.getUserById(userId);
091
092 PermissionChecker permissionChecker =
093 PermissionCheckerFactoryUtil.create(user, true);
094
095 PermissionThreadLocal.setPermissionChecker(permissionChecker);
096 }
097
098 StringServletResponse stringResponse = new StringServletResponse(
099 response);
100
101 if (_portletClassLoader == null) {
102 super.service(request, stringResponse);
103 }
104 else {
105 Thread currentThread = Thread.currentThread();
106
107 ClassLoader contextClassLoader =
108 currentThread.getContextClassLoader();
109
110 try {
111 currentThread.setContextClassLoader(_portletClassLoader);
112
113 super.service(request, stringResponse);
114 }
115 finally {
116 currentThread.setContextClassLoader(contextClassLoader);
117 }
118 }
119
120 String contentType = stringResponse.getContentType();
121
122 response.setContentType(contentType);
123
124 String content = stringResponse.getString();
125
126 if (contentType.contains(ContentTypes.TEXT_XML)) {
127 content = fixXml(content);
128 }
129
130 ServletResponseUtil.write(
131 new UncommittedServletResponse(response),
132 content.getBytes(StringPool.UTF8));
133 }
134 catch (Exception e) {
135 _log.error(e, e);
136 }
137 }
138
139 protected String fixXml(String xml) throws Exception {
140 if (xml.indexOf("<wsdl:definitions") == -1) {
141 return xml;
142 }
143
144 xml = StringUtil.replace(
145 xml,
146 new String[] {
147 "\r\n",
148 "\n",
149 " ",
150 "> <",
151 _INCORRECT_LONG_ARRAY,
152 _INCORRECT_STRING_ARRAY
153 },
154 new String[] {
155 StringPool.BLANK,
156 StringPool.BLANK,
157 StringPool.BLANK,
158 "><",
159 _CORRECT_LONG_ARRAY,
160 _CORRECT_STRING_ARRAY
161 });
162
163 xml = XMLFormatter.toString(xml);
164
165 return xml;
166 }
167
168 private static final String _INCORRECT_LONG_ARRAY =
169 "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
170 "</simpleContent></complexType>";
171
172 private static final String _CORRECT_LONG_ARRAY =
173 "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
174 "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
175 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
176 "</restriction></complexContent></complexType>";
177
178 private static final String _INCORRECT_STRING_ARRAY =
179 "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
180 "</simpleContent></complexType>";
181
182 private static final String _CORRECT_STRING_ARRAY =
183 "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
184 "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
185 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
186 "</restriction></complexContent></complexType>";
187
188 private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
189
190 private ClassLoader _portletClassLoader;
191
192 }