blob: 4479f1cf1f937881679be0ba49ca71f5cb6c059b [file] [log] [blame]
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.http.header;
import static org.apache.juneau.internal.StringUtils.*;
import static java.util.stream.Collectors.*;
import static java.util.Optional.*;
import static java.util.Collections.*;
import java.util.*;
import java.util.function.*;
/**
* Category of headers that consist of a comma-delimited list of entity validator values.
*
* <p>
* <h5 class='figure'>Example</h5>
* <p class='bcode w800'>
* If-Match: "xyzzy"
* If-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"
* If-Match: *
* </p>
*
* <ul class='seealso'>
* <li class='extlink'>{@doc ExtRFC2616}
* </ul>
*/
public class BasicEntityTagArrayHeader extends BasicHeader {
private static final long serialVersionUID = 1L;
/**
* Convenience creator.
*
* @param name The header name.
* @param value
* The header value.
* <br>Must be a comma-delimited list of entity validator values (e.g. <js>"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""</js>).
* <br>Can be <jk>null</jk>.
* @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
*/
public static BasicEntityTagArrayHeader of(String name, String value) {
if (isEmpty(name) || value == null)
return null;
return new BasicEntityTagArrayHeader(name, value);
}
/**
* Convenience creator.
*
* @param name The header name.
* @param value
* The header value.
* <br>Can be <jk>null</jk>.
* @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
*/
public static BasicEntityTagArrayHeader of(String name, List<EntityTag> value) {
if (isEmpty(name) || value == null)
return null;
return new BasicEntityTagArrayHeader(name, value);
}
/**
* Convenience creator with delayed value.
*
* <p>
* Header value is re-evaluated on each call to {@link #getValue()}.
*
* @param name The header name.
* @param value
* The supplier of the header value.
* <br>Can be <jk>null</jk>.
* @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
*/
public static BasicEntityTagArrayHeader of(String name, Supplier<List<EntityTag>> value) {
if (isEmpty(name) || value == null)
return null;
return new BasicEntityTagArrayHeader(name, value);
}
private final List<EntityTag> value;
private final Supplier<List<EntityTag>> supplier;
/**
* Constructor.
*
* @param name The header name.
* @param value
* The header value.
* <br>Must be a comma-delimited list of entity validator values (e.g. <js>"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""</js>).
* <br>Can be <jk>null</jk>.
*/
public BasicEntityTagArrayHeader(String name, String value) {
super(name, value);
this.value = parse(value);
this.supplier = null;
}
/**
* Constructor.
*
* @param name The header name.
* @param value
* The header value.
* <br>Can be <jk>null</jk>.
*/
public BasicEntityTagArrayHeader(String name, List<EntityTag> value) {
super(name, serialize(value));
this.value = value == null ? null : unmodifiableList(value);
this.supplier = null;
}
/**
* Constructor with delayed value.
*
* <p>
* Header value is re-evaluated on each call to {@link #getValue()}.
*
* @param name The header name.
* @param value
* The supplier of the header value.
* <br>Can be <jk>null</jk>.
*/
public BasicEntityTagArrayHeader(String name, Supplier<List<EntityTag>> value) {
super(name, null);
this.value = null;
this.supplier = value;
}
@Override /* Header */
public String getValue() {
if (supplier != null)
return serialize(supplier.get());
return super.getValue();
}
/**
* Returns this header value as an array of {@link EntityTag} objects.
*
* @return this header value as an array of {@link EntityTag} objects, or {@link Optional#empty()} if the value was <jk>null</jk>..
*/
public Optional<List<EntityTag>> asEntityTags() {
if (supplier != null)
return ofNullable(supplier.get());
return ofNullable(value);
}
private static String serialize(List<EntityTag> value) {
return join(value, ", ");
}
private List<EntityTag> parse(String value) {
return value == null ? null : Arrays.asList(split(value, ',')).stream().map(x -> EntityTag.of(x)).collect(toList());
}
}