| 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.BusinessObjectService; |
| 37 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
| 38 | |
import org.kuali.rice.kns.service.KNSServiceLocatorWeb; |
| 39 | |
import org.kuali.rice.kns.service.SessionDocumentService; |
| 40 | |
import org.kuali.rice.kns.util.KualiLRUMap; |
| 41 | |
import org.kuali.rice.kns.web.spring.form.DocumentFormBase; |
| 42 | |
import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase; |
| 43 | |
import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument; |
| 44 | |
import org.springframework.beans.factory.InitializingBean; |
| 45 | |
import org.springframework.transaction.annotation.Transactional; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
@Transactional |
| 55 | 0 | public class SessionDocumentServiceImpl implements SessionDocumentService, InitializingBean { |
| 56 | |
protected static final String IP_ADDRESS = "ipAddress"; |
| 57 | |
protected static final String PRINCIPAL_ID = "principalId"; |
| 58 | |
protected static final String DOCUMENT_NUMBER = "documentNumber"; |
| 59 | |
protected static final String SESSION_ID = "sessionId"; |
| 60 | |
|
| 61 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SessionDocumentServiceImpl.class); |
| 62 | |
|
| 63 | |
private BusinessObjectService businessObjectService; |
| 64 | |
private DataDictionaryService dataDictionaryService; |
| 65 | |
private SessionDocumentDao sessionDocumentDao; |
| 66 | |
private Map<String,CachedObject> cachedObjects; |
| 67 | |
private EncryptionService encryptionService; |
| 68 | |
private int maxCacheSize; |
| 69 | |
|
| 70 | 0 | public static class CachedObject { |
| 71 | |
private UserSession userSession; |
| 72 | |
private String formKey; |
| 73 | 0 | CachedObject(UserSession userSession, String formKey) { |
| 74 | 0 | this.userSession = userSession; |
| 75 | 0 | this.formKey = formKey; |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
@Override |
| 79 | |
public String toString() { |
| 80 | 0 | return "CachedObject: principalId="+userSession.getPrincipalId()+" / objectWithFormKey="+userSession.retrieveObject(formKey); |
| 81 | |
} |
| 82 | |
|
| 83 | |
public UserSession getUserSession() { |
| 84 | 0 | return this.userSession; |
| 85 | |
} |
| 86 | |
|
| 87 | |
public String getFormKey() { |
| 88 | 0 | return this.formKey; |
| 89 | |
} |
| 90 | |
} |
| 91 | |
|
| 92 | |
@Override |
| 93 | |
@SuppressWarnings("unchecked") |
| 94 | |
public void afterPropertiesSet() throws Exception { |
| 95 | 0 | cachedObjects = Collections.synchronizedMap( new KualiLRUMap(maxCacheSize) ); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public KualiDocumentFormBase getDocumentForm( String documentNumber, String docFormKey, UserSession userSession, String ipAddress){ |
| 103 | 0 | KualiDocumentFormBase documentForm = null; |
| 104 | |
|
| 105 | 0 | LOG.debug("getDocumentForm KualiDocumentFormBase from db"); |
| 106 | |
try{ |
| 107 | |
|
| 108 | 0 | documentForm = (KualiDocumentFormBase)retrieveDocumentForm(userSession, userSession.getKualiSessionId(), documentNumber, ipAddress); |
| 109 | |
|
| 110 | |
|
| 111 | 0 | KualiWorkflowDocument workflowDocument = documentForm.getDocument().getDocumentHeader().getWorkflowDocument(); |
| 112 | 0 | addDocumentToUserSession(userSession, workflowDocument); |
| 113 | 0 | } catch(Exception e) { |
| 114 | 0 | LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 115 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 116 | 0 | } |
| 117 | |
|
| 118 | 0 | return documentForm; |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public DocumentFormBase getUifDocumentForm(String documentNumber, String docFormKey, UserSession userSession, String ipAddress) { |
| 126 | 0 | DocumentFormBase documentForm = null; |
| 127 | |
|
| 128 | 0 | LOG.debug("getDocumentForm KualiDocumentFormBase from db"); |
| 129 | |
try{ |
| 130 | |
|
| 131 | 0 | documentForm = (DocumentFormBase)retrieveDocumentForm(userSession, docFormKey, documentNumber, ipAddress); |
| 132 | |
|
| 133 | |
|
| 134 | 0 | KualiWorkflowDocument workflowDocument = documentForm.getDocument().getDocumentHeader().getWorkflowDocument(); |
| 135 | 0 | addDocumentToUserSession(userSession, workflowDocument); |
| 136 | 0 | } catch(Exception e) { |
| 137 | 0 | LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 138 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 139 | 0 | } |
| 140 | |
|
| 141 | 0 | return documentForm; |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
protected Object retrieveDocumentForm(UserSession userSession, String sessionId, String documentNumber, String ipAddress) throws Exception{ |
| 146 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 147 | 0 | primaryKeys.put(SESSION_ID, sessionId); |
| 148 | 0 | if (documentNumber != null){ |
| 149 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 150 | |
} |
| 151 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 152 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 153 | |
|
| 154 | 0 | SessionDocument sessionDoc = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 155 | 0 | if(sessionDoc != null){ |
| 156 | 0 | byte[] formAsBytes = sessionDoc.getSerializedDocumentForm(); |
| 157 | 0 | if ( sessionDoc.isEncrypted() ) { |
| 158 | 0 | formAsBytes = getEncryptionService().decryptBytes(formAsBytes); |
| 159 | |
} |
| 160 | 0 | ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes); |
| 161 | 0 | ObjectInputStream ois = new ObjectInputStream(baip); |
| 162 | |
|
| 163 | 0 | return ois.readObject(); |
| 164 | |
} |
| 165 | |
|
| 166 | 0 | return null; |
| 167 | |
} |
| 168 | |
|
| 169 | |
@Override |
| 170 | |
public KualiWorkflowDocument getDocumentFromSession(UserSession userSession, String docId) { |
| 171 | |
@SuppressWarnings("unchecked") |
| 172 | 0 | Map<String, KualiWorkflowDocument> workflowDocMap = (Map<String, KualiWorkflowDocument>) userSession.retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); |
| 173 | |
|
| 174 | 0 | if (workflowDocMap == null) { |
| 175 | 0 | workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
| 176 | 0 | userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap); |
| 177 | 0 | return null; |
| 178 | |
} |
| 179 | 0 | return workflowDocMap.get(docId); |
| 180 | |
} |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
@Override |
| 187 | |
public void addDocumentToUserSession(UserSession userSession, KualiWorkflowDocument document) { |
| 188 | |
try { |
| 189 | |
@SuppressWarnings("unchecked") |
| 190 | 0 | Map<String, KualiWorkflowDocument> workflowDocMap = (Map<String, KualiWorkflowDocument>) userSession.retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); |
| 191 | 0 | if (workflowDocMap == null) { |
| 192 | 0 | workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
| 193 | |
} |
| 194 | 0 | workflowDocMap.put(document.getDocumentId(), document); |
| 195 | 0 | userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap); |
| 196 | 0 | } catch (WorkflowException e) { |
| 197 | 0 | throw new IllegalStateException("could not save the document in the session", e); |
| 198 | 0 | } |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
@Override |
| 206 | |
public void purgeDocumentForm(String documentNumber, String docFormKey, |
| 207 | |
UserSession userSession, String ipAddress) { |
| 208 | 0 | synchronized (userSession) { |
| 209 | |
|
| 210 | 0 | LOG.debug("purge document form from session"); |
| 211 | 0 | userSession.removeObject(docFormKey); |
| 212 | |
try { |
| 213 | 0 | LOG.debug("purge document form from database"); |
| 214 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 215 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 216 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 217 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 218 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 219 | 0 | getBusinessObjectService().deleteMatching(SessionDocument.class, primaryKeys); |
| 220 | 0 | } catch (Exception e) { |
| 221 | 0 | LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 222 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 223 | 0 | } |
| 224 | 0 | } |
| 225 | 0 | } |
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
@Override |
| 232 | |
public void setDocumentForm(KualiDocumentFormBase form, UserSession userSession, String ipAddress){ |
| 233 | 0 | synchronized ( userSession ) { |
| 234 | |
|
| 235 | 0 | String formKey = form.getFormKey(); |
| 236 | 0 | String key = userSession.getKualiSessionId() + "-" + formKey; |
| 237 | 0 | cachedObjects.put(key, new CachedObject( userSession, formKey )); |
| 238 | |
|
| 239 | 0 | String documentNumber = form.getDocument().getDocumentNumber(); |
| 240 | |
|
| 241 | 0 | if( StringUtils.isNotBlank(documentNumber)) { |
| 242 | 0 | persistDocumentForm(form, userSession, ipAddress, userSession.getKualiSessionId(), documentNumber); |
| 243 | |
} else { |
| 244 | 0 | LOG.warn("documentNumber is null on form's document: " + form); |
| 245 | |
} |
| 246 | 0 | } |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
@Override |
| 254 | |
public void setDocumentForm(DocumentFormBase form, UserSession userSession, String ipAddress){ |
| 255 | 0 | synchronized ( userSession ) { |
| 256 | |
|
| 257 | 0 | String formKey = form.getFormKey(); |
| 258 | 0 | String key = userSession.getKualiSessionId() + "-" + formKey; |
| 259 | |
|
| 260 | 0 | String documentNumber = form.getDocument().getDocumentNumber(); |
| 261 | 0 | if( StringUtils.isNotBlank(formKey)) { |
| 262 | |
|
| 263 | 0 | persistDocumentForm(form, userSession, ipAddress, formKey, documentNumber); |
| 264 | |
} else { |
| 265 | 0 | LOG.warn("documentNumber is null on form's document: " + form); |
| 266 | |
} |
| 267 | 0 | } |
| 268 | 0 | } |
| 269 | |
|
| 270 | |
protected void persistDocumentForm(Object form, UserSession userSession, String ipAddress, String sessionId, String documentNumber){ |
| 271 | |
try { |
| 272 | 0 | LOG.debug("set Document Form into database"); |
| 273 | 0 | Timestamp currentTime = new Timestamp(System.currentTimeMillis()); |
| 274 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 275 | 0 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 276 | 0 | oos.writeObject(form); |
| 277 | |
|
| 278 | 0 | byte[] formAsBytes = baos.toByteArray(); |
| 279 | 0 | boolean encryptContent = false; |
| 280 | 0 | if ((form instanceof KualiDocumentFormBase) && ((KualiDocumentFormBase)form).getDocTypeName() != null ) { |
| 281 | 0 | DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary().getDocumentEntry( ((KualiDocumentFormBase)form).getDocTypeName() ); |
| 282 | 0 | if ( documentEntry != null ) { |
| 283 | 0 | encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage(); |
| 284 | |
} |
| 285 | |
} |
| 286 | 0 | if ( encryptContent ) { |
| 287 | 0 | formAsBytes = getEncryptionService().encryptBytes(formAsBytes); |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 293 | 0 | primaryKeys.put(SESSION_ID, sessionId); |
| 294 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 295 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 296 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 297 | |
|
| 298 | 0 | SessionDocument sessionDocument = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 299 | 0 | if ( sessionDocument == null ) { |
| 300 | 0 | sessionDocument = new SessionDocument(); |
| 301 | 0 | sessionDocument.setSessionId(sessionId); |
| 302 | 0 | sessionDocument.setDocumentNumber(documentNumber); |
| 303 | 0 | sessionDocument.setPrincipalId(userSession.getPrincipalId()); |
| 304 | 0 | sessionDocument.setIpAddress(ipAddress); |
| 305 | |
} |
| 306 | 0 | sessionDocument.setSerializedDocumentForm(formAsBytes); |
| 307 | 0 | sessionDocument.setEncrypted(encryptContent); |
| 308 | 0 | sessionDocument.setLastUpdatedDate(currentTime); |
| 309 | |
|
| 310 | 0 | businessObjectService.save(sessionDocument); |
| 311 | |
|
| 312 | 0 | } catch(Exception e) { |
| 313 | 0 | final String className = form != null ? form.getClass().getName() : "null"; |
| 314 | 0 | LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" |
| 315 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress+"/"+className , e); |
| 316 | 0 | } |
| 317 | 0 | } |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
@Override |
| 323 | |
public void purgeAllSessionDocuments(Timestamp expirationDate){ |
| 324 | 0 | sessionDocumentDao.purgeAllSessionDocuments(expirationDate); |
| 325 | 0 | } |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
protected SessionDocumentDao getSessionDocumentDao() { |
| 331 | 0 | return this.sessionDocumentDao; |
| 332 | |
} |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) { |
| 338 | 0 | this.sessionDocumentDao = sessionDocumentDao; |
| 339 | 0 | } |
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
protected BusinessObjectService getBusinessObjectService() { |
| 345 | 0 | return this.businessObjectService; |
| 346 | |
} |
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
| 352 | 0 | this.businessObjectService = businessObjectService; |
| 353 | 0 | } |
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
public int getMaxCacheSize() { |
| 359 | 0 | return maxCacheSize; |
| 360 | |
} |
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
public void setMaxCacheSize(int maxCacheSize) { |
| 366 | 0 | this.maxCacheSize = maxCacheSize; |
| 367 | 0 | } |
| 368 | |
|
| 369 | |
protected EncryptionService getEncryptionService() { |
| 370 | 0 | if ( encryptionService == null ) { |
| 371 | 0 | encryptionService = CoreApiServiceLocator.getEncryptionService(); |
| 372 | |
} |
| 373 | 0 | return encryptionService; |
| 374 | |
} |
| 375 | |
|
| 376 | |
protected DataDictionaryService getDataDictionaryService() { |
| 377 | 0 | if ( dataDictionaryService == null ) { |
| 378 | 0 | dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService(); |
| 379 | |
} |
| 380 | 0 | return dataDictionaryService; |
| 381 | |
} |
| 382 | |
|
| 383 | |
} |