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