1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
40
41
42
43
44 public class ODCConnectionTransport implements Transport {
45
46
47
48
49 private final OssindexClientConfiguration configuration;
50
51
52
53 private final UserAgentSupplier userAgent;
54
55
56
57
58
59
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
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
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
93 }
94 }