|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025, Falco Nikolas |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.cloudbees.jenkins.plugins.bitbucket.impl.client; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.Socket; |
| 28 | +import java.security.NoSuchAlgorithmException; |
| 29 | +import javax.net.ssl.SSLContext; |
| 30 | +import javax.net.ssl.SSLSocket; |
| 31 | +import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy; |
| 32 | +import org.apache.hc.client5.http.ssl.TlsSocketStrategy; |
| 33 | +import org.apache.hc.core5.annotation.Contract; |
| 34 | +import org.apache.hc.core5.annotation.ThreadingBehavior; |
| 35 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 36 | + |
| 37 | +/** |
| 38 | + * Custom implementation of a TlsSocketStrategu to replicate what |
| 39 | + * {@code org.apache.http.impl.conn.DefaultHttpClientConnectionOperator#getSocketFactoryRegistry(HttpContext)} |
| 40 | + * did in Apache Client HTTP 4 implementation. |
| 41 | + */ |
| 42 | +@Contract(threading = ThreadingBehavior.SAFE) |
| 43 | +public class BitbucketTlsSocketStrategy implements TlsSocketStrategy { |
| 44 | + public static final String SOCKET_FACTORY_REGISTRY = "http.socket-factory-registry"; |
| 45 | + |
| 46 | + private TlsSocketStrategy defaultStrategy; |
| 47 | + |
| 48 | + @Override |
| 49 | + public SSLSocket upgrade(Socket socket, String target, int port, Object attachment, HttpContext context) throws IOException { |
| 50 | + TlsSocketStrategy strategy = defaultStrategy; |
| 51 | + |
| 52 | + Object value = context.getAttribute(SOCKET_FACTORY_REGISTRY); |
| 53 | + if (value instanceof SSLContext sslContext) { |
| 54 | + strategy = new DefaultClientTlsStrategy(sslContext); |
| 55 | + } else if (defaultStrategy == null) { |
| 56 | + try { |
| 57 | + strategy = new DefaultClientTlsStrategy(SSLContext.getDefault()); |
| 58 | + } catch (NoSuchAlgorithmException e) { |
| 59 | + throw new IOException(e); |
| 60 | + } |
| 61 | + defaultStrategy = strategy; |
| 62 | + } |
| 63 | + return strategy.upgrade(socket, target, port, attachment, context); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments