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 java.math.BigInteger;
25  
26  import org.bitrepository.bitrepositorymessages.MessageRequest;
27  import org.bitrepository.bitrepositorymessages.MessageResponse;
28  import org.bitrepository.protocol.ProtocolVersionLoader;
29  import org.bitrepository.service.contributor.ContributorContext;
30  
31  /**
32   * The interface for the request handlers.
33   * @param <T> The request class for the specific type of requests to be handled by this request handler.
34   */
35  public abstract class AbstractRequestHandler<T> implements RequestHandler<T> {
36      /** The constant for the VERSION of the messages.*/
37      protected static final BigInteger VERSION = ProtocolVersionLoader.loadProtocolVersion().getVersion();
38      /** The constant for the MIN_VERSION of the messages.*/
39      protected static final BigInteger MIN_VERSION = ProtocolVersionLoader.loadProtocolVersion().getMinVersion();
40      
41      /** The classpath to the 'xsd'.*/
42      protected static final String XSD_CLASSPATH = "xsd/";
43      /** The name of the XSD containing the BitRepositoryData elements. */
44      protected static final String XSD_BR_DATA = "BitRepositoryData.xsd";
45      
46      /** The context for the contributor.*/
47      private final ContributorContext context;
48      
49      /**
50       * Constructor.
51       * @param context The context for this contributor.
52       */
53      protected AbstractRequestHandler(ContributorContext context) {
54          this.context = context;
55      }
56  
57      /**
58       * @return The handler context as defined in the concrete classes.
59       */
60      protected ContributorContext getContext() {
61          return context;
62      }
63  
64      /**
65       * Delegates to the response dispatchers dispatchResponse method.
66       */
67      protected void dispatchResponse(MessageResponse response, MessageRequest request) {
68          context.getResponseDispatcher().dispatchResponse(response, request);
69      }
70  
71      /**
72       * Validates that the collectionID has been set.
73       * @param request The request to check the collectionID for.
74       */
75      protected void validateCollectionID(MessageRequest request) {
76          if(!request.isSetCollectionID()) {
77              throw new IllegalArgumentException(request.getClass().getSimpleName() +
78                      "'s requires a CollectionID");
79          }
80      }
81  }