View Javadoc

1   /*
2    * #%L
3    * Bitrepository Webclient
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.webservice;
23  
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  
27  public class WebserviceInputChecker {
28  
29      public static void checkFileIDParameter(String fileID) throws WebserviceIllegalArgumentException {
30          if(fileID == null || fileID.isEmpty()) {
31              throw new WebserviceIllegalArgumentException("Failure: missing fileID parameter."); 
32          }
33      }
34  
35      public static void checkFileSizeParameter(String fileSize) throws WebserviceIllegalArgumentException {
36          if(fileSize == null || fileSize.isEmpty()) {
37              throw new WebserviceIllegalArgumentException("Failure: missing filesize");
38          }
39          try {
40              Long.parseLong(fileSize);
41          } catch (Exception e) {
42              throw new WebserviceIllegalArgumentException("Failure: " + fileSize + " is not a valid number value");
43          }
44      }
45  
46      public static void checkURLParameter(String URL) throws WebserviceIllegalArgumentException {
47          if(URL == null || URL.isEmpty()) {
48              throw new  WebserviceIllegalArgumentException("Failure: missing url parameter.");
49          }
50          @SuppressWarnings("unused")
51          URL url;
52          try {
53              url = new URL(URL);
54          } catch (MalformedURLException e) {
55              throw new  WebserviceIllegalArgumentException("Failure: malformed url parameter.");
56          }
57      }
58  
59      public static void checkChecksumTypeParameter(String checksumType) throws WebserviceIllegalArgumentException {
60          if(checksumType == null || checksumType.isEmpty()) {
61              throw new WebserviceIllegalArgumentException("Failure: missing checksumType parameter."); 
62          }
63          // check for supported types?
64      }
65  
66      public static void checkPillarIDParameter(String pillarID) throws WebserviceIllegalArgumentException {
67          if(pillarID == null || pillarID.isEmpty()) {
68              throw new WebserviceIllegalArgumentException("Failure: missing pillarID parameter."); 
69          }
70      }
71  
72      public static void checkChecksumParameter(String checksum) throws WebserviceIllegalArgumentException {
73          if(checksum != null && !checksum.isEmpty()) {
74              if((checksum.length() % 2) != 0) {
75                  throw new WebserviceIllegalArgumentException("Failure: checksum parameter contains an uneven number of characters.");
76              }
77              if(!checksum.matches("^\\p{XDigit}*$")) {
78                  throw new WebserviceIllegalArgumentException("Failure: checksum parameter contains a non hexadecimal value.");
79              }
80          }    
81      }
82  
83      public static void checkSaltParameter(String salt) throws WebserviceIllegalArgumentException {
84          if(salt != null && !salt.isEmpty()) {
85              if((salt.length() % 2) != 0) {
86                  throw new WebserviceIllegalArgumentException("Failure: salt parameter contains an uneven number of characters.");
87              }
88              if(!salt.matches("^\\p{XDigit}*$")) {
89                  throw new WebserviceIllegalArgumentException("Failure: salt parameter contains a non hexadecimal value.");
90              }
91          }    
92      }
93  }