001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.archive.arcrepository.distribute;
024
025import java.io.File;
026
027import dk.netarkivet.archive.distribute.ArchiveMessage;
028import dk.netarkivet.archive.distribute.ArchiveMessageVisitor;
029import dk.netarkivet.common.distribute.ChannelID;
030import dk.netarkivet.common.distribute.Channels;
031import dk.netarkivet.common.distribute.RemoteFile;
032import dk.netarkivet.common.distribute.RemoteFileFactory;
033import dk.netarkivet.common.exceptions.ArgumentNotValid;
034import dk.netarkivet.common.utils.ChecksumCalculator;
035
036/**
037 * Messages requesting store of file. This message is sent to the arc repository which distributes the file to the known
038 * bitarchives, and checks the result, and then responds to the sender.
039 */
040@SuppressWarnings({"serial"})
041public class StoreMessage extends ArchiveMessage {
042    /** The actual data. */
043    private RemoteFile theRemoteFile;
044    private String precomputedChecksum;
045    /**
046     * Construct StoreMessage.
047     *
048     * @param replyTo Channel to reply back to
049     * @param arcfile The file to store
050     */
051    public StoreMessage(ChannelID replyTo, File arcfile) {
052        super(Channels.getTheRepos(), replyTo);
053        ArgumentNotValid.checkNotNull(arcfile, "arcfile");
054        theRemoteFile = RemoteFileFactory.getDistributefileInstance(arcfile);
055        precomputedChecksum = ChecksumCalculator.calculateMd5(arcfile);
056    }
057
058    /**
059     * Retrieve name of the stored file.
060     *
061     * @return current value of arcfileName
062     */
063    public String getArcfileName() {
064        return theRemoteFile.getName();
065    }
066
067    public String getPrecomputedChecksum() {
068        return precomputedChecksum;
069    }
070    
071    /**
072     * Get method for field theRemoteFile.
073     *
074     * @return Current value of theRemoteFile
075     */
076    public RemoteFile getRemoteFile() {
077        return theRemoteFile;
078    }
079
080    /**
081     * Should be implemented as a part of the visitor pattern. fx.: public void accept(ArchiveMessageVisitor v) {
082     * v.visit(this); }
083     *
084     * @param v A message visitor
085     */
086    public void accept(ArchiveMessageVisitor v) {
087        v.visit(this);
088    }
089
090    /**
091     * Generate String representation of this object.
092     *
093     * @return String representation of this object
094     */
095    public String toString() {
096        return super.toString() + " Arcfile: " + getArcfileName() + ", precomputed checksum: " 
097                        + precomputedChecksum;
098    }
099
100}