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.classic.response;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.http.classic.response.Created.*;
021
022import org.apache.http.*;
023import org.apache.juneau.commons.*;
024import org.apache.juneau.http.*;
025import org.apache.juneau.http.classic.*;
026
027/**
028 * Represents an <c>HTTP 201 Created</c> response.
029 *
030 * <p>
031 * The request has been fulfilled, resulting in the creation of a new resource.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
035 * </ul>
036 */
037@Response
038@StatusCode(STATUS_CODE)
039@Schema(description = REASON_PHRASE)
040public class Created extends BasicHttpResponse<Created> {
041
042   /** HTTP status code */
043   public static final int STATUS_CODE = 201;
044
045   /** Reason phrase */
046   public static final String REASON_PHRASE = "Created";
047
048   /** Default status line */
049   private static final BasicStatusLine STATUS_LINE = BasicStatusLine.create(STATUS_CODE, REASON_PHRASE);
050
051   /** Default unmodifiable instance */
052   public static final Created INSTANCE = new Created().unmodifiable();
053
054   /**
055    * Constructor.
056    */
057   public Created() {
058      super(STATUS_LINE);
059   }
060
061   /**
062    * Copy constructor.
063    *
064    * @param copyFrom The bean to copy from.  Must not be <jk>null</jk>.
065    */
066   public Created(Created copyFrom) {
067      super(copyFrom);
068   }
069
070   /**
071    * Constructor.
072    *
073    * <p>
074    * This is the constructor used when parsing an HTTP response.
075    *
076    * @param response The HTTP response to copy from.  Must not be <jk>null</jk>.
077    * @throws IllegalArgumentException If HTTP response status code does not match what was expected.
078    */
079   public Created(HttpResponse response) {
080      super(requireStatus(response, STATUS_CODE));
081   }
082
083   /**
084    * Creates a builder for this class initialized with the contents of this bean.
085    *
086    * @return A new builder bean.
087    */
088   public Created copy() {
089      return new Created(this);
090   }
091
092   @Override /* Overridden from BasicHttpResponse */
093   public Created unmodifiable() {
094      return this instanceof UnmodifiableBean ? this : new Unmodifiable(this);
095   }
096
097   /**
098    * Unmodifiable point-in-time snapshot of the enclosing {@link Created} response.
099    *
100    * <p>
101    * Its only behavioral override is {@link #modify(Runnable)}, which throws — because all mutation is funneled through
102    * {@code modify(...)}, this single override freezes the entire mutation surface.
103    */
104   public static class Unmodifiable extends Created implements UnmodifiableBean {
105
106      /**
107       * Constructor.
108       *
109       * @param copyFrom The response to snapshot.  Must not be <jk>null</jk>.
110       */
111      @SuppressWarnings({
112         "java:S1699" // Paradigm intentionally calls the overridable freeze() from the ctor to deep-freeze sub-beans.
113      })
114      protected Unmodifiable(Created copyFrom) {
115         super(copyFrom);
116         freeze();
117      }
118
119      @Override /* Overridden from BasicHttpResponse */
120      protected Created modify(Runnable mutation) {
121         throw uoex("Bean is unmodifiable.");
122      }
123   }
124}