Last active
September 29, 2017 19:19
-
-
Save andreybpanfilov/052f3566c9db8e437f11a31f4a7aed01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.documentum.ucf.client.ClientFactory | |
import com.documentum.ucf.client.transport.IClientRequest | |
import com.documentum.ucf.client.transport.IRequestHandler | |
import com.documentum.ucf.client.transport.IResultPackage | |
import com.documentum.ucf.client.transport.impl.BaseResultPackage | |
import com.documentum.ucf.client.transport.impl.ClientSession | |
import com.documentum.ucf.client.transport.impl.RequestProcessor | |
import com.documentum.ucf.client.transport.requesthandlers.spi.BaseRequestHandler | |
import com.documentum.ucf.client.transport.requesthandlers.spi.RequestHandlerMismatchException | |
import com.documentum.ucf.common.UCFMessageCodes | |
import com.documentum.ucf.common.transport.RequestHandlerError | |
import com.documentum.ucf.common.transport.impl.ResponseError | |
import com.documentum.ucf.common.transport.spi.BaseResponse | |
import org.apache.jmeter.threads.JMeterVariables | |
public class RequestProcessorCustom extends RequestProcessor { | |
@Override | |
public IRequestHandler getHandler(String command) { | |
return new HandlerWrapper(doGetHandler(command)); | |
} | |
public IRequestHandler doGetHandler(String command) { | |
try { | |
ClassLoader loader = RequestProcessor.class.getClassLoader(); | |
if ("LaunchApplication".equals(command)) { | |
return new LaunchApplicationHandler(); | |
} | |
InputStream stream = loader.getResourceAsStream(command + ".properties"); | |
if (stream == null) { | |
return null; | |
} | |
Properties props = new Properties(); | |
props.load(stream); | |
return (IRequestHandler) Class.forName(props.getProperty("class")).newInstance(); | |
} catch (IOException e) { | |
ClientFactory.getLogger().error(this, "{0}", | |
["Can not read Class for Request Handler for " + command] as String[], e); | |
throw new RequestHandlerError(e); | |
} catch (IllegalAccessException e) { | |
ClientFactory.getLogger().error(this, "{0}", | |
["Illegal access exception for Request Handler for " + command] as String[], e); | |
throw new RequestHandlerError(e); | |
} catch (ClassNotFoundException e) { | |
ClientFactory.getLogger().error(this, "{0}", | |
["Class not found for Request Handler for " + command] as String[], e); | |
throw new RequestHandlerError(e); | |
} catch (InstantiationException e) { | |
ClientFactory.getLogger().error(this, "{0}", | |
["Failed instantiate Request Handler for " + command] as String[], e); | |
throw new RequestHandlerError(e); | |
} | |
} | |
class LaunchApplicationHandler extends BaseRequestHandler { | |
public IResultPackage process(IClientRequest request) throws RequestHandlerMismatchException { | |
BaseResultPackage result = init(request); | |
BaseResponse response = (BaseResponse) result.getResponse(); | |
throw new RuntimeException(); | |
} | |
} | |
class HandlerWrapper implements IRequestHandler { | |
private final IRequestHandler _wrapped; | |
HandlerWrapper(IRequestHandler wrapped) { | |
_wrapped = wrapped | |
} | |
@Override | |
IResultPackage process(IClientRequest request) throws RequestHandlerMismatchException { | |
try { | |
return _wrapped.process(request); | |
} catch (Throwable t) { | |
BaseResultPackage result = new BaseResultPackage(); | |
result.setMode(IResultPackage.MODE_ABORT); | |
BaseResponse response = new BaseResponse(request.getCommand()); | |
result.setResponse(response); | |
String message = t.getMessage() + " - " + t.getClass().getName(); | |
response.addError(new ResponseError(UCFMessageCodes.UCF_E_ABORT_ALL_PROCESSING, message)); | |
return result; | |
} | |
} | |
@Override | |
String getVersion() { | |
return _wrapped.getVersion(); | |
} | |
} | |
} | |
class UCFHelper { | |
static { | |
ClientSession.s_requestProcessor = new RequestProcessorCustom(); | |
} | |
public static String getCookie(JMeterVariables vars) { | |
String cookie = vars.get("COOKIE_JSESSIONID"); | |
String route = vars.get("COOKIE_ROUTEID"); | |
if (route == null || route.isEmpty()) { | |
return "JSESSIONID=" + cookie; | |
} else { | |
return "JSESSIONID=" + cookie + "; ROUTEID=" + route; | |
} | |
} | |
public static String getUrl(JMeterVariables vars) { | |
return vars.get("WebtopProtocol") + "://" + vars.get("WebtopHostName") + ":" + vars.get("WebtopPort") + vars.get("WebtopContextRoot"); | |
} | |
public static void createUcfSession(JMeterVariables vars) { | |
vars.put("UCFKEY", ""); | |
String cookie = getCookie(vars); | |
ClientSession session = vars.getObject("UCFSESSION"); | |
boolean valid = session != null && session.isAlive(); | |
if (valid) { | |
valid = cookie.equals(session.getSessionId()); | |
} | |
if (valid) { | |
vars.put("UCFKEY", session.getUID()); | |
return; | |
} | |
if (session != null) { | |
session.interrupt(); | |
} | |
session = new ClientSession(getUrl(vars), "Gair", cookie); | |
session.startSession(); | |
vars.putObject("UCFSESSION", session); | |
vars.put("UCFKEY", session.getUID()); | |
} | |
} | |
try { | |
UCFHelper.createUcfSession(vars); | |
SampleResult.setSuccessful(true); | |
} catch (Exception ex) { | |
SampleResult.setSuccessful(false); | |
throw ex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment