use of org.apache.archiva.proxy.model.RepositoryProxyHandler in project archiva by apache.
the class ArchivaProxyRegistry method updateNetworkProxies.
private void updateNetworkProxies() {
this.networkProxyMap.clear();
List<NetworkProxyConfiguration> networkProxies = getArchivaConfiguration().getConfiguration().getNetworkProxies();
for (NetworkProxyConfiguration networkProxyConfig : networkProxies) {
String key = networkProxyConfig.getId();
NetworkProxy proxy = new NetworkProxy();
proxy.setProtocol(networkProxyConfig.getProtocol());
proxy.setHost(networkProxyConfig.getHost());
proxy.setPort(networkProxyConfig.getPort());
proxy.setUsername(networkProxyConfig.getUsername());
proxy.setPassword(networkProxyConfig.getPassword() == null ? new char[0] : networkProxyConfig.getPassword().toCharArray());
proxy.setUseNtlm(networkProxyConfig.isUseNtlm());
this.networkProxyMap.put(key, proxy);
}
for (RepositoryProxyHandler proxyHandler : repositoryProxyHandlers) {
proxyHandler.setNetworkProxies(this.networkProxyMap);
}
}
use of org.apache.archiva.proxy.model.RepositoryProxyHandler in project archiva by apache.
the class ArchivaProxyRegistry method updateHandler.
private void updateHandler() {
for (RepositoryProxyHandler handler : repositoryProxyHandlers) {
List<RepositoryType> types = handler.supports();
for (RepositoryType type : types) {
if (!handlerMap.containsKey(type)) {
handlerMap.put(type, new ArrayList<>());
}
handlerMap.get(type).add(handler);
}
handler.setPolicies(policies);
}
}
use of org.apache.archiva.proxy.model.RepositoryProxyHandler in project archiva by apache.
the class HttpProxyTransferTest method setUp.
@Before
public void setUp() throws Exception {
proxyHandler = applicationContext.getBean("repositoryProxyHandler#test", RepositoryProxyHandler.class);
// clear from previous tests - TODO the spring context should be initialised per test instead, or the config
// made a complete mock
config.getConfiguration().getProxyConnectors().clear();
// Setup source repository (using default layout)
String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
Path destRepoDir = Paths.get(repoPath);
// Cleanout destination dirs.
if (Files.exists(destRepoDir)) {
FileUtils.deleteDirectory(destRepoDir.toFile());
}
// Make the destination dir.
Files.createDirectories(destRepoDir);
Handler handler = new AbstractHandler() {
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print("get-default-layout-1.0.jar\n\n");
assertNotNull(request.getHeader("Proxy-Connection"));
((Request) request).setHandled(true);
}
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print("get-default-layout-1.0.jar\n\n");
assertNotNull(request.getHeader("Proxy-Connection"));
((Request) request).setHandled(true);
}
};
server = new Server();
ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory());
server.addConnector(serverConnector);
server.setHandler(handler);
server.start();
int port = serverConnector.getLocalPort();
NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
proxyConfig.setHost("localhost");
proxyConfig.setPort(port);
proxyConfig.setProtocol("http");
proxyConfig.setId(PROXY_ID);
config.getConfiguration().addNetworkProxy(proxyConfig);
((MockConfiguration) config).triggerChange("networkProxies.networkProxy(0).host", "localhost");
// Setup target (proxied to) repository.
RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
repoConfig.setId(PROXIED_ID);
repoConfig.setName("Proxied Repository 1");
repoConfig.setLayout("default");
repoConfig.setUrl("http://www.example.com/");
config.getConfiguration().addRemoteRepository(repoConfig);
Wagon wagon = new HttpWagon();
WagonDelegate delegate = (WagonDelegate) applicationContext.getBean("wagon#http", Wagon.class);
delegate.setDelegate(wagon);
proxyRegistry.reload();
repositoryRegistry.reload();
managedDefaultRepository = createRepository(MANAGED_ID, "Default Managed Repository", repoPath, "default");
}
use of org.apache.archiva.proxy.model.RepositoryProxyHandler in project archiva by apache.
the class DefaultBrowseService method artifactAvailable.
@Override
public AvailabilityStatus artifactAvailable(String groupId, String artifactId, String version, String classifier, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
boolean snapshot = VersionUtil.isSnapshot(version);
try {
for (String repoId : selectedRepos) {
org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repoId);
if (!proxyRegistry.hasHandler(managedRepo.getType())) {
throw new RepositoryException("No proxy handler found for repository type " + managedRepo.getType());
}
RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(managedRepo.getType()).get(0);
if ((snapshot && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT)) || (!snapshot && managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))) {
continue;
}
ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
// FIXME default to jar which can be wrong for war zip etc....
ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(StringUtils.isEmpty(classifier) ? "" : classifier).withType("jar").withArtifactId(artifactId).build();
org.apache.archiva.repository.content.Artifact archivaArtifact = managedRepositoryContent.getItem(itemSelector).adapt(org.apache.archiva.repository.content.Artifact.class);
StorageAsset file = archivaArtifact.getAsset();
if (file != null && file.exists()) {
return new AvailabilityStatus(true);
}
// in case of SNAPSHOT we can have timestamped version locally !
if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) {
StorageAsset metadataFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + MetadataTools.MAVEN_METADATA);
if (metadataFile.exists()) {
MetadataReader metadataReader = repositoryRegistry.getMetadataReader(managedRepositoryContent.getRepository().getType());
ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read(metadataFile);
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
// rebuild file name with timestamped version and build number
String timeStampFileName = //
new StringBuilder(artifactId).append('-').append(//
StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT)).append('-').append(//
timeStamp).append('-').append(//
Integer.toString(buildNumber)).append(//
(StringUtils.isEmpty(classifier) ? "" : "-" + classifier)).append(".jar").toString();
StorageAsset timeStampFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + timeStampFileName);
log.debug("try to find timestamped snapshot version file: {}", timeStampFile.getPath());
if (timeStampFile.exists()) {
return new AvailabilityStatus(true);
}
}
}
String path = managedRepositoryContent.toPath(archivaArtifact);
file = proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), path);
if (file != null && file.exists()) {
// download pom now
String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom";
proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), pomPath);
return new AvailabilityStatus(true);
}
}
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
return new AvailabilityStatus(false);
}
use of org.apache.archiva.proxy.model.RepositoryProxyHandler in project archiva by apache.
the class ArchivaDavResourceFactory method fetchContentFromProxies.
private boolean fetchContentFromProxies(ManagedRepository managedRepository, DavServletRequest request, LogicalResource resource) throws DavException {
String path = resource.getPath();
if (!proxyRegistry.hasHandler(managedRepository.getType())) {
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "No proxy handler found for repository type " + managedRepository.getType());
}
RepositoryRequestInfo repositoryRequestInfo = managedRepository.getRequestInfo();
RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(managedRepository.getType()).get(0);
if (repositoryRequestInfo.isSupportFile(path)) {
StorageAsset proxiedFile = proxyHandler.fetchFromProxies(managedRepository, path);
return (proxiedFile != null);
}
// Is it a Metadata resource?
if ("default".equals(repositoryRequestInfo.getLayout(path)) && repositoryRequestInfo.isMetadata(path)) {
return proxyHandler.fetchMetadataFromProxies(managedRepository, path).isModified();
}
// Is it an Archetype Catalog?
if (repositoryRequestInfo.isArchetypeCatalog(path)) {
// FIXME we must implement a merge of remote archetype catalog from remote servers.
StorageAsset proxiedFile = proxyHandler.fetchFromProxies(managedRepository, path);
return (proxiedFile != null);
}
// Not any of the above? Then it's gotta be an artifact reference.
try {
// Get the artifact reference in a layout neutral way.
// ArtifactReference artifact = repositoryRequestInfo.toArtifactReference( path );
ItemSelector selector = repositoryRequestInfo.toItemSelector(path);
if (selector != null) {
String repositoryLayout = managedRepository.getLayout();
RepositoryStorage repositoryStorage = this.applicationContext.getBean("repositoryStorage#" + repositoryLayout, RepositoryStorage.class);
selector = repositoryStorage.applyServerSideRelocation(managedRepository, selector);
StorageAsset proxiedFile = proxyHandler.fetchFromProxies(managedRepository, selector);
resource.setPath(managedRepository.getContent().toPath(selector));
log.debug("Proxied artifact '{}:{}:{}:{}'", selector.getNamespace(), selector.getArtifactId(), selector.getVersion(), selector.getArtifactVersion());
return (proxiedFile != null);
}
} catch (LayoutException e) {
/* eat it */
} catch (ProxyDownloadException e) {
log.error(e.getMessage(), e);
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to fetch artifact resource.");
}
return false;
}
Aggregations