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

io.parallec.core.util.PcHttpUtils Maven / Gradle / Ivy

Go to download

Parallec: Parallel Async HTTP/SSH/PING/TCP Client library. Details at: http://www.parallec.io

There is a newer version: 0.10.6
Show newest version
/*  
Copyright [2013-2015] eBay Software Foundation
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.
 */
package io.parallec.core.util;

import io.parallec.core.actor.message.NodeReqResponse;

import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;


/**
 * Adding http header logic TODO; should finally be data driven and specific for
 * each command. Now it is defined here and user can easily change.
 * 
 * @author Yuanteng (Jeff) Pei
 * 
 */
public class PcHttpUtils {

    /** The logger. */
    private static Logger logger = LoggerFactory.getLogger(PcHttpUtils.class);

    /**
     * currently not to use MalformedURLException or MalformedURLException as
     * our logic includes add HTTP :// etc Assuming not null .
     *
     * @param url
     *            the url
     * @return true, if is url valid
     */
    public static boolean isUrlValid(String url) {

        return (!url.trim().contains(" "));

    }

    /**
     * !!!! ASSUMPTION: all VAR exists in HTTP Header must of type:
     * APIVARREPLACE_NAME_PREFIX_HTTP_HEADER
     * 
     * 20140310 This may be costly (O(n^2)) of the updated related # of headers;
     * # of parameters in the requests.
     * 
     * Better to only do it when there are some replacement in the request
     * Parameters. a prefix :
     * 
     * TOBE tested
     *
     * @param httpHeaderMap
     *            the http header map
     * @param requestParameters
     *            the request parameters
     */

    public static void replaceHttpHeaderMapNodeSpecific(
            Map httpHeaderMap,
            Map requestParameters) {

        boolean needToReplaceVarInHttpHeader = false;
        for (String parameter : requestParameters.keySet()) {
            if (parameter.contains(PcConstants.NODE_REQUEST_PREFIX_REPLACE_VAR)) {
                needToReplaceVarInHttpHeader = true;
                break;
            }
        }

        if (!needToReplaceVarInHttpHeader) {
            logger.debug("No need to replace. Since there are no HTTP header variables. ");
            return;
        }
        // replace all the values in the (not the keys) in the header map.
        for (Entry entry : httpHeaderMap.entrySet()) {
            String key = entry.getKey();
            String valueOriginal = entry.getValue();
            String valueUpdated = NodeReqResponse.replaceStrByMap(
                    requestParameters, valueOriginal);
            httpHeaderMap.put(key, valueUpdated);
        }
    }

    /**
     * Adds the headers.
     *
     * @param builder
     *            the builder
     * @param headerMap
     *            the header map
     */
    public static void addHeaders(BoundRequestBuilder builder,
            Map headerMap) {
        for (Entry entry : headerMap.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            builder.addHeader(name, value);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy