001package org.apache.maven.wagon.observers; 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.maven.wagon.events.TransferEvent; 023import org.apache.maven.wagon.events.TransferListener; 024 025import java.security.MessageDigest; 026import java.security.NoSuchAlgorithmException; 027 028/** 029 * TransferListeners which computes MD5 checksum on the fly when files are transfered. 030 * 031 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 032 * 033 */ 034public class ChecksumObserver 035 implements TransferListener 036{ 037 private MessageDigest digester = null; 038 039 private String actualChecksum; 040 041 public ChecksumObserver() 042 throws NoSuchAlgorithmException 043 { 044 this( "MD5" ); 045 } 046 047 /** 048 * @param algorithm One of the algorithms supported by JDK: MD5, MD2 or SHA-1 049 */ 050 public ChecksumObserver( String algorithm ) 051 throws NoSuchAlgorithmException 052 { 053 digester = MessageDigest.getInstance( algorithm ); 054 } 055 056 public void transferInitiated( TransferEvent transferEvent ) 057 { 058 // This space left intentionally blank 059 } 060 061 /** 062 * @see org.apache.maven.wagon.events.TransferListener#transferStarted(org.apache.maven.wagon.events.TransferEvent) 063 */ 064 public void transferStarted( TransferEvent transferEvent ) 065 { 066 actualChecksum = null; 067 068 digester.reset(); 069 } 070 071 /** 072 * @see org.apache.maven.wagon.events.TransferListener#transferProgress(org.apache.maven.wagon.events.TransferEvent, byte[], int) 073 */ 074 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length ) 075 { 076 digester.update( buffer, 0, length ); 077 } 078 079 public void transferCompleted( TransferEvent transferEvent ) 080 { 081 actualChecksum = encode( digester.digest() ); 082 } 083 084 public void transferError( TransferEvent transferEvent ) 085 { 086 digester.reset(); 087 088 actualChecksum = null; 089 } 090 091 public void debug( String message ) 092 { 093 // left intentionally blank 094 } 095 096 /** 097 * Returns md5 checksum which was computed during transfer 098 * 099 * @return 100 */ 101 public String getActualChecksum() 102 { 103 return actualChecksum; 104 } 105 106 /** 107 * Encodes a 128 bit or 160-bit byte array into a String. 108 * 109 * @param binaryData Array containing the digest 110 * @return Encoded hex string, or null if encoding failed 111 */ 112 @SuppressWarnings( "checkstyle:magicnumber" ) 113 protected String encode( byte[] binaryData ) 114 { 115 116 if ( binaryData.length != 16 && binaryData.length != 20 ) 117 { 118 int bitLength = binaryData.length * 8; 119 throw new IllegalArgumentException( "Unrecognised length for binary data: " + bitLength + " bits" ); 120 } 121 122 StringBuilder retValue = new StringBuilder(); 123 124 for ( byte b : binaryData ) 125 { 126 String t = Integer.toHexString( b & 0xff ); 127 128 if ( t.length() == 1 ) 129 { 130 retValue.append( '0' ).append( t ); 131 } 132 else 133 { 134 retValue.append( t ); 135 } 136 } 137 138 return retValue.toString().trim(); 139 } 140 141 142}