Java URI to Local Name getLocalDomainURI(String originalHost, String dirPath, String childUri)

Here you can find the source of getLocalDomainURI(String originalHost, String dirPath, String childUri)

Description

get Local Domain URI

License

Open Source License

Declaration

public static String getLocalDomainURI(String originalHost, String dirPath, String childUri) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.  ja va  2s . co  m*/
Copyright (c) 2003 eInnovation Inc. All rights reserved
    
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.
 */

import java.net.URL;

public class Main {
    public static String getLocalDomainURI(String originalHost, String dirPath, String childUri) {
        if (childUri.contains(dirPath))
            return childUri;

        if (childUri.startsWith("/"))
            childUri = childUri.substring(1);

        // Verify that it is a URL from the same domain
        try {
            URL url = new URL(childUri);
            String host = url.getHost();
            if (originalHost.equals(host)) {
                String path = url.getPath();
                if (path.startsWith("/"))
                    return path;
                return "/" + path;
            }
            // This URL is of no interest to us
            return null;
        } catch (Exception e) {
            if (!dirPath.endsWith("/"))
                dirPath = dirPath + "/";
            return dirPath + childUri;
        }
    }
}

Related

  1. getLocalName(String uri)
  2. getLocalName(URI uri)
  3. getLocalName(URI uri)
  4. getLocalPart(String uri)