
com.github.gkutiel.flip.web.BombaFlipper Maven / Gradle / Ivy
package com.github.gkutiel.flip.web;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.gkutiel.flip.processor.pub.Flipper;
public class BombaFlipper implements Flipper {
private static final Logger LOGGER = Logger.getLogger(BombaFlipper.class.getCanonicalName());
HttpServletRequest req;
HttpServletResponse res;
public void del(final String key) {
final Cookie c = new Cookie(key, "");
c.setMaxAge(0);
res.addCookie(c);
LOGGER.info("DELETING COOKIE: " + key);
}
public final T get(final Key key) {
final T t = HttpUtils.get(req.getCookies(), key);
if (t == null) del(key.name());
return t;
}
public T redirect(final String location) {
try {
res.sendRedirect(location);
} catch (final IOException e) {
throw new RuntimeException(e);
}
return null;
}
public T redirectIfNull(final T value, final String location) {
if (value != null) return value;
return redirect(location);
}
@Override
public final void set(final HttpServletRequest req, final HttpServletResponse res) {
this.req = req;
this.res = res;
}
public final void set(final Key key, final T val) {
this.set(key, val, Integer.MAX_VALUE);
}
public final void set(final Key key, final T val, final int maxAge) {
if (val == null) throw new NullPointerException("val can't be null");
final Cookie c = HttpUtils.signedCookie(key.name(), val);
c.setPath("/");
c.setMaxAge(maxAge);
res.addCookie(c);
LOGGER.info("SETTING COOKIE: " + c.getName() + " : " + c.getValue());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy