001    /**
002     * Copyright (c) 2000-2013 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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    
023    import java.io.NotSerializableException;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Igor Spasic
029     */
030    public class NonSerializableObjectRequestWrapper
031            extends PersistentHttpServletRequestWrapper {
032    
033            public static boolean isWrapped(HttpServletRequest request) {
034                    if (!_WEBLOGIC_REQUEST_WRAP_NON_SERIALIZABLE) {
035                            return false;
036                    }
037    
038                    Class<?> clazz = request.getClass();
039    
040                    String className = clazz.getName();
041    
042                    if (className.startsWith("weblogic.")) {
043                            request.removeAttribute(
044                                    NonSerializableObjectRequestWrapper.class.getName());
045    
046                            return false;
047                    }
048    
049                    Boolean wrapped = (Boolean)request.getAttribute(
050                            NonSerializableObjectRequestWrapper.class.getName());
051    
052                    if (wrapped == null) {
053                            return false;
054                    }
055    
056                    return wrapped.booleanValue();
057            }
058    
059            public NonSerializableObjectRequestWrapper(HttpServletRequest request) {
060                    super(request);
061    
062                    request.setAttribute(
063                            NonSerializableObjectRequestWrapper.class.getName(), Boolean.TRUE);
064            }
065    
066            @Override
067            public Object getAttribute(String name) {
068                    Object object = null;
069    
070                    try {
071                            object = super.getAttribute(name);
072                    }
073                    catch (Exception e) {
074                            if (e instanceof NotSerializableException) {
075    
076                                    // LPS-31885
077    
078                                    String message = e.getMessage();
079    
080                                    if ((message == null) || !message.contains("BEA-101362")) {
081                                            _log.error(e, e);
082                                    }
083                            }
084    
085                            return null;
086                    }
087    
088                    object = NonSerializableObjectHandler.getValue(object);
089    
090                    return object;
091            }
092    
093            @Override
094            public void setAttribute(String name, Object object) {
095                    if (_WEBLOGIC_REQUEST_WRAP_NON_SERIALIZABLE) {
096                            object = new NonSerializableObjectHandler(object);
097                    }
098    
099                    super.setAttribute(name, object);
100            }
101    
102            private static final boolean _WEBLOGIC_REQUEST_WRAP_NON_SERIALIZABLE =
103                    GetterUtil.getBoolean(
104                            PropsUtil.get(PropsKeys.WEBLOGIC_REQUEST_WRAP_NON_SERIALIZABLE));
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    NonSerializableObjectRequestWrapper.class);
108    
109    }