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