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.admin.model.beans.ManagedRepository; 023import org.apache.archiva.consumers.AbstractMonitoredConsumer; 024import org.apache.archiva.consumers.ConsumerException; 025import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 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.File; 033import java.io.IOException; 034import java.util.ArrayList; 035import java.util.Date; 036import java.util.HashMap; 037import java.util.Iterator; 038import java.util.List; 039import java.util.Map; 040 041/** 042 * AutoRenameConsumer 043 */ 044@Service( "knownRepositoryContentConsumer#auto-rename" ) 045@Scope( "prototype" ) 046public class AutoRenameConsumer 047 extends AbstractMonitoredConsumer 048 implements KnownRepositoryContentConsumer 049{ 050 private Logger log = LoggerFactory.getLogger( AutoRenameConsumer.class ); 051 052 private String id = "auto-rename"; 053 054 private String description = "Automatically rename common artifact mistakes."; 055 056 private static final String RENAME_FAILURE = "rename_failure"; 057 058 private File repositoryDir; 059 060 private List<String> includes = new ArrayList<>( 3 ); 061 062 private Map<String, String> extensionRenameMap = new HashMap<>( ); 063 064 public AutoRenameConsumer( ) 065 { 066 includes.add( "**/*.distribution-tgz" ); 067 includes.add( "**/*.distribution-zip" ); 068 includes.add( "**/*.plugin" ); 069 070 extensionRenameMap.put( ".distribution-tgz", ".tar.gz" ); 071 extensionRenameMap.put( ".distribution-zip", ".zip" ); 072 extensionRenameMap.put( ".plugin", ".jar" ); 073 } 074 075 @Override 076 public String getId( ) 077 { 078 return this.id; 079 } 080 081 @Override 082 public String getDescription( ) 083 { 084 return this.description; 085 } 086 087 @Override 088 public void beginScan( ManagedRepository repository, Date whenGathered ) 089 throws ConsumerException 090 { 091 this.repositoryDir = new File( repository.getLocation( ) ); 092 } 093 094 @Override 095 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo ) 096 throws ConsumerException 097 { 098 beginScan( repository, whenGathered ); 099 } 100 101 @Override 102 public void completeScan( ) 103 { 104 /* do nothing */ 105 } 106 107 @Override 108 public void completeScan( boolean executeOnEntireRepo ) 109 { 110 completeScan( ); 111 } 112 113 @Override 114 public List<String> getExcludes( ) 115 { 116 return null; 117 } 118 119 @Override 120 public List<String> getIncludes( ) 121 { 122 return includes; 123 } 124 125 @Override 126 public void processFile( String path ) 127 throws ConsumerException 128 { 129 File file = new File( this.repositoryDir, path ); 130 if ( file.exists( ) ) 131 { 132 Iterator<String> itExtensions = this.extensionRenameMap.keySet( ).iterator( ); 133 while ( itExtensions.hasNext( ) ) 134 { 135 String extension = itExtensions.next( ); 136 if ( path.endsWith( extension ) ) 137 { 138 String fixedExtension = this.extensionRenameMap.get( extension ); 139 String correctedPath = path.substring( 0, path.length( ) - extension.length( ) ) + fixedExtension; 140 File to = new File( this.repositoryDir, correctedPath ); 141 try 142 { 143 // Rename the file. 144 FileUtils.moveFile( file, to ); 145 } 146 catch ( IOException e ) 147 { 148 log.warn( "Unable to rename {} to {} :", path, correctedPath, e ); 149 triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath + 150 ": " + e.getMessage( ) ); 151 } 152 } 153 } 154 155 log.info( "(Auto) Removing File: {} ", file.getAbsolutePath( ) ); 156 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath( ) ); 157 file.delete( ); 158 } 159 } 160 161 @Override 162 public void processFile( String path, boolean executeOnEntireRepo ) 163 throws ConsumerException 164 { 165 processFile( path ); 166 } 167}