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

io.undertow.attribute.SubstituteEmptyWrapper Maven / Gradle / Ivy

There is a newer version: 2.3.18.Final
Show newest version
package io.undertow.attribute;

import io.undertow.server.HttpServerExchange;

/**
 * @author Stuart Douglas
 */
public class SubstituteEmptyWrapper implements ExchangeAttributeWrapper {

    private final String substitute;

    public SubstituteEmptyWrapper(String substitute) {
        this.substitute = substitute;
    }

    @Override
    public ExchangeAttribute wrap(final ExchangeAttribute attribute) {
        return new SubstituteEmptyAttribute(attribute, substitute);
    }

    public static class SubstituteEmptyAttribute implements ExchangeAttribute {
        private final ExchangeAttribute attribute;
        private final String substitute;

        public SubstituteEmptyAttribute(ExchangeAttribute attribute, String substitute) {
            this.attribute = attribute;
            this.substitute = substitute;
        }

        @Override
        public String readAttribute(HttpServerExchange exchange) {
            String val = attribute.readAttribute(exchange);
            if(val == null || val.isEmpty()) {
                return substitute;
            }
            return val;
        }

        @Override
        public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException {
            attribute.writeAttribute(exchange, newValue);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy