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.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.PermissionCheckerFactoryUtil;
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          try {
49              PortalInstances.getCompanyId(request);
50  
51              String remoteUser = request.getRemoteUser();
52  
53              if (_log.isDebugEnabled()) {
54                  _log.debug("Remote user " + remoteUser);
55              }
56  
57              if (remoteUser != null) {
58                  PrincipalThreadLocal.setName(remoteUser);
59  
60                  long userId = GetterUtil.getLong(remoteUser);
61  
62                  User user = UserLocalServiceUtil.getUserById(userId);
63  
64                  PermissionChecker permissionChecker =
65                      PermissionCheckerFactoryUtil.create(user, true);
66  
67                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
68              }
69  
70              StringServletResponse stringResponse = new StringServletResponse(
71                  response);
72  
73              super.service(request, stringResponse);
74  
75              String contentType = stringResponse.getContentType();
76  
77              response.setContentType(contentType);
78  
79              String content = stringResponse.getString();
80  
81              if (contentType.contains(ContentTypes.TEXT_XML)) {
82                  content = fixXml(content);
83              }
84  
85              ServletResponseUtil.write(
86                  new UncommittedServletResponse(response),
87                  content.getBytes(StringPool.UTF8));
88          }
89          catch (Exception e) {
90              _log.error(e, e);
91          }
92      }
93  
94      protected String fixXml(String xml) throws Exception {
95          if (xml.indexOf("<wsdl:definitions") == -1) {
96              return xml;
97          }
98  
99          xml = StringUtil.replace(
100             xml,
101             new String[] {
102                 "\r\n",
103                 "\n",
104                 "  ",
105                 "> <",
106                 _INCORRECT_LONG_ARRAY,
107                 _INCORRECT_STRING_ARRAY
108             },
109             new String[] {
110                 StringPool.BLANK,
111                 StringPool.BLANK,
112                 StringPool.BLANK,
113                 "><",
114                 _CORRECT_LONG_ARRAY,
115                 _CORRECT_STRING_ARRAY
116             });
117 
118         xml = XMLFormatter.toString(xml);
119 
120         return xml;
121     }
122 
123     private static final String _INCORRECT_LONG_ARRAY =
124         "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
125             "</simpleContent></complexType>";
126 
127     private static final String _CORRECT_LONG_ARRAY =
128         "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
129             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
130                 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
131                     "</restriction></complexContent></complexType>";
132 
133     private static final String _INCORRECT_STRING_ARRAY =
134         "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
135             "</simpleContent></complexType>";
136 
137     private static final String _CORRECT_STRING_ARRAY =
138         "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
139             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
140                 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
141                     "</restriction></complexContent></complexType>";
142 
143     private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
144 
145 }