| 1 | |
package kg.apc.jmeter.functions; |
| 2 | |
|
| 3 | |
import org.apache.commons.codec.binary.Base64; |
| 4 | |
import org.apache.jmeter.engine.util.CompoundVariable; |
| 5 | |
import org.apache.jmeter.functions.AbstractFunction; |
| 6 | |
import org.apache.jmeter.functions.InvalidVariableException; |
| 7 | |
import org.apache.jmeter.samplers.SampleResult; |
| 8 | |
import org.apache.jmeter.samplers.Sampler; |
| 9 | |
import org.apache.jmeter.threads.JMeterVariables; |
| 10 | |
|
| 11 | |
import java.util.Collection; |
| 12 | |
import java.util.LinkedList; |
| 13 | |
import java.util.List; |
| 14 | |
|
| 15 | 3 | public class Base64Decode extends AbstractFunction { |
| 16 | |
|
| 17 | 1 | private static final List<String> desc = new LinkedList<String>(); |
| 18 | |
private static final String KEY = "__base64Decode"; |
| 19 | |
|
| 20 | |
|
| 21 | |
private static final int MIN_PARAMETER_COUNT = 1; |
| 22 | |
private static final int MAX_PARAMETER_COUNT = 2; |
| 23 | |
|
| 24 | |
static { |
| 25 | 1 | desc.add("Base64 string to be decoded"); |
| 26 | 1 | desc.add("Name of variable in which to store the result (optional)"); |
| 27 | 1 | } |
| 28 | |
|
| 29 | |
private CompoundVariable[] values; |
| 30 | |
|
| 31 | |
@Override |
| 32 | |
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) |
| 33 | |
throws InvalidVariableException { |
| 34 | 1 | String sourceString = values[0].execute(); |
| 35 | |
|
| 36 | 1 | String decodedValue = new String(Base64.decodeBase64(sourceString)); |
| 37 | 1 | if (values.length > 1) { |
| 38 | 1 | String variableName = values[1].execute(); |
| 39 | 1 | if (variableName.length() > 0) { |
| 40 | 1 | final JMeterVariables variables = getVariables(); |
| 41 | 1 | if (variables != null) { |
| 42 | 1 | variables.put(variableName, decodedValue); |
| 43 | |
} |
| 44 | |
} |
| 45 | |
} |
| 46 | 1 | return decodedValue; |
| 47 | |
} |
| 48 | |
|
| 49 | |
@Override |
| 50 | |
public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { |
| 51 | 1 | checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT); |
| 52 | 1 | values = parameters.toArray(new CompoundVariable[parameters.size()]); |
| 53 | 1 | } |
| 54 | |
|
| 55 | |
@Override |
| 56 | |
public String getReferenceKey() { |
| 57 | 1 | return KEY; |
| 58 | |
} |
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public List<String> getArgumentDesc() { |
| 62 | 1 | return desc; |
| 63 | |
} |
| 64 | |
} |