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 * <ul class='seealso'>
057 *    <li class='extlink'>{@doc RFC2616}
058 * </ul>
059 */
060@Header("Retry-After")
061public final class RetryAfter extends HeaderString {
062
063   /**
064    * Returns a parsed <c>Retry-After</c> header.
065    *
066    * @param value The <c>Retry-After</c> header string.
067    * @return The parsed <c>Retry-After</c> header, or <jk>null</jk> if the string was null.
068    */
069   public static RetryAfter forString(String value) {
070      if (value == null)
071         return null;
072      return new RetryAfter(value);
073   }
074
075   private RetryAfter(String value) {
076      super(value);
077   }
078
079   /**
080    * Returns this header value as a {@link java.util.Date} object.
081    *
082    * @return This header value as a {@link java.util.Date} object, or <jk>null</jk> if the value is not a date.
083    */
084   public java.util.Date asDate() {
085      char c0 = charAt(value, 0);
086      if (c0 >= '0' && c0 <= '9')
087         return null;
088      return DateUtils.parseDate(toString());
089   }
090
091   /**
092    * Returns this header value as an integer.
093    *
094    * @return This header value as a integer, or <c>-1</c> if the value is not an integer.
095    */
096   public int asInt() {
097      char c0 = charAt(value, 0);
098      if (c0 >= '0' && c0 <= '9') {
099         try {
100            return Integer.parseInt(value);
101         } catch (NumberFormatException e) {
102            return -1;
103         }
104      }
105      return -1;
106   }
107}