Java URI Parse parseURI(String uriStr)

Here you can find the source of parseURI(String uriStr)

Description

Run a URI through the Java URI parser class to validate the URI.

License

Open Source License

Return

the parsed URI, or null if the URI is invalid.

Declaration

public static final URI parseURI(String uriStr) 

Method Source Code

//package com.java2s;
/**/*from  www.j  a  v  a  2s .  com*/
 * This file is part of the Gribbit Web Framework.
 * 
 *     https://github.com/lukehutch/gribbit
 * 
 * @author Luke Hutchison
 * 
 * --
 * 
 * @license Apache 2.0 
 * 
 * Copyright 2015 Luke Hutchison
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**
     * Run a URI through the Java URI parser class to validate the URI.
     * 
     * @return the parsed URI, or null if the URI is invalid.
     */
    public static final URI parseURI(String uriStr) {
        // Double-check for XSS-unsafe characters in URIs. Most of these (except for single quote) are
        // caught by the URI parser, but just to be safe we also manually check here.
        for (int i = 0; i < uriStr.length(); i++) {
            char c = uriStr.charAt(i);
            if (c < (char) 33 || c > (char) 126 || c == '<' || c == '>' || c == '\'' || c == '"' || c == '\\') {
                return null;
            }
        }
        try {
            // Returns new URI object if URI parses OK
            return new URI(uriStr);
        } catch (URISyntaxException e) {
            return null;
        }
    }
}

Related

  1. parseURI(final String string)
  2. parseURI(final String value)
  3. parseURI(String connectionString, URI defaultURI)
  4. parseUri(String s)
  5. parseURI(String target)
  6. parseUriParameters(URI uri)
  7. parseUriQueryParams(URI uri)
  8. parseURIs(String uri)