Java URL Normalize normalize(String url)

Here you can find the source of normalize(String url)

Description

Normalizes a given URL by applying URI#normalize() , removing trailing "/" and making sure the path part starts with "/".

License

LGPL

Parameter

Parameter Description
url the url in string format to normalize.

Exception

Parameter Description
URISyntaxException if problems parsing the url occur.
NullPointerException if the given url is null.

Return

the normalized url as specified in the description.

Declaration

public static String normalize(String url) throws URISyntaxException, NullPointerException 

Method Source Code

//package com.java2s;
/**//from   w ww.  ja  v  a  2  s. com
 * 
 *        -- class header / Copyright (C) 2008  100 % INRIA / LGPL v2.1 --
 * 
 *  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *  Copyright (C) 2008  100 % INRIA
 *  Authors :
 *                       
 *                       Gerome Canals
 *                     Nabil Hachicha
 *                     Gerald Hoster
 *                     Florent Jouille
 *                     Julien Maire
 *                     Pascal Molli
 * 
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 *  INRIA disclaims all copyright interest in the application XWoot written
 *  by :    
 *          
 *          Gerome Canals
 *         Nabil Hachicha
 *         Gerald Hoster
 *         Florent Jouille
 *         Julien Maire
 *         Pascal Molli
 * 
 *  contact : maire@loria.fr
 *  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *  
 */

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

public class Main {
    /**
     * Normalizes a given URL by applying {@link URI#normalize()}, removing trailing "/" and making sure the path part
     * starts with "/".
     * 
     * @param url the url in string format to normalize.
     * @return the normalized url as specified in the description.
     * @throws URISyntaxException if problems parsing the url occur.
     * @throws NullPointerException if the given url is null.
     */
    public static String normalize(String url) throws URISyntaxException, NullPointerException {
        String pathSeparator = "/";

        URI uri = new URI(url);
        uri = uri.normalize();

        String path = uri.getPath();

        if (!path.startsWith(pathSeparator)) {
            path = pathSeparator + path;
        }

        if (path.endsWith(pathSeparator)) {
            path = path.substring(0, path.length() - 1);
        }

        String urlStr = uri.getScheme() + "://" + uri.getHost();
        int port = uri.getPort();

        if (port != -1) {
            urlStr = urlStr + ":" + port;
        }

        urlStr = urlStr + path;

        return urlStr;
    }
}

Related

  1. normalize(final String taintedURL)
  2. normalize(String absoluteURL)
  3. normalize(String url)
  4. normalize(String url)
  5. normalize(String url_str)
  6. normalize(URL u)