1 package org.apache.archiva.policies;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksummedFile;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Service;
28
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
33
34
35
36
37
38
39
40 @Service( "postDownloadPolicy#checksum" )
41 public class ChecksumPolicy
42 implements PostDownloadPolicy
43 {
44 private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
45
46
47
48
49
50
51 public static final String IGNORE = "ignore";
52
53
54
55
56
57
58 public static final String FAIL = "fail";
59
60
61
62
63
64
65 public static final String FIX = "fix";
66
67 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
68
69 private List<String> options = new ArrayList<>( 3 );
70
71 public ChecksumPolicy()
72 {
73 options.add( FAIL );
74 options.add( FIX );
75 options.add( IGNORE );
76 }
77
78 @Override
79 public void applyPolicy( String policySetting, Properties request, File localFile )
80 throws PolicyViolationException, PolicyConfigurationException
81 {
82 if ( "resource".equals( request.getProperty( "filetype" ) ) )
83 {
84 return;
85 }
86
87 if ( !options.contains( policySetting ) )
88 {
89
90 throw new PolicyConfigurationException(
91 "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
92 options.iterator(), "," ) + "]" );
93 }
94
95 if ( IGNORE.equals( policySetting ) )
96 {
97
98 log.debug( "Checksum policy set to IGNORE." );
99 return;
100 }
101
102 if ( !localFile.exists() )
103 {
104
105 throw new PolicyViolationException(
106 "Checksum policy failure, local file " + localFile.getAbsolutePath() + " does not exist to check." );
107 }
108
109 if ( FAIL.equals( policySetting ) )
110 {
111 ChecksummedFile checksum = new ChecksummedFile( localFile );
112 if ( checksum.isValidChecksums( algorithms ) )
113 {
114 return;
115 }
116
117 for ( ChecksumAlgorithm algorithm : algorithms )
118 {
119 File file = new File( localFile.getAbsolutePath() + "." + algorithm.getExt() );
120 if ( file.exists() )
121 {
122 file.delete();
123 }
124 }
125
126 localFile.delete();
127 throw new PolicyViolationException(
128 "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
129 + localFile.getAbsolutePath() + "." );
130 }
131
132 if ( FIX.equals( policySetting ) )
133 {
134 ChecksummedFile checksum = new ChecksummedFile( localFile );
135 if ( checksum.fixChecksums( algorithms ) )
136 {
137 log.debug( "Checksum policy set to FIX, checksum files have been updated." );
138 return;
139 }
140 else
141 {
142 throw new PolicyViolationException(
143 "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
144 + localFile.getAbsolutePath() + "." );
145 }
146 }
147
148 throw new PolicyConfigurationException(
149 "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
150 }
151
152 @Override
153 public String getDefaultOption()
154 {
155 return FIX;
156 }
157
158 @Override
159 public String getId()
160 {
161 return "checksum";
162 }
163
164 @Override
165 public String getName()
166 {
167 return "Checksum";
168 }
169
170 @Override
171 public List<String> getOptions()
172 {
173 return options;
174 }
175 }