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.MovedPermanently.*;
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 301 Moved Permanently</c> response.
029 *
030 * <p>
031 * This and all future requests should be directed to the given URI.
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 MovedPermanently extends BasicHttpResponse<MovedPermanently> {
041
042   /** HTTP status code */
043   public static final int STATUS_CODE = 301;
044
045   /** Reason phrase */
046   public static final String REASON_PHRASE = "Moved Permanently";
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 MovedPermanently INSTANCE = new MovedPermanently().unmodifiable();
053
054   /**
055    * Constructor.
056    */
057   public MovedPermanently() {
058      super(STATUS_LINE);
059   }
060
061   /**
062    * Constructor.
063    *
064    * <p>
065    * This is the constructor used when parsing an HTTP response.
066    *
067    * @param response The HTTP response to copy from.  Must not be <jk>null</jk>.
068    * @throws IllegalArgumentException If HTTP response status code does not match what was expected.
069    */
070   public MovedPermanently(HttpResponse response) {
071      super(requireStatus(response, STATUS_CODE));
072   }
073
074   /**
075    * Copy constructor.
076    *
077    * @param copyFrom The bean to copy from.  Must not be <jk>null</jk>.
078    */
079   public MovedPermanently(MovedPermanently copyFrom) {
080      super(copyFrom);
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 MovedPermanently copy() {
089      return new MovedPermanently(this);
090   }
091
092   @Override /* Overridden from BasicHttpResponse */
093   public MovedPermanently unmodifiable() {
094      return this instanceof UnmodifiableBean ? this : new Unmodifiable(this);
095   }
096
097   /**
098    * Unmodifiable point-in-time snapshot of the enclosing {@link MovedPermanently} 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 MovedPermanently 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(MovedPermanently copyFrom) {
115         super(copyFrom);
116         freeze();
117      }
118
119      @Override /* Overridden from BasicHttpResponse */
120      protected MovedPermanently modify(Runnable mutation) {
121         throw uoex("Bean is unmodifiable.");
122      }
123   }
124}