1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
51  
52      public void service(
53          HttpServletRequest request, HttpServletResponse response) {
54  
55          PermissionChecker permissionChecker = null;
56  
57          try {
58              String remoteUser = request.getRemoteUser();
59  
60              if (_log.isDebugEnabled()) {
61                  _log.debug("Remote user " + remoteUser);
62              }
63  
64              if (remoteUser != null) {
65                  PrincipalThreadLocal.setName(remoteUser);
66  
67                  long userId = GetterUtil.getLong(remoteUser);
68  
69                  User user = UserLocalServiceUtil.getUserById(userId);
70  
71                  permissionChecker = PermissionCheckerFactory.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          finally {
99              try {
100                 PermissionCheckerFactory.recycle(permissionChecker);
101             }
102             catch (Exception e) {
103             }
104         }
105     }
106 
107     protected String fixXml(String xml) throws Exception {
108         if (xml.indexOf("<wsdl:definitions") == -1) {
109             return xml;
110         }
111 
112         xml = StringUtil.replace(
113             xml,
114             new String[] {
115                 "\r\n",
116                 "\n",
117                 "  ",
118                 "> <",
119                 _INCORRECT_LONG_ARRAY,
120                 _INCORRECT_STRING_ARRAY
121             },
122             new String[] {
123                 StringPool.BLANK,
124                 StringPool.BLANK,
125                 StringPool.BLANK,
126                 "><",
127                 _CORRECT_LONG_ARRAY,
128                 _CORRECT_STRING_ARRAY
129             });
130 
131         xml = XMLFormatter.toString(xml);
132 
133         return xml;
134     }
135 
136     private static final String _INCORRECT_LONG_ARRAY =
137         "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
138             "</simpleContent></complexType>";
139 
140     private static final String _CORRECT_LONG_ARRAY =
141         "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
142             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
143                 "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
144                     "</restriction></complexContent></complexType>";
145 
146     private static final String _INCORRECT_STRING_ARRAY =
147         "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
148             "</simpleContent></complexType>";
149 
150     private static final String _CORRECT_STRING_ARRAY =
151         "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
152             "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
153                 "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
154                     "</restriction></complexContent></complexType>";
155 
156     private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
157 
158 }