View Javadoc
1   /*
2    * Copyright 2015 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.validate;
17  
18  import org.openehealth.ipf.commons.core.URN;
19  
20  import java.net.URI;
21  import java.util.Optional;
22  import java.util.UUID;
23  import java.util.regex.Pattern;
24  
25  import static org.apache.commons.lang3.Validate.notNull;
26  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_UUID;
27  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
28  
29  /**
30   * Validator for UUIDs
31   * @author Boris Stanojevic
32   */
33  public class UUIDValidator implements ValueValidator {
34  
35      private static final Pattern UUID_PATTERN =
36              Pattern.compile(".*[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}");
37  
38      @Override
39      public void validate(String uuid) throws XDSMetaDataException {
40          notNull(uuid, "uuid cannot be null");
41          metaDataAssert(UUID_PATTERN.matcher(uuid).matches(), INVALID_UUID, uuid);
42      }
43  
44      /**
45       * @param id identifier
46       * @return identifier if the id is not an UUID, Optional.empty otherwise
47       */
48      public Optional<String> getSymbolicId(String id) {
49          return !isUUID(id) ? Optional.of(id) : Optional.empty();
50      }
51  
52      /**
53       * @param id identifier
54       * @return true if identifier is a valid UUID
55       */
56      public boolean isUUID(String id) {
57          return getAsUUID(id).isPresent();
58      }
59  
60      /**
61       * @param urn urn
62       * @return UUID if uri is a valid UUID, Optional.empty otherwise
63       */
64      public Optional<UUID> getAsUUID(String urn) {
65          try {
66              return Optional.of(UUID.fromString(urn.substring("urn:uuid:".length())));
67          } catch (RuntimeException e) {
68              return Optional.empty();
69          }
70      }
71  
72      /**
73       * @param uri uri
74       * @return UUID if uri is a valid UUID, Optional.empty otherwise
75       */
76      public Optional<UUID> getAsUUID(URI uri) {
77          return getAsUUID(uri.toString());
78      }
79  
80      /**
81       * @param urn urn
82       * @return UUID if uri is a valid UUID, Optional.empty otherwise
83       */
84      public Optional<UUID> getAsUUID(URN urn) {
85          return getAsUUID(urn.toString());
86      }
87  }