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