View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.spi.connector.transport.http;
20  
21  import java.net.URI;
22  
23  import org.eclipse.aether.spi.connector.transport.Transporter;
24  import org.slf4j.Logger;
25  
26  /**
27   * A transporter using HTTP protocol.
28   *
29   * @since 2.0.0
30   */
31  public interface HttpTransporter extends Transporter {
32      @Override
33      default int classify(Throwable error) {
34          if (error instanceof HttpTransporterException) {
35              int statusCode = ((HttpTransporterException) error).getStatusCode();
36              if (statusCode == HttpConstants.NOT_FOUND || statusCode == HttpConstants.GONE) {
37                  return ERROR_NOT_FOUND;
38              }
39          }
40          return ERROR_OTHER;
41      }
42  
43      /**
44       * Log a retry message for an immediate retry.
45       * @param logger the logger to log the retry message
46       * @param method the HTTP method of the request (e.g. GET, PUT, etc.)
47       * @param uri the URI of the request
48       * @param requestAttempt the 0-based counter of the previous request attempt (0 for the first attempt, 1 for the first retry, etc.)
49       * @param maxRequestAttempt the maximum number of request attempts (this is including this original request)
50       * @param exception the exception that caused the immediate retry
51       */
52      static void logImmediateRetry(
53              Logger logger, String method, URI uri, int requestAttempt, int maxRequestAttempt, Throwable exception) {
54          String reason = String.format("%s: %s", exception.getClass().getName(), exception.getMessage());
55          logRetry(logger, method, uri, requestAttempt, maxRequestAttempt, reason, 0L, null);
56      }
57  
58      /**
59       * Log a retry message for a delayed retry.
60       * @param logger the logger to log the retry message
61       * @param method the HTTP method of the request (e.g. GET, PUT, etc.)
62       * @param uri the URI of the request
63       * @param requestAttempt the 0-based counter of the previous request attempt (0 for the first attempt, 1 for the first retry, etc.)
64       * @param maxRequestAttempt the maximum number of request attempts (this is including this original request)
65       * @param reason the reason for the retry (usually a status code or an exception)
66       * @param retryDelay the interval in milliseconds before the next retry (0 or negative for immediate retry)
67       * @param retryDelayReason the reason for the retry interval (may be empty if unknown)
68       */
69      @SuppressWarnings("checkstyle:parameternumber")
70      static void logRetry(
71              Logger logger,
72              String method,
73              URI uri,
74              int requestAttempt,
75              int maxRequestAttempt,
76              String reason,
77              long retryDelay,
78              String retryDelayReason) {
79  
80          final String retryingDelayMessage;
81          if (retryDelay > 0L) {
82              if (retryDelayReason != null && !retryDelayReason.isEmpty()) {
83                  retryingDelayMessage = "Retrying in " + retryDelay + " ms (due to " + retryDelayReason + ")";
84              } else {
85                  retryingDelayMessage = "Retrying in " + retryDelay + " ms";
86              }
87          } else {
88              retryingDelayMessage = "Retrying immediately";
89          }
90          logger.warn(
91                  "{} request to {} failed (attempt {} of {}) due to {}. {}...",
92                  method,
93                  uri,
94                  requestAttempt + 1,
95                  maxRequestAttempt,
96                  reason,
97                  retryingDelayMessage);
98      }
99  }