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.part; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.net.*; 022import java.util.*; 023import java.util.function.*; 024 025/** 026 * An {@link org.apache.juneau.http.HttpPart} whose value is a URI. 027 * 028 * <p> 029 * Mirrors the semantics of {@code BasicUriPart} from {@code juneau-rest-common-classic} without 030 * the Apache HttpCore dependency. 031 * 032 * @since 10.0.0 033 */ 034@SuppressWarnings({ 035 "java:S2160" // Equality is fully determined by name + getValue() in HttpPartBean; the typed fields are derived from the wire value and add no state to compare 036}) 037public class HttpUriPart extends HttpPartBean { 038 039 /** 040 * Creates an {@link HttpUriPart} with the given name and URI value. 041 * 042 * @param name The part name. Must not be <jk>null</jk>. 043 * @param value The URI value. May be <jk>null</jk>. 044 * @return A new instance. Never <jk>null</jk>. 045 */ 046 public static HttpUriPart of(String name, URI value) { 047 return new HttpUriPart(name, value); 048 } 049 050 /** 051 * Creates an {@link HttpUriPart} by parsing the given wire value. 052 * 053 * @param name The part name. Must not be <jk>null</jk>. 054 * @param value The wire value. May be <jk>null</jk> or empty. 055 * @return A new instance. Never <jk>null</jk>. 056 */ 057 public static HttpUriPart ofString(String name, String value) { 058 return new HttpUriPart(name, value); 059 } 060 061 /** 062 * Creates an {@link HttpUriPart} with a lazy URI supplier. 063 * 064 * @param name The part name. Must not be <jk>null</jk>. 065 * @param supplier Supplier for the URI value. Must not be <jk>null</jk>. 066 * @return A new instance. Never <jk>null</jk>. 067 */ 068 public static HttpUriPart ofLazy(String name, Supplier<URI> supplier) { 069 return new HttpUriPart(name, supplier); 070 } 071 072 private final URI typedValue; 073 private final Supplier<URI> typedSupplier; 074 075 /** 076 * Constructor accepting a wire-format string value. 077 * 078 * @param name The part name. Must not be <jk>null</jk>. 079 * @param value The wire value. May be <jk>null</jk> or empty. 080 */ 081 public HttpUriPart(String name, String value) { 082 super(name, value); 083 this.typedValue = ie(value) ? null : URI.create(value); 084 this.typedSupplier = null; 085 } 086 087 /** 088 * Constructor accepting a {@link URI} value. 089 * 090 * @param name The part name. Must not be <jk>null</jk>. 091 * @param value The URI value. May be <jk>null</jk>. 092 */ 093 public HttpUriPart(String name, URI value) { 094 super(name, value == null ? null : value.toString()); 095 this.typedValue = value; 096 this.typedSupplier = null; 097 } 098 099 /** 100 * Constructor accepting a lazy {@link URI} supplier. 101 * 102 * @param name The part name. Must not be <jk>null</jk>. 103 * @param supplier Supplier for the URI value. Must not be <jk>null</jk>. 104 */ 105 public HttpUriPart(String name, Supplier<URI> supplier) { 106 super(name, () -> { 107 var v = supplier.get(); 108 return v == null ? null : v.toString(); 109 }); 110 this.typedValue = null; 111 this.typedSupplier = supplier; 112 } 113 114 /** 115 * Returns the value as an {@link Optional}{@code <URI>}. 116 * 117 * @return The URI value, wrapped in an {@link Optional}. Never <jk>null</jk>. 118 */ 119 public Optional<URI> asUri() { 120 return o(toUri()); 121 } 122 123 /** 124 * Returns the value if present, otherwise returns {@code other}. 125 * 126 * @param other The default value. 127 * @return The value or {@code other}. 128 */ 129 public URI orElse(URI other) { 130 var x = toUri(); 131 return nn(x) ? x : other; 132 } 133 134 /** 135 * Returns the value as a {@link URI}, or {@code null} if not set. 136 * 137 * @return The URI value, possibly {@code null}. 138 */ 139 public URI toUri() { 140 if (typedSupplier != null) 141 return typedSupplier.get(); 142 return typedValue; 143 } 144}