[1240] | 1 | package gov.va.med.edp.web.controller;
|
---|
| 2 |
|
---|
| 3 | import gov.va.med.edp.web.servlet.listener.TimeOutIntegrationSessionAttributeListener;
|
---|
| 4 |
|
---|
| 5 | import java.util.Random;
|
---|
| 6 | import java.util.Set;
|
---|
| 7 |
|
---|
| 8 | import javax.servlet.http.HttpServletRequest;
|
---|
| 9 | import javax.servlet.http.HttpServletResponse;
|
---|
| 10 |
|
---|
| 11 | import org.springframework.beans.factory.InitializingBean;
|
---|
| 12 | import org.springframework.core.io.Resource;
|
---|
| 13 | import org.springframework.dao.DataAccessException;
|
---|
| 14 | import org.springframework.util.Assert;
|
---|
| 15 | import org.springframework.util.StringUtils;
|
---|
| 16 | import org.springframework.web.servlet.ModelAndView;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * TODO: document PackageClientVersionSynchronizationController
|
---|
| 20 | */
|
---|
| 21 | public class ClientVersionSynchronizationController extends SiteCodeController implements InitializingBean {
|
---|
| 22 |
|
---|
| 23 | public static final String DEFAULT_INCOMPATIBILITY_VIEW = "incompatibilityView";
|
---|
| 24 |
|
---|
| 25 | private String clientArtifactId;
|
---|
| 26 | private String viewName;
|
---|
| 27 | private String incompatibilityViewName = DEFAULT_INCOMPATIBILITY_VIEW;
|
---|
| 28 | private Random random = new Random();
|
---|
| 29 |
|
---|
| 30 | public ClientVersionSynchronizationController() {
|
---|
| 31 | setRequireSession(true);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public void afterPropertiesSet() throws Exception {
|
---|
| 35 | Assert.notNull(clientArtifactId, "clientArtifactId must not be null");
|
---|
| 36 | Assert.notNull(viewName, "viewName must not be null");
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
---|
| 40 | String serverPackageVersion = (String) request.getSession().getAttribute(SessionConstants.SERVER_PACKAGE_VERSION_KEY);
|
---|
| 41 | if (serverPackageVersion == null) {
|
---|
| 42 | DataAccessException error = (DataAccessException) request.getSession().getAttribute(SessionConstants.SERVER_ERROR_KEY);
|
---|
| 43 | if (error != null) {
|
---|
| 44 | throw error;
|
---|
| 45 | } else {
|
---|
| 46 | throw new IllegalStateException("Didn't find server package version number or an error explaining its absence in session. Is the " +
|
---|
| 47 | TimeOutIntegrationSessionAttributeListener.class.getName() + " session listener configured correctly?");
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | String swfID = generateAndRememberSwfId(request);
|
---|
| 52 |
|
---|
| 53 | return createModelAndView(serverPackageVersion, swfID);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // generate a swf id and add to list for this session
|
---|
| 57 | private String generateAndRememberSwfId(HttpServletRequest request) {
|
---|
| 58 | String swfID = generateUid();
|
---|
| 59 | String ids = (String) request.getSession().getAttribute(SessionConstants.RECOGNIZED_SWF_IDS_KEY);
|
---|
| 60 |
|
---|
| 61 | Set idSet = StringUtils.commaDelimitedListToSet(ids);
|
---|
| 62 | while (idSet.contains(swfID)) {
|
---|
| 63 | swfID = generateUid();
|
---|
| 64 | }
|
---|
| 65 | idSet.add(swfID);
|
---|
| 66 | ids = StringUtils.collectionToCommaDelimitedString(idSet);
|
---|
| 67 |
|
---|
| 68 | request.getSession().setAttribute(SessionConstants.RECOGNIZED_SWF_IDS_KEY, ids);
|
---|
| 69 | return swfID;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | protected ModelAndView createModelAndView(String serverPackageVersion, String swfID) {
|
---|
| 73 | ModelAndView mav = new ModelAndView(isCompatible(serverPackageVersion) ? getViewName() : getIncompatibilityViewName());
|
---|
| 74 | mav.addObject("swfID", swfID);
|
---|
| 75 | mav.addObject("serverPackageVersion", serverPackageVersion);
|
---|
| 76 | mav.addObject("clientVersion", serverPackageVersion); // TODO: remove this from flashVars after Flex 3 migration (use resource bundle inside SWF)
|
---|
| 77 | mav.addObject("clientArtifactId", getClientArtifactId());
|
---|
| 78 | mav.addObject("clientFinalName", getClientFinalName(serverPackageVersion));
|
---|
| 79 | return mav;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private String generateUid() {
|
---|
| 83 | return Integer.toString(Math.abs(random.nextInt()));
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private boolean isCompatible(String serverPackageVersion) {
|
---|
| 87 | Resource r = getWebApplicationContext().getResource(getClientFileName(serverPackageVersion));
|
---|
| 88 | return r.exists();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private String getClientFinalName(String serverPackageVersion) {
|
---|
| 92 | return getClientArtifactId() + "-" + serverPackageVersion;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private String getClientFileName(String serverPackageVersion) {
|
---|
| 96 | return getClientFinalName(serverPackageVersion) + ".swf";
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public String getClientArtifactId() {
|
---|
| 100 | return clientArtifactId;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public void setClientArtifactId(String clientArtifactId) {
|
---|
| 104 | this.clientArtifactId = clientArtifactId;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public String getViewName() {
|
---|
| 108 | return viewName;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public void setViewName(String viewName) {
|
---|
| 112 | this.viewName = viewName;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public String getIncompatibilityViewName() {
|
---|
| 116 | return incompatibilityViewName;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public void setIncompatibilityViewName(String incompatibilityViewName) {
|
---|
| 120 | this.incompatibilityViewName = incompatibilityViewName;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|