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