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