Search in sources :

Example 1 with ArgumentWrapper

use of com.axway.ats.agent.webapp.client.ArgumentWrapper in project ats-framework by Axway.

the class RemoteExecutor method executeAction.

@Override
public Object executeAction(ActionRequest actionRequest) throws AgentException {
    String actionName = actionRequest.getActionName();
    String componentName = actionRequest.getComponentName();
    Object[] arguments = actionRequest.getArguments();
    Object result = null;
    List<ArgumentWrapper> wrappedArguments = new ArrayList<ArgumentWrapper>();
    try {
        //a byte stream
        for (Object argument : arguments) {
            ArgumentWrapper argWrapper = new ArgumentWrapper();
            ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream);
            objectOutStream.writeObject(argument);
            argWrapper.setArgumentValue(byteOutStream.toByteArray());
            wrappedArguments.add(argWrapper);
        }
    } catch (IOException ioe) {
        throw new AgentException("Could not serialize input arguments", ioe);
    }
    //get the client
    AgentService agentServicePort = AgentServicePool.getInstance().getClient(atsAgent);
    try {
        //FIXME: swap with ActionWrapper
        byte[] resultAsBytes = agentServicePort.executeAction(componentName, actionName, wrappedArguments);
        //the result is returned as serialized stream
        //so we need to deserialize it
        ByteArrayInputStream byteInStream = new ByteArrayInputStream(resultAsBytes);
        ObjectInputStream objectInStream = new ObjectInputStream(byteInStream);
        result = objectInStream.readObject();
    } catch (IOException ioe) {
        throw new AgentException("Could not deserialize returned result from agent at " + atsAgent, ioe);
    } catch (AgentException_Exception ae) {
        throw new AgentException("Error while executing action on agent at " + atsAgent + ". Exception message: " + ae.getMessage());
    } catch (InternalComponentException_Exception ice) {
        //we need to get internal component exception info from the soap fault
        InternalComponentException faultInfo = ice.getFaultInfo();
        //then construct and throw a real InternalComponentException (not the JAXB mapping type above)
        throw new com.axway.ats.agent.core.exceptions.InternalComponentException(faultInfo.getComponentName(), faultInfo.getActionName(), faultInfo.getExceptionMessage() + "\n[" + HostUtils.getLocalHostIP() + " stacktrace]", atsAgent);
    } catch (Exception e) {
        throw new AgentException(e.getMessage(), e);
    }
    return result;
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) InternalComponentException_Exception(com.axway.ats.agent.webapp.client.InternalComponentException_Exception) ArrayList(java.util.ArrayList) ArgumentWrapper(com.axway.ats.agent.webapp.client.ArgumentWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InternalComponentException(com.axway.ats.agent.webapp.client.InternalComponentException) ObjectOutputStream(java.io.ObjectOutputStream) AgentException(com.axway.ats.agent.core.exceptions.AgentException) InternalComponentException_Exception(com.axway.ats.agent.webapp.client.InternalComponentException_Exception) IOException(java.io.IOException) AgentException_Exception(com.axway.ats.agent.webapp.client.AgentException_Exception) InternalComponentException(com.axway.ats.agent.webapp.client.InternalComponentException) AgentService(com.axway.ats.agent.webapp.client.AgentService) ByteArrayInputStream(java.io.ByteArrayInputStream) AgentException_Exception(com.axway.ats.agent.webapp.client.AgentException_Exception) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with ArgumentWrapper

use of com.axway.ats.agent.webapp.client.ArgumentWrapper in project ats-framework by Axway.

the class Test_RemoteExecutor method executeActionPositive.

@SuppressWarnings("unchecked")
@Test
public void executeActionPositive() throws Exception {
    Object resultToReturn = new Integer(4);
    ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream);
    objectOutStream.writeObject(resultToReturn);
    expect(AgentServicePool.getInstance()).andReturn(mockAgentServicePool);
    expect(mockAgentServicePool.getClient("10.1.1.3")).andReturn(mockAgentService);
    expect(mockAgentService.executeAction(eq(TEST_COMPONENT_NAME), eq("action 1"), (List<ArgumentWrapper>) notNull())).andReturn(byteOutStream.toByteArray());
    replayAll();
    RemoteExecutor remoteExecutor = new RemoteExecutor("10.1.1.3");
    Object actualResult = remoteExecutor.executeAction(new ActionRequest(TEST_COMPONENT_NAME, "action 1", new Object[] { 1 }));
    verifyAll();
    assertEquals(resultToReturn, actualResult);
}
Also used : ActionRequest(com.axway.ats.agent.core.action.ActionRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArgumentWrapper(com.axway.ats.agent.webapp.client.ArgumentWrapper) ObjectOutputStream(java.io.ObjectOutputStream) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with ArgumentWrapper

use of com.axway.ats.agent.webapp.client.ArgumentWrapper in project ats-framework by Axway.

the class RemoteExecutor method wrapActionRequest.

/**
     * Wrap the action request into an ActionWrapper so it is easily
     * passed to the web service
     *
     * @param actionRequest the action request to wrap
     * @return the action wrapper
     * @throws AgentException on error
     */
protected final ActionWrapper wrapActionRequest(ActionRequest actionRequest) throws AgentException {
    Object[] arguments = actionRequest.getArguments();
    List<ArgumentWrapper> wrappedArguments = new ArrayList<ArgumentWrapper>();
    try {
        //a byte stream
        for (Object argument : arguments) {
            ArgumentWrapper argWrapper = new ArgumentWrapper();
            ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream);
            objectOutStream.writeObject(argument);
            argWrapper.setArgumentValue(byteOutStream.toByteArray());
            wrappedArguments.add(argWrapper);
        }
    } catch (IOException ioe) {
        throw new AgentException("Could not serialize input arguments", ioe);
    }
    //construct the action wrapper
    ActionWrapper actionWrapper = new ActionWrapper();
    actionWrapper.setComponentName(actionRequest.getComponentName());
    actionWrapper.setActionName(actionRequest.getActionName());
    actionWrapper.getArgs().addAll(wrappedArguments);
    return actionWrapper;
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) ArrayList(java.util.ArrayList) ActionWrapper(com.axway.ats.agent.webapp.client.ActionWrapper) ArgumentWrapper(com.axway.ats.agent.webapp.client.ArgumentWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ArgumentWrapper (com.axway.ats.agent.webapp.client.ArgumentWrapper)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 AgentException (com.axway.ats.agent.core.exceptions.AgentException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ActionRequest (com.axway.ats.agent.core.action.ActionRequest)1 ActionWrapper (com.axway.ats.agent.webapp.client.ActionWrapper)1 AgentException_Exception (com.axway.ats.agent.webapp.client.AgentException_Exception)1 AgentService (com.axway.ats.agent.webapp.client.AgentService)1 InternalComponentException (com.axway.ats.agent.webapp.client.InternalComponentException)1 InternalComponentException_Exception (com.axway.ats.agent.webapp.client.InternalComponentException_Exception)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1