View Javadoc

1   /*
2    * #%L
3    * bitrepository-access-client
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.pillar.checksumpillar.messagehandler;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.bitrepository.bitrepositoryelements.FileIDs;
31  import org.bitrepository.bitrepositoryelements.ResponseCode;
32  import org.bitrepository.bitrepositoryelements.ResponseInfo;
33  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForGetChecksumsRequest;
34  import org.bitrepository.bitrepositorymessages.IdentifyPillarsForGetChecksumsResponse;
35  import org.bitrepository.bitrepositorymessages.MessageResponse;
36  import org.bitrepository.common.utils.TimeMeasurementUtils;
37  import org.bitrepository.pillar.cache.ChecksumStore;
38  import org.bitrepository.pillar.common.MessageHandlerContext;
39  import org.bitrepository.service.exception.IdentifyContributorException;
40  import org.bitrepository.service.exception.RequestHandlerException;
41  
42  /**
43   * Class for handling the identification of this pillar for the purpose of performing the GetChecksums operation.
44   */
45  public class IdentifyPillarsForGetChecksumsRequestHandler
46          extends ChecksumPillarMessageHandler<IdentifyPillarsForGetChecksumsRequest> {
47      
48      /**
49       * Constructor.
50       * @param context The context of the message handler.
51       * @param refCache The cache for the checksum data.
52       */
53      public IdentifyPillarsForGetChecksumsRequestHandler(MessageHandlerContext context, ChecksumStore refCache) {
54          super(context, refCache);
55      }
56  
57      @Override
58      public Class<IdentifyPillarsForGetChecksumsRequest> getRequestClass() {
59          return IdentifyPillarsForGetChecksumsRequest.class;
60      }
61  
62      @Override
63      public void processRequest(IdentifyPillarsForGetChecksumsRequest message) throws RequestHandlerException {
64          validateCollectionID(message);
65          validateMessage(message);
66          checkThatAllRequestedFilesAreAvailable(message);
67          respondSuccesfullIdentification(message);
68      }
69  
70      @Override
71      public MessageResponse generateFailedResponse(IdentifyPillarsForGetChecksumsRequest message) {
72          return createFinalResponse(message);
73      }
74      
75      /**
76       * Validates the identification message.
77       * @param message The identify message to validate.
78       */
79      private void validateMessage(IdentifyPillarsForGetChecksumsRequest message) throws RequestHandlerException {
80          validateChecksumSpec(message.getChecksumRequestForExistingFile(), message.getCollectionID());        
81      }
82      
83      /**
84       * Validates that all the requested files in the filelist are present. 
85       * Otherwise an {@link IdentifyContributorException} with the appropriate errorcode is thrown.
86       * @param message The message containing the list files. An empty filelist is expected 
87       * when "AllFiles" or the parameter option is used.
88       */
89      public void checkThatAllRequestedFilesAreAvailable(IdentifyPillarsForGetChecksumsRequest message) 
90              throws RequestHandlerException {
91          FileIDs fileids = message.getFileIDs();
92          validateFileID(message.getFileIDs().getFileID());
93          
94          List<String> missingFiles = new ArrayList<String>();
95          String fileID = fileids.getFileID();
96          if(fileID != null && !getCache().hasFile(fileID, message.getCollectionID())) {
97              missingFiles.add(fileID);
98          }
99          
100         // Throw exception if any files are missing.
101         if(!missingFiles.isEmpty()) {
102             ResponseInfo irInfo = new ResponseInfo();
103             irInfo.setResponseCode(ResponseCode.FILE_NOT_FOUND_FAILURE);
104             
105             throw new IdentifyContributorException(irInfo, message.getCollectionID());
106         }
107     }
108     
109     /**
110      * Method for making a successful response to the identification.
111      * @param request The request to respond to.
112      */
113     private void respondSuccesfullIdentification(IdentifyPillarsForGetChecksumsRequest request) {
114         IdentifyPillarsForGetChecksumsResponse response = createFinalResponse(request);
115 
116         response.setTimeToDeliver(TimeMeasurementUtils.getTimeMeasurementFromMiliseconds(
117             getSettings().getReferenceSettings().getPillarSettings().getTimeToStartDeliver()));
118         
119         ResponseInfo irInfo = new ResponseInfo();
120         irInfo.setResponseCode(ResponseCode.IDENTIFICATION_POSITIVE);
121         response.setResponseInfo(irInfo);
122 
123         dispatchResponse(response, request);
124     }
125     
126     /**
127      * Creates a IdentifyPillarsForGetChecksumsResponse based on a 
128      * IdentifyPillarsForGetFileRequest. The following fields are not inserted:
129      * <br/> - TimeToDeliver
130      * <br/> - AuditTrailInformation
131      * <br/> - IdentifyResponseInfo
132      * <br/> - PillarChecksumSpec
133      * 
134      * @param msg The IdentifyPillarsForGetFileRequest to base the response on.
135      * @return The response to the request.
136      */
137     private IdentifyPillarsForGetChecksumsResponse createFinalResponse(
138             IdentifyPillarsForGetChecksumsRequest msg) {
139         IdentifyPillarsForGetChecksumsResponse res 
140                 = new IdentifyPillarsForGetChecksumsResponse();
141         res.setFileIDs(msg.getFileIDs());
142         res.setChecksumRequestForExistingFile(msg.getChecksumRequestForExistingFile());
143         res.setPillarID(getSettings().getReferenceSettings().getPillarSettings().getPillarID());
144         res.setPillarChecksumSpec(getChecksumType());
145         
146         return res;
147     }
148 }