View Javadoc
1   /*
2    * Copyright 2016 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  
17  package org.openehealth.ipf.commons.ihe.fhir.translation;
18  
19  import org.openehealth.ipf.commons.core.URN;
20  
21  import java.net.URISyntaxException;
22  import java.util.Objects;
23  import java.util.Optional;
24  
25  /**
26   * URI mapper base implementation that recognizes and creates OID URNs
27   *
28   * @author Christian Ohr
29   * @since 3.1
30   */
31  public abstract class AbstractUriMapper implements UriMapper {
32  
33      @Override
34      public Optional<String> uriToOid(String uri) {
35          return UriMapper.findFirst(
36                  () -> translateURN(uri, URN.OID),
37                  () -> mapUriToOid(uri));
38      }
39  
40      @Override
41      public Optional<String> uriToNamespace(String uri) {
42          return UriMapper.findFirst(
43                  () -> translateURN(uri, URN.PIN),
44                  () -> mapUriToNamespace(uri));
45      }
46  
47      @Override
48      public String oidToUri(String oid) {
49          return mapOidToUri(oid)
50                  .filter(uri -> !oid.equals(uri))
51                  .orElse(urn(URN.OID, oid).toString());
52      }
53  
54      @Override
55      public String namespaceToUri(String namespace) {
56          return mapNamespaceToUri(namespace)
57                  .filter(uri -> !namespace.equals(uri))
58                  .orElse(urn(URN.PIN, namespace.replaceAll("\\s+", "_")).toString());
59      }
60  
61      /**
62       * Translate a non-URN URI (e.g. a URL) into an OID
63       *
64       * @param uri URI
65       * @return OID string
66       */
67      protected abstract Optional<String> mapUriToOid(String uri);
68  
69      /**
70       * Translate a non-URN URI (e.g. a URL) into an OID
71       *
72       * @param uri URI
73       * @return OID string
74       */
75      protected abstract Optional<String> mapUriToNamespace(String uri);
76  
77      /**
78       * Translate a OID into an URI. Can either return null or throw an
79       * exception if no URL was found.
80       *
81       * @param oid OID
82       * @return valid URI or null if no URI was found
83       * @throws Exception if no URI was found
84       */
85      protected abstract Optional<String> mapOidToUri(String oid);
86  
87      /**
88       * Translate a namespace into an URI. Can either return null or throw an
89       * exception if no URL was found.
90       *
91       * @param namespace namespace
92       * @return valid URI or null if no URI was found
93       * @throws Exception if no URI was found
94       */
95      protected abstract Optional<String> mapNamespaceToUri(String namespace);
96  
97      /**
98       * Returns the NSS of an URI if it is an URN with the given NID, e.g. urn:oid:1.2.3.4
99       * will return 1.2.3.4 if nid = 'oid'
100      *
101      * @param uri URI
102      * @param nid namespace ID
103      * @return namespace-specific string
104      */
105     private Optional<String> translateURN(String uri, String nid) {
106         if (URN.isURN(uri)) {
107             URN urn = urn(uri);
108             if (Objects.equals(urn.getNamespaceId(), nid)) {
109                 return Optional.of(urn.getNamespaceSpecificString());
110             }
111         }
112         return Optional.empty();
113     }
114 
115     private URN urn(String uri) {
116         try {
117             return URN.create(uri);
118         } catch (URISyntaxException e) {
119             throw new InvalidUriSyntaxException(uri, e);
120         }
121     }
122 
123     private URN urn(String nid, String nss) {
124         try {
125             return new URN(nid, nss);
126         } catch (URISyntaxException e) {
127             throw new InvalidUriSyntaxException("uri:urn:" + nss, e);
128         }
129     }
130 
131 }