Java Code Examples for java.net.URI.getRawPath()

The following are Jave code examples for showing how to use getRawPath() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Limitart   File: QueryStringDecoderV2.java   View Source Code Vote up 6 votes
public QueryStringDecoderV2(URI uri, Charset charset, int maxParams) {
	if (uri == null) {
		throw new NullPointerException("getUri");
	}
	if (charset == null) {
		throw new NullPointerException("charset");
	}
	if (maxParams <= 0) {
		throw new IllegalArgumentException("maxParams: " + maxParams + " (expected: a positive integer)");
	}

	String rawPath = uri.getRawPath();
	if (rawPath != null) {
		hasPath = true;
	} else {
		rawPath = "";
		hasPath = false;
	}
	// Also take care of cut of things like "http://localhost"
	this.uri = rawPath + (uri.getRawQuery() == null ? "" : '?' + uri.getRawQuery());

	this.charset = charset;
	this.maxParams = maxParams;
}
 
Example 2
Project: QiNiuTest   File: Auth.java   View Source Code Vote up 6 votes
/**
 * 生成HTTP请求签名字符串
 *
 * @param urlString
 * @param body
 * @param contentType
 * @return
 */
public String signRequest(String urlString, byte[] body, String contentType) {
    URI uri = URI.create(urlString);
    String path = uri.getRawPath();
    String query = uri.getRawQuery();

    Mac mac = createMac();

    mac.update(StringUtils.utf8Bytes(path));

    if (query != null && query.length() != 0) {
        mac.update((byte) ('?'));
        mac.update(StringUtils.utf8Bytes(query));
    }
    mac.update((byte) '\n');
    if (body != null && body.length > 0 && !StringUtils.isNullOrEmpty(contentType)) {
        if (contentType.equals(Client.FormMime)
                || contentType.equals(Client.JsonMime)) {
            mac.update(body);
        }
    }

    String digest = UrlSafeBase64.encodeToString(mac.doFinal());

    return this.accessKey + ":" + digest;
}
 
Example 3
Project: mxhsd   File: HttpFederationClient.java   View Source Code Vote up 6 votes
private String getAuthObj(String remoteDomain, String method, URI target, JsonObject content) {
    String uri = target.getRawPath();
    if (StringUtils.isNotBlank(target.getRawQuery())) {
        uri += "?" + target.getRawQuery();
    }

    JsonObject authObj = new JsonObject();
    authObj.addProperty("method", method);
    authObj.addProperty("uri", uri);
    authObj.addProperty("origin", global.getDomain());
    authObj.addProperty("destination", remoteDomain);
    Optional.ofNullable(content).ifPresent(c -> authObj.add("content", c));
    String data = MatrixJson.encodeCanonical(authObj);
    log.info("Auth object: {}", data);
    return data;
}
 
Example 4
Project: app-ms   File: Conversions.java   View Source Code Vote up 6 votes
/**
 * Builds the request options suitable for HttpClient from a URI and a relative
 * path.
 *
 * @param uri
 *            URI
 * @return request options
 */
public static RequestOptions toRequestOptions(final URI uri,
    final String relativeUri) {

    final RequestOptions options = new RequestOptions()
        .setSsl("https".equals(uri.getScheme()))
        .setHost(uri.getHost());
    if (uri.getPort() > 0) {
        options.setPort(uri.getPort());
    } else if (options.isSsl()) {
        options.setPort(443);
    } else {
        options.setPort(80);
    }
    final String rawPath = uri.getRawPath() + relativeUri;
    if (uri.getRawQuery() == null) {
        options.setURI(rawPath);
    } else {
        options.setURI(rawPath + "?" + uri.getRawQuery());
    }
    return options;
}
 
Example 5
Project: truevfs   File: FsNodeName.java   View Source Code Vote up 5 votes
/**
 * Returns {@code true} if and only if the path component of this file
 *         system node name is empty and no query component is defined.
 * 
 * @return {@code true} if and only if the path component of this file
 *         system node name is empty and no query component is defined.
 */
public boolean isRoot() {
    //return getUri().toString().isEmpty();
    final URI uri = getUri();
    final String path = uri.getRawPath();
    if (null != path && !path.isEmpty())
        return false;
    final String query = uri.getRawQuery();
    return null == query;
}
 
Example 6
Project: vase   File: HttpUtils.java   View Source Code Vote up 5 votes
public static String relativePath(String line) throws RuntimeException {
    String connects[] = line.split(" +");
    if (connects[1].toLowerCase().startsWith("http://")) {
        URI uri = Uri.create(connects[1]);
        String path = uri.getRawPath();
        String query = uri.getRawQuery();
        String frag = uri.getRawFragment();

        if (path == null) {
            path = "/";
        }

        if (query == null) {
            query = "";
        } else {
            query = "?" + query;
        }

        if (frag == null) {
            frag = "";
        } else {
            frag = "#" + frag;
        }
        return connects[0] + " " + path + query + frag + " " + connects[2];
    } else {
        return line;
    }
}
 
Example 7
Project: ProjectAres   File: LinkComponent.java   View Source Code Vote up 5 votes
private BaseComponent displayLink(URI uri) {
    String display = uri.getHost();

    // Don't append the path if it's just "/"
    // Use the raw path with illegal chars, which tends to look nicer.
    if(!"/".equals(uri.getRawPath())) {
        display = display + uri.getRawPath();
    }

    if(!compact) {
        display = uri.getScheme() + "://" + display;
    }
    return new Component(display);
}
 
Example 8
Project: JInsight   File: RequestExecutorBasedClientInstrumentationTest.java   View Source Code Vote up 5 votes
public HttpResponse doGET(URI url) throws IOException, HttpException {
  String uri = url.getRawPath() + (url.getRawQuery() != null ? "?" + url.getRawQuery() : "");
  HttpRequest request = new BasicHttpRequest("GET", uri);
  String hostHeader = (url.getPort() == 0 || url.getPort() == 80)
      ? url.getHost() : (url.getHost() + ":" + url.getPort());
  request.addHeader("Host", hostHeader);
  return execute(request);
}
 
Example 9
Project: lams   File: URIBuilder.java   View Source Code Vote up 5 votes
private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), Consts.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}
 
Example 10
Project: FirefoxData-android   File: URIBuilder.java   View Source Code Vote up 5 votes
private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), Consts.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}
 
Example 11
Project: FirefoxData-android   File: HMACAuthHeaderProvider.java   View Source Code Vote up 5 votes
/**
 * Generate an HMAC request string.
 * <p>
 * This method trusts its inputs to be valid as per the MAC Authentication spec.
 *
 * @param request HTTP request.
 * @param timestamp to use.
 * @param nonce to use.
 * @param extra to use.
 * @return request string.
 */
protected static String getRequestString(HttpUriRequest request, long timestamp, String nonce, String extra) {
  String method = request.getMethod().toUpperCase();

  URI uri = request.getURI();
  String host = uri.getHost();

  String path = uri.getRawPath();
  if (uri.getRawQuery() != null) {
    path += "?";
    path += uri.getRawQuery();
  }
  if (uri.getRawFragment() != null) {
    path += "#";
    path += uri.getRawFragment();
  }

  int port = uri.getPort();
  String scheme = uri.getScheme();
  if (port != -1) {
  } else if ("http".equalsIgnoreCase(scheme)) {
    port = 80;
  } else if ("https".equalsIgnoreCase(scheme)) {
    port = 443;
  } else {
    throw new IllegalArgumentException("Unsupported URI scheme: " + scheme + ".");
  }

  String requestString = timestamp + "\n" +
      nonce       + "\n" +
      method      + "\n" +
      path        + "\n" +
      host        + "\n" +
      port        + "\n" +
      extra       + "\n";

  return requestString;
}
 
Example 12
Project: OpenJSharp   File: UnixUriUtils.java   View Source Code Vote up 5 votes
/**
 * Converts URI to Path
 */
static Path fromUri(NetFileSystem fs, URI uri) {
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");
    String scheme = uri.getScheme();
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getAuthority() != null)
        throw new IllegalArgumentException("URI has an authority component");
    if (uri.getFragment() != null)
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)
        throw new IllegalArgumentException("URI has a query component");

    // compatibility with java.io.File
    if (!uri.toString().startsWith("file:///"))
        return new File(uri).toPath();

    // transformation use raw path
    String p = uri.getRawPath();
    int len = p.length();
    if (len == 0)
        throw new IllegalArgumentException("URI path component is empty");

    // transform escaped octets and unescaped characters to bytes
    if (p.endsWith("/") && len > 1)
        p = p.substring(0, len - 1);

    return new NetPath(fs, p);
}
 
Example 13
Project: Reer   File: ManifestUtil.java   View Source Code Vote up 4 votes
private static String constructRelativeClasspathUri(File jarFile, File file) {
    URI jarFileUri = jarFile.getParentFile().toURI();
    URI fileUri = file.toURI();
    URI relativeUri = jarFileUri.relativize(fileUri);
    return relativeUri.getRawPath();
}
 
Example 14
Project: incubator-servicecomb-java-chassis   File: CseClientHttpRequest.java   View Source Code Vote up 4 votes
protected String findUriPath(URI uri) {
  return uri.getRawPath();
}
 
Example 15
Project: incubator-servicecomb-java-chassis   File: UrlWithServiceNameClientHttpRequestFactory.java   View Source Code Vote up 4 votes
@Override
protected String findUriPath(URI uri) {
  return "/" + uri.getAuthority() + uri.getRawPath();
}
 
Example 16
Project: incubator-netbeans   File: HgURL.java   View Source Code Vote up 4 votes
/**
 *
 * @param urlString
 * @param username
 * @param password value is cloned, if you want to null the field, call {@link #clearPassword()}
 * @throws URISyntaxException
 */
public HgURL(String urlString, String username, char[] password) throws URISyntaxException {
    URI originalUri;

    if (urlString == null) {
        throw new IllegalArgumentException("<null> URL string");    //NOI18N
    }

    if (urlString.length() == 0) {
        throw new IllegalArgumentException("empty URL string");     //NOI18N
    }

    if (looksLikePlainFilePath(urlString)) {
        originalUri = new File(urlString).toURI();
        scheme = Scheme.FILE;
    } else {
        originalUri = new URI(urlString).parseServerAuthority();
        String originalScheme = originalUri.getScheme();
        scheme = (originalScheme != null) ? determineScheme(originalScheme)
                                          : null;
    }

    if (scheme == null) {
        throw new URISyntaxException(
                urlString,
                NbBundle.getMessage(HgURL.class,
                                    "MSG_UNSUPPORTED_PROTOCOL",     //NOI18N
                                    originalUri.getScheme()));
    }

    verifyUserInfoData(scheme, username, password);

    if (username != null) {
        this.username = username;
        this.password = password == null ? null : (char[])password.clone();
    } else {
        String rawUserInfo = originalUri.getRawUserInfo();
        if (rawUserInfo == null) {
            this.username = null;
            this.password = null;
        } else {
            int colonIndex = rawUserInfo.indexOf(':');
            if (colonIndex == -1) {
                this.username = rawUserInfo;
                this.password = null;
            } else {
                this.username = rawUserInfo.substring(0, colonIndex);
                this.password = rawUserInfo.substring(colonIndex + 1).toCharArray();
            }
        }
    }

    host = originalUri.getHost();
    port = originalUri.getPort();
    rawPath     = originalUri.getRawPath();
    rawQuery    = originalUri.getRawQuery();
    rawFragment = originalUri.getRawFragment();

    path = originalUri.getPath();
}
 
Example 17
Project: centraldogma   File: Mirror.java   View Source Code Vote up 4 votes
/**
 * Splits the specified 'remoteUri' into:
 * - the actual remote repository URI
 * - the path in the remote repository
 * - the branch name.
 *
 * <p>e.g. git+ssh://foo.com/bar.git/some-path#master is split into:
 * - remoteRepoUri: git+ssh://foo.com/bar.git
 * - remotePath:    /some-path
 * - remoteBranch:  master
 *
 * <p>e.g. dogma://foo.com/bar/qux.dogma is split into:
 * - remoteRepoUri: dogma://foo.com/bar/qux.dogma
 * - remotePath:    / (default)
 * - remoteBranch:  {@code defaultBranch}
 */
private static String[] split(URI remoteUri, String suffix, @Nullable String defaultBranch) {
    final String host = remoteUri.getHost();
    if (host == null && !remoteUri.getScheme().endsWith("+file")) {
        throw new IllegalArgumentException("no host in remoteUri: " + remoteUri);
    }

    final String path = remoteUri.getRawPath();
    if (path == null) {
        throw new IllegalArgumentException("no path in remoteUri: " + remoteUri);
    }

    final Matcher matcher = Pattern.compile("^(.*?\\." + suffix + ")(?:$|/)").matcher(path);
    if (!matcher.find()) {
        throw new IllegalArgumentException("no '." + suffix + "' in remoteUri path: " + remoteUri);
    }

    final String newRemoteUri;
    final int port = remoteUri.getPort();
    if (host != null) {
        if (port > 0) {
            newRemoteUri = remoteUri.getScheme() + "://" + host + ':' + port +
                           matcher.group(1);
        } else {
            newRemoteUri = remoteUri.getScheme() + "://" + host + matcher.group(1);
        }
    } else {
        newRemoteUri = remoteUri.getScheme() + "://" + matcher.group(1);
    }

    final String remotePath;
    try {
        String decoded = URLDecoder.decode(path.substring(matcher.group(1).length()), "UTF-8");
        decoded = normalizePath(decoded);

        remotePath = decoded;
    } catch (UnsupportedEncodingException e) {
        throw new Error(e);
    }

    final String fragment = remoteUri.getFragment();
    final String remoteBranch = fragment != null ? fragment : defaultBranch;

    return new String[] { newRemoteUri, remotePath, remoteBranch };
}
 
Example 18
Project: RLibrary   File: WebSocketFactory.java   View Source Code Vote up 4 votes
/**
 * Create a WebSocket.
 *
 * <p>
 * A socket factory (= a {@link SocketFactory} instance) to create a raw
 * socket (= a {@link Socket} instance) is determined as described below.
 * </p>
 *
 * <ol>
 * <li>
 *   If the scheme of the URI is either {@code wss} or {@code https},
 *   <ol type="i">
 *     <li>
 *       If an {@link SSLContext} instance has been set by {@link
 *       #setSSLContext(SSLContext)}, the value returned from {@link
 *       SSLContext#getSocketFactory()} method of the instance is used.
 *     <li>
 *       Otherwise, if an {@link SSLSocketFactory} instance has been
 *       set by {@link #setSSLSocketFactory(SSLSocketFactory)}, the
 *       instance is used.
 *     <li>
 *       Otherwise, the value returned from {@link SSLSocketFactory#getDefault()}
 *       is used.
 *   </ol>
 * <li>
 *   Otherwise (= the scheme of the URI is either {@code ws} or {@code http}),
 *   <ol type="i">
 *     <li>
 *       If a {@link SocketFactory} instance has been set by {@link
 *       #setSocketFactory(SocketFactory)}, the instance is used.
 *     <li>
 *       Otherwise, the value returned from {@link SocketFactory#getDefault()}
 *       is used.
 *   </ol>
 * </ol>
 *
 * @param uri
 *         The URI of the WebSocket endpoint on the server side.
 *         The scheme part of the URI must be one of {@code ws},
 *         {@code wss}, {@code http} and {@code https}
 *         (case-insensitive).
 *
 * @param timeout
 *         The timeout value in milliseconds for socket connection.
 *
 * @return
 *         A WebSocket.
 *
 * @throws IllegalArgumentException
 *         The given URI is {@code null} or violates RFC 2396, or
 *         the given timeout value is negative.
 *
 * @throws IOException
 *         Failed to create a socket.
 *
 * @since 1.10
 */
public WebSocket createSocket(URI uri, int timeout) throws IOException
{
    if (uri == null)
    {
        throw new IllegalArgumentException("The given URI is null.");
    }

    if (timeout < 0)
    {
        throw new IllegalArgumentException("The given timeout value is negative.");
    }

    // Split the URI.
    String scheme   = uri.getScheme();
    String userInfo = uri.getUserInfo();
    String host     = Misc.extractHost(uri);
    int port        = uri.getPort();
    String path     = uri.getRawPath();
    String query    = uri.getRawQuery();

    return createSocket(scheme, userInfo, host, port, path, query, timeout);
}
 
Example 19
Project: FirefoxData-android   File: HawkAuthHeaderProvider.java   View Source Code Vote up 4 votes
/**
 * Generate a normalized Hawk request string. This is under-specified; the
 * code here was reverse engineered from the code at
 * <a href="https://github.com/hueniverse/hawk/blob/871cc597973110900467bd3dfb84a3c892f678fb/lib/crypto.js#L55">https://github.com/hueniverse/hawk/blob/871cc597973110900467bd3dfb84a3c892f678fb/lib/crypto.js#L55</a>.
 * <p>
 * This method trusts its inputs to be valid.
 */
protected static String getRequestString(HttpUriRequest request, String type, long timestamp, String nonce, String hash, String extra, String app, String dlg) {
  String method = request.getMethod().toUpperCase(Locale.US);

  URI uri = request.getURI();
  String host = uri.getHost();

  String path = uri.getRawPath();
  if (uri.getRawQuery() != null) {
    path += "?";
    path += uri.getRawQuery();
  }
  if (uri.getRawFragment() != null) {
    path += "#";
    path += uri.getRawFragment();
  }

  int port = uri.getPort();
  String scheme = uri.getScheme();
  if (port != -1) {
  } else if ("http".equalsIgnoreCase(scheme)) {
    port = 80;
  } else if ("https".equalsIgnoreCase(scheme)) {
    port = 443;
  } else {
    throw new IllegalArgumentException("Unsupported URI scheme: " + scheme + ".");
  }

  StringBuilder sb = new StringBuilder();
  sb.append("hawk.");
  sb.append(HAWK_HEADER_VERSION);
  sb.append('.');
  sb.append(type);
  sb.append('\n');
  sb.append(timestamp);
  sb.append('\n');
  sb.append(nonce);
  sb.append('\n');
  sb.append(method);
  sb.append('\n');
  sb.append(path);
  sb.append('\n');
  sb.append(host);
  sb.append('\n');
  sb.append(port);
  sb.append('\n');
  if (hash != null) {
    sb.append(hash);
  }
  sb.append("\n");
  if (extra != null && extra.length() > 0) {
    sb.append(escapeExtraString(extra));
  }
  sb.append("\n");
  if (app != null) {
    sb.append(app);
    sb.append("\n");
    if (dlg != null) {
      sb.append(dlg);
    }
    sb.append("\n");
  }

  return sb.toString();
}
 
Example 20
Project: BUbiNG   File: BURL.java   View Source Code Vote up 2 votes
/** Returns the concatenated {@linkplain URI#getRawPath() raw path} and {@linkplain URI#getRawQuery() raw query} of a BUbiNG URL.
 *
 * @param url a BUbiNG URL.
 * @return the concatenated {@linkplain URI#getRawPath() raw path} and {@linkplain URI#getRawQuery() raw query} of <code>uri</code>.
 */
public static String pathAndQuery(final URI url) {
	final String query = url.getRawQuery();
	return query != null ? url.getRawPath() + '?' + query : url.getRawPath();
}