1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.StringServletResponse;
20  import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
21  import com.liferay.portal.kernel.util.ContentTypes;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.security.auth.PrincipalThreadLocal;
27  import com.liferay.portal.security.permission.PermissionChecker;
28  import com.liferay.portal.security.permission.PermissionCheckerFactory;
29  import com.liferay.portal.security.permission.PermissionThreadLocal;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.util.PortalInstances;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  import com.liferay.util.xml.XMLFormatter;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="AxisServlet.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
44  
45      public void service(
46          HttpServletRequest request, HttpServletResponse response) {
47  
48          PermissionChecker permissionChecker = null;
49  
50          try {
51              PortalInstances.getCompanyId(request);
52  
53              String remoteUser = request.getRemoteUser();
54  
55              if (_log.isDebugEnabled()) {
56                  _log.debug("Remote user " + remoteUser);
57              }
58  
59              if (remoteUser != null) {
60                  PrincipalThreadLocal.setName(remoteUser);
61  
62                  long userId = GetterUtil.getLong(remoteUser);
63  
64                  User user = UserLocalServiceUtil.getUserById(userId);
65  
66                  permissionChecker = PermissionCheckerFactory.create(user, true);
67  
68                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
69              }
70  
71              StringServletResponse stringResponse = new StringServletResponse(
72                  response);
73  
74              super.service(request, stringResponse);
75  
76              String contentType = stringResponse.getContentType();
77  
78              response.setContentType(contentType);
79  
80              String content = stringResponse.getString();
81  
82              if (contentType.contains(ContentTypes.TEXT_XML)) {
83                  content = fixXml(content);
84              }
85  
86              ServletResponseUtil.write(
87                  new UncommittedServletResponse(response),
88                  content.getBytes(StringPool.UTF8));
89          }
90          catch (Exception e) {
91              _log.error(e, e);
92          }
93          finally {
94              try {
95                  PermissionCheckerFactory.recycle(permissionChecker);
96              }
97              catch (Exception e) {
98              }
99          }
100     }
101 
102     protected String fixXml(String xml) throws Exception {
103         if (xml.indexOf("<wsdl:definitions") == -1) {
104             return xml;
105         }
106 
107         xml = StringUtil.replace(
108             xml,
109             new String[] {
110                 "\r\n",
111                 "\n",
112                 "  ",
113                 "> <",
114                 _INCORRECT_LONG_ARRAY,
115                 _INCORRECT_STRING_ARRAY
116             },
117             new String[] {
118                 StringPool.BLANK,
119                 StringPool.BLANK,
120                 StringPool.BLANK,
121                 "><",
122                 _CORRECT_LONG_ARRAY,
123                 _CORRECT_STRING_ARRAY
124             });
125 
126         xml = XMLFormatter.toString(xml);
127 
128         return xml;
129     }
130 
131     private static final String _INCORRECT_LONG_ARRAY =
132         "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
133             "</simpleContent></complexType>";
134 
135     private static final String _CORRECT_LONG_ARRAY =
136         "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
137             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
138                 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
139                     "</restriction></complexContent></complexType>";
140 
141     private static final String _INCORRECT_STRING_ARRAY =
142         "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
143             "</simpleContent></complexType>";
144 
145     private static final String _CORRECT_STRING_ARRAY =
146         "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
147             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
148                 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
149                     "</restriction></complexContent></complexType>";
150 
151     private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
152 
153 }