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 (the 006 * "License"); you may not use this file except in compliance with the 007 * 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; 018import static org.apache.juneau.commons.utils.Shorts.*; 019 020import java.net.*; 021import java.util.*; 022import java.util.function.*; 023 024/** 025 * Base for single-URI headers. 026 * 027 * @since 9.2.1 028 */ 029@SuppressWarnings({ 030 "java:S2160", // equals() on HttpHeaderBean uses name + getValue(); typed state is reflected in getValue() 031 "java:S9149", // Public Juneau DSL factory; hiding parent of()/create() is intentional. 032 "unchecked" // Supplier<?> branches cast to typed suppliers after lazy-mode check 033}) 034public class HttpUriHeader extends HttpHeaderBean { 035 036 public static final int LAZY_WIRE_STRING = 0; 037 public static final int LAZY_URI = 1; 038 039 private final URI cachedUri; 040 private final Supplier<?> lazySupplier; 041 private final int lazyMode; 042 043 /** 044 * Creates an {@link HttpUriHeader} by parsing the given wire-format value. 045 * 046 * @param name Header name. Must not be {@code null}. 047 * @param wireValue Wire value. May be {@code null} or empty. 048 * @return A new instance. Never {@code null}. 049 */ 050 public static HttpUriHeader of(String name, String wireValue) { 051 return new HttpUriHeader(name, wireValue); 052 } 053 054 /** 055 * Creates an {@link HttpUriHeader} with the given typed value. 056 * 057 * @param name Header name. Must not be {@code null}. 058 * @param typedValue The URI value. May be {@code null}. 059 * @return A new instance. Never {@code null}. 060 */ 061 public static HttpUriHeader of(String name, URI typedValue) { 062 return new HttpUriHeader(name, typedValue); 063 } 064 065 /** 066 * Constructor. 067 * 068 * @param name Header name. Must not be <jk>null</jk>. 069 * @param value Wire value. Can be <jk>null</jk> or empty, in which case the parsed value is <jk>null</jk>. 070 */ 071 protected HttpUriHeader(String name, String value) { 072 super(name, value); 073 this.cachedUri = ie(value) ? null : URI.create(value); 074 this.lazySupplier = null; 075 this.lazyMode = -1; 076 } 077 078 /** 079 * Constructor. 080 * 081 * @param name Header name. Must not be <jk>null</jk>. 082 * @param value The URI value. Can be <jk>null</jk>, in which case the parsed value is <jk>null</jk>. 083 */ 084 protected HttpUriHeader(String name, URI value) { 085 super(name, s(value)); 086 this.cachedUri = value; 087 this.lazySupplier = null; 088 this.lazyMode = -1; 089 } 090 091 /** 092 * Constructor with lazy value supplier. 093 * 094 * @param name Header name. Must not be <jk>null</jk>. 095 * @param supplier The lazy value supplier. Must not be <jk>null</jk>. 096 * @param lazyMode Either {@link #LAZY_WIRE_STRING} or {@link #LAZY_URI}. 097 */ 098 protected HttpUriHeader(String name, Supplier<?> supplier, int lazyMode) { 099 super(name, lazyMode == LAZY_WIRE_STRING 100 ? ((Supplier<String>) supplier)::get 101 : () -> s(((Supplier<URI>) supplier).get())); 102 this.cachedUri = null; 103 this.lazySupplier = supplier; 104 this.lazyMode = lazyMode; 105 } 106 107 public Optional<URI> asUri() { 108 return o(toUri()); 109 } 110 111 /** 112 * Returns the wire-format value of this header. 113 * 114 * @return The wire value, or <jk>null</jk> if the value is unset. 115 */ 116 @Override 117 public String getValue() { 118 return s(toUri()); 119 } 120 121 /** 122 * Returns the parsed value of this header, or the specified default if unset. 123 * 124 * @param other The default value. Can be <jk>null</jk>. 125 * @return The parsed value, or <c>other</c> if the value is unset. Can be <jk>null</jk> if <c>other</c> is <jk>null</jk>. 126 */ 127 public URI orElse(URI other) { 128 var x = toUri(); 129 return nn(x) ? x : other; 130 } 131 132 /** 133 * Returns the parsed value of this header. 134 * 135 * @return The parsed value, or <jk>null</jk> if the value is unset. 136 */ 137 public URI toUri() { 138 if (lazyMode == LAZY_URI) 139 return ((Supplier<URI>) lazySupplier).get(); 140 if (cachedUri != null) 141 return cachedUri; 142 var v = super.getValue(); 143 return ie(v) ? null : URI.create(v); 144 } 145}