001package org.apache.archiva.consumers.core; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.consumers.AbstractMonitoredConsumer; 023import org.apache.archiva.consumers.ConsumerException; 024import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 025import org.apache.archiva.repository.ManagedRepository; 026import org.apache.commons.io.FileUtils; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.springframework.context.annotation.Scope; 030import org.springframework.stereotype.Service; 031 032import java.io.IOException; 033import java.nio.file.Files; 034import java.nio.file.Path; 035import java.nio.file.Paths; 036import java.util.ArrayList; 037import java.util.Date; 038import java.util.HashMap; 039import java.util.Iterator; 040import java.util.List; 041import java.util.Map; 042 043/** 044 * AutoRenameConsumer 045 */ 046@Service( "knownRepositoryContentConsumer#auto-rename" ) 047@Scope( "prototype" ) 048public class AutoRenameConsumer 049 extends AbstractMonitoredConsumer 050 implements KnownRepositoryContentConsumer 051{ 052 private Logger log = LoggerFactory.getLogger( AutoRenameConsumer.class ); 053 054 private String id = "auto-rename"; 055 056 private String description = "Automatically rename common artifact mistakes."; 057 058 private static final String RENAME_FAILURE = "rename_failure"; 059 060 private Path repositoryDir; 061 062 private List<String> includes = new ArrayList<>( 3 ); 063 064 private Map<String, String> extensionRenameMap = new HashMap<>( ); 065 066 public AutoRenameConsumer( ) 067 { 068 includes.add( "**/*.distribution-tgz" ); 069 includes.add( "**/*.distribution-zip" ); 070 includes.add( "**/*.plugin" ); 071 072 extensionRenameMap.put( ".distribution-tgz", ".tar.gz" ); 073 extensionRenameMap.put( ".distribution-zip", ".zip" ); 074 extensionRenameMap.put( ".plugin", ".jar" ); 075 } 076 077 @Override 078 public String getId( ) 079 { 080 return this.id; 081 } 082 083 @Override 084 public String getDescription( ) 085 { 086 return this.description; 087 } 088 089 @Override 090 public void beginScan( ManagedRepository repository, Date whenGathered ) 091 throws ConsumerException 092 { 093 this.repositoryDir = Paths.get( repository.getLocation( ) ); 094 } 095 096 @Override 097 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo ) 098 throws ConsumerException 099 { 100 beginScan( repository, whenGathered ); 101 } 102 103 @Override 104 public void completeScan( ) 105 { 106 /* do nothing */ 107 } 108 109 @Override 110 public void completeScan( boolean executeOnEntireRepo ) 111 { 112 completeScan( ); 113 } 114 115 @Override 116 public List<String> getExcludes( ) 117 { 118 return null; 119 } 120 121 @Override 122 public List<String> getIncludes( ) 123 { 124 return includes; 125 } 126 127 @Override 128 public void processFile( String path ) 129 throws ConsumerException 130 { 131 Path file = this.repositoryDir.resolve( path ); 132 if ( Files.exists(file) ) 133 { 134 Iterator<String> itExtensions = this.extensionRenameMap.keySet( ).iterator( ); 135 while ( itExtensions.hasNext( ) ) 136 { 137 String extension = itExtensions.next( ); 138 if ( path.endsWith( extension ) ) 139 { 140 String fixedExtension = this.extensionRenameMap.get( extension ); 141 String correctedPath = path.substring( 0, path.length( ) - extension.length( ) ) + fixedExtension; 142 Path to = repositoryDir.resolve(correctedPath); 143 try 144 { 145 // Rename the file. 146 FileUtils.moveFile( file.toFile(), to.toFile() ); 147 } 148 catch ( IOException e ) 149 { 150 log.warn( "Unable to rename {} to {} :", path, correctedPath, e ); 151 triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath + 152 ": " + e.getMessage( ) ); 153 } 154 } 155 } 156 157 log.info( "(Auto) Removing File: {} ", file.toAbsolutePath( ) ); 158 triggerConsumerInfo( "(Auto) Removing File: " + file.toAbsolutePath( ) ); 159 try 160 { 161 Files.delete( file ); 162 } 163 catch ( IOException e ) 164 { 165 log.error("Could not delete file {}: {}", file, e.getMessage(), e); 166 throw new ConsumerException( "File deletion failed "+file ); 167 } 168 } 169 } 170 171 @Override 172 public void processFile( String path, boolean executeOnEntireRepo ) 173 throws ConsumerException 174 { 175 processFile( path ); 176 } 177}