001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.http.header; 018 019import java.util.*; 020import java.util.function.*; 021 022import org.apache.juneau.*; 023import org.apache.juneau.assertions.*; 024import org.apache.juneau.common.utils.*; 025import org.apache.juneau.http.annotation.*; 026import org.apache.juneau.internal.*; 027 028/** 029 * Represents a parsed <l>Client-Version</l> HTTP request header. 030 * 031 * <p> 032 * Specifies a client-side version number. 033 * 034 * <h5 class='figure'>Example</h5> 035 * <p class='bcode'> 036 * Client-Version: 2.0.1 037 * </p> 038 * 039 * <p> 040 * Not part of the RFC2616 specification, but provided to allow for HTTP responses to be tailored to specified 041 * known client versions. 042 * 043 * <h5 class='section'>See Also:</h5><ul> 044 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a> 045 * </ul> 046 * 047 * @serial exclude 048 */ 049@Header("Client-Version") 050public class ClientVersion extends BasicStringHeader { 051 052 //----------------------------------------------------------------------------------------------------------------- 053 // Static 054 //----------------------------------------------------------------------------------------------------------------- 055 056 private static final long serialVersionUID = 1L; 057 private static final String NAME = "Client-Version"; 058 059 private static final Cache<String,ClientVersion> CACHE = Cache.of(String.class, ClientVersion.class).build(); 060 061 /** 062 * Static creator. 063 * 064 * @param value 065 * The header value. 066 * <br>Must be parsable by {@link Version#of(String)} 067 * <br>Can be <jk>null</jk>. 068 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 069 */ 070 public static ClientVersion of(String value) { 071 return value == null ? null : CACHE.get(value, ()->new ClientVersion(value)); 072 } 073 074 /** 075 * Static creator. 076 * 077 * @param value 078 * The header value. 079 * <br>Can be <jk>null</jk>. 080 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 081 */ 082 public static ClientVersion of(Version value) { 083 return value == null ? null : new ClientVersion(value); 084 } 085 086 /** 087 * Static creator with delayed value. 088 * 089 * <p> 090 * Header value is re-evaluated on each call to {@link #getValue()}. 091 * 092 * @param value 093 * The supplier of the header value. 094 * <br>Can be <jk>null</jk>. 095 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 096 */ 097 public static ClientVersion of(Supplier<Version> value) { 098 return value == null ? null : new ClientVersion(value); 099 } 100 101 //----------------------------------------------------------------------------------------------------------------- 102 // Instance 103 //----------------------------------------------------------------------------------------------------------------- 104 105 private final Version value; 106 private final Supplier<Version> supplier; 107 108 /** 109 * Constructor. 110 * 111 * @param value 112 * The header value. 113 * <br>Must be parsable by {@link Version#of(String)} 114 * <br>Can be <jk>null</jk>. 115 */ 116 public ClientVersion(String value) { 117 super(NAME, value); 118 this.value = Version.of(value); 119 this.supplier = null; 120 } 121 122 /** 123 * Constructor. 124 * 125 * @param value 126 * The header value. 127 * <br>Can be <jk>null</jk>. 128 */ 129 public ClientVersion(Version value) { 130 super(NAME, Utils.s(value)); 131 this.value = value; 132 this.supplier = null; 133 } 134 135 /** 136 * Constructor with delayed value. 137 * 138 * <p> 139 * Header value is re-evaluated on each call to {@link #getValue()}. 140 * 141 * @param value 142 * The supplier of the header value. 143 * <br>Can be <jk>null</jk>. 144 */ 145 public ClientVersion(Supplier<Version> value) { 146 super(NAME, (String)null); 147 this.value = null; 148 this.supplier = value; 149 } 150 151 @Override /* Header */ 152 public String getValue() { 153 if (supplier != null) 154 return Utils.s(supplier.get()); 155 return super.getValue(); 156 } 157 158 /** 159 * Returns the header value as a {@link Version} object. 160 * 161 * @return The header value as a {@link Version} object, or {@link Optional#empty()} if the value is <jk>null</jk>. 162 */ 163 public Optional<Version> asVersion() { 164 return Utils.opt(value); 165 } 166 167 /** 168 * Provides the ability to perform fluent-style assertions on this header. 169 * 170 * <h5 class='section'>Examples:</h5> 171 * <p class='bjava'> 172 * <jc>// Validates the response content is older than 1.</jc> 173 * <jv>client</jv> 174 * .get(<jsf>URL</jsf>) 175 * .run() 176 * .getHeader(ClientVersion.<jk>class</jk>).assertVersion().major().isGreaterThan(1); 177 * </p> 178 * 179 * @return A new fluent assertion object. 180 * @throws AssertionError If assertion failed. 181 */ 182 public FluentVersionAssertion<ClientVersion> assertVersion() { 183 return new FluentVersionAssertion<>(asVersion().orElse(null), this); 184 } 185}