All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.loom.appengine.scope.MemcacheFlashContextFactory Maven / Gradle / Ivy

The newest version!
package org.loom.appengine.scope;

import javax.servlet.http.HttpServletRequest;

import org.loom.servlet.LoomServletRequest;
import org.loom.servlet.names.SessionAttributeNames;
import org.loom.servlet.scope.FlashContext;
import org.loom.servlet.scope.FlashContextFactory;
import org.loom.util.StringUtils;

import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * An implementation of FlashContextFactory that is based in memcache, for 
 * stateless GAE systems.
 * For this to work, you should also register MemcacheFlashContextWriter in web.xml
 * @author icoloma
 *
 */
public class MemcacheFlashContextFactory implements FlashContextFactory {

	/** the number of seconds that the flash context will be alive in memcache */
	private int expirationSeconds = 60;

	public FlashContext create(LoomServletRequest request) {
		MemcacheService memcache = getMemcache();
		User user = UserServiceFactory.getUserService().getCurrentUser();
		String cacheKey = user == null? "" : user.getEmail();
		String id = StringUtils.numberToAlpha(memcache.increment(cacheKey, 1, 0L));
		FlashContext instance = new MemcacheFlashContext(memcache, id, expirationSeconds);
		return instance;
	}
	
	public FlashContext retrieve(HttpServletRequest request, String flashID) {
		MemcacheService memcache = getMemcache();
		String key = SessionAttributeNames.FLASH_PREFIX + flashID;
		FlashContext context = (FlashContext) memcache.get(key);
		memcache.delete(key);
		return context;
	}

	public MemcacheService getMemcache() {
		return MemcacheServiceFactory.getMemcacheService("loom-flash");
	}

	public void setExpirationSeconds(int expirationSeconds) {
		this.expirationSeconds = expirationSeconds;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy