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 * Represents an <c>HTTP 204 No Content</c> response.
025 *
026 * <p>
027 * The server successfully processed the request and is not returning any content.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a>
031 * </ul>
032 *
033 * @since 9.2.1
034 */
035public class NoContent extends BasicHttpResponse<NoContent> {
036
037   /** HTTP status code */
038   public static final int STATUS_CODE = 204;
039
040   /** Reason phrase */
041   public static final String REASON_PHRASE = "No Content";
042
043   /** Default status line */
044   private static final HttpStatusLine STATUS_LINE = HttpStatusLineBean.of(STATUS_CODE, REASON_PHRASE);
045
046   /** Default unmodifiable instance */
047   public static final NoContent INSTANCE = new NoContent().unmodifiable();
048
049   /**
050    * Constructor.
051    */
052   public NoContent() {
053      super(STATUS_LINE);
054   }
055
056   /**
057    * Constructor with a response body.
058    *
059    * @param body The response body. May be <jk>null</jk>.
060    */
061   public NoContent(HttpBody body) {
062      super(STATUS_LINE, body);
063   }
064
065   /**
066    * Constructor with a plain-text string body.
067    *
068    * @param body The response body as a plain-text string. May be <jk>null</jk>.
069    */
070   public NoContent(String body) {
071      super(STATUS_LINE, body);
072   }
073
074   /**
075    * Copy constructor.
076    *
077    * @param copyFrom The bean to copy from. Must not be <jk>null</jk>.
078    */
079   public NoContent(NoContent copyFrom) {
080      super(copyFrom);
081   }
082
083   @Override /* Overridden from BasicHttpResponse */
084   public NoContent unmodifiable() {
085      return this instanceof UnmodifiableBean ? this : new Unmodifiable(this);
086   }
087
088   /**
089    * Unmodifiable point-in-time snapshot of the enclosing {@link NoContent} response.
090    *
091    * <p>
092    * Its only behavioral override is {@link #modify(Runnable)}, which throws — because all mutation is funneled through
093    * {@code modify(...)}, this single override freezes the entire mutation surface.
094    */
095   public static class Unmodifiable extends NoContent implements UnmodifiableBean {
096
097      /**
098       * Constructor.
099       *
100       * @param copyFrom The response to snapshot.  Must not be <jk>null</jk>.
101       */
102      @SuppressWarnings({
103         "java:S1699" // Paradigm intentionally calls the overridable freeze() from the ctor to deep-freeze sub-beans.
104      })
105      protected Unmodifiable(NoContent copyFrom) {
106         super(copyFrom);
107         freeze();
108      }
109
110      @Override /* Overridden from BasicHttpResponse */
111      protected NoContent modify(Runnable mutation) {
112         throw uoex("Bean is unmodifiable.");
113      }
114   }
115}