org.zodiac.netty.http.headers.AllowHeader Maven / Gradle / Ivy
package org.zodiac.netty.http.headers;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.util.internal.AppendableCharSequence;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
final class AllowHeader extends AbstractHeader {
protected Logger log = LoggerFactory.getLogger(getClass());
AllowHeader(boolean isAllowOrigin) {
super(Method[].class, isAllowOrigin ? HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS : HttpHeaderNames.ALLOW);
}
@Override
public CharSequence toCharSequence(Method[] value) {
Objects.requireNonNull(value, "value");
if (value.length == 1) {
return value[0].toCharSequence();
}
AppendableCharSequence append = new AppendableCharSequence(15);
for (int i = 0; i < value.length; i++) {
append.append(value[i].toCharSequence());
if (i != value.length -1) {
append.append(',');
}
}
return append;
}
@Override
public Method[] toValue(CharSequence value) {
Objects.requireNonNull(value, "value");
CharSequence[] s = StrUtil.splitToArray(value, ',');
Method[] result = new Method[s.length];
for (int i = 0; i < s.length; i++) {
try {
result[i] = Method.valueOf(s[i]);
} catch (Exception e) {
log.warn(String.format("Bad methods in allow header '%s'", value), e);
return null;
}
}
return result;
}
}