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
026 * to an HTTP request.
027 */
028public interface HttpResponse {
029  /**
030   * Sets the numeric HTTP
031   * <a 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
033   * named constants in {@link java.net.HttpURLConnection}, such as
034   * {@link java.net.HttpURLConnection#HTTP_OK HTTP_OK}, can be used as an alternative to writing
035   * numbers in your source code.
036   *
037   * @param code the status code.
038   */
039  void setStatusCode(int code);
040
041  /**
042   * Sets the numeric HTTP
043   * <a 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
046   * named constants in {@link java.net.HttpURLConnection}, such as
047   * {@link java.net.HttpURLConnection#HTTP_BAD_REQUEST HTTP_BAD_REQUEST}, can be used as an
048   * alternative to writing numbers in your source code.
049   *
050   * @param code the status code.
051   * @param message the status message.
052   */
053  void setStatusCode(int code, String message);
054
055  /**
056   * Sets the value to use for the {@code Content-Type} header in the response. This may include
057   * a character encoding, for example {@code setContentType("text/plain; charset=utf-8")}.
058   *
059   * @param contentType the content type.
060   */
061  void setContentType(String contentType);
062
063  /**
064   * Returns the {@code Content-Type} that was previously set by {@link #setContentType}, or by
065   * {@link #appendHeader} with a header name of {@code Content-Type}. If no {@code Content-Type}
066   * has been set, returns {@code Optional.empty()}.
067   *
068   * @return the content type, if any.
069   */
070  Optional<String> getContentType();
071
072  /**
073   * Includes the given header name with the given value in the response. This method may be called
074   * several times for the same header, in which case the response will contain the header the same
075   * number of times.
076   *
077   * @param header an HTTP header, such as {@code Content-Type}.
078   * @param value a value to associate with that header.
079   */
080  void appendHeader(String header, String value);
081
082  /**
083   * Returns the headers that have been defined for the response so far. This will contain at least
084   * the headers that have been set via {@link #appendHeader} or {@link #setContentType}, and may
085   * contain additional headers such as {@code Date}.
086   *
087   * @return a map where each key is a header name and the corresponding {@code List} value has one
088   *     entry for every value associated with that header.
089   */
090  Map<String, List<String>> getHeaders();
091
092  /**
093   * Returns an {@link OutputStream} that can be used to write the body of the response.
094   * This method is typically used to write binary data. If the body is text, the
095   * {@link #getWriter()} method is more appropriate.
096   *
097   * @return the output stream.
098   * @throws IOException if a valid {@link OutputStream} cannot be returned for some reason.
099   * @throws IllegalStateException if {@link #getWriter} has already been called on this instance.
100   */
101  OutputStream getOutputStream() throws IOException;
102
103  /**
104   * Returns a {@link BufferedWriter} that can be used to write the text body of the response.
105   * If the written text will not be US-ASCII, you should specify a character encoding by calling
106   * {@link #setContentType setContentType("text/foo; charset=bar")} or
107   * {@link #appendHeader appendHeader("Content-Type", "text/foo; charset=bar")}
108   * before calling this method.
109   *
110   * @return the writer.
111   * @throws IOException if a valid {@link BufferedWriter} cannot be returned for some reason.
112   * @throws IllegalStateException if {@link #getOutputStream} has already been called on this
113   *     instance.
114   */
115  BufferedWriter getWriter() throws IOException;
116}