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.checksum.distribute;
024
025import dk.netarkivet.archive.distribute.ArchiveMessage;
026import dk.netarkivet.archive.distribute.ArchiveMessageVisitor;
027import dk.netarkivet.common.distribute.ChannelID;
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029
030/**
031 * The GetChecksumMessage has the purpose to retrieve the checksum of a specific file.
032 * <p>
033 * This is checksum replica alternative to sending a ChecksumBatchJob, with a filename limitation.
034 */
035@SuppressWarnings({"serial"})
036public class GetChecksumMessage extends ArchiveMessage {
037    /** The name of the arc file to retrieve the checksum from. */
038    private String arcFilename;
039    /** The resulting checksum for the arcFile. */
040    private String checksum;
041    /** The id of the replica where the checksum should be retrieved. */
042    private String replicaId;
043    /** Variable to tell whether this is a reply. */
044    private boolean isReply = false;
045
046    /**
047     * Constructor.
048     *
049     * @param to Where this message should be sent.
050     * @param replyTo Where the reply for this message should be sent.
051     * @param filename The name of the file.
052     * @param repId The id of the replica where the message is to be sent.
053     */
054    public GetChecksumMessage(ChannelID to, ChannelID replyTo, String filename, String repId) {
055        super(to, replyTo);
056        // validate arguments (channels are validated in 'super').
057        ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");
058        ArgumentNotValid.checkNotNullOrEmpty(repId, "String repId");
059
060        this.arcFilename = filename;
061        this.replicaId = repId;
062    }
063
064    /**
065     * Retrieve name of the uploaded file.
066     *
067     * @return current value of arcfileName
068     */
069    public String getArcfileName() {
070        return arcFilename;
071    }
072
073    /**
074     * Retrieves the replica id.
075     *
076     * @return The replica id.
077     */
078    public String getReplicaId() {
079        return replicaId;
080    }
081
082    /**
083     * Retrieves the checksum. This method is intended for the reply. If this checksum has not been sent, then the value
084     * is null.
085     *
086     * @return The retrieved checksum, or null if the entry was not found in the archive.
087     */
088    public String getChecksum() {
089        return checksum;
090    }
091
092    /**
093     * Retrieves the variable for telling whether this it currently is a reply to this message or not.
094     *
095     * @return Whether this is a reply or not.
096     */
097    public boolean getIsReply() {
098        return isReply;
099    }
100
101    /**
102     * Set that this is a reply. This should be set when there is replied to this message. <b>isReply = true</b>.
103     */
104    public void setIsReply() {
105        isReply = true;
106    }
107
108    /**
109     * Method for returning the result of the checksum.
110     *
111     * @param cs The checksum.
112     * @throws ArgumentNotValid If the checksum which is attempted to be set is either null or an empty string.
113     */
114    public void setChecksum(String cs) throws ArgumentNotValid {
115        ArgumentNotValid.checkNotNullOrEmpty(cs, "String cs");
116
117        checksum = cs;
118    }
119
120    /**
121     * Accept this message.
122     *
123     * @param v The message visitor accepting this message.
124     */
125    public void accept(ArchiveMessageVisitor v) {
126        v.visit(this);
127    }
128
129    /**
130     * Generate String representation of this object.
131     *
132     * @return String representation of this object
133     */
134    public String toString() {
135        return super.toString() + " Arcfiles: " + arcFilename + ", ReplicaId: " + replicaId + ", Checksum: " + checksum;
136    }
137}