All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ocelotds.messaging.MessageFromClient Maven / Gradle / Ivy

There is a newer version: 2.12.0
Show newest version
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
package org.ocelotds.messaging;

import org.ocelotds.Constants;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonString;
import javax.json.JsonValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Message from client for call any service. the topic of command identify the resolver for get instance of service
 *
 * @author hhfrancois
 */
public class MessageFromClient {

	private static final Logger logger = LoggerFactory.getLogger(MessageFromClient.class);
	protected String id;
	protected String dataService;
	protected String operation;
	/**
	 * parameters json format
	 */
	protected List parameters = new ArrayList<>();
	/**
	 * parameter names
	 */
	protected List parameterNames = new ArrayList<>();

	public String getId() {
		return id;
	}

	public void setId(String messageId) {
		this.id = messageId;
	}

	public String getDataService() {
		return dataService;
	}

	public void setDataService(String dataService) {
		this.dataService = dataService;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public List getParameters() {
		return parameters;
	}

	public void setParameters(List parameters) {
		this.parameters = parameters;
	}

	public List getParameterNames() {
		return parameterNames;
	}

	public void setParameterNames(List parameterNames) {
		this.parameterNames = parameterNames;
	}

	@Override
	public int hashCode() {
		int hash = 5;
		hash = 79 * hash + Objects.hashCode(this.id);
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final MessageFromClient other = (MessageFromClient) obj;
		return Objects.equals(this.id, other.id);
	}

	public static MessageFromClient createFromJson(String json) {
		MessageFromClient message = new MessageFromClient();
		try (JsonReader reader = Json.createReader(new StringReader(json))) {
			JsonObject root = reader.readObject();
			message.setId(root.getString(Constants.Message.ID));
			message.setDataService(root.getString(Constants.Message.DATASERVICE));
			message.setOperation(root.getString(Constants.Message.OPERATION));
			JsonArray argArray = root.getJsonArray(Constants.Message.ARGUMENTS);
			List params = new ArrayList<>();
			message.setParameters(params);
			int idx = 0;
			logger.debug("Get arguments and argumentNames from message '{}'", json);
			if (!argArray.isEmpty()) {
				while (idx < argArray.size()) {
					JsonValue arg = argArray.get(idx++);
					logger.debug("Get argument Type : '{}'. Value : '{}'", arg.getValueType().name(), arg.toString());
					params.add(arg.toString());
				}
			}
			argArray = root.getJsonArray(Constants.Message.ARGUMENTNAMES);
			params = new ArrayList<>();
			message.setParameterNames(params);
			idx = 0;
			if (!argArray.isEmpty()) {
				while (idx < argArray.size()) {
					JsonString arg = argArray.getJsonString(idx++);
					logger.debug("Get argumentName : '{}'.", arg.toString());
					params.add(arg.getString());
				}
			}
		}
		return message;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy