View Javadoc

1   /*
2    * #%L
3    * Bitrepository Access
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.access.getfile.conversation;
26  
27  import java.util.Collection;
28  import java.util.HashSet;
29  import org.bitrepository.bitrepositorymessages.GetFileRequest;
30  import org.bitrepository.bitrepositorymessages.MessageResponse;
31  import org.bitrepository.client.conversation.ConversationContext;
32  import org.bitrepository.client.conversation.PerformingOperationState;
33  import org.bitrepository.client.conversation.selector.SelectedComponentInfo;
34  import org.bitrepository.client.eventhandler.ContributorCompleteEvent;
35  import org.bitrepository.common.exceptions.UnableToFinishException;
36  
37  /**
38   * Models the behavior of a GetFile conversation during the file exchange phase. That is, it begins with the sending of
39   * a <code>GetFileRequest</code> and finishes with on the reception of a <code>GetFileFinalResponse</code> message.
40   * 
41   * Note that this is only used by the GetFileConversation in the same package, therefore the visibility is package 
42   * protected.
43   */
44  class GettingFile extends PerformingOperationState {
45      private final GetFileConversationContext context;
46      private final SelectedComponentInfo selectedPillar;
47  
48      /**
49       * @param context The related conversation containing context information.
50       * @param pillar The pillar the file should be requested from.
51       */
52      public GettingFile(GetFileConversationContext context, SelectedComponentInfo pillar) {
53          super(pillar.getID());
54          this.context = context;
55          this.selectedPillar = pillar;
56          Collection<String> contributors = new HashSet<String>();
57          contributors.add(pillar.getID());
58      }
59  
60      @Override
61      protected void sendRequest() {
62          GetFileRequest msg = new GetFileRequest();
63          initializeMessage(msg);
64          msg.setFileAddress(context.getUrlForResult().toExternalForm());
65          msg.setFileID(context.getFileID());
66          msg.setFilePart(context.getFilePart());
67          msg.setPillarID(selectedPillar.getID());
68          msg.setDestination(selectedPillar.getDestination());
69          context.getMonitor().requestSent("Sending GetFileRequest to ", selectedPillar.toString());
70          context.getMessageSender().sendMessage(msg);
71      }
72  
73      @Override
74      protected boolean handleFailureResponse(MessageResponse msg) throws UnableToFinishException {
75          getContext().getMonitor().contributorFailed(
76                  msg.getResponseInfo().getResponseText(), msg.getFrom(), msg.getResponseInfo().getResponseCode());
77          throw new UnableToFinishException("Failed to get file from " + msg.getFrom() +
78                  ", " + msg.getResponseInfo());
79      }
80  
81      @Override
82      protected void generateContributorCompleteEvent(MessageResponse msg) {
83          getContext().getMonitor().contributorComplete(new ContributorCompleteEvent(msg.getFrom(), msg.getCollectionID()));
84      }
85  
86      @Override
87      protected ConversationContext getContext() {
88          return context;
89      }
90       
91      @Override
92      protected String getPrimitiveName() {
93          return "GetFile";
94      }
95  }