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 java.util.*;
022
023import org.apache.juneau.http.*;
024
025/**
026 * A complete HTTP response message: status line, headers, and optional body.
027 *
028 * <p>
029 * Mirrors the semantics of {@code org.apache.http.HttpResponse} without the Apache HttpCore dependency.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a>
033 * </ul>
034 *
035 * @since 9.2.1
036 */
037public interface HttpResponseMessage {
038
039   /**
040    * Returns the status line of this response.
041    *
042    * @return The status line. Never <jk>null</jk>.
043    */
044   HttpStatusLine getStatusLine();
045
046   /**
047    * Returns all response headers, in the order they were received.
048    *
049    * @return An unmodifiable list of response headers. Never <jk>null</jk>.
050    */
051   List<HttpHeader> getHeaders();
052
053   /**
054    * Returns the first response header with the given name (case-insensitive), or <jk>null</jk> if absent.
055    *
056    * @param name The header name. Must not be <jk>null</jk>.
057    * @return The first matching header, or <jk>null</jk> if absent.
058    */
059   default HttpHeader getFirstHeader(String name) {
060      return getHeaders().stream()
061         .filter(h -> eqic(h.getName(), name))
062         .findFirst()
063         .orElse(null);
064   }
065
066   /**
067    * Returns all response headers with the given name (case-insensitive).
068    *
069    * @param name The header name. Must not be <jk>null</jk>.
070    * @return A list of matching headers (possibly empty). Never <jk>null</jk>.
071    */
072   default List<HttpHeader> getHeaders(String name) {
073      return getHeaders().stream()
074         .filter(h -> eqic(h.getName(), name))
075         .toList();
076   }
077
078   /**
079    * Returns the response body, or <jk>null</jk> if the response has no body.
080    *
081    * @return The response body, possibly <jk>null</jk>.
082    */
083   HttpBody getBody();
084}