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.util.*; 022import java.util.function.*; 023 024/** 025 * An {@link org.apache.juneau.http.HttpPart} whose value is a string. 026 * 027 * <p> 028 * Mirrors the semantics of {@code BasicStringPart} from {@code juneau-rest-common-classic} without 029 * the Apache HttpCore dependency. 030 * 031 * @since 10.0.0 032 */ 033@SuppressWarnings({ 034 "java:S9149" // Public Juneau DSL factory; hiding parent of()/create() is intentional. 035}) 036public class HttpStringPart extends HttpPartBean { 037 038 /** 039 * Creates an {@link HttpStringPart} with the given name and string value. 040 * 041 * @param name The part name. Must not be <jk>null</jk>. 042 * @param value The part value. May be <jk>null</jk>. 043 * @return A new instance. Never <jk>null</jk>. 044 */ 045 public static HttpStringPart of(String name, String value) { 046 return new HttpStringPart(name, value); 047 } 048 049 /** 050 * Creates an {@link HttpStringPart} with the given name and lazy value supplier. 051 * 052 * @param name The part name. Must not be <jk>null</jk>. 053 * @param valueSupplier Supplier for the value. Must not be <jk>null</jk>. 054 * @return A new instance. Never <jk>null</jk>. 055 */ 056 public static HttpStringPart of(String name, Supplier<String> valueSupplier) { 057 return new HttpStringPart(name, valueSupplier); 058 } 059 060 /** 061 * Parses a part pair of the form {@code "name=value"} (or {@code "name:value"}) into an 062 * {@link HttpStringPart}. 063 * 064 * <p> 065 * If the input does not contain {@code '='} or {@code ':'}, the entire string is treated as a part name 066 * with an empty value. 067 * 068 * @param pair The part pair. May be {@code null}. 069 * @return A new instance, or {@code null} if {@code pair} is {@code null}. 070 */ 071 public static HttpStringPart ofPair(String pair) { 072 if (pair == null) 073 return null; 074 var i = pair.indexOf('='); 075 if (i == -1) 076 i = pair.indexOf(':'); 077 if (i == -1) 078 return of(pair, ""); 079 return of(pair.substring(0, i).trim(), pair.substring(i + 1).trim()); 080 } 081 082 /** 083 * Constructor. 084 * 085 * @param name The part name. Must not be <jk>null</jk>. 086 * @param value The part value. May be <jk>null</jk>. 087 */ 088 protected HttpStringPart(String name, String value) { 089 super(name, value); 090 } 091 092 /** 093 * Constructor with lazy value supplier. 094 * 095 * @param name The part name. Must not be <jk>null</jk>. 096 * @param valueSupplier Supplier that provides the value at request time. Must not be <jk>null</jk>. 097 */ 098 protected HttpStringPart(String name, Supplier<String> valueSupplier) { 099 super(name, valueSupplier); 100 } 101 102 /** 103 * Returns the value as an {@link Optional}. 104 * 105 * @return The value, wrapped in an {@link Optional}. Never <jk>null</jk>. 106 */ 107 public Optional<String> asString() { 108 return o(getValue()); 109 } 110 111 /** 112 * Returns the value if present, otherwise returns {@code other}. 113 * 114 * @param other The default value. 115 * @return The value or {@code other}. 116 */ 117 public String orElse(String other) { 118 var x = getValue(); 119 return nn(x) ? x : other; 120 } 121}