001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.http.annotation.*;
018import org.apache.juneau.internal.*;
019
020/**
021 * Represents a parsed <l>Retry-After</l> HTTP response header.
022 *
023 * <p>
024 * If an entity is temporarily unavailable, this instructs the client to try again later.
025 * Value could be a specified period of time (in seconds) or a HTTP-date.
026 *
027 * <h5 class='figure'>Example</h5>
028 * <p class='bcode w800'>
029 *    Retry-After: 120
030 *    Retry-After: Fri, 07 Nov 2014 23:59:59 GMT
031 * </p>
032 *
033 * <h5 class='topic'>RFC2616 Specification</h5>
034 *
035 * The Retry-After response-header field can be used with a 503 (Service Unavailable) response to indicate how long the
036 * service is expected to be unavailable to the requesting client.
037 * This field MAY also be used with any 3xx (Redirection) response to indicate the minimum time the user-agent is asked
038 * wait before issuing the redirected request.
039 * The value of this field can be either an HTTP-date or an integer number of seconds (in decimal) after the time of the
040 * response.
041 *
042 * <p class='bcode w800'>
043 *    Retry-After  = "Retry-After" ":" ( HTTP-date | delta-seconds )
044 * </p>
045 *
046 * <p>
047 * Two examples of its use are
048 * <p class='bcode w800'>
049 *    Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
050 *    Retry-After: 120
051 * </p>
052 *
053 * <p>
054 * In the latter example, the delay is 2 minutes.
055 *
056 * <h5 class='section'>See Also:</h5>
057 * <ul class='doctree'>
058 *    <li class='extlink'>{@doc RFC2616}
059 * </ul>
060 */
061@Header("Retry-After")
062public final class RetryAfter extends HeaderString {
063
064   /**
065    * Returns a parsed <code>Retry-After</code> header.
066    *
067    * @param value The <code>Retry-After</code> header string.
068    * @return The parsed <code>Retry-After</code> header, or <jk>null</jk> if the string was null.
069    */
070   public static RetryAfter forString(String value) {
071      if (value == null)
072         return null;
073      return new RetryAfter(value);
074   }
075
076   private RetryAfter(String value) {
077      super(value);
078   }
079
080   /**
081    * Returns this header value as a {@link java.util.Date} object.
082    *
083    * @return This header value as a {@link java.util.Date} object, or <jk>null</jk> if the value is not a date.
084    */
085   public java.util.Date asDate() {
086      char c0 = charAt(value, 0);
087      if (c0 >= '0' && c0 <= '9')
088         return null;
089      return DateUtils.parseDate(toString());
090   }
091
092   /**
093    * Returns this header value as an integer.
094    *
095    * @return This header value as a integer, or <code>-1</code> if the value is not an integer.
096    */
097   public int asInt() {
098      char c0 = charAt(value, 0);
099      if (c0 >= '0' && c0 <= '9') {
100         try {
101            return Integer.parseInt(value);
102         } catch (NumberFormatException e) {
103            return -1;
104         }
105      }
106      return -1;
107   }
108}