1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.PortletServlet;
20  import com.liferay.portal.kernel.servlet.StringServletResponse;
21  import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
22  import com.liferay.portal.kernel.util.ContentTypes;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.security.auth.PrincipalThreadLocal;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
30  import com.liferay.portal.security.permission.PermissionThreadLocal;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  import com.liferay.portal.util.PortalInstances;
33  import com.liferay.util.servlet.ServletResponseUtil;
34  import com.liferay.util.xml.XMLFormatter;
35  
36  import javax.servlet.ServletConfig;
37  import javax.servlet.ServletContext;
38  import javax.servlet.ServletException;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  /**
43   * <a href="AxisServlet.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
48  
49      public void init(ServletConfig servletConfig) throws ServletException {
50          ServletContext servletContext = servletConfig.getServletContext();
51  
52          _portletClassLoader = (ClassLoader)servletContext.getAttribute(
53              PortletServlet.PORTLET_CLASS_LOADER);
54  
55          if (_portletClassLoader == null) {
56              super.init(servletConfig);
57          }
58          else {
59              Thread currentThread = Thread.currentThread();
60  
61              ClassLoader contextClassLoader =
62                  currentThread.getContextClassLoader();
63  
64              try {
65                  currentThread.setContextClassLoader(_portletClassLoader);
66  
67                  super.init(servletConfig);
68              }
69              finally {
70                  currentThread.setContextClassLoader(contextClassLoader);
71              }
72          }
73      }
74  
75      public void service(
76          HttpServletRequest request, HttpServletResponse response) {
77  
78          try {
79              PortalInstances.getCompanyId(request);
80  
81              String remoteUser = request.getRemoteUser();
82  
83              if (_log.isDebugEnabled()) {
84                  _log.debug("Remote user " + remoteUser);
85              }
86  
87              if (remoteUser != null) {
88                  PrincipalThreadLocal.setName(remoteUser);
89  
90                  long userId = GetterUtil.getLong(remoteUser);
91  
92                  User user = UserLocalServiceUtil.getUserById(userId);
93  
94                  PermissionChecker permissionChecker =
95                      PermissionCheckerFactoryUtil.create(user, true);
96  
97                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
98              }
99  
100             StringServletResponse stringResponse = new StringServletResponse(
101                 response);
102 
103             if (_portletClassLoader == null) {
104                 super.service(request, stringResponse);
105             }
106             else {
107                 Thread currentThread = Thread.currentThread();
108 
109                 ClassLoader contextClassLoader =
110                     currentThread.getContextClassLoader();
111 
112                 try {
113                     currentThread.setContextClassLoader(_portletClassLoader);
114 
115                     super.service(request, stringResponse);
116                 }
117                 finally {
118                     currentThread.setContextClassLoader(contextClassLoader);
119                 }
120             }
121 
122             String contentType = stringResponse.getContentType();
123 
124             response.setContentType(contentType);
125 
126             String content = stringResponse.getString();
127 
128             if (contentType.contains(ContentTypes.TEXT_XML)) {
129                 content = fixXml(content);
130             }
131 
132             ServletResponseUtil.write(
133                 new UncommittedServletResponse(response),
134                 content.getBytes(StringPool.UTF8));
135         }
136         catch (Exception e) {
137             _log.error(e, e);
138         }
139     }
140 
141     protected String fixXml(String xml) throws Exception {
142         if (xml.indexOf("<wsdl:definitions") == -1) {
143             return xml;
144         }
145 
146         xml = StringUtil.replace(
147             xml,
148             new String[] {
149                 "\r\n",
150                 "\n",
151                 "  ",
152                 "> <",
153                 _INCORRECT_LONG_ARRAY,
154                 _INCORRECT_STRING_ARRAY
155             },
156             new String[] {
157                 StringPool.BLANK,
158                 StringPool.BLANK,
159                 StringPool.BLANK,
160                 "><",
161                 _CORRECT_LONG_ARRAY,
162                 _CORRECT_STRING_ARRAY
163             });
164 
165         xml = XMLFormatter.toString(xml);
166 
167         return xml;
168     }
169 
170     private static final String _INCORRECT_LONG_ARRAY =
171         "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
172             "</simpleContent></complexType>";
173 
174     private static final String _CORRECT_LONG_ARRAY =
175         "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
176             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
177                 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
178                     "</restriction></complexContent></complexType>";
179 
180     private static final String _INCORRECT_STRING_ARRAY =
181         "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
182             "</simpleContent></complexType>";
183 
184     private static final String _CORRECT_STRING_ARRAY =
185         "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
186             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
187                 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
188                     "</restriction></complexContent></complexType>";
189 
190     private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
191 
192     private ClassLoader _portletClassLoader;
193 
194 }