Skip to content

Instantly share code, notes, and snippets.

@agiertli
Created December 23, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agiertli/2a9ab2b93fabe6e3b17e to your computer and use it in GitHub Desktop.
Save agiertli/2a9ab2b93fabe6e3b17e to your computer and use it in GitHub Desktop.
package org.redhat.gss;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.drools.core.command.impl.GenericCommand;
import org.drools.core.command.runtime.BatchExecutionCommandImpl;
import org.kie.api.command.BatchExecutionCommand;
import org.kie.internal.command.CommandFactory;
import org.kie.internal.runtime.helper.BatchExecutionHelper;
import org.kie.server.api.marshalling.ModelWrapper;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.client.KieServicesClient;
import org.kie.server.client.KieServicesConfiguration;
import org.kie.server.client.KieServicesFactory;
import org.kie.server.client.RuleServicesClient;
//import org.kie.server.api.model.ServiceResponse;
//import org.kie.server.client.KieServicesClient;
//import org.kie.server.client.KieServicesConfiguration;
//import org.kie.server.client.KieServicesFactory;
import org.redhat.gss.inoutproject.ErrorInput;
import org.redhat.gss.inoutproject.ErrorOutput;
public class RESTClient {
public static final String USERNAME = "anton";
public static final String PASSWORD = "password1!";
public static final String SERVER_URL = "http://localhost:8080/kie-server/services/rest/server";
public static final String CONTAINER = "InOutContainer";
public static final String KIESESSION = "myStateless";
public static void main(String args[]) throws JAXBException {
// configure client
KieServicesClient client = configure(SERVER_URL, USERNAME, PASSWORD);
// generate commands
List<GenericCommand<?>> commands = new ArrayList<GenericCommand<?>>();
ErrorInput x = new ErrorInput();
x.setErrorCode(0);
commands.add((GenericCommand<?>) CommandFactory.newInsert(x, "insert-identifier"));
commands.add((GenericCommand<?>) CommandFactory.newFireAllRules("fire-identifier"));
BatchExecutionCommand command = new BatchExecutionCommandImpl(commands, "MySession");
// marshal object to xml
// JAXB
// Set<Class<?>> allClasses = new HashSet<Class<?>>();
// allClasses.add(ErrorInput.class);
// allClasses.add(ErrorOutput.class);
// allClasses.add(BatchExecutionCommandImpl.class);
// allClasses.add(org.drools.core.command.runtime.rule.FireAllRulesCommand.class);
// allClasses.add(org.drools.core.command.runtime.rule.InsertObjectCommand.class);
// allClasses.add(org.drools.core.common.DefaultFactHandle.class);
// allClasses.add(org.drools.core.command.runtime.rule.GetObjectCommand.class);
//
// javax.xml.bind.JAXBContext context = JAXBContext.newInstance(allClasses.toArray(new Class[allClasses.size()]));
// javax.xml.bind.Marshaller marshaller = context.createMarshaller();
// marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
//
// StringWriter writer = new StringWriter();
//
// marshaller.marshal(ModelWrapper.wrap(command), writer);
//
// String jaxbxml = writer.toString();
// System.out.println(jaxbxml);
String xStreamXml =
BatchExecutionHelper.newXStreamMarshaller().toXML(command);
System.out.println("===xstream xml request====");
System.out.println(xStreamXml);
// send the xml command via REST API to the Decision Server
System.out.println("===server response====");
//jaxb
//ServiceResponse<String> response = client.executeCommands(CONTAINER, jaxbxml);
ServiceResponse<String> response = client.executeCommands(CONTAINER, xStreamXml);
System.out.println(response.getResult());
}
public static KieServicesClient configure(String url, String username, String password) {
//
KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(url, username, password);
return KieServicesFactory.newKieServicesClient(config);
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment