Example usage for java.net URL getUserInfo

List of usage examples for java.net URL getUserInfo

Introduction

In this page you can find the example usage for java.net URL getUserInfo.

Prototype

public String getUserInfo() 

Source Link

Document

Gets the userInfo part of this URL .

Usage

From source file:com.threadswarm.imagefeedarchiver.FeedUtils.java

/**
 * Returns a hierarchical {@code URI} constructed from individual components 
 * of the supplied {@code urlString} argument.
 * <p>/*from   ww  w.ja  va2  s.c o  m*/
 * The {@code urlString} argument is first used to instantiate a {@code URL} 
 * which in turn is used to construct a {@code URI} based on the individual 
 * components of the former.  This more robust then simply calling {@code URL.toURI()}.
 * 
 * @param urlString the {@code String} based representation of a URL
 * @return a {@code URI} constructed from the individual URL components
 * @throws URISyntaxException if a valid {@code URI} cannot be constructed from the supplied {@code urlString} argument
 * @throws MalformedURLException if the {@code urlString} cannot be used to instantiate a {@code URL}
 */
public static URI getUriFromUrlString(String urlString) throws URISyntaxException, MalformedURLException {
    URL url = new URL(urlString);
    return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());
}

From source file:com.jaeksoft.searchlib.util.LinkUtils.java

public final static URI newEncodedURI(String u) throws MalformedURLException, URISyntaxException {
    URL tmpUrl = new URL(u);
    return new URI(tmpUrl.getProtocol(), tmpUrl.getUserInfo(), tmpUrl.getHost(), tmpUrl.getPort(),
            tmpUrl.getPath(), tmpUrl.getQuery(), tmpUrl.getRef());
}

From source file:fr.dutra.confluence2wordpress.util.UrlUtils.java

/**
 * Sanitizes the given URL by escaping the path and query parameters, if necessary.
 * @see "http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java"
 * @param url/*from  w w w. ja  v a 2  s . co m*/
 * @return
 * @throws URISyntaxException
 * @throws MalformedURLException
 */
public static String sanitize(String url) throws URISyntaxException, MalformedURLException {
    URL temp = new URL(url);
    URI uri = new URI(temp.getProtocol(), temp.getUserInfo(), temp.getHost(), temp.getPort(), temp.getPath(),
            temp.getQuery(), temp.getRef());
    return uri.toASCIIString();
}

From source file:SageCollegeProject.guideBox.java

public static String GetEncWebCall(String webcall) {
    try {//from   w w  w .  j  ava 2s . c  om
        URL url = new URL(webcall);
        URI test = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        return test.toASCIIString();
    } catch (URISyntaxException | MalformedURLException ex) {
        Logger.getLogger(guideBox.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:org.eclipse.skalli.commons.URLUtils.java

/**
 * Converts a given string into a corresponding URL.
 * <p>/*w ww  .java 2  s  .  c o m*/
 * Encodes path and/or query parts of the given string according to
 * {@link URI#URI(String, String, String, int, String, String, String)}.
 * For example, blanks in the path are converted to <tt>%20</tt>.
 *
 * @param s  the string to convert to an URL.
 * @return  an URL, or <code>null</code> if the string is <code>null</code>, empty or whitespace.
 *
 * @throws MalformedURLException  if the given string is not a valid URL and cannot be
 * "sanitized" to yield a valid URL even after proper encoding of its parts.
 */
public static URL stringToURL(String s) throws MalformedURLException {
    if (StringUtils.isBlank(s)) {
        return null;
    }
    URI uri = null;
    try {
        uri = new URI(s);
    } catch (URISyntaxException e) {
        URL url = new URL(s);
        try {
            uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                    url.getQuery(), url.getRef());
        } catch (URISyntaxException e1) {
            MalformedURLException e2 = new MalformedURLException(e1.getMessage());
            e2.initCause(e1);
            throw e2;
        }
    }
    return new URL(uri.toASCIIString());
}

From source file:com.threerings.getdown.util.ConnectionUtil.java

/**
 * Opens a connection to a URL, setting the authentication header if user info is present.
 *///from w w w  . j  a v  a 2s  .  c o m
public static URLConnection open(URL url) throws IOException {
    URLConnection conn = url.openConnection();

    // If URL has a username:password@ before hostname, use HTTP basic auth
    String userInfo = url.getUserInfo();
    if (userInfo != null) {
        // Remove any percent-encoding in the username/password
        userInfo = URLDecoder.decode(userInfo, "UTF-8");
        // Now base64 encode the auth info and make it a single line
        String encoded = Base64.encodeBase64String(userInfo.getBytes("UTF-8")).replaceAll("\\n", "")
                .replaceAll("\\r", "");
        conn.setRequestProperty("Authorization", "Basic " + encoded);
    }

    return conn;
}

From source file:org.n52.web.common.RequestUtils.java

/**
 * Get the request {@link URL} without the query parameter
 *
 * @return Request {@link URL} without query parameter
 * @throws IOException/*from w  w  w.  j a va 2  s . com*/
 * @throws URISyntaxException
 */
public static String resolveQueryLessRequestUrl() throws IOException, URISyntaxException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();

    URL url = new URL(request.getRequestURL().toString());

    String scheme = url.getProtocol();
    String userInfo = url.getUserInfo();
    String host = url.getHost();

    int port = url.getPort();

    String path = request.getRequestURI();
    if (path != null && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }

    URI uri = new URI(scheme, userInfo, host, port, path, null, null);
    return uri.toString();
}

From source file:org.n52.web.common.RequestUtils.java

/**
 * Get the full request {@link URL} including the query parameter
 *
 * @return Request {@link URL} with query parameter
 * @throws IOException//from w w  w .  ja  v  a2s. c o  m
 * @throws URISyntaxException
 */
public static String resolveFullRequestUrl() throws IOException, URISyntaxException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();

    URL url = new URL(request.getRequestURL().toString());

    String scheme = url.getProtocol();
    String userInfo = url.getUserInfo();
    String host = url.getHost();

    int port = url.getPort();

    String path = request.getRequestURI();
    if (path != null && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    String query = request.getQueryString();

    URI uri = new URI(scheme, userInfo, host, port, path, query, null);
    return uri.toString();
}

From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java

public static String doGet(String urlStr) {
    URLConnection connection = null;
    try {//from   w w w  .  j  a va  2  s.  co  m
        URL url = new URL(urlStr);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        url = uri.toURL();

        // Open the connection
        connection = url.openConnection();
    } catch (Exception e) {
    }

    try {
        InputStream in = connection.getInputStream();

        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "utf-8");
        String theString = writer.toString();
        return theString;
    } catch (Exception e) {
    }
    return "";
}

From source file:org.talend.commons.utils.network.NetworkUtil.java

/**
 * encode url//from ww w  .  j a v a  2  s .  com
 * 
 * @param urlStr url not encoded yet!
 * @return
 * @throws Exception
 */
public static URL encodeUrl(String urlStr) throws Exception {
    try {
        // String decodedURL = URLDecoder.decode(urlStr, "UTF-8"); //$NON-NLS-1$
        URL url = new URL(urlStr);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        return uri.toURL();
    } catch (Exception e) {
        throw e;
    }
}