Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
collapsetrue
package com.sos.jms;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Properties;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sos.commons.httpclient.SOSRestApiClient;
import com.sos.jms.consumer.SOSConsumer;
import com.sos.jms.producer.SOSProducer;

public class JmsExecute {

    private static final String DEFAULT_JMS_URI = "tcp://[MESSAGE_SERVER_HOST]activemq-5-15:61616";
    private static final String DEFAULT_JOC_API_URL = "http://[JOC_COCKPIT_HOST]:[JOC_COCKPIT_PORT]centostest_primary.sos:7446/joc/api/";
    private static final String API_ADD_ORDER = "orders/add";
    private static final String API_LOGIN = "authentication/login";
    private static final String API_LOGOUT = "authentication/logout";
    private static final String DEFAULT_JOC_API_REQUEST_BODY = "{\"controllerId\":\"testsuite\",\"orders\"" 
            + ":[{\"workflowPath\":\"/JS7Demo/01_HelloWorld/jdHelloWorld\",\"scheduledFor\":\"now\"}],\"auditLog\":{}}";
    private static final String ACCESSDEFAULT_TOKEN_HEADERUSERNAME = "X-Access-Tokenroot";
    private static final String APPLICATIONDEFAULT_JSONPWD = "application/jsonroot";
    private static final String CONTENTAPI_ADD_TYPEORDER = "Content-Typeorders/add";
    private static final LoggerString LOGGERAPI_LOGIN = LoggerFactory.getLogger(JmsExecute.class);
"authentication/login";
    publicprivate static voidfinal main(String[] args) {String API_LOGOUT = "authentication/logout";
    private static final String  if("produce".equals(args[0])) {
ACCESS_TOKEN_HEADER = "X-Access-Token";
    private static final String APPLICATION_JSON = "application/json";
    private static final String CONTENT_TYPE = "Content-Type";
    private static final Logger LOGGER = LoggerFactory.getLogger(JmsExecute.class);
    private static String jmsServerUri = null;
    private static String jocApiUri = null;
    private static String controllerId = null;
    private static String workflowPath = null;
    private static String requestBody = null;
    private static String username = null;
    private static String pwd = null;


    public static void main(String[] args) {
        if(args[1] != null) {
            readPropertiesFile(Paths.get(args[1]));
        } else {
            jmsServerUri = DEFAULT_JMS_URI;
            jocApiUri = DEFAULT_JOC_API_URL;
            requestBody = DEFAULT_JOC_API_REQUEST_BODY;
            username = DEFAULT_USERNAME;
            pwd = DEFAULT_PWD;
        }
        if("produce".equals(args[0])) {
            SOSProducer producer = new SOSProducer(jmsServerUri);
            producer.write(requestBody);
        } else if ("consume".equals(args[0])) {
            SOSConsumer consumer = new SOSConsumer(jmsServerUri);
            String consumedMessage = consumer.receiveFromQueue();
            SOSRestApiClient client = setupHttpClient(username, pwd);
            try {
                URI jocUri = URI.create(jocApiUri);
                LOGGER.info("send login to: " + jocUri.resolve(API_LOGIN).toString());
                String response = client.postRestService(jocUri.resolve(API_LOGIN), null);
                LOGGER.info("HTTP status code: " + client.statusCode());
                if (client.statusCode() == 200) {
                    JsonReader jsonReader = null;
                    String accessToken = null;
                    try {
                        jsonReader = Json.createReader(new StringReader(response));
                        JsonObject json = jsonReader.readObject();
                        accessToken = json.getString("accessToken", "");
                    } catch(Exception e) {
                        LOGGER.warn("Could not determine accessToken.");
                  SOSProducer producer =} new SOSProducer(JMS_URI);
finally {
             producer.write(JOC_API_REQUEST_BODY);
        } else if ("consume".equals(args[0])) {
jsonReader.close();
                SOSConsumer consumer = new SOSConsumer(JMS_URI);
 }
                 String consumedMessage = consumerclient.receiveFromQueue(addHeader(ACCESS_TOKEN_HEADER, accessToken);
                SOSRestApiClient  client = setupHttpClient(client.addHeader(CONTENT_TYPE, APPLICATION_JSON);
                    try {
LOGGER.info("REQUEST: " + API_ADD_ORDER);
                 URI jocUri = URILOGGER.create(JOC_API_URLinfo("PARAMS: " + consumedMessage);
                  LOGGER.info("send login to:String "apiUrl + jocUri.resolve(API_LOGIN).toString());
= null;
                  String response = client.postRestService(jocUri.resolve(API_LOGIN), null);
if (!API_ADD_ORDER.toLowerCase().startsWith(DEFAULT_JOC_API_URL)) {
                      LOGGER.info("HTTP status code: " + client.statusCode())  apiUrl = DEFAULT_JOC_API_URL + API_ADD_ORDER;
                if (client.statusCode() == 200) {    }
                    JsonReader jsonReader = nullLOGGER.info("resolvedUri: " + jocUri.resolve(apiUrl).toString());
                    Stringresponse accessToken = null= client.postRestService(jocUri.resolve(apiUrl), consumedMessage);
                    try {
    LOGGER.info("HTTP status code: " + client.statusCode());
                    jsonReaderresponse = Jsonclient.createReader(new StringReader(response)postRestService(jocUri.resolve(API_LOGOUT), null);
                      LOGGER.info("HTTP status JsonObjectcode: json" =+ jsonReaderclient.readObjectstatusCode());
                        accessToken = json.getString("accessToken", "");
 }
                   } catch (Exception e) {
                        LOGGER.error(e.warngetMessage("Could not determine accessToken."), e);
                    } finally {
                        jsonReaderclient.closecloseHttpClient();
            }
        }
    }
    
    private static SOSRestApiClient setupHttpClient(String username, String   client.addHeader(ACCESS_TOKEN_HEADER, accessToken);password) {
        SOSRestApiClient client =          client.addHeader(CONTENT_TYPE, APPLICATION_JSONnew SOSRestApiClient();
        String basicAuth = Base64.getMimeEncoder().encodeToString((username + ":" + password).getBytes());
     LOGGER.info("REQUEST: " + API_ADD_ORDER     client.setBasicAuthorization(basicAuth);
        return client;
    }
    
     LOGGER.info("PARAMS: " + consumedMessage);private static String cleanupValue(String value) {
        if(value.startsWith("\"")) {
           String apiUrlvalue = nullvalue.substring(1);
        }
            if (!API_ADD_ORDER.toLowerCase().startsWith(JOC_API_URL(value.endsWith("\"")) {
            value = value.substring(0, value.length() -1);
        }
     apiUrl = JOC_API_URL + API_ADD_ORDERreturn value;
    }
    
    private static void readPropertiesFile (Path path) {
    }
    Properties props = new Properties();
        try {
   LOGGER.info("resolvedUri: " + jocUri.resolve(apiUrl).toString(         props.load(Files.newInputStream(path));
                    responsejmsServerUri = client.postRestServicecleanupValue(jocUriprops.resolve(apiUrl), consumedMessagegetProperty("JMS_URI"));
            jocApiUri = cleanupValue(props.getProperty("JOC_API_URL"));
      LOGGER.info("HTTP status code: " + client.statusCode(      controllerId = cleanupValue(props.getProperty("controllerId"));
                    responseworkflowPath = client.postRestServicecleanupValue(jocUriprops.resolve(API_LOGOUT), nullgetProperty("workflowPath"));
            username = cleanupValue(props.getProperty("username"));
         LOGGER.info("HTTP status code: "pwd += clientcleanupValue(props.statusCodegetProperty("password"));
            requestBody = "{\"controllerId\":\"" + }
controllerId +    "\",\"orders\":[{\"workflowPath\":\"" +
       } catch (Exception e) {
              workflowPath  LOGGER.error(e.getMessage(), e)+ "\",\"scheduledFor\":\"now\"}],\"auditLog\":{}}";
        } catch (IOException e) }{
 finally {
          LOGGER.warn("could not read properties file, use client.closeHttpClient(defaults instead.");
            }

jmsServerUri        }= DEFAULT_JMS_URI;
    }
     
   jocApiUri private static SOSRestApiClient setupHttpClient() {
= DEFAULT_JOC_API_URL;
           SOSRestApiClient clientrequestBody = new SOSRestApiClient()DEFAULT_JOC_API_REQUEST_BODY;
          String  basicAuthusername = Base64.getMimeEncoder().encodeToString(("[USERNAME]:[PASSWORD]").getBytes())DEFAULT_USERNAME;
        client.setBasicAuthorization(basicAuth);
    pwd = DEFAULT_PWD;
   return client;
    }
    }
}

  


Maven Configuration Example

...