1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.servlet.StringServletResponse;
25  import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
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.PermissionChecker;
33  import com.liferay.portal.security.permission.PermissionCheckerFactory;
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 javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  /**
43   * <a href="AxisServlet.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
49  
50      public void service(
51          HttpServletRequest request, HttpServletResponse response) {
52  
53          PermissionChecker permissionChecker = null;
54  
55          try {
56              String remoteUser = request.getRemoteUser();
57  
58              if (_log.isDebugEnabled()) {
59                  _log.debug("Remote user " + remoteUser);
60              }
61  
62              if (remoteUser != null) {
63                  PrincipalThreadLocal.setName(remoteUser);
64  
65                  long userId = GetterUtil.getLong(remoteUser);
66  
67                  User user = UserLocalServiceUtil.getUserById(userId);
68  
69                  permissionChecker = PermissionCheckerFactory.create(user, true);
70  
71                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
72              }
73  
74              StringServletResponse stringResponse = new StringServletResponse(
75                  response);
76  
77              super.service(request, stringResponse);
78  
79              String contentType = stringResponse.getContentType();
80  
81              response.setContentType(contentType);
82  
83              String content = stringResponse.getString();
84  
85              if (contentType.contains(ContentTypes.TEXT_XML)) {
86                  content = fixXml(content);
87              }
88  
89              ServletResponseUtil.write(
90                  new UncommittedServletResponse(response),
91                  content.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 = LogFactoryUtil.getLog(AxisServlet.class);
155 
156 }