1 5 package com.tc.tomcat.session; 6 7 import org.apache.catalina.Manager; 8 import org.apache.catalina.Realm; 9 import org.apache.catalina.Session; 10 import org.apache.catalina.SessionListener; 11 import org.apache.catalina.cluster.session.SerializablePrincipal; 12 import org.apache.catalina.realm.GenericPrincipal; 13 14 import com.terracotta.session.SessionData; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.security.Principal ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import javax.servlet.http.HttpSession ; 26 27 public class SessionInternal implements Session { 28 29 private static final String AUTH_TYPE = "tomcatAuthType"; 30 private static final String NOTE = "tomcatNote"; 31 private static final String TOMCAT_PRINCIPAL = "tomcatPrincipal"; 32 private static final String PORTABLE_PRINCIPAL = "tomcatPortablePrincipal"; 33 34 private final com.terracotta.session.Session tcSession; 35 private final SessionData sessionData; 36 private final Realm realm; 37 38 public SessionInternal(com.terracotta.session.Session tcSession, Realm realm) { 39 this.tcSession = tcSession; 40 this.sessionData = tcSession.getSessionData(); 41 this.realm = realm; 42 } 43 44 public void access() { 45 } 47 48 public void addSessionListener(SessionListener listener) { 49 } 51 52 public void endAccess() { 53 } 55 56 public void expire() { 57 throw new UnsupportedOperationException (); 58 } 59 60 public String getAuthType() { 61 return (String ) sessionData.getTransientAttribute(AUTH_TYPE); 62 } 63 64 public long getCreationTime() { 65 return tcSession.getCreationTime(); 66 } 67 68 public String getId() { 69 return tcSession.getId(); 70 } 71 72 public String getIdInternal() { 73 return tcSession.getId(); 74 } 75 76 public String getInfo() { 77 return "TerracottaSessionInternal"; 78 } 79 80 public long getLastAccessedTime() { 81 return tcSession.getLastAccessedTime(); 82 } 83 84 public long getLastAccessedTimeInternal() { 85 throw new UnsupportedOperationException (); 86 } 87 88 public Manager getManager() { 89 throw new UnsupportedOperationException (); 90 } 91 92 public int getMaxInactiveInterval() { 93 return tcSession.getMaxInactiveInterval(); 94 } 95 96 public Object getNote(String name) { 97 return sessionData.getTransientAttribute(makeNoteName(name)); 98 } 99 100 public Iterator getNoteNames() { 101 Collection keys = sessionData.getTransientAttributeKeys(); 102 for (Iterator i = keys.iterator(); i.hasNext();) { 103 String key = (String ) i.next(); 104 if (!key.startsWith(NOTE)) { 105 i.remove(); 106 } 107 } 108 109 return keys.iterator(); 110 } 111 112 public Principal getPrincipal() { 113 Principal p = (Principal) sessionData.getInternalAttribute(PORTABLE_PRINCIPAL); 114 if (p != null) { return p; } 115 116 byte[] principal = (byte[]) sessionData.getInternalAttribute(TOMCAT_PRINCIPAL); 117 if (principal != null) { return deserializeGenericPrincipal(principal); } 118 119 return null; 120 } 121 122 public HttpSession getSession() { 123 return tcSession; 124 } 125 126 public boolean isValid() { 127 return tcSession.isValid(); 128 } 129 130 public void recycle() { 131 } 133 134 public void removeNote(String name) { 135 sessionData.removeTransientAttribute(makeNoteName(name)); 136 } 137 138 public void removeSessionListener(SessionListener listener) { 139 } 141 142 public void setAuthType(String authType) { 143 sessionData.setTransientAttribute(AUTH_TYPE, authType); 144 } 145 146 public void setCreationTime(long time) { 147 throw new UnsupportedOperationException (); 148 } 149 150 public void setId(String id) { 151 throw new UnsupportedOperationException (); 152 } 153 154 public void setManager(Manager manager) { 155 throw new UnsupportedOperationException (); 156 } 157 158 public void setMaxInactiveInterval(int interval) { 159 throw new UnsupportedOperationException (); 160 } 161 162 public void setNew(boolean isNew) { 163 throw new UnsupportedOperationException (); 164 } 165 166 public void setNote(String name, Object value) { 167 sessionData.setTransientAttribute(makeNoteName(name), value); 168 } 169 170 public void setPrincipal(Principal principal) { 171 if (principal == null) { 172 sessionData.removeInternalAttribute(TOMCAT_PRINCIPAL); 173 sessionData.removeAttribute(PORTABLE_PRINCIPAL); 174 } 175 176 final boolean isTomcatInternalPrincipal = (principal instanceof GenericPrincipal); 177 sessionData.removeAttribute(isTomcatInternalPrincipal ? PORTABLE_PRINCIPAL : TOMCAT_PRINCIPAL); 178 179 if (isTomcatInternalPrincipal) { 180 sessionData.setInternalAttribute(TOMCAT_PRINCIPAL, serializeGenericPrincipal((GenericPrincipal) principal)); 181 } else { 182 sessionData.setInternalAttribute(PORTABLE_PRINCIPAL, principal); 183 } 184 } 185 186 private static byte[] serializeGenericPrincipal(GenericPrincipal principal) { 187 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 188 189 try { 190 ObjectOutputStream oos = new ObjectOutputStream (baos); 191 SerializablePrincipal.writePrincipal(principal, oos); 192 oos.flush(); 193 } catch (IOException e) { 194 throw new RuntimeException ("Error serializing principal", e); 195 } 196 197 return baos.toByteArray(); 198 } 199 200 private GenericPrincipal deserializeGenericPrincipal(byte[] data) { 201 try { 202 return SerializablePrincipal.readPrincipal(new ObjectInputStream (new ByteArrayInputStream (data)), realm); 203 } catch (IOException e) { 204 throw new RuntimeException ("Error creating principal", e); 205 } 206 } 207 208 public void setValid(boolean isValid) { 209 throw new UnsupportedOperationException (); 210 } 211 212 private static String makeNoteName(String name) { 213 return NOTE + name; 214 } 215 216 } | Popular Tags |