001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.PortletServlet;
020    import com.liferay.portal.kernel.servlet.StringServletResponse;
021    import com.liferay.portal.kernel.servlet.UncommittedServletResponse;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.security.auth.PrincipalThreadLocal;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
030    import com.liferay.portal.security.permission.PermissionThreadLocal;
031    import com.liferay.portal.service.UserLocalServiceUtil;
032    import com.liferay.portal.util.PortalInstances;
033    import com.liferay.util.servlet.ServletResponseUtil;
034    import com.liferay.util.xml.XMLFormatter;
035    
036    import javax.servlet.ServletConfig;
037    import javax.servlet.ServletContext;
038    import javax.servlet.ServletException;
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class AxisServlet extends org.apache.axis.transport.http.AxisServlet {
046    
047            public void init(ServletConfig servletConfig) throws ServletException {
048                    ServletContext servletContext = servletConfig.getServletContext();
049    
050                    _portletClassLoader = (ClassLoader)servletContext.getAttribute(
051                            PortletServlet.PORTLET_CLASS_LOADER);
052    
053                    if (_portletClassLoader == null) {
054                            super.init(servletConfig);
055                    }
056                    else {
057                            Thread currentThread = Thread.currentThread();
058    
059                            ClassLoader contextClassLoader =
060                                    currentThread.getContextClassLoader();
061    
062                            try {
063                                    currentThread.setContextClassLoader(_portletClassLoader);
064    
065                                    super.init(servletConfig);
066                            }
067                            finally {
068                                    currentThread.setContextClassLoader(contextClassLoader);
069                            }
070                    }
071            }
072    
073            public void service(
074                    HttpServletRequest request, HttpServletResponse response) {
075    
076                    try {
077                            PortalInstances.getCompanyId(request);
078    
079                            String remoteUser = request.getRemoteUser();
080    
081                            if (_log.isDebugEnabled()) {
082                                    _log.debug("Remote user " + remoteUser);
083                            }
084    
085                            if (remoteUser != null) {
086                                    PrincipalThreadLocal.setName(remoteUser);
087    
088                                    long userId = GetterUtil.getLong(remoteUser);
089    
090                                    User user = UserLocalServiceUtil.getUserById(userId);
091    
092                                    PermissionChecker permissionChecker =
093                                            PermissionCheckerFactoryUtil.create(user, true);
094    
095                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
096                            }
097    
098                            StringServletResponse stringResponse = new StringServletResponse(
099                                    response);
100    
101                            if (_portletClassLoader == null) {
102                                    super.service(request, stringResponse);
103                            }
104                            else {
105                                    Thread currentThread = Thread.currentThread();
106    
107                                    ClassLoader contextClassLoader =
108                                            currentThread.getContextClassLoader();
109    
110                                    try {
111                                            currentThread.setContextClassLoader(_portletClassLoader);
112    
113                                            super.service(request, stringResponse);
114                                    }
115                                    finally {
116                                            currentThread.setContextClassLoader(contextClassLoader);
117                                    }
118                            }
119    
120                            String contentType = stringResponse.getContentType();
121    
122                            response.setContentType(contentType);
123    
124                            String content = stringResponse.getString();
125    
126                            if (contentType.contains(ContentTypes.TEXT_HTML)) {
127                                    content = _HTML_TOP_WRAPPER.concat(content).concat(
128                                            _HTML_BOTTOM_WRAPPER);
129                            }
130                            else if (contentType.contains(ContentTypes.TEXT_XML)) {
131                                    content = fixXml(content);
132                            }
133    
134                            ServletResponseUtil.write(
135                                    new UncommittedServletResponse(response),
136                                    content.getBytes(StringPool.UTF8));
137                    }
138                    catch (Exception e) {
139                            _log.error(e, e);
140                    }
141            }
142    
143            protected String fixXml(String xml) throws Exception {
144                    if (xml.indexOf("<wsdl:definitions") == -1) {
145                            return xml;
146                    }
147    
148                    xml = StringUtil.replace(
149                            xml,
150                            new String[] {
151                                    "\r\n",
152                                    "\n",
153                                    "  ",
154                                    "> <",
155                                    _INCORRECT_LONG_ARRAY,
156                                    _INCORRECT_STRING_ARRAY
157                            },
158                            new String[] {
159                                    StringPool.BLANK,
160                                    StringPool.BLANK,
161                                    StringPool.BLANK,
162                                    "><",
163                                    _CORRECT_LONG_ARRAY,
164                                    _CORRECT_STRING_ARRAY
165                            });
166    
167                    xml = XMLFormatter.toString(xml);
168    
169                    return xml;
170            }
171    
172            private static final String _CORRECT_LONG_ARRAY =
173                    "<complexType name=\"ArrayOf_xsd_long\"><complexContent>" +
174                            "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
175                                    "arrayType\" wsdl:arrayType=\"soapenc:long[]\"/>" +
176                                            "</restriction></complexContent></complexType>";
177    
178            private static final String _CORRECT_STRING_ARRAY =
179                    "<complexType name=\"ArrayOf_xsd_string\"><complexContent>" +
180                            "<restriction base=\"soapenc:Array\"><attribute ref=\"soapenc:" +
181                                    "arrayType\" wsdl:arrayType=\"soapenc:string[]\"/>" +
182                                            "</restriction></complexContent></complexType>";
183    
184            private static final String _HTML_BOTTOM_WRAPPER = "</body></html>";
185    
186            private static final String _HTML_TOP_WRAPPER = "<html><body>";
187    
188            private static final String _INCORRECT_LONG_ARRAY =
189                    "<complexType name=\"ArrayOf_xsd_long\"><simpleContent><extension/>" +
190                            "</simpleContent></complexType>";
191    
192            private static final String _INCORRECT_STRING_ARRAY =
193                    "<complexType name=\"ArrayOf_xsd_string\"><simpleContent><extension/>" +
194                            "</simpleContent></complexType>";
195    
196            private static Log _log = LogFactoryUtil.getLog(AxisServlet.class);
197    
198            private ClassLoader _portletClassLoader;
199    
200    }