View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2019 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.ossindex;
19  
20  import java.io.IOException;
21  import java.net.URI;
22  import java.nio.charset.StandardCharsets;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.hc.core5.http.ContentType;
27  import org.apache.hc.core5.http.Header;
28  import org.apache.hc.core5.http.HttpHeaders;
29  import org.apache.hc.core5.http.message.BasicHeader;
30  import org.owasp.dependencycheck.utils.Downloader;
31  import org.owasp.dependencycheck.utils.ResourceNotFoundException;
32  import org.owasp.dependencycheck.utils.TooManyRequestsException;
33  import org.sonatype.ossindex.service.client.OssindexClientConfiguration;
34  import org.sonatype.ossindex.service.client.transport.BasicAuthHelper;
35  import org.sonatype.ossindex.service.client.transport.Transport;
36  import org.sonatype.ossindex.service.client.transport.UserAgentSupplier;
37  
38  /**
39   * ODC connection transport is used instead of HttpUrlConnectionTransport
40   * because the proxy information is already configured.
41   *
42   * @author Jeremy Long
43   */
44  public class ODCConnectionTransport implements Transport {
45  
46      /**
47       * The OSS Index client configuration.
48       */
49      private final OssindexClientConfiguration configuration;
50      /**
51       * The user agent to send in the HTTP connection.
52       */
53      private final UserAgentSupplier userAgent;
54  
55      /**
56       * Constructs a new transport object to connect to the OSS Index.
57       *
58       * @param config the OSS client configuration
59       * @param userAgent the user agent to send to OSS Index
60       */
61      public ODCConnectionTransport(OssindexClientConfiguration config, UserAgentSupplier userAgent) {
62          this.userAgent = userAgent;
63          this.configuration = config;
64      }
65  
66      @Override
67      public void init(OssindexClientConfiguration configuration) {
68          // no initialisation needed
69      }
70  
71      @Override
72      public String post(URI url, String payloadType, String payload, String acceptType) throws TransportException, IOException {
73          try {
74              final List<Header> headers = new ArrayList<>(3);
75              headers.add(new BasicHeader(HttpHeaders.ACCEPT, acceptType));
76              headers.add(new BasicHeader(HttpHeaders.USER_AGENT, userAgent.get()));
77              // TODO consider to promote pre-emptive authentication by default to the Downloader and also load the OSSIndex credentials there.
78              final String authorization = BasicAuthHelper.authorizationHeader(configuration.getAuthConfiguration());
79              if (authorization != null) {
80                  headers.add(new BasicHeader(HttpHeaders.AUTHORIZATION, authorization));
81              }
82              return Downloader.getInstance().postBasedFetchContent(url, payload, ContentType.create(payloadType, StandardCharsets.UTF_8), headers);
83          } catch (TooManyRequestsException e) {
84              throw new TransportException("Too many requests for " + url.toString() + " HTTP status 429", e);
85          } catch (ResourceNotFoundException e) {
86              throw new TransportException("Not found for " + url.toString() + "HTTP status 404", e);
87          }
88      }
89  
90      @Override
91      public void close() throws Exception {
92          // no resource closure needed; fully delegated to HTTPClient
93      }
94  }