⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urihelper.java

📁 一个java方面的消息订阅发送的源码
💻 JAVA
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: URIHelper.java,v 1.3 2005/05/27 13:58:03 tanderson Exp $
 */
package org.exolab.jms.net.uri;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.exolab.jms.common.security.BasicPrincipal;


/**
 * Helper for operations on URIs
 *
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.3 $ $Date: 2005/05/27 13:58:03 $
 * @see URI
 */
public final class URIHelper {

    /**
     * Prevent construction of helper class
     */
    private URIHelper() {
    }

    /**
     * Helper to create an URI
     *
     * @param scheme the URI scheme
     * @param host   the host
     * @param port   the port
     * @return a new URI
     * @throws InvalidURIException if any argument is invalid
     */
    public static URI create(String scheme, String host, int port)
            throws InvalidURIException {
        return create(scheme, host, port, "/");
    }

    /**
     * Helper to create an URI
     *
     * @param scheme the URI scheme
     * @param host   the host
     * @param port   the port
     * @param path   the path
     * @return a new URI
     * @throws InvalidURIException if any argument is invalid
     */
    public static URI create(String scheme, String host, int port, String path)
            throws InvalidURIException {
        URI result;
        try {
            result = new URI(scheme, null, host, port, path, null, null);
        } catch (URI.MalformedURIException exception) {
            throw new InvalidURIException(exception.getMessage());
        }
        return result;
    }

    /**
     * Helper to parse a String to an URI. If no path was specified in, the URI,
     * it will default to <code>"/"</code>
     *
     * @param uri the URI to parse
     * @return the parsed URI
     * @throws InvalidURIException if <code>uri</code> is invalid
     */
    public static URI parse(String uri) throws InvalidURIException {
        URI result;
        try {
            result = new URI(uri);
            fixPath(result);
        } catch (URI.MalformedURIException exception) {
            throw new InvalidURIException(exception.getMessage());
        }
        return result;
    }

    /**
     * Helper to parse an URI, verifying that it has the correct scheme
     *
     * @param uri    the URI to parse
     * @param scheme the expected scheme
     * @return the parsed URI
     * @throws InvalidURIException if <code>uri</code> is invalid
     */
    public static URI parse(String uri, String scheme)
            throws InvalidURIException {
        URI result = parse(uri);
        if (!result.getScheme().equals(scheme)) {
            throw new InvalidURIException(
                    "Invalid scheme: " + result.getScheme());
        }
        return result;
    }

    /**
     * Helper to parse an URI, verifying that it has the correct scheme, host
     * and port specification, and no path
     *
     * @param uri    the URI to parse
     * @param scheme the expected scheme
     * @return the parsed URI
     * @throws InvalidURIException if <code>uri</code> is invalid
     */
    public static URI parseHostPort(String uri, String scheme)
            throws InvalidURIException {
        URI result = parse(uri, scheme);
        if (result.getHost() == null) {
            throw new InvalidURIException("No host specified in URI: " + uri);
        }
        if (result.getPort() == -1) {
            throw new InvalidURIException("No port specified in URI: " + uri);
        }
        if (result.getPath() != null && !result.getPath().equals("")
                && !result.getPath().equals("/")) {
            throw new InvalidURIException(
                    "URI must not specify a path: " + uri);
        }
        return result;
    }

    /**
     * Helper to convert the host name portion of a URI to its corresponding IP
     * address. If the URI doesn't contain a host, or the host is specified as
     * an IP address, or the IP address can't be determined, then the URI is
     * returned unchanged.
     *
     * @param uri the uri to convert
     * @return the converted URI
     */
    public static URI convertHostToAddress(URI uri) {
        URI result = uri;
        String host = uri.getHost();
        if (host != null && !host.equals("")) {
            try {
                InetAddress address = InetAddress.getByName(host);
                result = new URI(uri.getScheme(), uri.getUserinfo(),
                                 address.getHostAddress(), uri.getPort(),
                                 uri.getPath(), uri.getQueryString(),
                                 uri.getFragment());
                fixPath(result);
            } catch (UnknownHostException ignore) {
            } catch (URI.MalformedURIException exception) {
                // should *never* happen
                throw new IllegalArgumentException("Failed to construct URI: "
                                                   + exception.getMessage());
            }
        }
        return result;
    }

    /**
     * Returns a {@link BasicPrincipal} corresponding to the user info specified
     * in a URI
     *
     * @param uri the URI to get the user info from
     * @return a new BasicPrincipal containing the user info, or
     *         <code>null</code> if none was specified
     */
    public static BasicPrincipal getPrincipal(URI uri) {
        BasicPrincipal principal = null;
        String userinfo = uri.getUserinfo();
        if (userinfo != null && userinfo.length() != 0) {
            int index = userinfo.indexOf(":");
            String user;
            String password = "";
            if (index != -1) {
                user = userinfo.substring(0, index);
                password = userinfo.substring(index + 1);
            } else {
                user = userinfo;
            }
            principal = new BasicPrincipal(user, password);
        }
        return principal;
    }

    /**
     * Ensures that the path is set to <code>"/"</code> if not specified in a
     * URI, so equality tests return true for instances of the form a://b:1234
     * == a://b:1234/
     *
     * @param uri the URI the fix the path for
     * @throws URI.MalformedURIException if the path can't be set
     */
    private static void fixPath(URI uri) throws URI.MalformedURIException {
        String path = uri.getPath();
        if (path == null || path.equals("")) {
            uri.setPath("/");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -