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; 018 019import java.util.function.*; 020 021/** 022 * Base type for HTTP headers whose value is an opaque string (or lazy {@link Supplier} of string). 023 * 024 * <h5 class='section'>See Also:</h5><ul> 025 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a> 026 * </ul> 027 * 028 * @since 9.2.1 029 */ 030@SuppressWarnings({ 031 "java:S9149" // Public Juneau DSL factory; hiding parent of()/create() is intentional. 032}) 033public class HttpStringHeader extends HttpHeaderBean { 034 035 /** 036 * Creates an {@link HttpStringHeader} with the given name and value. 037 * 038 * @param name Header name. Must not be {@code null}. 039 * @param value Wire value. May be {@code null}. 040 * @return A new instance. Never {@code null}. 041 */ 042 public static HttpStringHeader of(String name, String value) { 043 return new HttpStringHeader(name, value); 044 } 045 046 /** 047 * Creates an {@link HttpStringHeader} with the given name and lazy value supplier. 048 * 049 * @param name Header name. Must not be {@code null}. 050 * @param valueSupplier Lazy wire value supplier. Must not be {@code null}. 051 * @return A new instance. Never {@code null}. 052 */ 053 public static HttpStringHeader of(String name, Supplier<String> valueSupplier) { 054 return new HttpStringHeader(name, valueSupplier); 055 } 056 057 /** 058 * Parses a header line of the form {@code "Name: value"} (or {@code "Name=value"}) into an 059 * {@link HttpStringHeader}. 060 * 061 * <p> 062 * If the input does not contain {@code ':'} or {@code '='}, the entire string is treated as a header name 063 * with an empty value. 064 * 065 * @param pair The header pair. May be {@code null}. 066 * @return A new instance, or {@code null} if {@code pair} is {@code null}. 067 */ 068 public static HttpStringHeader ofPair(String pair) { 069 if (pair == null) 070 return null; 071 var i = pair.indexOf(':'); 072 if (i == -1) 073 i = pair.indexOf('='); 074 if (i == -1) 075 return of(pair, ""); 076 return of(pair.substring(0, i).trim(), pair.substring(i + 1).trim()); 077 } 078 079 /** 080 * @param name Header name. Must not be {@code null}. 081 * @param value Wire value. May be {@code null}. 082 */ 083 protected HttpStringHeader(String name, String value) { 084 super(name, value); 085 } 086 087 /** 088 * @param name Header name. Must not be {@code null}. 089 * @param valueSupplier Lazy wire value supplier. Must not be {@code null}. 090 */ 091 protected HttpStringHeader(String name, Supplier<String> valueSupplier) { 092 super(name, valueSupplier); 093 } 094}