Search in sources :

Example 56 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class ContainerJmsResponseHandlerIntegrationTest method setupClients.

@Before
public void setupClients() throws Exception {
    asyncClient = createDefaultClient();
    Marshaller marshaller = MarshallerFactory.getMarshaller(new HashSet<Class<?>>(extraClasses.values()), configuration.getMarshallingFormat(), asyncClient.getClassLoader());
    responseCallback = new BlockingResponseCallback(marshaller);
    asyncClient.setResponseHandler(new AsyncResponseHandler(responseCallback));
    fireAndForgetClient = createDefaultClient();
    fireAndForgetClient.setResponseHandler(new FireAndForgetResponseHandler());
}
Also used : FireAndForgetResponseHandler(org.kie.server.client.jms.FireAndForgetResponseHandler) Marshaller(org.kie.server.api.marshalling.Marshaller) AsyncResponseHandler(org.kie.server.client.jms.AsyncResponseHandler) BlockingResponseCallback(org.kie.server.client.jms.BlockingResponseCallback) BeforeClass(org.junit.BeforeClass) Before(org.junit.Before)

Example 57 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class CustomResource method insertFireReturn.

@POST
@Path("/{ksessionId}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response insertFireReturn(@Context HttpHeaders headers, @PathParam("id") String id, @PathParam("ksessionId") String ksessionId, String cmdPayload) {
    Variant v = RestUtils.getVariant(headers);
    String contentType = RestUtils.getContentType(headers);
    MarshallingFormat format = MarshallingFormat.fromType(contentType);
    try {
        KieContainerInstance kci = registry.getContainer(id, ContainerLocatorProvider.get().getLocator());
        Marshaller marshaller = kci.getMarshaller(format);
        List<?> listOfFacts = marshaller.unmarshall(cmdPayload, List.class);
        List<Command<?>> commands = new ArrayList<Command<?>>();
        BatchExecutionCommand executionCommand = commandsFactory.newBatchExecution(commands, ksessionId);
        for (Object fact : listOfFacts) {
            commands.add(commandsFactory.newInsert(fact, fact.toString()));
        }
        commands.add(commandsFactory.newFireAllRules());
        commands.add(commandsFactory.newGetObjects());
        ExecutionResults results = rulesExecutionService.call(kci, executionCommand);
        String result = marshaller.marshall(results);
        logger.debug("Returning OK response with content '{}'", result);
        return RestUtils.createResponse(result, v, Response.Status.OK);
    } catch (Exception e) {
        // in case marshalling failed return the call container response to keep backward compatibility
        String response = "Execution failed with error : " + e.getMessage();
        logger.debug("Returning Failure response with content '{}'", response);
        return RestUtils.createResponse(response, v, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) KieContainerInstance(org.kie.server.services.api.KieContainerInstance) Variant(javax.ws.rs.core.Variant) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 58 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class JSONMarshallerExtensionTest method testObjectInsideCommand.

@Test
public void testObjectInsideCommand() {
    Set<Class<?>> extraClasses = new HashSet<Class<?>>();
    extraClasses.add(CustomPerson.class);
    Marshaller marshaller = MarshallerFactory.getMarshaller(extraClasses, MarshallingFormat.JSON, this.getClass().getClassLoader());
    CustomPerson john = new CustomPerson("John", 20);
    InsertObjectCommand command = new InsertObjectCommand(john);
    String marshall = marshaller.marshall(command);
    // verify if it's processed by JSONMarshallerExtensionCustomPerson serializer
    Assertions.assertThat(marshall).contains("John is CustomPerson");
    InsertObjectCommand unmarshall = marshaller.unmarshall(marshall, InsertObjectCommand.class);
    CustomPerson result = (CustomPerson) unmarshall.getObject();
    // verify if it's processed by JSONMarshallerExtensionCustomPerson deserializer
    assertEquals(50, result.getAge());
}
Also used : CustomPerson(org.kie.server.api.marshalling.objects.CustomPerson) Marshaller(org.kie.server.api.marshalling.Marshaller) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 59 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class JSONMarshallerTypeFactoryTest method testTypeFactoryCacheSize.

@Test
public void testTypeFactoryCacheSize() {
    Set<Class<?>> extraClasses = new HashSet<>();
    extraClasses.add(CustomPerson.class);
    Marshaller marshaller1 = MarshallerFactory.getMarshaller(extraClasses, MarshallingFormat.JSON, this.getClass().getClassLoader());
    CustomPerson john = new CustomPerson("John", 20);
    InsertObjectCommand command1 = new InsertObjectCommand(john);
    marshaller1.marshall(command1);
    Marshaller marshaller2 = MarshallerFactory.getMarshaller(extraClasses, MarshallingFormat.JSON, this.getClass().getClassLoader());
    CustomPerson paul = new CustomPerson("Paul", 17);
    InsertObjectCommand command2 = new InsertObjectCommand(paul);
    marshaller2.marshall(command2);
    TypeFactory typeFactory1 = ((JSONMarshaller) marshaller1).getTypeFactory();
    TypeFactory typeFactory2 = ((JSONMarshaller) marshaller2).getTypeFactory();
    try {
        Field field = TypeFactory.class.getDeclaredField("_typeCache");
        field.setAccessible(true);
        LookupCache<Object, JavaType> _typeCache1 = (LookupCache<Object, JavaType>) field.get(typeFactory1);
        LookupCache<Object, JavaType> _typeCache2 = (LookupCache<Object, JavaType>) field.get(typeFactory2);
        assertThat(_typeCache1.size()).isPositive();
        assertThat(_typeCache2.size()).isPositive();
        marshaller1.dispose();
        assertThat(_typeCache1.size()).isZero();
        // marshaller2 is not affected
        assertThat(_typeCache2.size()).isPositive();
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        fail("failed with reflection", e);
    }
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) CustomPerson(org.kie.server.api.marshalling.objects.CustomPerson) Field(java.lang.reflect.Field) JavaType(com.fasterxml.jackson.databind.JavaType) LookupCache(com.fasterxml.jackson.databind.util.LookupCache) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 60 with Marshaller

use of org.kie.server.api.marshalling.Marshaller in project droolsjbpm-integration by kiegroup.

the class FormServiceRestOnlyIntegrationTest method testGetTaskFormTest.

@Test
public void testGetTaskFormTest() throws Exception {
    Map<String, Object> valuesMap = new HashMap<String, Object>();
    valuesMap.put(RestURI.CONTAINER_ID, CONTAINER_ID);
    valuesMap.put(RestURI.PROCESS_ID, HIRING_PROCESS_ID);
    Marshaller marshaller = MarshallerFactory.getMarshaller(marshallingFormat, ClassLoader.getSystemClassLoader());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", "john");
    // start process instance
    WebTarget clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), PROCESS_URI + "/" + START_PROCESS_POST_URI, valuesMap));
    logger.info("[POST] " + clientRequest.getUri());
    response = clientRequest.request(getMediaType()).post(createEntity(marshaller.marshall(params)));
    Assert.assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
    Long result = response.readEntity(JaxbLong.class).unwrap();
    assertNotNull(result);
    // find tasks by process instance id
    valuesMap.put(RestURI.PROCESS_INST_ID, result);
    clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), QUERY_URI + "/" + TASK_BY_PROCESS_INST_ID_GET_URI, valuesMap));
    logger.info("[GET] " + clientRequest.getUri());
    response = clientRequest.request(getMediaType()).get();
    Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    TaskSummaryList taskSummaryList = marshaller.unmarshall(response.readEntity(String.class), TaskSummaryList.class);
    logger.debug("Form content is '{}'", taskSummaryList);
    assertNotNull(taskSummaryList);
    TaskSummary[] task = taskSummaryList.getTasks();
    assertEquals(1, task.length);
    Long taskId = task[0].getId();
    valuesMap.put(RestURI.TASK_INSTANCE_ID, taskId);
    changeUser(USER_JOHN, DEFAULT_PASSWORD);
    clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), FORM_URI + "/" + TASK_FORM_GET_URI, valuesMap));
    logger.info("[GET] " + clientRequest.getUri());
    response = clientRequest.request(getMediaType()).get();
    Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    String formdata = response.readEntity(String.class);
    logger.debug("Form content is '{}'", formdata);
    assertNotNull(formdata);
    assertFalse(formdata.isEmpty());
    clientRequest = newRequest(build(TestConfig.getKieServerHttpUrl(), PROCESS_URI + "/" + ABORT_PROCESS_INST_DEL_URI, valuesMap));
    logger.info("[DELETE] " + clientRequest.getUri());
    response = clientRequest.request().delete();
    int noContentStatusCode = Response.Status.NO_CONTENT.getStatusCode();
    int okStatusCode = Response.Status.OK.getStatusCode();
    assertTrue("Wrong status code returned: " + response.getStatus(), response.getStatus() == noContentStatusCode || response.getStatus() == okStatusCode);
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) HashMap(java.util.HashMap) JaxbLong(org.kie.server.api.model.type.JaxbLong) TaskSummary(org.kie.server.api.model.instance.TaskSummary) WebTarget(javax.ws.rs.client.WebTarget) JaxbLong(org.kie.server.api.model.type.JaxbLong) TaskSummaryList(org.kie.server.api.model.instance.TaskSummaryList) Test(org.junit.Test)

Aggregations

Marshaller (org.kie.server.api.marshalling.Marshaller)65 Test (org.junit.Test)48 HashMap (java.util.HashMap)12 WebTarget (javax.ws.rs.client.WebTarget)11 BeforeClass (org.junit.BeforeClass)10 ArrayList (java.util.ArrayList)9 MarshallingFormat (org.kie.server.api.marshalling.MarshallingFormat)9 Response (javax.ws.rs.core.Response)8 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)8 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 HashSet (java.util.HashSet)7 JaxbLong (org.kie.server.api.model.type.JaxbLong)6 KieContainerInstance (org.kie.server.services.api.KieContainerInstance)4 CallContainerCommand (org.kie.server.api.commands.CallContainerCommand)3 KieContainerResource (org.kie.server.api.model.KieContainerResource)3 KieServerCommand (org.kie.server.api.model.KieServerCommand)3 ServiceResponse (org.kie.server.api.model.ServiceResponse)3 Date (java.util.Date)2 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2