View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.utils;
20  
21  import java.io.File;
22  import java.net.MalformedURLException;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.net.URL;
26  
27  public final class URIUtils {
28  
29      private URIUtils() {
30          // empty constructor for static utility class
31      }
32  
33      /**
34       * Build a valid URI out of the given location.
35       * Only "file", "connid" and "connids" schemes are allowed.
36       * For "file", invalid characters are handled via intermediate transformation into URL.
37       *
38       * @param location the candidate location for URI
39       * @return valid URI for the given location
40       * @throws MalformedURLException if the intermediate URL is not valid
41       * @throws URISyntaxException if the given location does not correspond to a valid URI
42       */
43      public static URI buildForConnId(final String location) throws MalformedURLException, URISyntaxException {
44          final String candidate = location.trim();
45  
46          if (!candidate.startsWith("file:")
47                  && !candidate.startsWith("connid:") && !candidate.startsWith("connids:")) {
48  
49              throw new IllegalArgumentException(candidate + " is not a valid URI for file or connid(s) schemes");
50          }
51  
52          URI uri;
53          if (candidate.startsWith("file:")) {
54              uri = new File(new URL(candidate).getFile()).getAbsoluteFile().toURI();
55          } else {
56              uri = new URI(candidate);
57          }
58  
59          return uri;
60      }
61  }