1   /**
2    * Copyright (c) 2000-2008 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.servlet.StringServletResponse;
26  import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.security.auth.PrincipalThreadLocal;
33  import com.liferay.portal.security.permission.PermissionChecker;
34  import com.liferay.portal.security.permission.PermissionCheckerFactory;
35  import com.liferay.portal.security.permission.PermissionThreadLocal;
36  import com.liferay.portal.service.UserLocalServiceUtil;
37  import com.liferay.util.servlet.ServletResponseUtil;
38  import com.liferay.util.xml.XMLFormatter;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="AxisServlet.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
53  
54      public void service(
55          HttpServletRequest request, HttpServletResponse response) {
56  
57          PermissionChecker permissionChecker = null;
58  
59          try {
60              String remoteUser = request.getRemoteUser();
61  
62              if (_log.isDebugEnabled()) {
63                  _log.debug("Remote user " + remoteUser);
64              }
65  
66              if (remoteUser != null) {
67                  PrincipalThreadLocal.setName(remoteUser);
68  
69                  long userId = GetterUtil.getLong(remoteUser);
70  
71                  User user = UserLocalServiceUtil.getUserById(userId);
72  
73                  permissionChecker = PermissionCheckerFactory.create(user, true);
74  
75                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
76              }
77  
78              StringServletResponse stringResponse = new StringServletResponse(
79                  response);
80  
81              super.service(request, stringResponse);
82  
83              String contentType = stringResponse.getContentType();
84  
85              response.setContentType(contentType);
86  
87              String content = stringResponse.getString();
88  
89              if (contentType.contains(ContentTypes.TEXT_XML)) {
90                  content = fixXml(content);
91              }
92  
93              ServletResponseUtil.write(
94                  new UncommittedServletResponse(response),
95                  content.getBytes(StringPool.UTF8));
96          }
97          catch (Exception e) {
98              _log.error(e, e);
99          }
100         finally {
101             try {
102                 PermissionCheckerFactory.recycle(permissionChecker);
103             }
104             catch (Exception e) {
105             }
106         }
107     }
108 
109     protected String fixXml(String xml) throws Exception {
110         if (xml.indexOf("<wsdl:definitions") == -1) {
111             return xml;
112         }
113 
114         xml = StringUtil.replace(
115             xml,
116             new String[] {
117                 "\r\n",
118                 "\n",
119                 "  ",
120                 "> <",
121                 _INCORRECT_LONG_ARRAY,
122                 _INCORRECT_STRING_ARRAY
123             },
124             new String[] {
125                 StringPool.BLANK,
126                 StringPool.BLANK,
127                 StringPool.BLANK,
128                 "><",
129                 _CORRECT_LONG_ARRAY,
130                 _CORRECT_STRING_ARRAY
131             });
132 
133         xml = XMLFormatter.toString(xml);
134 
135         return xml;
136     }
137 
138     private static final String _INCORRECT_LONG_ARRAY =
139         "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
140             "</simpleContent></complexType>";
141 
142     private static final String _CORRECT_LONG_ARRAY =
143         "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
144             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
145                 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
146                     "</restriction></complexContent></complexType>";
147 
148     private static final String _INCORRECT_STRING_ARRAY =
149         "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
150             "</simpleContent></complexType>";
151 
152     private static final String _CORRECT_STRING_ARRAY =
153         "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
154             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
155                 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
156                     "</restriction></complexContent></complexType>";
157 
158     private static Log _log = LogFactory.getLog(AxisServlet.class);
159 
160 }