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.internal.*;
018
019/**
020 * Represents a parsed <l>Retry-After</l> HTTP response header.
021 * 
022 * <p>
023 * If an entity is temporarily unavailable, this instructs the client to try again later.
024 * Value could be a specified period of time (in seconds) or a HTTP-date.
025 * 
026 * <h5 class='figure'>Example</h5>
027 * <p class='bcode'>
028 *    Retry-After: 120
029 *    Retry-After: Fri, 07 Nov 2014 23:59:59 GMT
030 * </p>
031 * 
032 * <h5 class='topic'>RFC2616 Specification</h5>
033 * 
034 * The Retry-After response-header field can be used with a 503 (Service Unavailable) response to indicate how long the
035 * service is expected to be unavailable to the requesting client.
036 * This field MAY also be used with any 3xx (Redirection) response to indicate the minimum time the user-agent is asked
037 * wait before issuing the redirected request.
038 * The value of this field can be either an HTTP-date or an integer number of seconds (in decimal) after the time of the
039 * response.
040 * 
041 * <p class='bcode'>
042 *    Retry-After  = "Retry-After" ":" ( HTTP-date | delta-seconds )
043 * </p>
044 * 
045 * <p>
046 * Two examples of its use are
047 * <p class='bcode'>
048 *    Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
049 *    Retry-After: 120
050 * </p>
051 * 
052 * <p>
053 * In the latter example, the delay is 2 minutes.
054 * 
055 * <h5 class='section'>See Also:</h5>
056 * <ul class='doctree'>
057 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
058 * </ul>
059 */
060public final class RetryAfter extends HeaderString {
061
062   /**
063    * Returns a parsed <code>Retry-After</code> header.
064    * 
065    * @param value The <code>Retry-After</code> header string.
066    * @return The parsed <code>Retry-After</code> header, or <jk>null</jk> if the string was null.
067    */
068   public static RetryAfter forString(String value) {
069      if (value == null)
070         return null;
071      return new RetryAfter(value);
072   }
073
074   private RetryAfter(String value) {
075      super(value);
076   }
077
078   /**
079    * Returns this header value as a {@link java.util.Date} object.
080    * 
081    * @return This header value as a {@link java.util.Date} object, or <jk>null</jk> if the value is not a date.
082    */
083   public java.util.Date asDate() {
084      char c0 = charAt(value, 0);
085      if (c0 >= '0' && c0 <= '9')
086         return null;
087      return DateUtils.parseDate(toString());
088   }
089
090   /**
091    * Returns this header value as an integer.
092    * 
093    * @return This header value as a integer, or <code>-1</code> if the value is not an integer.
094    */
095   public int asInt() {
096      char c0 = charAt(value, 0);
097      if (c0 >= '0' && c0 <= '9') {
098         try {
099            return Integer.parseInt(value);
100         } catch (NumberFormatException e) {
101            return -1;
102         }
103      }
104      return -1;
105   }
106}