|  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 |    2 |    public class NonSerializableSessionListener implements HttpSessionAttributeListener { | 
  |  35 |    1 |        private static final Log LOG = LogFactory.getLog(NonSerializableSessionListener.class);  | 
  |  36 |     | 
     | 
  |  37 |     | 
       @Override  | 
  |  38 |     | 
       public void attributeAdded(HttpSessionBindingEvent se) { | 
  |  39 |    2 |            logSerializationViolations(se, "added");  | 
  |  40 |    2 |        }  | 
  |  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 |    2 |            if (!productionEnvironmentDetected()) { | 
  |  57 |    1 |                checkSerialization(se, action);  | 
  |  58 |     | 
           }  | 
  |  59 |    2 |        }  | 
  |  60 |     | 
     | 
  |  61 |     | 
         | 
  |  62 |     | 
     | 
  |  63 |     | 
     | 
  |  64 |     | 
       private static boolean productionEnvironmentDetected() { | 
  |  65 |    2 |            Config c = ConfigContext.getCurrentContextConfig();  | 
  |  66 |    2 |            return c != null && c.isProductionEnvironment();  | 
  |  67 |     | 
       }  | 
  |  68 |     | 
     | 
  |  69 |     | 
         | 
  |  70 |     | 
     | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
     | 
  |  74 |     | 
     | 
  |  75 |     | 
       protected void checkSerialization(final HttpSessionBindingEvent se, String action) { | 
  |  76 |    1 |            final Object o = se.getValue();  | 
  |  77 |    1 |            if(o != null) { | 
  |  78 |    1 |                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 |    1 |                } 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 |    1 |        }  | 
  |  85 |     | 
     | 
  |  86 |     | 
         | 
  |  87 |     | 
     | 
  |  88 |     | 
     | 
  |  89 |     | 
       private static boolean isSerializable(Object o) { | 
  |  90 |    1 |            return o instanceof Serializable;  | 
  |  91 |     | 
       }  | 
  |  92 |     | 
     | 
  |  93 |     | 
         | 
  |  94 |     | 
     | 
  |  95 |     | 
     | 
  |  96 |     | 
       private static boolean canBeSerialized(Serializable o) { | 
  |  97 |    1 |            ByteArrayOutputStream baos = null;  | 
  |  98 |    1 |            ObjectOutputStream out = null;  | 
  |  99 |     | 
           try { | 
  |  100 |    1 |                baos = new ByteArrayOutputStream(512);  | 
  |  101 |    1 |                out = new ObjectOutputStream(baos);  | 
  |  102 |    1 |                out.writeObject((Serializable) o);  | 
  |  103 |    1 |                return true;  | 
  |  104 |    0 |            } catch (IOException e) { | 
  |  105 |    0 |                LOG.warn("error serializing object" , e); | 
  |  106 |     | 
           } finally { | 
  |  107 |    0 |                try { | 
  |  108 |    1 |                    if (baos != null) { | 
  |  109 |     | 
                       try { | 
  |  110 |    1 |                            baos.close();  | 
  |  111 |    0 |                        } catch (IOException e) { | 
  |  112 |    0 |                            LOG.warn("error closing stream" , e); | 
  |  113 |    1 |                        }  | 
  |  114 |     | 
                   }  | 
  |  115 |     | 
               } finally { | 
  |  116 |    1 |                    if (out != null) { | 
  |  117 |     | 
                       try { | 
  |  118 |    1 |                            out.close();  | 
  |  119 |    0 |                        } catch (IOException e) { | 
  |  120 |    0 |                             LOG.warn("error closing stream" , e); | 
  |  121 |    1 |                        }  | 
  |  122 |     | 
                   }  | 
  |  123 |     | 
               }  | 
  |  124 |    0 |            }  | 
  |  125 |     | 
     | 
  |  126 |    0 |            return false;  | 
  |  127 |     | 
       }  | 
  |  128 |     | 
   }  |