001// Copyright 2019 Google LLC
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//      http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package com.google.cloud.functions;
016
017import java.io.BufferedWriter;
018import java.io.IOException;
019import java.io.OutputStream;
020import java.util.List;
021import java.util.Map;
022import java.util.Optional;
023
024/**
025 * Represents the contents of an HTTP response that is being sent by a Cloud Function in response to
026 * an HTTP request.
027 */
028public interface HttpResponse {
029  /**
030   * Sets the numeric HTTP <a
031   * href="https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml">status
032   * code</a> to use in the response. Most often this will be 200, which is the OK status. The named
033   * constants in {@link java.net.HttpURLConnection}, such as {@link
034   * java.net.HttpURLConnection#HTTP_OK HTTP_OK}, can be used as an alternative to writing numbers
035   * in your source code.
036   *
037   * @param code the status code.
038   */
039  void setStatusCode(int code);
040
041  /**
042   * Sets the numeric HTTP <a
043   * href="https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml">status
044   * code</a> and reason message to use in the response. For example<br>
045   * {@code setStatusCode(400, "Something went wrong")}. The named constants in {@link
046   * java.net.HttpURLConnection}, such as {@link java.net.HttpURLConnection#HTTP_BAD_REQUEST
047   * HTTP_BAD_REQUEST}, can be used as an alternative to writing numbers in your source code.
048   *
049   * @param code the status code.
050   * @param message the status message.
051   */
052  void setStatusCode(int code, String message);
053
054  /**
055   * Sets the value to use for the {@code Content-Type} header in the response. This may include a
056   * character encoding, for example {@code setContentType("text/plain; charset=utf-8")}.
057   *
058   * @param contentType the content type.
059   */
060  void setContentType(String contentType);
061
062  /**
063   * Returns the {@code Content-Type} that was previously set by {@link #setContentType}, or by
064   * {@link #appendHeader} with a header name of {@code Content-Type}. If no {@code Content-Type}
065   * has been set, returns {@code Optional.empty()}.
066   *
067   * @return the content type, if any.
068   */
069  Optional<String> getContentType();
070
071  /**
072   * Includes the given header name with the given value in the response. This method may be called
073   * several times for the same header, in which case the response will contain the header the same
074   * number of times.
075   *
076   * @param header an HTTP header, such as {@code Content-Type}.
077   * @param value a value to associate with that header.
078   */
079  void appendHeader(String header, String value);
080
081  /**
082   * Returns the headers that have been defined for the response so far. This will contain at least
083   * the headers that have been set via {@link #appendHeader} or {@link #setContentType}, and may
084   * contain additional headers such as {@code Date}.
085   *
086   * @return a map where each key is a header name and the corresponding {@code List} value has one
087   *     entry for every value associated with that header.
088   */
089  Map<String, List<String>> getHeaders();
090
091  /**
092   * Returns an {@link OutputStream} that can be used to write the body of the response. This method
093   * is typically used to write binary data. If the body is text, the {@link #getWriter()} method is
094   * more appropriate.
095   *
096   * @return the output stream.
097   * @throws IOException if a valid {@link OutputStream} cannot be returned for some reason.
098   * @throws IllegalStateException if {@link #getWriter} has already been called on this instance.
099   */
100  OutputStream getOutputStream() throws IOException;
101
102  /**
103   * Returns a {@link BufferedWriter} that can be used to write the text body of the response. If
104   * the written text will not be US-ASCII, you should specify a character encoding by calling
105   * {@link #setContentType setContentType("text/foo; charset=bar")} or {@link #appendHeader
106   * appendHeader("Content-Type", "text/foo; charset=bar")} before calling this method.
107   *
108   * @return the writer.
109   * @throws IOException if a valid {@link BufferedWriter} cannot be returned for some reason.
110   * @throws IllegalStateException if {@link #getOutputStream} has already been called on this
111   *     instance.
112   */
113  BufferedWriter getWriter() throws IOException;
114}