use of org.apache.jorphan.util.JMeterException in project jmeter by apache.
the class BeanShellTimer method delay.
/**
* {@inheritDoc}
*/
@Override
public long delay() {
String ret = "0";
final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
if (bshInterpreter == null) {
log.error("BeanShell not found");
return 0;
}
try {
Object o = processFileOrScript(bshInterpreter);
if (o != null) {
ret = o.toString();
}
} catch (JMeterException e) {
if (log.isWarnEnabled()) {
log.warn("Problem in BeanShell script. {}", e.toString());
}
}
try {
return Long.decode(ret).longValue();
} catch (NumberFormatException e) {
log.warn("Number format exception while decoding number: '{}'", ret);
return 0;
}
}
use of org.apache.jorphan.util.JMeterException in project jmeter by apache.
the class ClassTools method construct.
/**
* Call a class constructor with an integer parameter
* @param className name of the class to be constructed
* @param parameter the value to be used in the constructor
* @return an instance of the class
* @throws JMeterException if class cannot be created
*/
public static Object construct(String className, int parameter) throws JMeterException {
Object instance = null;
try {
Class<?> clazz = ClassUtils.getClass(className);
Constructor<?> constructor = clazz.getConstructor(Integer.TYPE);
instance = constructor.newInstance(Integer.valueOf(parameter));
} catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | NoSuchMethodException | SecurityException | IllegalAccessException | InstantiationException e) {
throw new JMeterException(e);
}
return instance;
}
use of org.apache.jorphan.util.JMeterException in project jmeter by apache.
the class ClassTools method invoke.
/**
* Invoke a public method on a class instance
*
* @param instance
* object on which the method should be called
* @param methodName
* name of the method to be called
* @throws SecurityException
* if a security violation occurred while looking for the method
* @throws IllegalArgumentException
* if the method parameters (none given) do not match the
* signature of the method
* @throws JMeterException
* if something went wrong in the invoked method
*/
public static void invoke(Object instance, String methodName) throws SecurityException, IllegalArgumentException, JMeterException {
Method m;
try {
m = ClassUtils.getPublicMethod(instance.getClass(), methodName, new Class[] {});
m.invoke(instance, (Object[]) null);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new JMeterException(e);
}
}
use of org.apache.jorphan.util.JMeterException in project jmeter by apache.
the class Proxy method run.
/**
* Main processing method for the Proxy object
*/
@Override
public void run() {
// Check which HTTPSampler class we should use
String httpSamplerName = target.getSamplerTypeName();
HttpRequestHdr request = new HttpRequestHdr(httpSamplerName);
SampleResult result = null;
HeaderManager headers = null;
HTTPSamplerBase sampler = null;
final boolean isDebug = log.isDebugEnabled();
if (isDebug) {
log.debug(port + "====================================================================");
}
SamplerCreator samplerCreator = null;
try {
// Now, parse initial request (in case it is a CONNECT request)
byte[] ba = request.parse(new BufferedInputStream(clientSocket.getInputStream()));
if (ba.length == 0) {
if (isDebug) {
log.debug(port + "Empty request, ignored");
}
// hack to skip processing
throw new JMeterException();
}
if (isDebug) {
log.debug(port + "Initial request: " + new String(ba));
}
outStreamClient = clientSocket.getOutputStream();
if ((request.getMethod().startsWith(HTTPConstants.CONNECT)) && (outStreamClient != null)) {
if (isDebug) {
log.debug(port + "Method CONNECT => SSL");
}
// write a OK response to browser, to engage SSL exchange
// $NON-NLS-1$
outStreamClient.write(("HTTP/1.0 200 OK\r\n\r\n").getBytes(SampleResult.DEFAULT_HTTP_ENCODING));
outStreamClient.flush();
// With ssl request, url is host:port (without https:// or path)
// $NON-NLS-1$
String[] param = request.getUrl().split(":");
if (param.length == 2) {
if (isDebug) {
log.debug(port + "Start to negotiate SSL connection, host: " + param[0]);
}
clientSocket = startSSL(clientSocket, param[0]);
} else {
// Should not happen, but if it does we don't want to continue
log.error("In SSL request, unable to find host and port in CONNECT request: " + request.getUrl());
// hack to skip processing
throw new JMeterException();
}
// Re-parse (now it's the http request over SSL)
try {
ba = request.parse(new BufferedInputStream(clientSocket.getInputStream()));
} catch (IOException ioe) {
// most likely this is because of a certificate error
// param.length is 2 here
final String url = " for '" + param[0] + "'";
log.warn(port + "Problem with SSL certificate" + url + "? Ensure browser is set to accept the JMeter proxy cert: " + ioe.getMessage());
// won't work: writeErrorToClient(HttpReplyHdr.formInternalError());
// Generate result (if nec.) and populate it
result = generateErrorResult(result, request, ioe, "\n**ensure browser is set to accept the JMeter proxy certificate**");
// hack to skip processing
throw new JMeterException();
}
if (isDebug) {
log.debug(port + "Reparse: " + new String(ba));
}
if (ba.length == 0) {
log.warn(port + "Empty response to http over SSL. Probably waiting for user to authorize the certificate for " + request.getUrl());
// hack to skip processing
throw new JMeterException();
}
}
samplerCreator = SAMPLERFACTORY.getSamplerCreator(request, pageEncodings, formEncodings);
sampler = samplerCreator.createAndPopulateSampler(request, pageEncodings, formEncodings);
/*
* Create a Header Manager to ensure that the browsers headers are
* captured and sent to the server
*/
headers = request.getHeaderManager();
sampler.setHeaderManager(headers);
// Needed for HTTPSampler2
sampler.threadStarted();
if (isDebug) {
log.debug(port + "Execute sample: " + sampler.getMethod() + " " + sampler.getUrl());
}
result = sampler.sample();
// Find the page encoding and possibly encodings for forms in the page
// in the response from the web server
String pageEncoding = addPageEncoding(result);
addFormEncodings(result, pageEncoding);
writeToClient(result, new BufferedOutputStream(clientSocket.getOutputStream()));
samplerCreator.postProcessSampler(sampler, result);
} catch (JMeterException jme) {
// ignored, already processed
} catch (UnknownHostException uhe) {
log.warn(port + "Server Not Found.", uhe);
writeErrorToClient(HttpReplyHdr.formServerNotFound());
// Generate result (if nec.) and populate it
result = generateErrorResult(result, request, uhe);
} catch (IllegalArgumentException e) {
log.error(port + "Not implemented (probably used https)", e);
writeErrorToClient(HttpReplyHdr.formNotImplemented("Probably used https instead of http. " + "To record https requests, see " + "<a href=\"http://jmeter.apache.org/usermanual/component_reference.html#HTTP(S)_Test_Script_Recorder\">HTTP(S) Test Script Recorder documentation</a>"));
// Generate result (if nec.) and populate it
result = generateErrorResult(result, request, e);
} catch (Exception e) {
log.error(port + "Exception when processing sample", e);
writeErrorToClient(HttpReplyHdr.formInternalError());
// Generate result (if nec.) and populate it
result = generateErrorResult(result, request, e);
} finally {
if (sampler != null && isDebug) {
log.debug(port + "Will deliver sample " + sampler.getName());
}
/*
* We don't want to store any cookies in the generated test plan
*/
if (headers != null) {
// Always remove cookies
headers.removeHeaderNamed(HTTPConstants.HEADER_COOKIE);
// Remove additional headers
for (String hdr : HEADERS_TO_REMOVE) {
headers.removeHeaderNamed(hdr);
}
}
if (// deliverSampler allows sampler to be null, but result must not be null
result != null) {
List<TestElement> children = new ArrayList<>();
if (captureHttpHeaders) {
children.add(headers);
}
if (samplerCreator != null) {
children.addAll(samplerCreator.createChildren(sampler, result));
}
target.deliverSampler(sampler, children.toArray(new TestElement[children.size()]), result);
}
try {
clientSocket.close();
} catch (Exception e) {
log.error(port + "Failed to close client socket", e);
}
if (sampler != null) {
// Needed for HTTPSampler2
sampler.threadFinished();
}
}
}
use of org.apache.jorphan.util.JMeterException in project jmeter by apache.
the class BeanShellPostProcessor method process.
@Override
public void process() {
JMeterContext jmctx = JMeterContextService.getContext();
SampleResult prev = jmctx.getPreviousResult();
if (prev == null) {
// TODO - should we skip processing here?
return;
}
final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
if (bshInterpreter == null) {
log.error("BeanShell not found");
return;
}
try {
// Add variables for access to context and variables
//$NON-NLS-1$
bshInterpreter.set("data", prev.getResponseData());
processFileOrScript(bshInterpreter);
} catch (JMeterException e) {
if (log.isWarnEnabled()) {
log.warn("Problem in BeanShell script: {}", e.toString());
}
}
}
Aggregations