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