@Override public Marshaller build(Set<Class<?>> classes, MarshallingFormat format, ClassLoader classLoader) { if (format.equals(MarshallingFormat.XSTREAM)) { return new XStreamMarshaller(classes, classLoader) { @Override protected void buildMarshaller(Set<Class<?>> classes, ClassLoader classLoader) { xstream = XStreamUtils.createNonTrustingXStream(new PureJavaReflectionProvider(), new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_"))); xstream.addPermission(new WildcardTypePermission(new String[]{"org.kie.server.api.**"})); String[] voidDeny = {"void.class", "Void.class"}; xstream.denyTypes(voidDeny); } }; } return super.build(classes, format, classLoader); } }
@ApiOperation(value="Evaluates rules by executing commands on the rule session", response=ServiceResponse.class, code=200) @ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error") }) @POST @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response manageContainer(@Context HttpHeaders headers, @ApiParam(value = "Container id where rules should be evaluated on", required = true) @PathParam("id") String id, @ApiParam(value = "Commands to be executed on rule engine given as BatchExecutionCommand type", required = true) String cmdPayload) { Variant v = getVariant(headers); String contentType = getContentType(headers); String classType = getClassType(headers); MarshallingFormat format = MarshallingFormat.fromType(contentType); if (format == null) { format = MarshallingFormat.valueOf(contentType); } logger.debug("Received request with content '{}'", cmdPayload); Object result = delegate.callContainer(id, cmdPayload, format, classType); Header conversationIdHeader = buildConversationIdHeader(id, registry, headers); try { String response = marshallerHelper.marshal(id, format.getType(), result, ContainerLocatorProvider.get().getLocator()); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (IllegalArgumentException e) { // in case marshalling failed return the call container response to keep backward compatibility String response = marshallerHelper.marshal(format.getType(), result); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } }
protected void updateTimer(String containerId, Long processInstanceId, long timerId, long delay, long period, int repeatLimit, boolean relative) { Map<String, Number> timerUpdate = new HashMap<>(); timerUpdate.put("delay", delay); timerUpdate.put("period", period); timerUpdate.put("repeatLimit", repeatLimit); if( config.isRest() ) { Map<String, Object> valuesMap = new HashMap<String, Object>(); valuesMap.put(CONTAINER_ID, containerId); valuesMap.put(PROCESS_INST_ID, processInstanceId); valuesMap.put(TIMER_INSTANCE_ID, timerId); Map<String, String> headers = new HashMap<String, String>(); makeHttpPutRequestAndCreateCustomResponse( build(loadBalancer.getUrl(), ADMIN_PROCESS_URI + "/" + UPDATE_TIMER_PROCESS_INST_PUT_URI, valuesMap)+ "?relative=" + relative, timerUpdate, null, headers); } else { CommandScript script = new CommandScript( Collections.singletonList( (KieServerCommand) new DescriptorCommand( "ProcessAdminService", "updateTimer", serialize(timerUpdate), marshaller.getFormat().getType(), new Object[]{containerId, processInstanceId, timerId, relative}))); ServiceResponse<?> response = (ServiceResponse<?>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM", containerId ).getResponses().get(0); throwExceptionOnFailure(response); } } }
public static MediaType getMediaType(HttpHeaders httpHeaders) { String contentType = getContentType(httpHeaders); try { return MediaType.valueOf(contentType); } catch (IllegalArgumentException e) { MarshallingFormat format = MarshallingFormat.fromType(contentType); switch (format) { case JAXB: return MediaType.APPLICATION_XML_TYPE; case XSTREAM: return MediaType.APPLICATION_XML_TYPE; case JSON: return MediaType.APPLICATION_JSON_TYPE; default: return MediaType.APPLICATION_XML_TYPE; } } }
public static MarshallingFormat fromType( String type ) { if(startsWithIgnoreCase(type, "xstream") || startsWithIgnoreCase(type, "application/xstream")) { return XSTREAM; } else if (startsWithIgnoreCase(type, "xml") || startsWithIgnoreCase(type, "application/xml")) { return JAXB; } else if (startsWithIgnoreCase(type, "json") || startsWithIgnoreCase(type, "application/json")) { return JSON; } else { try { return MarshallingFormat.valueOf(upperCase(type)); } catch(Exception ignored) {} throw new RuntimeException("Invalid marshalling format ["+type+"]"); } } }
@Override public QueryDefinition replaceQuery(QueryDefinition queryDefinition) { CommandScript script = new CommandScript(Collections.singletonList((KieServerCommand) new DescriptorCommand("QueryDataService", "replaceQuery", WebSocketUtils.marshal(queryDefinition), MarshallingFormat.JSON.toString(), new Object[]{queryDefinition.getName()}))); sendCommandToAllSessions(script, new WebSocketServiceResponse(true, (message) -> { WebSocketUtils.unmarshal(message, ServiceResponsesList.class); return null; })).getResponses(); return queryDefinition; }
textMsg.setIntProperty( JMSConstants.SERIALIZATION_FORMAT_PROPERTY_NAME, config.getMarshallingFormat().getId() );
@Override public TaskInstance getTaskInstance(String containerId, Long taskId, boolean withInputs, boolean withOutputs, boolean withAssignments) { TaskInstance result = null; if( config.isRest() ) { Map<String, Object> valuesMap = new HashMap<String, Object>(); valuesMap.put(CONTAINER_ID, containerId); valuesMap.put(TASK_INSTANCE_ID, taskId); StringBuilder queryString = new StringBuilder(); queryString.append("?withInputData").append("=").append(withInputs) .append("&withOutputData").append("=").append(withOutputs) .append("&withAssignments").append("=").append(withAssignments); result = makeHttpGetRequestAndCreateCustomResponse( build(loadBalancer.getUrl(), TASK_URI + "/" + TASK_INSTANCE_GET_URI, valuesMap) + queryString.toString(), TaskInstance.class); } else { CommandScript script = new CommandScript( Collections.singletonList( (KieServerCommand) new DescriptorCommand( "UserTaskService", "getTask", marshaller.getFormat().getType(), new Object[]{containerId, taskId, withInputs, withOutputs, withAssignments}) ) ); ServiceResponse<String> response = (ServiceResponse<String>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM", containerId ).getResponses().get(0); throwExceptionOnFailure(response); if (shouldReturnWithNullResponse(response)) { return null; } result = deserialize(response.getResult(), TaskInstance.class); } return result; }
@Test public void testExpectedMarshallingFormats() { assertEquals(MarshallingFormat.JSON, MarshallingFormat.fromType("json")); assertEquals(MarshallingFormat.JAXB, MarshallingFormat.fromType("xml")); assertEquals(MarshallingFormat.XSTREAM, MarshallingFormat.fromType("xstream")); assertEquals(MarshallingFormat.JSON, MarshallingFormat.fromType("application/json")); assertEquals(MarshallingFormat.JAXB, MarshallingFormat.fromType("application/xml")); assertEquals(MarshallingFormat.XSTREAM, MarshallingFormat.fromType("application/xstream")); }
protected KieServerHttpRequest newRequest(String uri) { KieServerHttpRequest httpRequest = KieServerHttpRequest.newRequest( uri ).followRedirects( true ).timeout( config.getTimeout() ); httpRequest.accept( getMediaType( config.getMarshallingFormat() ) ); httpRequest.header(KieServerConstants.KIE_CONTENT_TYPE_HEADER, config.getMarshallingFormat().toString()); if (config.getHeaders() != null) { for (Map.Entry<String, String> header : config.getHeaders().entrySet()) { httpRequest.header(header.getKey(), header.getValue()); logger.debug("Adding additional header {} value {}", header.getKey(), header.getValue()); } } // apply authorization if (config.getCredentialsProvider() != null) { String authorization = config.getCredentialsProvider().getAuthorization(); // add authorization only when it's not empty to allow anonymous requests if (authorization != null && !authorization.isEmpty()) { httpRequest.header(config.getCredentialsProvider().getHeaderName(), authorization); } } // apply conversationId if (owner.getConversationId() != null) { httpRequest.header(KieServerConstants.KIE_CONVERSATION_ID_TYPE_HEADER, owner.getConversationId()); } return httpRequest; }
textMsg.setIntProperty( JMSConstants.SERIALIZATION_FORMAT_PROPERTY_NAME, config.getMarshallingFormat().getId() ); textMsg.setIntProperty( JMSConstants.INTERACTION_PATTERN_PROPERTY_NAME, responseHandler.getInteractionPattern() ); if (classType != null) {
@Override public void signalProcessInstance(String containerId, Long processInstanceId, String signalName, Object event) { if( config.isRest() ) { Map<String, Object> valuesMap = new HashMap<String, Object>(); valuesMap.put(CONTAINER_ID, containerId); valuesMap.put(PROCESS_INST_ID, processInstanceId); valuesMap.put(SIGNAL_NAME, signalName); Map<String, String> headers = new HashMap<String, String>(); makeHttpPostRequestAndCreateCustomResponse( build(loadBalancer.getUrl(), PROCESS_URI + "/" + SIGNAL_PROCESS_INST_POST_URI, valuesMap), event, String.class, headers); } else { CommandScript script = new CommandScript( Collections.singletonList( (KieServerCommand) new DescriptorCommand( "ProcessService", "signalProcessInstance", serialize(event), marshaller.getFormat().getType(), new Object[]{containerId, processInstanceId, signalName}))); ServiceResponse<?> response = (ServiceResponse<?>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM", containerId ).getResponses().get(0); throwExceptionOnFailure(response); } }
@Test public void testMarshallingFormatsWithExtraneousParameters() { assertEquals(MarshallingFormat.JSON, MarshallingFormat.fromType("application/json;")); assertEquals(MarshallingFormat.JAXB, MarshallingFormat.fromType("application/xml;")); assertEquals(MarshallingFormat.XSTREAM, MarshallingFormat.fromType("application/xstream;")); assertEquals(MarshallingFormat.JSON, MarshallingFormat.fromType("application/json;encode=")); assertEquals(MarshallingFormat.JAXB, MarshallingFormat.fromType("application/xml;encode=utf-8")); assertEquals(MarshallingFormat.XSTREAM, MarshallingFormat.fromType("application/xstream;utf-8")); }
protected Comparable<?> subtractOneMinuteFromDate(Date date) { Instant instant = Instant.from(date.toInstant()); instant = instant.minus(Duration.ofMinutes(1)); Date calculated = Date.from(instant); if (marshallingFormat.equals(MarshallingFormat.JAXB)) { return new JaxbDate(calculated); } return calculated; } }
@Override public void signal(String containerId, String signalName, Object event) { if( config.isRest() ) { Map<String, Object> valuesMap = new HashMap<String, Object>(); valuesMap.put(CONTAINER_ID, containerId); valuesMap.put(SIGNAL_NAME, signalName); Map<String, String> headers = new HashMap<String, String>(); makeHttpPostRequestAndCreateCustomResponse( build(loadBalancer.getUrl(), PROCESS_URI + "/" + SIGNAL_PROCESS_INSTANCES_PORT_URI, valuesMap), event, String.class, headers); } else { CommandScript script = new CommandScript( Collections.singletonList( (KieServerCommand) new DescriptorCommand( "ProcessService", "signal", serialize(event), marshaller.getFormat().getType(), new Object[]{containerId, signalName}))); ServiceResponse<?> response = (ServiceResponse<?>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM", containerId ).getResponses().get(0); throwExceptionOnFailure(response); } }
handler = queryDataServiceBase; if (marshallingFormat.equals(MarshallingFormat.JAXB)) { wrapResults = true;
@Override public void addExcludedOwners(String containerId, Long taskId, boolean removeExisting, OrgEntities orgEntities) { if( config.isRest() ) { Map<String, Object> valuesMap = new HashMap<String, Object>(); valuesMap.put(CONTAINER_ID, containerId); valuesMap.put(TASK_INSTANCE_ID, taskId); Map<String, String> headers = new HashMap<String, String>(); String queryString = "?remove=" + removeExisting; makeHttpPutRequestAndCreateCustomResponse( build(loadBalancer.getUrl(), ADMIN_TASK_URI + "/" + TASK_INSTANCE_EXL_OWNERS_USERS_URI, valuesMap) + queryString, orgEntities, null, headers); } else { CommandScript script = new CommandScript( Collections.singletonList( (KieServerCommand) new DescriptorCommand("UserTaskAdminService", "addExcludedOwners", serialize(orgEntities), marshaller.getFormat().getType(), new Object[]{containerId, taskId, removeExisting}))); ServiceResponse<?> response = (ServiceResponse<?>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM", containerId ).getResponses().get(0); throwExceptionOnFailure(response); } }