Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package io.undertow.servlet.attribute;
 
 
An attribute in the servlet request

Author(s):
Stuart Douglas
public class ServletSessionAttribute implements ExchangeAttribute {
    private final String attributeName;
    public ServletSessionAttribute(final String attributeName) {
        this. = attributeName;
    }
    @Override
    public String readAttribute(final HttpServerExchange exchange) {
        if (context != null) {
            ServletRequest req = context.getServletRequest();
            if (req instanceof HttpServletRequest) {
                HttpSession session = ((HttpServletRequestreq).getSession(false);
                if (session != null) {
                    Object result = session.getAttribute();
                    if (result != null) {
                        return result.toString();
                    }
                }
            }
        }
        return null;
    }
    @Override
    public void writeAttribute(final HttpServerExchange exchangefinal String newValuethrows ReadOnlyAttributeException {
        if (context != null) {
            ServletRequest req = context.getServletRequest();
            if (req instanceof HttpServletRequest) {
                HttpSession session = ((HttpServletRequestreq).getSession(false);
                if (session != null) {
                    session.setAttribute(newValue);
                }
            }
        }
    }
    public static final class Builder implements ExchangeAttributeBuilder {
        @Override
        public String name() {
            return "Servlet session attribute";
        }
        @Override
        public ExchangeAttribute build(final String token) {
            if (token.startsWith("%{s,") && token.endsWith("}")) {
                final String attributeName = token.substring(4, token.length() - 1);
                return new ServletSessionAttribute(attributeName);
            }
            return null;
        }
    }
New to GrepCode? Check out our FAQ X