<commit_before>/*
 * #%L
 * Bitrepository Access
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.modify.putfile.conversation;

import org.bitrepository.bitrepositoryelements.ResponseCode;
import org.bitrepository.bitrepositorymessages.IdentifyPillarsForPutFileRequest;
import org.bitrepository.bitrepositorymessages.IdentifyPillarsForPutFileResponse;
import org.bitrepository.bitrepositorymessages.MessageResponse;
import org.bitrepository.client.conversation.ConversationContext;
import org.bitrepository.client.conversation.FinishedState;
import org.bitrepository.client.conversation.GeneralConversationState;
import org.bitrepository.client.conversation.IdentifyingState;
import org.bitrepository.client.conversation.selector.ComponentSelector;
import org.bitrepository.client.conversation.selector.MultipleComponentSelector;
import org.bitrepository.client.exceptions.UnexpectedResponseException;

/**
 * The first state of the PutFile communication. The identification of the pillars involved.
 */
public class IdentifyPillarsForPutFile extends IdentifyingState {
    private final PutFileConversationContext context;
    private final MultipleComponentSelector selector;

    /**
     * Constructor.
     * @param conversation The conversation in this given state.
     */
    public IdentifyPillarsForPutFile(PutFileConversationContext context) {
        this.context = context;
        selector = new PutFilePillarSelector(context.getSettings().getCollectionSettings().getClientSettings().getPillarIDs());

    }

    @Override
    protected void processMessage(MessageResponse msg) throws UnexpectedResponseException {
        if(msg instanceof IdentifyPillarsForPutFileResponse) {
            IdentifyPillarsForPutFileResponse response = (IdentifyPillarsForPutFileResponse) msg;
            ResponseCode responseCode = response.getResponseInfo().getResponseCode();
            switch (responseCode) {
            case IDENTIFICATION_POSITIVE:
                getContext().getMonitor().pillarIdentified(response);
                break;
            case DUPLICATE_FILE_FAILURE:
                if(response.isSetChecksumDataForExistingFile()) {
                    if(response.getChecksumDataForExistingFile().getChecksumValue().equals(
                            context.getChecksumForValidationAtPillar().getChecksumValue())) {
                        getContext().getMonitor().complete(
                                new PutFileCompletePillarEvent(response.getChecksumDataForExistingFile(),
                                        response.getPillarID(),
                                        "File already existed on " + response.getPillarID(),
                                        response.getCorrelationID()));
                    } else {
                        getContext().getMonitor().contributorFailed(
                                "Received negative response from component " + response.getFrom() +
                                ":  " + response.getResponseInfo() + "(existing file checksum does not match)", 
                                response.getFrom(), response.getResponseInfo().getResponseCode());
                    }
                } else {
                    getContext().getMonitor().contributorFailed(
                            "Received negative response from component " + response.getFrom() + ":  " +
                            response.getResponseInfo(), response.getFrom(), response.getResponseInfo().getResponseCode());
                }
                break;
            default:
                getContext().getMonitor().contributorFailed(
                        "Received negative response from component " + response.getFrom() + ":  " + 
                        response.getResponseInfo(), response.getFrom(), response.getResponseInfo().getResponseCode());
            }
            getSelector().processResponse(response);
        } else {
            throw new UnexpectedResponseException("Are currently only expecting IdentifyPillarsForPutFileResponse's");
        }
    }

    @Override
    public ComponentSelector getSelector() {
        return selector;
    }

    @Override
    public GeneralConversationState getOperationState() {
        if(selector.getOutstandingComponents().isEmpty()) {
            return new PuttingFile(context, selector.getSelectedComponents());
        } else {
            context.getMonitor().operationFailed("Failed to put file, the following pillars didn't respond: " + 
                    selector.getOutstandingComponents());
            return new FinishedState(context);
        }
    }

    @Override
    protected void sendRequest() {
        IdentifyPillarsForPutFileRequest msg = new IdentifyPillarsForPutFileRequest();
        initializeMessage(msg);
        msg.setFileID(context.getFileID());
        msg.setFileSize(context.getFileSize());
        msg.setTo(context.getSettings().getCollectionDestination());
        context.getMessageSender().sendMessage(msg);
        context.getMonitor().identifyPillarsRequestSent("Identifying pillars for put file");
    }

    @Override
    protected ConversationContext getContext() {
        return context;
    }

    @Override
    protected String getName() {
        return "Identifying pillars for put file";
    }

}
<commit_msg>Possible fix for bad comparison of checkums in put file client<commit_after>/*
 * #%L
 * Bitrepository Access
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.modify.putfile.conversation;

import org.bitrepository.bitrepositoryelements.ResponseCode;
import org.bitrepository.bitrepositorymessages.IdentifyPillarsForPutFileRequest;
import org.bitrepository.bitrepositorymessages.IdentifyPillarsForPutFileResponse;
import org.bitrepository.bitrepositorymessages.MessageResponse;
import org.bitrepository.client.conversation.ConversationContext;
import org.bitrepository.client.conversation.FinishedState;
import org.bitrepository.client.conversation.GeneralConversationState;
import org.bitrepository.client.conversation.IdentifyingState;
import org.bitrepository.client.conversation.selector.ComponentSelector;
import org.bitrepository.client.conversation.selector.MultipleComponentSelector;
import org.bitrepository.client.exceptions.UnexpectedResponseException;
import org.bitrepository.common.utils.Base16Utils;

/**
 * The first state of the PutFile communication. The identification of the pillars involved.
 */
public class IdentifyPillarsForPutFile extends IdentifyingState {
    private final PutFileConversationContext context;
    private final MultipleComponentSelector selector;

    /**
     * Constructor.
     * @param conversation The conversation in this given state.
     */
    public IdentifyPillarsForPutFile(PutFileConversationContext context) {
        this.context = context;
        selector = new PutFilePillarSelector(context.getSettings().getCollectionSettings().getClientSettings().getPillarIDs());

    }

    @Override
    protected void processMessage(MessageResponse msg) throws UnexpectedResponseException {
        if(msg instanceof IdentifyPillarsForPutFileResponse) {
            IdentifyPillarsForPutFileResponse response = (IdentifyPillarsForPutFileResponse) msg;
            ResponseCode responseCode = response.getResponseInfo().getResponseCode();
            switch (responseCode) {
            case IDENTIFICATION_POSITIVE:
                getContext().getMonitor().pillarIdentified(response);
                break;
            case DUPLICATE_FILE_FAILURE:
                if(response.isSetChecksumDataForExistingFile()) {
                    if(Base16Utils.decodeBase16(response.getChecksumDataForExistingFile().getChecksumValue()).equals(
                            Base16Utils.decodeBase16(context.getChecksumForValidationAtPillar().getChecksumValue()))) {
                        getContext().getMonitor().complete(
                                new PutFileCompletePillarEvent(response.getChecksumDataForExistingFile(),
                                        response.getPillarID(),
                                        "File already existed on " + response.getPillarID(),
                                        response.getCorrelationID()));
                    } else {
                        getContext().getMonitor().contributorFailed(
                                "Received negative response from component " + response.getFrom() +
                                ":  " + response.getResponseInfo() + " (existing file checksum does not match)", 
                                response.getFrom(), response.getResponseInfo().getResponseCode());
                    }
                } else {
                    getContext().getMonitor().contributorFailed(
                            "Received negative response from component " + response.getFrom() + ":  " +
                            response.getResponseInfo(), response.getFrom(), response.getResponseInfo().getResponseCode());
                }
                break;
            default:
                getContext().getMonitor().contributorFailed(
                        "Received negative response from component " + response.getFrom() + ":  " + 
                        response.getResponseInfo(), response.getFrom(), response.getResponseInfo().getResponseCode());
            }
            getSelector().processResponse(response);
        } else {
            throw new UnexpectedResponseException("Are currently only expecting IdentifyPillarsForPutFileResponse's");
        }
    }

    @Override
    public ComponentSelector getSelector() {
        return selector;
    }

    @Override
    public GeneralConversationState getOperationState() {
        if(selector.getOutstandingComponents().isEmpty()) {
            return new PuttingFile(context, selector.getSelectedComponents());
        } else {
            context.getMonitor().operationFailed("Failed to put file, the following pillars didn't respond: " + 
                    selector.getOutstandingComponents());
            return new FinishedState(context);
        }
    }

    @Override
    protected void sendRequest() {
        IdentifyPillarsForPutFileRequest msg = new IdentifyPillarsForPutFileRequest();
        initializeMessage(msg);
        msg.setFileID(context.getFileID());
        msg.setFileSize(context.getFileSize());
        msg.setTo(context.getSettings().getCollectionDestination());
        context.getMessageSender().sendMessage(msg);
        context.getMonitor().identifyPillarsRequestSent("Identifying pillars for put file");
    }

    @Override
    protected ConversationContext getContext() {
        return context;
    }

    @Override
    protected String getName() {
        return "Identifying pillars for put file";
    }

}

{'fraction_non_alphanumeric': 0.08315186708605533, 'fraction_numerical': 0.0027421566255343174, 'mean_word_length': 2.721488595438175, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5457879', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2835, 'n_words': 698}