Java URL Sanitize sanitizeUrlWithoutProtocol(String url, String protocol)

Here you can find the source of sanitizeUrlWithoutProtocol(String url, String protocol)

Description

Sanitizes a given URL to NOT include a protocol prefix.

License

Apache License

Declaration

public static String sanitizeUrlWithoutProtocol(String url, String protocol) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*from  w w  w .  jav a  2 s .  c  o  m*/
 *  Copyright 2011 Dan Bjorge
 *
 *  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.
 */

public class Main {
    /**
     * Sanitizes a given URL to NOT include a protocol prefix. Raises an error if this function
     * is given a URL with a protocol which is not the given one.
     *
     * Inputs may not be null.
     */
    public static String sanitizeUrlWithoutProtocol(String url, String protocol) throws IllegalArgumentException {
        int i = url.indexOf("://");
        if (i >= 0 && url.indexOf('/') - 1 == i) {
            if (!url.substring(0, i).equals(protocol)) {
                throw new IllegalArgumentException(
                        "Invalid URL: Uses protocol " + url.substring(0, i) + ", but " + protocol + " required");
            }
            return url.substring(i + 3);
        }
        return url;
    }
}

Related

  1. sanitizeUri(String uri)
  2. sanitizeURL(String url)
  3. sanitizeUrl(String url)
  4. sanitizeUrl(String url)
  5. sanitizeURL(String url)
  6. sanitizeUrlWithProtocol(String url, String defaultProtocol)