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.apache.maven.repository;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.util.Collection;
28  
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.metadata.Metadata;
31  import org.eclipse.aether.repository.RemoteRepository;
32  import org.eclipse.aether.spi.connector.ArtifactDownload;
33  import org.eclipse.aether.spi.connector.ArtifactUpload;
34  import org.eclipse.aether.spi.connector.MetadataDownload;
35  import org.eclipse.aether.spi.connector.MetadataUpload;
36  import org.eclipse.aether.spi.connector.RepositoryConnector;
37  import org.eclipse.aether.transfer.ArtifactNotFoundException;
38  import org.eclipse.aether.transfer.ArtifactTransferException;
39  import org.eclipse.aether.transfer.MetadataNotFoundException;
40  import org.eclipse.aether.transfer.MetadataTransferException;
41  
42  /**
43   */
44  public class TestRepositoryConnector implements RepositoryConnector {
45  
46      private RemoteRepository repository;
47  
48      private File basedir;
49  
50      public TestRepositoryConnector(RemoteRepository repository) {
51          this.repository = repository;
52          String repositoryUrl = repository.getUrl();
53          if (repositoryUrl.contains("${")) {
54              // the repository url contains unresolved properties and getting the basedir is not possible
55              // in JDK 20+ 'new URL(string)' will fail if the string contains a curly brace
56              this.basedir = null;
57          } else {
58              try {
59                  URL url = new URL(repository.getUrl());
60                  if ("file".equals(url.getProtocol())) {
61                      basedir = new File(url.getPath());
62                  }
63              } catch (MalformedURLException e) {
64                  throw new IllegalStateException(e);
65              }
66          }
67      }
68  
69      public void close() {}
70  
71      public void get(
72              Collection<? extends ArtifactDownload> artifactDownloads,
73              Collection<? extends MetadataDownload> metadataDownloads) {
74          if (artifactDownloads != null) {
75              for (ArtifactDownload download : artifactDownloads) {
76                  File remoteFile = new File(basedir, path(download.getArtifact()));
77                  try {
78                      Path dest = download.getPath();
79                      Files.createDirectories(dest.getParent());
80                      Files.copy(remoteFile.toPath(), dest);
81                  } catch (IOException e) {
82                      if (!remoteFile.exists()) {
83                          download.setException(new ArtifactNotFoundException(download.getArtifact(), repository));
84                      } else {
85                          download.setException(new ArtifactTransferException(download.getArtifact(), repository, e));
86                      }
87                  }
88              }
89          }
90          if (metadataDownloads != null) {
91              for (final MetadataDownload download : metadataDownloads) {
92                  File remoteFile = new File(basedir, path(download.getMetadata()));
93                  try {
94                      Path dest = download.getPath();
95                      Files.createDirectories(dest.getParent());
96                      Files.copy(remoteFile.toPath(), dest);
97                  } catch (IOException e) {
98                      if (!remoteFile.exists()) {
99                          download.setException(new MetadataNotFoundException(download.getMetadata(), repository));
100                     } else {
101                         download.setException(new MetadataTransferException(download.getMetadata(), repository, e));
102                     }
103                 }
104             }
105         }
106     }
107 
108     private String path(Artifact artifact) {
109         StringBuilder path = new StringBuilder(128);
110 
111         path.append(artifact.getGroupId().replace('.', '/')).append('/');
112 
113         path.append(artifact.getArtifactId()).append('/');
114 
115         path.append(artifact.getBaseVersion()).append('/');
116 
117         path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
118 
119         if (!artifact.getClassifier().isEmpty()) {
120             path.append('-').append(artifact.getClassifier());
121         }
122 
123         path.append('.').append(artifact.getExtension());
124 
125         return path.toString();
126     }
127 
128     private String path(Metadata metadata) {
129         StringBuilder path = new StringBuilder(128);
130 
131         path.append(metadata.getGroupId().replace('.', '/')).append('/');
132 
133         path.append(metadata.getArtifactId()).append('/');
134 
135         path.append("maven-metadata.xml");
136 
137         return path.toString();
138     }
139 
140     public void put(
141             Collection<? extends ArtifactUpload> artifactUploads,
142             Collection<? extends MetadataUpload> metadataUploads) {
143         // TODO Auto-generated method stub
144 
145     }
146 }