| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.service.impl; |
| 17 | |
|
| 18 | |
import java.io.ByteArrayInputStream; |
| 19 | |
import java.io.ByteArrayOutputStream; |
| 20 | |
import java.io.ObjectInputStream; |
| 21 | |
import java.io.ObjectOutputStream; |
| 22 | |
import java.sql.Timestamp; |
| 23 | |
import java.util.Collections; |
| 24 | |
import java.util.HashMap; |
| 25 | |
import java.util.Map; |
| 26 | |
|
| 27 | |
import org.apache.commons.lang.StringUtils; |
| 28 | |
import org.kuali.rice.core.api.encryption.EncryptionService; |
| 29 | |
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
| 30 | |
import org.kuali.rice.kew.exception.WorkflowException; |
| 31 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 32 | |
import org.kuali.rice.kns.UserSession; |
| 33 | |
import org.kuali.rice.kns.bo.SessionDocument; |
| 34 | |
import org.kuali.rice.kns.dao.SessionDocumentDao; |
| 35 | |
import org.kuali.rice.kns.datadictionary.DocumentEntry; |
| 36 | |
import org.kuali.rice.kns.service.*; |
| 37 | |
import org.kuali.rice.kns.util.KualiLRUMap; |
| 38 | |
import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase; |
| 39 | |
import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument; |
| 40 | |
import org.springframework.beans.factory.InitializingBean; |
| 41 | |
import org.springframework.transaction.annotation.Transactional; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
@Transactional |
| 51 | 0 | public class SessionDocumentServiceImpl implements SessionDocumentService, InitializingBean { |
| 52 | |
protected static final String IP_ADDRESS = "ipAddress"; |
| 53 | |
protected static final String PRINCIPAL_ID = "principalId"; |
| 54 | |
protected static final String DOCUMENT_NUMBER = "documentNumber"; |
| 55 | |
protected static final String SESSION_ID = "sessionId"; |
| 56 | |
|
| 57 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SessionDocumentServiceImpl.class); |
| 58 | |
|
| 59 | |
private BusinessObjectService businessObjectService; |
| 60 | |
private DataDictionaryService dataDictionaryService; |
| 61 | |
private SessionDocumentDao sessionDocumentDao; |
| 62 | |
private Map<String,CachedObject> cachedObjects; |
| 63 | |
private EncryptionService encryptionService; |
| 64 | |
private int maxCacheSize; |
| 65 | |
|
| 66 | 0 | public static class CachedObject { |
| 67 | |
private UserSession userSession; |
| 68 | |
private String formKey; |
| 69 | 0 | CachedObject(UserSession userSession, String formKey) { |
| 70 | 0 | this.userSession = userSession; |
| 71 | 0 | this.formKey = formKey; |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
@Override |
| 75 | |
public String toString() { |
| 76 | 0 | return "CachedObject: principalId="+userSession.getPrincipalId()+" / objectWithFormKey="+userSession.retrieveObject(formKey); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public UserSession getUserSession() { |
| 80 | 0 | return this.userSession; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public String getFormKey() { |
| 84 | 0 | return this.formKey; |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | |
@Override |
| 89 | |
@SuppressWarnings("unchecked") |
| 90 | |
public void afterPropertiesSet() throws Exception { |
| 91 | 0 | cachedObjects = Collections.synchronizedMap( new KualiLRUMap(maxCacheSize) ); |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public KualiDocumentFormBase getDocumentForm( String documentNumber, String docFormKey, UserSession userSession, String ipAddress){ |
| 99 | 0 | KualiDocumentFormBase documentForm = null; |
| 100 | |
|
| 101 | 0 | LOG.debug("getDocumentForm KualiDocumentFormBase from db"); |
| 102 | |
try{ |
| 103 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 104 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 105 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 106 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 107 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 108 | |
|
| 109 | 0 | SessionDocument sessionDoc = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 110 | 0 | if(sessionDoc != null){ |
| 111 | 0 | byte[] formAsBytes = sessionDoc.getSerializedDocumentForm(); |
| 112 | 0 | if ( sessionDoc.isEncrypted() ) { |
| 113 | 0 | formAsBytes = getEncryptionService().decryptBytes(formAsBytes); |
| 114 | |
} |
| 115 | 0 | ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes); |
| 116 | 0 | ObjectInputStream ois = new ObjectInputStream(baip); |
| 117 | |
|
| 118 | |
|
| 119 | 0 | documentForm = (KualiDocumentFormBase)ois.readObject(); |
| 120 | |
|
| 121 | |
|
| 122 | 0 | KualiWorkflowDocument workflowDocument = documentForm.getDocument().getDocumentHeader().getWorkflowDocument(); |
| 123 | 0 | addDocumentToUserSession(userSession, workflowDocument); |
| 124 | |
} |
| 125 | 0 | } catch(Exception e) { |
| 126 | 0 | LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 127 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 128 | 0 | } |
| 129 | |
|
| 130 | 0 | return documentForm; |
| 131 | |
} |
| 132 | |
|
| 133 | |
@Override |
| 134 | |
public KualiWorkflowDocument getDocumentFromSession(UserSession userSession, String docId) { |
| 135 | |
@SuppressWarnings("unchecked") |
| 136 | 0 | Map<String, KualiWorkflowDocument> workflowDocMap = (Map<String, KualiWorkflowDocument>) userSession.retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); |
| 137 | |
|
| 138 | 0 | if (workflowDocMap == null) { |
| 139 | 0 | workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
| 140 | 0 | userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap); |
| 141 | 0 | return null; |
| 142 | |
} |
| 143 | 0 | return workflowDocMap.get(docId); |
| 144 | |
} |
| 145 | |
|
| 146 | |
@Override |
| 147 | |
public void addDocumentToUserSession(UserSession userSession, KualiWorkflowDocument document) { |
| 148 | |
try { |
| 149 | |
@SuppressWarnings("unchecked") |
| 150 | 0 | Map<String, KualiWorkflowDocument> workflowDocMap = (Map<String, KualiWorkflowDocument>) userSession.retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); |
| 151 | 0 | if (workflowDocMap == null) { |
| 152 | 0 | workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
| 153 | |
} |
| 154 | 0 | workflowDocMap.put(document.getRouteHeaderId().toString(), document); |
| 155 | 0 | userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap); |
| 156 | 0 | } catch (WorkflowException e) { |
| 157 | 0 | throw new IllegalStateException("could not save the document in the session", e); |
| 158 | 0 | } |
| 159 | 0 | } |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
@Override |
| 166 | |
public void purgeDocumentForm(String documentNumber, String docFormKey, |
| 167 | |
UserSession userSession, String ipAddress) { |
| 168 | 0 | synchronized (userSession) { |
| 169 | |
|
| 170 | 0 | LOG.debug("purge document form from session"); |
| 171 | 0 | userSession.removeObject(docFormKey); |
| 172 | |
try { |
| 173 | 0 | LOG.debug("purge document form from database"); |
| 174 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 175 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 176 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 177 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 178 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 179 | 0 | getBusinessObjectService().deleteMatching(SessionDocument.class, primaryKeys); |
| 180 | 0 | } catch (Exception e) { |
| 181 | 0 | LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 182 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 183 | 0 | } |
| 184 | 0 | } |
| 185 | 0 | } |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
@Override |
| 192 | |
public void setDocumentForm(KualiDocumentFormBase form, UserSession userSession, String ipAddress){ |
| 193 | 0 | synchronized ( userSession ) { |
| 194 | |
|
| 195 | 0 | String formKey = form.getFormKey(); |
| 196 | 0 | String key = userSession.getKualiSessionId() + "-" + formKey; |
| 197 | 0 | cachedObjects.put(key, new CachedObject( userSession, formKey )); |
| 198 | |
|
| 199 | 0 | String documentNumber = form.getDocument().getDocumentNumber(); |
| 200 | |
|
| 201 | 0 | if( StringUtils.isNotBlank(documentNumber)) { |
| 202 | |
try { |
| 203 | 0 | LOG.debug("set Document Form into database"); |
| 204 | 0 | Timestamp currentTime = new Timestamp(System.currentTimeMillis()); |
| 205 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 206 | 0 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 207 | 0 | oos.writeObject(form); |
| 208 | |
|
| 209 | 0 | byte[] formAsBytes = baos.toByteArray(); |
| 210 | 0 | boolean encryptContent = false; |
| 211 | 0 | if ( form.getDocTypeName() != null ) { |
| 212 | 0 | DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary().getDocumentEntry( form.getDocTypeName() ); |
| 213 | 0 | if ( documentEntry != null ) { |
| 214 | 0 | encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage(); |
| 215 | |
} |
| 216 | |
} |
| 217 | 0 | if ( encryptContent ) { |
| 218 | 0 | formAsBytes = getEncryptionService().encryptBytes(formAsBytes); |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 224 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 225 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 226 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 227 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 228 | |
|
| 229 | 0 | SessionDocument sessionDocument = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 230 | 0 | if ( sessionDocument == null ) { |
| 231 | 0 | sessionDocument = new SessionDocument(); |
| 232 | 0 | sessionDocument.setSessionId(userSession.getKualiSessionId()); |
| 233 | 0 | sessionDocument.setDocumentNumber(documentNumber); |
| 234 | 0 | sessionDocument.setPrincipalId(userSession.getPrincipalId()); |
| 235 | 0 | sessionDocument.setIpAddress(ipAddress); |
| 236 | |
} |
| 237 | 0 | sessionDocument.setSerializedDocumentForm(formAsBytes); |
| 238 | 0 | sessionDocument.setEncrypted(encryptContent); |
| 239 | 0 | sessionDocument.setLastUpdatedDate(currentTime); |
| 240 | |
|
| 241 | 0 | businessObjectService.save(sessionDocument); |
| 242 | |
|
| 243 | 0 | } catch(Exception e) { |
| 244 | 0 | final String className = form != null ? form.getClass().getName() : "null"; |
| 245 | 0 | LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" |
| 246 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress+"/"+className , e); |
| 247 | 0 | } |
| 248 | |
} else { |
| 249 | 0 | LOG.warn("documentNumber is null on form's document: " + form); |
| 250 | |
} |
| 251 | 0 | } |
| 252 | 0 | } |
| 253 | |
|
| 254 | |
@Override |
| 255 | |
public void purgeAllSessionDocuments(Timestamp expirationDate){ |
| 256 | 0 | sessionDocumentDao.purgeAllSessionDocuments(expirationDate); |
| 257 | 0 | } |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
protected SessionDocumentDao getSessionDocumentDao() { |
| 265 | 0 | return this.sessionDocumentDao; |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) { |
| 272 | 0 | this.sessionDocumentDao = sessionDocumentDao; |
| 273 | 0 | } |
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
protected BusinessObjectService getBusinessObjectService() { |
| 279 | 0 | return this.businessObjectService; |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
| 286 | 0 | this.businessObjectService = businessObjectService; |
| 287 | 0 | } |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
public int getMaxCacheSize() { |
| 293 | 0 | return maxCacheSize; |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
public void setMaxCacheSize(int maxCacheSize) { |
| 300 | 0 | this.maxCacheSize = maxCacheSize; |
| 301 | 0 | } |
| 302 | |
|
| 303 | |
protected EncryptionService getEncryptionService() { |
| 304 | 0 | if ( encryptionService == null ) { |
| 305 | 0 | encryptionService = CoreApiServiceLocator.getEncryptionService(); |
| 306 | |
} |
| 307 | 0 | return encryptionService; |
| 308 | |
} |
| 309 | |
|
| 310 | |
protected DataDictionaryService getDataDictionaryService() { |
| 311 | 0 | if ( dataDictionaryService == null ) { |
| 312 | 0 | dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService(); |
| 313 | |
} |
| 314 | 0 | return dataDictionaryService; |
| 315 | |
} |
| 316 | |
|
| 317 | |
} |