alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

JMeter example source code file (EncoderCache.java)

This example JMeter source code file (EncoderCache.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - JMeter tags/keywords

cache, cachelru, encodercache, encodercache, error, io, net, network, object, should, string, string, unsupportedencodingexception, unsupportedencodingexception, url_argument_encoding, url_argument_encoding, utf-8

The JMeter EncoderCache.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.
 *
 */

package org.apache.jmeter.protocol.http.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.oro.util.Cache;
import org.apache.oro.util.CacheLRU;

public class EncoderCache {

    /** The encoding which should be usd for URLs, according to HTTP specification */
    public static final String URL_ARGUMENT_ENCODING = "UTF-8";

    private Cache cache;

    public EncoderCache(int cacheSize) {
        cache = new CacheLRU(cacheSize);
    }


    /**
     * Get the specified value URL encoded using UTF-8 encoding
     *
     * @param k the value to encode
     * @return the value URL encoded using UTF-8
     */
    public String getEncoded(String k) {
        try {
            return getEncoded(k, URL_ARGUMENT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            // This can't happen (how should utf8 not be supported!?!),
            // so just throw an Error:
            throw new Error("Should not happen: " + e.toString());
        }
    }

    /**
     * Get the specified value URL encoded using the specified encoding
     *
     * @param k the value to encode
     * @param contentEncoding the encoding to use when URL encoding
     * @return the value URL encoded using the specified encoding
     * @throws UnsupportedEncodingException if the specified encoding is not supported
     */
    public String getEncoded(String k, String contentEncoding) throws UnsupportedEncodingException {
        String cacheKey = k + contentEncoding;
        // Check if we have it in the cache
        Object encodedValue = cache.getElement(cacheKey);
        if (encodedValue != null) {
            return (String) encodedValue;
        }
        // Perform the encoding
        encodedValue = URLEncoder.encode(k, contentEncoding);
        // Add to cache
        cache.addElement(cacheKey, encodedValue);
        return (String) encodedValue;
    }
}

Other JMeter examples (source code examples)

Here is a short list of links related to this JMeter EncoderCache.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.