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.net.*; 020import java.util.*; 021import java.util.function.*; 022 023import org.apache.juneau.common.utils.*; 024 025/** 026 * Category of headers that consist of a single URL value. 027 * 028 * <p> 029 * <h5 class='figure'>Example</h5> 030 * <p class='bcode'> 031 * Location: http://www.w3.org/pub/WWW/People.html 032 * </p> 033 * 034 * <h5 class='section'>See Also:</h5><ul> 035 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a> 036 * <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a> 037 * </ul> 038 * 039 * @serial exclude 040 */ 041public class BasicUriHeader extends BasicHeader { 042 043 //----------------------------------------------------------------------------------------------------------------- 044 // Static 045 //----------------------------------------------------------------------------------------------------------------- 046 047 private static final long serialVersionUID = 1L; 048 049 /** 050 * Static creator. 051 * 052 * @param name The header name. 053 * @param value 054 * The header value. 055 * <br>Must be parsable by {@link URI#create(String)}. 056 * <br>Can be <jk>null</jk>. 057 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 058 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 059 */ 060 public static BasicUriHeader of(String name, String value) { 061 return value == null ? null : new BasicUriHeader(name, value); 062 } 063 064 /** 065 * Static creator. 066 * 067 * @param name The header name. 068 * @param value 069 * The header value. 070 * <br>Can be <jk>null</jk>. 071 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 072 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 073 */ 074 public static BasicUriHeader of(String name, URI value) { 075 return value == null ? null : new BasicUriHeader(name, value); 076 } 077 078 /** 079 * Static creator with delayed value. 080 * 081 * <p> 082 * Header value is re-evaluated on each call to {@link #getValue()}. 083 * 084 * @param name The header name. 085 * @param value 086 * The supplier of the header value. 087 * <br>Can be <jk>null</jk>. 088 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 089 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 090 */ 091 public static BasicUriHeader of(String name, Supplier<URI> value) { 092 return value == null ? null : new BasicUriHeader(name, value); 093 } 094 095 //----------------------------------------------------------------------------------------------------------------- 096 // Instance 097 //----------------------------------------------------------------------------------------------------------------- 098 099 private final URI value; 100 private final Supplier<URI> supplier; 101 102 /** 103 * Constructor. 104 * 105 * @param name The header name. 106 * @param value 107 * The header value. 108 * <br>Must be parsable by {@link URI#create(String)}. 109 * <br>Can be <jk>null</jk>. 110 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 111 */ 112 public BasicUriHeader(String name, String value) { 113 super(name, value); 114 this.value = Utils.isEmpty(value) ? null : URI.create(value); 115 this.supplier = null; 116 } 117 118 /** 119 * Constructor. 120 * 121 * @param name The header name. 122 * @param value 123 * The header value. 124 * <br>Can be <jk>null</jk>. 125 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 126 */ 127 public BasicUriHeader(String name, URI value) { 128 super(name, Utils.s(value)); 129 this.value = value; 130 this.supplier = null; 131 } 132 133 /** 134 * Constructor with delayed value. 135 * 136 * <p> 137 * Header value is re-evaluated on each call to {@link #getValue()}. 138 * 139 * @param name The header name. 140 * @param value 141 * The supplier of the header value. 142 * <br>Can be <jk>null</jk>. 143 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 144 */ 145 public BasicUriHeader(String name, Supplier<URI> value) { 146 super(name, null); 147 this.value = null; 148 this.supplier = value; 149 } 150 151 @Override /* Header */ 152 public String getValue() { 153 return Utils.s(value()); 154 } 155 156 /** 157 * Returns the header value as a {@link URI} wrapped in an {@link Optional}. 158 * 159 * @return The header value as a {@link URI} wrapped in an {@link Optional}. Never <jk>null</jk>. 160 */ 161 public Optional<URI> asUri() { 162 return Utils.opt(value()); 163 } 164 165 /** 166 * Returns the header value as a {@link URI} wrapped in an {@link Optional}. 167 * 168 * @return The header value as a {@link URI} wrapped in an {@link Optional}. Never <jk>null</jk>. 169 */ 170 public URI toUri() { 171 return value(); 172 } 173 174 /** 175 * Return the value if present, otherwise return <c>other</c>. 176 * 177 * <p> 178 * This is a shortened form for calling <c>asUri().orElse(<jv>other</jv>)</c>. 179 * 180 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 181 * @return The value, if present, otherwise <c>other</c>. 182 */ 183 public URI orElse(URI other) { 184 URI x = value(); 185 return x != null ? x : other; 186 } 187 188 private URI value() { 189 if (supplier != null) 190 return supplier.get(); 191 return value; 192 } 193}