Java URI to Path getPathParamValue(URI uri)

Here you can find the source of getPathParamValue(URI uri)

Description

get Path Param Value

License

Open Source License

Declaration

public static String getPathParamValue(URI uri) 

Method Source Code

//package com.java2s;
/*/*from www .  j  a v  a  2 s  . c  o  m*/
 * Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.net.URI;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static final String FORWARDING_URI_PARAM_NAME_PATH = "path";
    public static final String URI_QUERY_PARAM_LINK_CHAR = "&";
    public static final String URI_QUERY_PARAM_KV_CHAR = "=";

    public static String getPathParamValue(URI uri) {
        return getODataParamValueAsString(uri, FORWARDING_URI_PARAM_NAME_PATH);
    }

    public static String getODataParamValueAsString(final URI uri, final String uriParamOdataType) {
        String query = uri.getQuery();
        if (query == null || query.isEmpty()) {
            return null;
        }

        if (!query.contains(uriParamOdataType)) {
            return null;
        }

        Map<String, String> queryParams = parseUriQueryParams(uri);

        String paramValue = queryParams.get(uriParamOdataType);
        if (paramValue == null || paramValue.isEmpty()) {
            return null;
        }
        return paramValue;
    }

    public static Map<String, String> parseUriQueryParams(URI uri) {
        Map<String, String> params = new HashMap<String, String>();
        String query = uri.getQuery();
        if (query == null || query.isEmpty()) {
            return params;
        }

        String[] keyValuePairs = query.split(URI_QUERY_PARAM_LINK_CHAR);
        for (String kvPair : keyValuePairs) {
            String[] kvSplit = kvPair.split(URI_QUERY_PARAM_KV_CHAR);
            if (kvSplit.length < 2) {
                continue;
            }
            String key = kvSplit[0];
            String value = kvSplit[1];
            if (kvSplit.length > 2) {
                StringBuilder sb = new StringBuilder();
                sb.append(value);
                for (int i = 2; i < kvSplit.length; i++) {
                    sb.append(URI_QUERY_PARAM_KV_CHAR);
                    sb.append(kvSplit[i]);
                }
                value = sb.toString();
            }
            params.put(key, value);
        }
        return params;
    }
}

Related

  1. getPath(URI uri)
  2. getPathAndQueryURI(URI requestUri)
  3. getPathAsArray(URI uri)
  4. getPathByTrimmingEndingFileSeparator(URI uri)
  5. getPathComponents(URI uri)
  6. getPathSegments(URI uri)