View Javadoc

1   /*
2    * #%L
3    * Bitrepository Core
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.service.contributor.handler;
23  
24  import org.bitrepository.bitrepositoryelements.ResponseCode;
25  import org.bitrepository.bitrepositoryelements.ResponseInfo;
26  import org.bitrepository.bitrepositoryelements.ResultingStatus;
27  import org.bitrepository.bitrepositoryelements.StatusCode;
28  import org.bitrepository.bitrepositoryelements.StatusInfo;
29  import org.bitrepository.bitrepositorymessages.GetStatusFinalResponse;
30  import org.bitrepository.bitrepositorymessages.GetStatusProgressResponse;
31  import org.bitrepository.bitrepositorymessages.GetStatusRequest;
32  import org.bitrepository.common.utils.CalendarUtils;
33  import org.bitrepository.common.utils.ResponseInfoUtils;
34  import org.bitrepository.protocol.MessageVersionValidator;
35  import org.bitrepository.service.contributor.ContributorContext;
36  
37  /**
38   * Class for handling the GetStatusRequest messages.
39   */
40  public class GetStatusRequestHandler extends AbstractRequestHandler<GetStatusRequest> {
41      /**
42       * Constructor.
43       * @param context The context for the contributor.
44       */
45      public GetStatusRequestHandler(ContributorContext context) {
46          super(context);
47      }
48  
49      @Override
50      public Class<GetStatusRequest> getRequestClass() {
51          return GetStatusRequest.class;
52      }
53  
54      @Override
55      public void processRequest(GetStatusRequest request) {
56          validateMessage(request);
57          responseProgress(request);
58          sendFinalSuccess(request);
59      }
60      
61      @Override
62      public GetStatusFinalResponse generateFailedResponse(GetStatusRequest request) {
63          GetStatusFinalResponse response = createFinalResponse(request);
64          response.setResultingStatus(failedStatus());
65          return response;
66      }
67      
68      /**
69       * Validates the content of the message.
70       * @param request The message to validate.
71       */
72      private void validateMessage(GetStatusRequest request) {
73          if(!request.getContributor().equals(getContext().getSettings().getComponentID())) {
74              throw new IllegalArgumentException("Illegal argument exception. Expected: " + getContext().getSettings().getComponentID()
75                      + ", but it was " + request.getContributor());
76          }
77      }
78  
79      /**
80       * Creates and sends the progress response.
81       * @param request The request to base the response upon.
82       */
83      private void responseProgress(GetStatusRequest request) {
84          GetStatusProgressResponse response = new GetStatusProgressResponse();
85          response.setResponseInfo(ResponseInfoUtils.getInitialProgressResponse());
86          response.setContributor(getContext().getSettings().getComponentID());
87          getContext().getResponseDispatcher().dispatchResponse(response, request);
88      }
89      
90      /**
91       * Creates and sends the final response telling about success.
92       * @param request The request to base the response upon.
93       */
94      private void sendFinalSuccess(GetStatusRequest request) {
95          GetStatusFinalResponse response = createFinalResponse(request);
96          response.setResultingStatus(getStatus());
97  
98          ResponseInfo info = new ResponseInfo();
99          info.setResponseCode(ResponseCode.OPERATION_COMPLETED);
100         info.setResponseText("Returning status");
101         response.setResponseInfo(info);
102 
103         getContext().getResponseDispatcher().dispatchResponse(response, request);
104     }
105     
106     /**
107      * Creates the final response for the status message.
108      * Missing:
109      * <br/> ResponseInfo
110      * <br/> ResultingStatus
111      * 
112      * @param request The request to base the response upon.
113      * @return The final response.
114      */
115     private GetStatusFinalResponse createFinalResponse(GetStatusRequest request) {
116         GetStatusFinalResponse response = new GetStatusFinalResponse();
117         response.setContributor(getContext().getSettings().getComponentID());
118         return response;
119     }
120 
121     /**
122      * @return Positive status info for the contributor.
123      */
124     protected ResultingStatus getStatus() {
125         ResultingStatus res = new ResultingStatus();
126         
127         StatusInfo status = new StatusInfo();
128         status.setStatusCode(StatusCode.OK);
129         status.setStatusText("Version: " + getClass().getPackage().getImplementationVersion() + 
130                 " MessageXML version: " + MessageVersionValidator.getProtocolVersion());
131         
132         res.setStatusInfo(status);
133         res.setStatusTimestamp(CalendarUtils.getNow());
134         return res;
135     }
136     
137     /**
138      * @return Negative status info for the contributor.
139      */
140     protected ResultingStatus failedStatus() {
141         ResultingStatus res = new ResultingStatus();
142         
143         StatusInfo status = new StatusInfo();
144         status.setStatusCode(StatusCode.ERROR);
145         status.setStatusText("Unexpected error.");
146         
147         res.setStatusInfo(status);
148         res.setStatusTimestamp(CalendarUtils.getNow());
149         return res;
150     }
151 }