All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.craftercms.search.service.impl.QueryParams Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2007-2013 Crafter Software Corporation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.craftercms.search.service.impl;

import org.apache.commons.lang.ArrayUtils;
import org.craftercms.search.service.Query;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Simple {@link Query} represented as a map of query parameters.
 *
 * @author Alfonso Vásquez
 */
public class QueryParams implements Query {

    private Map params;

    public QueryParams() {
        params = new LinkedHashMap();
    }

    public QueryParams(Map params) {
        this.params = new LinkedHashMap(params);
    }

    public boolean hasParam(String name) {
        return params.containsKey(name);
    }

    public String[] getParam(String name) {
        return params.get(name);
    }

    public QueryParams addParam(String name, String value) {
        String[] oldValues = params.get(name);
        String[] values;

        if (ArrayUtils.isNotEmpty(oldValues)) {
            values = (String[])ArrayUtils.add(oldValues, value);
        } else {
            values = new String[1];
            values[0] = value;
        }

        params.put(name, values);

        return this;
    }

    public QueryParams addParam(String name, String... values) {
        String[] oldValues = params.get(name);
        if (ArrayUtils.isNotEmpty(oldValues)) {
            values = (String[])ArrayUtils.addAll(oldValues, values);
        }

        params.put(name, values);

        return this;
    }

    public Map getParams() {
        return params;
    }


    public String toQueryString() {
        StringBuilder queryStr = new StringBuilder();

        for (Map.Entry param : params.entrySet()) {
            String name = param.getKey();

            for (String value : param.getValue()) {
                if (queryStr.length() > 0) {
                    queryStr.append('&');
                }

                try {
                    queryStr.append(name).append('=').append(URLEncoder.encode(value, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    // Shouldn't happen, UTF-8 is a valid encoding
                    throw new RuntimeException();
                }
            }
        }

        return queryStr.toString();
    }

    @Override
    public String toString() {
        return toQueryString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy