| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.web.session; |
| 17 | |
|
| 18 | |
import org.apache.commons.logging.Log; |
| 19 | |
import org.apache.commons.logging.LogFactory; |
| 20 | |
import org.kuali.rice.core.api.config.property.Config; |
| 21 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 22 | |
|
| 23 | |
import javax.servlet.http.HttpSessionAttributeListener; |
| 24 | |
import javax.servlet.http.HttpSessionBindingEvent; |
| 25 | |
import java.io.ByteArrayOutputStream; |
| 26 | |
import java.io.IOException; |
| 27 | |
import java.io.ObjectOutputStream; |
| 28 | |
import java.io.Serializable; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class NonSerializableSessionListener implements HttpSessionAttributeListener { |
| 35 | 0 | private static final Log LOG = LogFactory.getLog(NonSerializableSessionListener.class); |
| 36 | |
|
| 37 | |
@Override |
| 38 | |
public void attributeAdded(HttpSessionBindingEvent se) { |
| 39 | 0 | logSerializationViolations(se, "added"); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
@Override |
| 43 | |
public void attributeRemoved(HttpSessionBindingEvent se) { |
| 44 | |
|
| 45 | 0 | } |
| 46 | |
|
| 47 | |
@Override |
| 48 | |
public void attributeReplaced(HttpSessionBindingEvent se) { |
| 49 | 0 | logSerializationViolations(se, "replaced"); |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private void logSerializationViolations(HttpSessionBindingEvent se, String action) { |
| 56 | 0 | if (!productionEnvironmentDetected()) { |
| 57 | 0 | checkSerialization(se, action); |
| 58 | |
} |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
private static boolean productionEnvironmentDetected() { |
| 65 | 0 | Config c = ConfigContext.getCurrentContextConfig(); |
| 66 | 0 | return c != null && c.isProductionEnvironment(); |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
protected void checkSerialization(final HttpSessionBindingEvent se, String action) { |
| 76 | 0 | final Object o = se.getValue(); |
| 77 | 0 | if(o != null) { |
| 78 | 0 | if (!isSerializable(o)) { |
| 79 | 0 | LOG.error("Attribute of class " + o.getClass().getName() + " with name " + se.getName() + " from source " + se.getSource().getClass().getName() + " was " + action + " to session and does not implement " + Serializable.class.getName()); |
| 80 | 0 | } else if (!canBeSerialized((Serializable) o)){ |
| 81 | 0 | LOG.error("Attribute of class " + o.getClass().getName() + " with name " + se.getName() + " from source " + se.getSource().getClass().getName() + " was " + action + " to session and cannot be Serialized"); |
| 82 | |
} |
| 83 | |
} |
| 84 | 0 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
private static boolean isSerializable(Object o) { |
| 90 | 0 | return o instanceof Serializable; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private static boolean canBeSerialized(Serializable o) { |
| 97 | 0 | ByteArrayOutputStream baos = null; |
| 98 | 0 | ObjectOutputStream out = null; |
| 99 | |
try { |
| 100 | 0 | baos = new ByteArrayOutputStream(512); |
| 101 | 0 | out = new ObjectOutputStream(baos); |
| 102 | 0 | out.writeObject((Serializable) o); |
| 103 | 0 | return true; |
| 104 | 0 | } catch (IOException e) { |
| 105 | 0 | LOG.warn("error serializing object" , e); |
| 106 | |
} finally { |
| 107 | 0 | try { |
| 108 | 0 | if (baos != null) { |
| 109 | |
try { |
| 110 | 0 | baos.close(); |
| 111 | 0 | } catch (IOException e) { |
| 112 | 0 | LOG.warn("error closing stream" , e); |
| 113 | 0 | } |
| 114 | |
} |
| 115 | |
} finally { |
| 116 | 0 | if (out != null) { |
| 117 | |
try { |
| 118 | 0 | out.close(); |
| 119 | 0 | } catch (IOException e) { |
| 120 | 0 | LOG.warn("error closing stream" , e); |
| 121 | 0 | } |
| 122 | |
} |
| 123 | |
} |
| 124 | 0 | } |
| 125 | |
|
| 126 | 0 | return false; |
| 127 | |
} |
| 128 | |
} |