Java URI to extractFromURIParams(String paramsRule, String uri)

Here you can find the source of extractFromURIParams(String paramsRule, String uri)

Description

Extract and build a dispatch criteria string from URI parameters

License

Apache License

Parameter

Parameter Description
paramsRule The dispatch rules referencing parameters to consider
uri The URI from which we should build a specific dispatch criteria

Return

A string representing a dispatch criteria for the corresponding incoming request.

Declaration

public static String extractFromURIParams(String paramsRule, String uri) 

Method Source Code


//package com.java2s;
/*//w  w  w .j  a  va 2 s  .c o  m
 * Licensed to Laurent Broudoux (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author licenses this
 * file to you 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.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.Map;
import java.util.TreeMap;

public class Main {
    /**
     * Extract and build a dispatch criteria string from URI parameters
     * @param paramsRule The dispatch rules referencing parameters to consider
     * @param uri The URI from which we should build a specific dispatch criteria
     * @return A string representing a dispatch criteria for the corresponding incoming request.
     */
    public static String extractFromURIParams(String paramsRule, String uri) {
        Map<String, String> criteriaMap = new TreeMap<String, String>();

        if (uri.contains("?") && uri.contains("=")) {
            String parameters = uri.substring(uri.indexOf("?") + 1);

            for (String parameter : parameters.split("&")) {
                String[] pair = parameter.split("=");
                try {
                    String key = URLDecoder.decode(pair[0], "UTF-8");
                    String value = URLDecoder.decode(pair[1], "UTF-8");
                    criteriaMap.put(key, value);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            // Just appends sorted entries, separating them with ?.
            StringBuilder result = new StringBuilder();
            for (String criteria : criteriaMap.keySet()) {
                if (paramsRule.contains(criteria)) {
                    result.append("?").append(criteria).append("=").append(criteriaMap.get(criteria));
                }
            }
            return result.toString();
        }
        return "";
    }
}

Related

  1. convertToAbsPath(URI baseURI, String path)
  2. convertToEncodedURIString(String input)
  3. extractBaseUriFromRDF(String rdfString)
  4. extractContentDigest(URI contentDigest)
  5. extractForwardURIFrom(URI requestURI)
  6. extractHostAddress(String uri)
  7. extractHostname(String uri)
  8. extractLocalName(String uri)
  9. extractOntologyNameUriFromRDF(String rdfString, boolean generateURI)