Subversion Repositories SmartDukaan

Rev

Rev 3561 | Rev 5625 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3561 Rev 3565
Line 62... Line 62...
62
    	final Object action = invocation.getAction();
62
    	final Object action = invocation.getAction();
63
        request = ServletActionContext.getRequest();
63
        request = ServletActionContext.getRequest();
64
        response = ServletActionContext.getResponse();
64
        response = ServletActionContext.getResponse();
65
        
65
        
66
        createCookiesMap();
66
        createCookiesMap();
-
 
67
        sourceIdCookie = cookiesMap.get(SOURCE_ID_COOKIE);
-
 
68
        
67
        String sourceIdString = updateSourceIdCookie();
69
        String sourceIdString = updateSourceIdCookie();
68
 
70
 
69
        long sourceId = -1;
71
        long sourceId = -1;
70
        if(sourceIdString != null){
72
        if(sourceIdString != null){
71
        	try{
73
        	try{
72
        		sourceId = Long.parseLong(sourceIdString);
74
        		sourceId = Long.parseLong(sourceIdString);
73
        	}catch (NumberFormatException e) {
75
        	}catch (NumberFormatException e) {
74
        		log.error("Invalid source cookie. Clear the cookie.");
76
        		log.error("Invalid source cookie. Clear the cookie.");
75
        		//FIXME Clear the cookie, although it will work without it
77
        		expireSourceIdCookie();
76
			}
78
			}
77
        }
79
        }
78
        
80
        
79
		if (action instanceof SourceAware) {
81
		if (action instanceof SourceAware) {
80
			SourceAware sessionAction = (SourceAware) action;
82
			SourceAware sessionAction = (SourceAware) action;
Line 94... Line 96...
94
        if(sourceId == null){
96
        if(sourceId == null){
95
        	return sourceId;
97
        	return sourceId;
96
        }
98
        }
97
        
99
        
98
        DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
100
        DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
99
        String sessionSourceCookieVal = des.encrypt(sourceId);
101
        String sourceIdCookieVal = des.encrypt(sourceId);
100
 
102
 
101
        //session source
103
        //session source
102
        sourceIdCookie = new Cookie(SOURCE_ID_COOKIE,
104
        sourceIdCookie = new Cookie(SOURCE_ID_COOKIE,
103
                sessionSourceCookieVal);
105
                sourceIdCookieVal);
104
        sourceIdCookie.setPath("/");
106
        sourceIdCookie.setPath("/");
105
        sourceIdCookie.setMaxAge(SECONDS_IN_DAY);
107
        sourceIdCookie.setMaxAge(SECONDS_IN_DAY);
106
        if (!cookieDomain.isEmpty()) {
108
        if (!cookieDomain.isEmpty()) {
107
            sourceIdCookie.setDomain(cookieDomain);
109
            sourceIdCookie.setDomain(cookieDomain);
108
        }
110
        }
Line 121... Line 123...
121
            cookiesMap.put(cookie.getName(), cookie);
123
            cookiesMap.put(cookie.getName(), cookie);
122
        }
124
        }
123
    }
125
    }
124
 
126
 
125
    /**
127
    /**
126
     * Check if referer is not saholic or null, try to match with all sources. return source id if matches else return null.
128
     * Check if referer is not saholic or null, try to match with all sources. return source id if matches else return null and clear source cookie if exists.
127
     * If referer is saholic, return sourceId from cookie if set, else return null.
129
     * If referer is saholic, return sourceId from cookie if set, else return null.
128
     */
130
     */
129
    private String getSourceId() {
131
    private String getSourceId() {
130
    	String sourceId = null;
132
    	String sourceId = null;
131
        String referer = request.getHeader("referer");
133
        String referer = request.getHeader("referer");
Line 133... Line 135...
133
        	for(Source source: allSources){
135
        	for(Source source: allSources){
134
        		if(referer.matches(source.getIdentifier())){
136
        		if(referer.matches(source.getIdentifier())){
135
        			return source.getId()+"";
137
        			return source.getId()+"";
136
        		}
138
        		}
137
        	}
139
        	}
-
 
140
        	// If previously we had source cookie, but now she has come from other source which is not listed.
-
 
141
        	// Now we should clear the cookie. 
-
 
142
        	if (sourceIdCookie != null) {
-
 
143
        		expireSourceIdCookie();
-
 
144
        	}
138
        }else{
145
        }else{
139
        	sourceIdCookie = (Cookie) cookiesMap.get(SOURCE_ID_COOKIE);
-
 
140
            if (sourceIdCookie != null) {
146
        	if (sourceIdCookie != null) {
141
            	DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
147
            	DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
142
            	sourceId = des.decrypt(sourceIdCookie.getValue());
148
            	sourceId = des.decrypt(sourceIdCookie.getValue());
143
                return sourceId;
149
                return sourceId;
144
            }
150
            }
145
        }
151
        }
146
        return sourceId;
152
        return sourceId;
147
    }
153
    }
-
 
154
    
-
 
155
	
-
 
156
    /**
-
 
157
     * Expires the Source ID cookie.
-
 
158
     */
-
 
159
    private void expireSourceIdCookie() {
-
 
160
        Cookie sourceIdCookie = new Cookie(SOURCE_ID_COOKIE, "-1"); 
-
 
161
        sourceIdCookie.setMaxAge(0);                     // Expire this cookie now
-
 
162
        sourceIdCookie.setPath("/");
-
 
163
        sourceIdCookie.setDomain(cookieDomain);
-
 
164
        HttpServletResponse response = ServletActionContext.getResponse();
-
 
165
        response.addCookie(sourceIdCookie);
-
 
166
    }
148
}
167
}
149
168