|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2008-2023 Sonatype, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Sonatype, Inc. - initial API and implementation |
| 12 | + * Hannes Wellmann - Create HttxWagon based on FilexWagon |
| 13 | + *******************************************************************************/ |
| 14 | + |
| 15 | +package org.eclipse.m2e.tests.common; |
| 16 | + |
| 17 | +import java.io.InputStream; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.apache.maven.wagon.ConnectionException; |
| 22 | +import org.apache.maven.wagon.OutputData; |
| 23 | +import org.apache.maven.wagon.ResourceDoesNotExistException; |
| 24 | +import org.apache.maven.wagon.TransferFailedException; |
| 25 | +import org.apache.maven.wagon.authentication.AuthenticationException; |
| 26 | +import org.apache.maven.wagon.authentication.AuthenticationInfo; |
| 27 | +import org.apache.maven.wagon.authorization.AuthorizationException; |
| 28 | +import org.apache.maven.wagon.proxy.ProxyInfoProvider; |
| 29 | +import org.apache.maven.wagon.repository.Repository; |
| 30 | +import org.apache.maven.wagon.resource.Resource; |
| 31 | + |
| 32 | +import io.takari.aether.wagon.OkHttpsWagon; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * A special wagon for testing that allows to record the requests made to a repository. Use |
| 37 | + * {@link #setRequestFilterPattern(String, boolean)} to configure what to record and to optionally clear previous |
| 38 | + * records. The repository URL to use with this wagon looks like {@code httx://localhost/<path-relative-to-project>}. |
| 39 | + */ |
| 40 | +public class HttxWagon extends OkHttpsWagon { |
| 41 | + |
| 42 | +//MUST NOT start with "http", because otherwise the io.takari.aether.connector.AetherRepositoryConnector will consider it as default http(s) and will handle the connection. |
| 43 | + static String PROTOCOL = "httx"; |
| 44 | + |
| 45 | + private static List<String> requests = new ArrayList<>(); |
| 46 | + |
| 47 | + private static String requestFilterPattern; |
| 48 | + |
| 49 | + private static String requestFailPattern; |
| 50 | + |
| 51 | + public static List<String> getRequests() { |
| 52 | + return requests; |
| 53 | + } |
| 54 | + |
| 55 | + public static void setRequestFilterPattern(String regex, boolean clear) { |
| 56 | + requestFilterPattern = regex; |
| 57 | + if(clear) { |
| 58 | + requests.clear(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static void setRequestFailPattern(String regex) { |
| 63 | + requestFailPattern = regex; |
| 64 | + } |
| 65 | + |
| 66 | + public void connect(Repository repository, AuthenticationInfo authenticationInfo, ProxyInfoProvider proxyInfoProvider) |
| 67 | + throws ConnectionException, AuthenticationException { |
| 68 | + if(PROTOCOL.equals(repository.getProtocol())) { |
| 69 | + repository.setUrl("https" + repository.getUrl().substring(PROTOCOL.length())); |
| 70 | + } |
| 71 | + super.connect(repository, authenticationInfo, proxyInfoProvider); |
| 72 | + } |
| 73 | + |
| 74 | + protected InputStream getInputStream(Resource resource) |
| 75 | + throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { |
| 76 | + recordOperation("GET", resource); |
| 77 | + return super.getInputStream(resource); |
| 78 | + } |
| 79 | + |
| 80 | + public void fillOutputData(OutputData outputData) throws TransferFailedException { |
| 81 | + recordOperation("PUT", outputData.getResource()); |
| 82 | + super.fillOutputData(outputData); |
| 83 | + } |
| 84 | + |
| 85 | + private static void recordOperation(String op, Resource resource) throws TransferFailedException { |
| 86 | + String name = resource.getName(); |
| 87 | + if(requestFilterPattern == null || name.matches(requestFilterPattern)) { |
| 88 | + requests.add(op + " " + name); |
| 89 | + } |
| 90 | + if(requestFailPattern != null && name.matches(requestFailPattern)) { |
| 91 | + throw new TransferFailedException("Test failure"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static void reset() { |
| 96 | + requestFailPattern = null; |
| 97 | + requestFilterPattern = null; |
| 98 | + requests.clear(); |
| 99 | + } |
| 100 | +} |
0 commit comments