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.response; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import org.apache.juneau.http.*; 022 023/** 024 * Default immutable implementation of {@link HttpStatusLine}. 025 * 026 * <p> 027 * Create instances via the static factory methods: 028 * <p class='bjava'> 029 * <jc>// 200 OK HTTP/1.1</jc> 030 * HttpStatusLine <jv>sl</jv> = HttpStatusLineBean.<jsm>of</jsm>(200, <js>"OK"</js>); 031 * 032 * <jc>// Custom protocol version (parsed)</jc> 033 * HttpStatusLine <jv>sl2</jv> = HttpStatusLineBean.<jsm>of</jsm>(<js>"HTTP/2"</js>, 200, <js>"OK"</js>); 034 * 035 * <jc>// Custom protocol version (typed)</jc> 036 * HttpStatusLine <jv>sl3</jv> = HttpStatusLineBean.<jsm>of</jsm>(HttpProtocolVersion.<jsf>HTTP_2_0</jsf>, 200, <js>"OK"</js>); 037 * </p> 038 * 039 * <h5 class='section'>See Also:</h5><ul> 040 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a> 041 * </ul> 042 * 043 * @since 9.2.1 044 */ 045public final class HttpStatusLineBean implements HttpStatusLine { 046 047 private final HttpProtocolVersion protocolVersion; 048 private final int statusCode; 049 private final String reasonPhrase; 050 051 private HttpStatusLineBean(HttpProtocolVersion protocolVersion, int statusCode, String reasonPhrase) { 052 this.protocolVersion = reqnn("protocolVersion", protocolVersion); 053 this.statusCode = statusCode; 054 this.reasonPhrase = reasonPhrase; 055 } 056 057 /** 058 * Creates an {@code HTTP/1.1} status line with the given status code and reason phrase. 059 * 060 * @param statusCode The HTTP status code (e.g. {@code 200}). 061 * @param reasonPhrase The reason phrase (e.g. {@code "OK"}). May be <jk>null</jk>. 062 * @return A new instance. Never <jk>null</jk>. 063 */ 064 public static HttpStatusLineBean of(int statusCode, String reasonPhrase) { 065 return new HttpStatusLineBean(HttpProtocolVersion.HTTP_1_1, statusCode, reasonPhrase); 066 } 067 068 /** 069 * Creates a status line with a parsed protocol version, status code, and reason phrase. 070 * 071 * @param protocolVersion The protocol version string (e.g. {@code "HTTP/2"}). Must not be <jk>null</jk>. 072 * @param statusCode The HTTP status code. 073 * @param reasonPhrase The reason phrase. May be <jk>null</jk>. 074 * @return A new instance. Never <jk>null</jk>. 075 */ 076 public static HttpStatusLineBean of(String protocolVersion, int statusCode, String reasonPhrase) { 077 return new HttpStatusLineBean(HttpProtocolVersion.parse(protocolVersion), statusCode, reasonPhrase); 078 } 079 080 /** 081 * Creates a status line with the given typed protocol version, status code, and reason phrase. 082 * 083 * @param protocolVersion The protocol version. Must not be <jk>null</jk>. 084 * @param statusCode The HTTP status code. 085 * @param reasonPhrase The reason phrase. May be <jk>null</jk>. 086 * @return A new instance. Never <jk>null</jk>. 087 */ 088 public static HttpStatusLineBean of(HttpProtocolVersion protocolVersion, int statusCode, String reasonPhrase) { 089 return new HttpStatusLineBean(protocolVersion, statusCode, reasonPhrase); 090 } 091 092 @Override /* HttpStatusLine */ 093 public int getStatusCode() { 094 return statusCode; 095 } 096 097 @Override /* HttpStatusLine */ 098 public String getReasonPhrase() { 099 return reasonPhrase; 100 } 101 102 @Override /* HttpStatusLine */ 103 public HttpProtocolVersion getProtocolVersion() { 104 return protocolVersion; 105 } 106 107 @Override /* Object */ 108 public String toString() { 109 var sb = new StringBuilder(protocolVersion.toString()).append(' ').append(statusCode); 110 if (reasonPhrase != null) 111 sb.append(' ').append(reasonPhrase); 112 return sb.toString(); 113 } 114 115 @Override /* Object */ 116 public boolean equals(Object obj) { 117 return (obj instanceof HttpStatusLineBean o2) && eq(this, o2, (x, y) -> 118 x.statusCode == y.statusCode && eq(x.protocolVersion, y.protocolVersion) && eq(x.reasonPhrase, y.reasonPhrase)); 119 } 120 121 @Override /* Object */ 122 public int hashCode() { 123 return h(protocolVersion, statusCode, reasonPhrase); 124 } 125}