1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.StringServletResponse;
28  import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.security.auth.PrincipalThreadLocal;
35  import com.liferay.portal.security.permission.PermissionChecker;
36  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
37  import com.liferay.portal.security.permission.PermissionThreadLocal;
38  import com.liferay.portal.service.UserLocalServiceUtil;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  import com.liferay.util.xml.XMLFormatter;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="AxisServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
52  
53      public void service(
54          HttpServletRequest request, HttpServletResponse response) {
55  
56          try {
57              String remoteUser = request.getRemoteUser();
58  
59              if (_log.isDebugEnabled()) {
60                  _log.debug("Remote user " + remoteUser);
61              }
62  
63              if (remoteUser != null) {
64                  PrincipalThreadLocal.setName(remoteUser);
65  
66                  long userId = GetterUtil.getLong(remoteUser);
67  
68                  User user = UserLocalServiceUtil.getUserById(userId);
69  
70                  PermissionChecker permissionChecker =
71                      PermissionCheckerFactoryUtil.create(user, true);
72  
73                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
74              }
75  
76              StringServletResponse stringResponse = new StringServletResponse(
77                  response);
78  
79              super.service(request, stringResponse);
80  
81              String contentType = stringResponse.getContentType();
82  
83              response.setContentType(contentType);
84  
85              String content = stringResponse.getString();
86  
87              if (contentType.contains(ContentTypes.TEXT_XML)) {
88                  content = fixXml(content);
89              }
90  
91              ServletResponseUtil.write(
92                  new UncommittedServletResponse(response),
93                  content.getBytes(StringPool.UTF8));
94          }
95          catch (Exception e) {
96              _log.error(e, e);
97          }
98      }
99  
100     protected String fixXml(String xml) throws Exception {
101         if (xml.indexOf("<wsdl:definitions") == -1) {
102             return xml;
103         }
104 
105         xml = StringUtil.replace(
106             xml,
107             new String[] {
108                 "\r\n",
109                 "\n",
110                 "  ",
111                 "> <",
112                 _INCORRECT_LONG_ARRAY,
113                 _INCORRECT_STRING_ARRAY
114             },
115             new String[] {
116                 StringPool.BLANK,
117                 StringPool.BLANK,
118                 StringPool.BLANK,
119                 "><",
120                 _CORRECT_LONG_ARRAY,
121                 _CORRECT_STRING_ARRAY
122             });
123 
124         xml = XMLFormatter.toString(xml);
125 
126         return xml;
127     }
128 
129     private static final String _INCORRECT_LONG_ARRAY =
130         "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
131             "</simpleContent></complexType>";
132 
133     private static final String _CORRECT_LONG_ARRAY =
134         "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
135             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
136                 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
137                     "</restriction></complexContent></complexType>";
138 
139     private static final String _INCORRECT_STRING_ARRAY =
140         "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
141             "</simpleContent></complexType>";
142 
143     private static final String _CORRECT_STRING_ARRAY =
144         "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
145             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
146                 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
147                     "</restriction></complexContent></complexType>";
148 
149     private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
150 
151 }