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 org.bitrepository.bitrepositoryelements.FileAction;
28  import org.bitrepository.bitrepositoryelements.ResponseCode;
29  import org.bitrepository.bitrepositoryelements.ResponseInfo;
30  import org.bitrepository.bitrepositorymessages.GetFileFinalResponse;
31  import org.bitrepository.bitrepositorymessages.GetFileRequest;
32  import org.bitrepository.bitrepositorymessages.MessageResponse;
33  import org.bitrepository.pillar.cache.ChecksumStore;
34  import org.bitrepository.pillar.common.MessageHandlerContext;
35  import org.bitrepository.service.exception.IllegalOperationException;
36  import org.bitrepository.service.exception.RequestHandlerException;
37  
38  /**
39   * Class for performing the GetFile operation.
40   */
41  public class GetFileRequestHandler extends ChecksumPillarMessageHandler<GetFileRequest> {
42      /**
43       * Constructor.
44       * @param context The context of the message handler.
45       * @param refCache The cache for the checksum data.
46       */
47      public GetFileRequestHandler(MessageHandlerContext context, ChecksumStore refCache) {
48          super(context,  refCache);
49      }
50      
51      @Override
52      public Class<GetFileRequest> getRequestClass() {
53          return GetFileRequest.class;
54      }
55  
56      @Override
57      public void processRequest(GetFileRequest message) throws RequestHandlerException {
58          validateCollectionID(message);
59          validatePillarId(message.getPillarID());
60  
61          getAuditManager().addAuditEvent(message.getCollectionID(), message.getFileID(), message.getFrom(), 
62                  "Failed getting file.", message.getAuditTrailInformation(), FileAction.FAILURE);
63  
64          ResponseInfo ri = new ResponseInfo();
65          ri.setResponseCode(ResponseCode.REQUEST_NOT_SUPPORTED);
66          ri.setResponseText("The Checksum pillar is unable to deliver actual files.");
67          throw new IllegalOperationException(ri, message.getCollectionID());
68      }
69  
70      @Override
71      public MessageResponse generateFailedResponse(GetFileRequest message) {
72          return createFinalResponse(message);
73      }
74  
75      /**
76       * Creates a GetFileFinalResponse based on a GetFileRequest. Missing the 
77       * following fields:
78       * <br/> - FinalResponseInfo
79       * 
80       * @param request The GetFileRequest to base the final response on.
81       * @return The GetFileFinalResponse based on the request.
82       */
83      private GetFileFinalResponse createFinalResponse(GetFileRequest request) {
84          GetFileFinalResponse res = new GetFileFinalResponse();
85          res.setFileAddress(request.getFileAddress());
86          res.setFileID(request.getFileID());
87          res.setFilePart(request.getFilePart());
88          res.setPillarID(getSettings().getReferenceSettings().getPillarSettings().getPillarID());
89  
90          return res;
91      }
92  }