Search in sources :

Example 6 with SerializableSessionObject

use of com.hp.oo.sdk.content.plugin.SerializableSessionObject in project cs-actions by CloudSlang.

the class CookieStoreBuilderTest method buildCookieStoreWithoutCookies.

@Test
public void buildCookieStoreWithoutCookies() throws IOException {
    BasicCookieStore basicCookieStore = new BasicCookieStore();
    SerializableSessionObject sessionObjectHolder = new SerializableSessionObject();
    sessionObjectHolder.setValue(CookieStoreBuilder.serialize(basicCookieStore));
    BasicCookieStore cookieStore = (BasicCookieStore) cookieStoreBuilder.setUseCookies("false").buildCookieStore();
    assertNull(cookieStore);
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) SerializableSessionObject(com.hp.oo.sdk.content.plugin.SerializableSessionObject) Test(org.junit.Test)

Example 7 with SerializableSessionObject

use of com.hp.oo.sdk.content.plugin.SerializableSessionObject in project cs-actions by CloudSlang.

the class CookieStoreBuilderTest method buildCookieStore.

@Test
public void buildCookieStore() {
    SerializableSessionObject sessionObjectHolder = new SerializableSessionObject();
    CookieStore cookieStore = cookieStoreBuilder.setCookieStoreSessionObject(sessionObjectHolder).buildCookieStore();
    assertNotNull(cookieStore);
    assertEquals(0, cookieStore.getCookies().size());
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) SerializableSessionObject(com.hp.oo.sdk.content.plugin.SerializableSessionObject) Test(org.junit.Test)

Example 8 with SerializableSessionObject

use of com.hp.oo.sdk.content.plugin.SerializableSessionObject in project cs-actions by CloudSlang.

the class HttpClientService method execute.

public static Map<String, String> execute(HttpClientInputs httpClientInputs) throws Exception {
    URI uri = UriBuilder.getUri(httpClientInputs);
    HttpUriRequestBase httpRequest = new HttpUriRequestBase(httpClientInputs.getMethod(), uri);
    SSLConnectionSocketFactory socketFactory = CustomSSLSocketFactory.createSSLSocketFactory(httpClientInputs);
    CustomConnectionManager customConnectionManager = new CustomConnectionManager();
    GlobalSessionObject globalSessionObject = httpClientInputs.getConnectionPoolSessionObject();
    if (globalSessionObject == null)
        customConnectionManager.setConnectionPoolHolder(new GlobalSessionObject());
    else
        customConnectionManager.setConnectionPoolHolder(httpClientInputs.getConnectionPoolSessionObject());
    String connectionKey = CustomConnectionManager.buildConnectionManagerMapKey(httpClientInputs.getTrustAllRoots(), httpClientInputs.getX509HostnameVerifier(), httpClientInputs.getKeystore(), httpClientInputs.getTrustKeystore());
    customConnectionManager.setConnectionManagerMapKey(connectionKey);
    SerializableSessionObject cookieStoreSessionObject = httpClientInputs.getCookieStoreSessionObject();
    if (cookieStoreSessionObject == null) {
        cookieStoreSessionObject = new SerializableSessionObject();
    }
    CookieStore cookieStore = CookieStoreBuilder.buildCookieStore(cookieStoreSessionObject, httpClientInputs.getUseCookies());
    PoolingHttpClientConnectionManager connectionManager = customConnectionManager.getConnectionManager(httpClientInputs, socketFactory, uri);
    CredentialsProvider credentialsProvider = CustomCredentialsProvider.getCredentialsProvider(httpClientInputs, uri);
    RequestConfig requestConfig = CustomRequestConfig.getDefaultRequestConfig(httpClientInputs);
    HttpClientContext context = CustomHttpClientContext.getHttpClientContext(httpClientInputs, credentialsProvider, uri);
    HttpEntity httpEntity = CustomEntity.getHttpEntity(httpClientInputs);
    httpRequest.setEntity(httpEntity);
    HeaderBuilder.headerBuiler(httpRequest, httpClientInputs);
    HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig);
    if (cookieStore != null)
        httpClientBuilder.setDefaultCookieStore(cookieStore);
    CloseableHttpClient httpclient = httpClientBuilder.build();
    Map<String, String> result = new HashMap<>();
    if (httpClientInputs.getExecutionTimeout().equals(ZERO))
        try (final CloseableHttpResponse response = httpclient.execute(httpRequest, context)) {
            ResponseHandler.consume(result, response, httpClientInputs.getResponseCharacterSet(), httpClientInputs.getDestinationFile());
            ResponseHandler.getResponseHeaders(result, response.getHeaders());
            ResponseHandler.getStatusResponse(result, response);
            ResponseHandler.getFinalLocationResponse(result, uri, context.getRedirectLocations().getAll());
        }
    else
        ExecutionTimeout.runWithTimeout(new Runnable() {

            @Override
            public void run() {
                try (final CloseableHttpResponse response = httpclient.execute(httpRequest, context)) {
                    ResponseHandler.consume(result, response, httpClientInputs.getResponseCharacterSet(), httpClientInputs.getDestinationFile());
                    ResponseHandler.getResponseHeaders(result, response.getHeaders());
                    ResponseHandler.getStatusResponse(result, response);
                    ResponseHandler.getFinalLocationResponse(result, uri, context.getRedirectLocations().getAll());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }, Integer.parseInt(httpClientInputs.getExecutionTimeout()), TimeUnit.SECONDS);
    if (cookieStore != null) {
        try {
            cookieStoreSessionObject.setValue(CookieStoreBuilder.serialize(cookieStore));
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    result.put(RETURN_CODE, SUCCESS);
    return result;
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase) GlobalSessionObject(com.hp.oo.sdk.content.plugin.GlobalSessionObject) HttpEntity(org.apache.hc.core5.http.HttpEntity) HashMap(java.util.HashMap) SerializableSessionObject(com.hp.oo.sdk.content.plugin.SerializableSessionObject) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) CredentialsProvider(org.apache.hc.client5.http.auth.CredentialsProvider) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) IOException(java.io.IOException) URI(java.net.URI) SSLConnectionSocketFactory(org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager) CookieStore(org.apache.hc.client5.http.cookie.CookieStore) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Example 9 with SerializableSessionObject

use of com.hp.oo.sdk.content.plugin.SerializableSessionObject in project cloud-slang by CloudSlang.

the class ActionExecutionData method doAction.

public void doAction(@Param(EXECUTION_RUNTIME_SERVICES) ExecutionRuntimeServices executionRuntimeServices, @Param(ScoreLangConstants.RUN_ENV) RunEnvironment runEnv, @Param(ExecutionParametersConsts.NON_SERIALIZABLE_EXECUTION_DATA) Map<String, Map<String, Object>> nonSerializableExecutionData, @Param(ScoreLangConstants.NEXT_STEP_ID_KEY) Long nextStepId, @Param(ScoreLangConstants.ACTION_TYPE) ActionType actionType, @Param(ScoreLangConstants.JAVA_ACTION_CLASS_KEY) String className, @Param(ScoreLangConstants.JAVA_ACTION_METHOD_KEY) String methodName, @Param(ScoreLangConstants.JAVA_ACTION_GAV_KEY) String gav, @Param(ScoreLangConstants.PYTHON_ACTION_SCRIPT_KEY) String script, @Param(ScoreLangConstants.PYTHON_ACTION_USE_JYTHON_KEY) Boolean useJython, @Param(ScoreLangConstants.PYTHON_ACTION_DEPENDENCIES_KEY) Collection<String> dependencies, @Param(ScoreLangConstants.SEQ_STEPS_KEY) List<SeqStep> steps, @Param(ScoreLangConstants.SEQ_EXTERNAL_KEY) Boolean external, @Param(ExecutionParametersConsts.EXECUTION) Serializable execution) {
    Map<String, Value> returnValue = new HashMap<>();
    Map<String, Value> callArguments = runEnv.removeCallArguments();
    Map<String, Value> callArgumentsDeepCopy = new HashMap<>();
    for (Map.Entry<String, Value> entry : callArguments.entrySet()) {
        callArgumentsDeepCopy.put(entry.getKey(), ValueFactory.create(entry.getValue()));
    }
    Map<String, SerializableSessionObject> serializableSessionData = runEnv.getSerializableDataMap();
    fireEvent(executionRuntimeServices, ScoreLangConstants.EVENT_ACTION_START, "Preparing to run action " + actionType, runEnv.getExecutionPath().getParentPath(), LanguageEventData.StepType.ACTION, null, callArgumentsDeepCopy, Pair.of(LanguageEventData.CALL_ARGUMENTS, (Serializable) callArgumentsDeepCopy));
    try {
        switch(actionType) {
            case JAVA:
                returnValue = runJavaAction(serializableSessionData, callArguments, nonSerializableExecutionData, gav, className, methodName, executionRuntimeServices.getNodeNameWithDepth(), runEnv.getParentFlowStack().size());
                break;
            case PYTHON:
                returnValue = prepareAndRunPythonAction(dependencies, script, callArguments, useJython);
                break;
            case SEQUENTIAL:
                returnValue = runSequentialAction(callArguments, gav, steps, Boolean.TRUE.equals(external), execution, runEnv, nextStepId);
                break;
            default:
                break;
        }
        if (stepDataConsumer != null) {
            stepDataConsumer.consumeStepData(callArguments, returnValue);
        }
    } catch (RuntimeException ex) {
        fireEvent(executionRuntimeServices, ScoreLangConstants.EVENT_ACTION_ERROR, ex.getMessage(), runEnv.getExecutionPath().getParentPath(), LanguageEventData.StepType.ACTION, null, callArgumentsDeepCopy, Pair.of(LanguageEventData.EXCEPTION, ex.getMessage()));
        logger.error(ex);
        throw (ex);
    }
    ReturnValues returnValues = new ReturnValues(returnValue, null);
    runEnv.putReturnValues(returnValues);
    fireEvent(executionRuntimeServices, ScoreLangConstants.EVENT_ACTION_END, "Action performed", runEnv.getExecutionPath().getParentPath(), LanguageEventData.StepType.ACTION, null, callArgumentsDeepCopy);
    if (!SEQUENTIAL.equals(actionType.getValue())) {
        /*
            Due to the way Sequential Actions work, we have to pause the execution BEFORE the actual run.
            Thus, we have to populate the run environment with the required data before pausing and persisting.
            We let the sequential action handler do it, since here it would be too late.
             */
        runEnv.putNextStepPosition(nextStepId);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SerializableSessionObject(com.hp.oo.sdk.content.plugin.SerializableSessionObject) Value(io.cloudslang.lang.entities.bindings.values.Value) HashMap(java.util.HashMap) Map(java.util.Map) ReturnValues(io.cloudslang.lang.runtime.env.ReturnValues)

Example 10 with SerializableSessionObject

use of com.hp.oo.sdk.content.plugin.SerializableSessionObject in project cloud-slang by CloudSlang.

the class ActionStepsTest method doJavaActionGetNonExistingKeyFromNonExistingSerializableSessionTest.

@Test(timeout = DEFAULT_TIMEOUT)
public void doJavaActionGetNonExistingKeyFromNonExistingSerializableSessionTest() {
    // prepare doAction arguments
    RunEnvironment runEnv = new RunEnvironment();
    Map<String, Value> initialCallArguments = new HashMap<>();
    runEnv.putCallArguments(initialCallArguments);
    // invoke doAction
    actionSteps.doAction(executionRuntimeServicesMock, runEnv, nonSerializableExecutionData, 2L, JAVA, ContentTestActions.class.getName(), "getNameFromSerializableSession", GAV_DEFAULT, null, true, DEPENDENCIES_DEFAULT, seqSteps, null, null);
    Map<String, SerializableSessionObject> serializableSessionMap = runEnv.getSerializableDataMap();
    Assert.assertTrue(serializableSessionMap.containsKey("name"));
}
Also used : RunEnvironment(io.cloudslang.lang.runtime.env.RunEnvironment) HashMap(java.util.HashMap) SerializableSessionObject(com.hp.oo.sdk.content.plugin.SerializableSessionObject) Value(io.cloudslang.lang.entities.bindings.values.Value) Test(org.junit.Test)

Aggregations

SerializableSessionObject (com.hp.oo.sdk.content.plugin.SerializableSessionObject)10 Test (org.junit.Test)5 GlobalSessionObject (com.hp.oo.sdk.content.plugin.GlobalSessionObject)4 HashMap (java.util.HashMap)4 Value (io.cloudslang.lang.entities.bindings.values.Value)3 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)3 Action (com.hp.oo.sdk.content.annotations.Action)2 RunEnvironment (io.cloudslang.lang.runtime.env.RunEnvironment)2 Map (java.util.Map)2 Output (com.hp.oo.sdk.content.annotations.Output)1 Param (com.hp.oo.sdk.content.annotations.Param)1 Response (com.hp.oo.sdk.content.annotations.Response)1 MatchType (com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType)1 ResponseType (com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType)1 OutputNames (io.cloudslang.content.constants.OutputNames)1 ResponseNames (io.cloudslang.content.constants.ResponseNames)1 ReturnCodes (io.cloudslang.content.constants.ReturnCodes)1 AddMemberConstants (io.cloudslang.content.cyberark.utils.Constants.AddMemberConstants)1 CommonConstants (io.cloudslang.content.cyberark.utils.Constants.CommonConstants)1 OtherConstants (io.cloudslang.content.cyberark.utils.Constants.OtherConstants)1