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

com.app55.message.Response Maven / Gradle / Ivy

The newest version!
package com.app55.message;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

import com.app55.Gateway;
import com.app55.util.ReflectionUtil;
import com.googlecode.openbeans.PropertyDescriptor;

public class Response extends Message
{
	private String	signature;
	private String	timestamp;

	protected Response()
	{
	}

	protected Response(Gateway gateway, Map ht)
	{
		setGateway(gateway);
		this.populate(ht);
	}

	@Override
	@JsonProperty(value = "sig")
	public String getSignature()
	{
		return signature;
	}

	public void setSignature(String signature)
	{
		this.signature = signature;
	}

	@Override
	@JsonProperty(value = "ts")
	public String getTimestamp()
	{
		return timestamp;
	}

	public void setTimestamp(String timestamp)
	{
		this.timestamp = timestamp;
	}

	@JsonIgnore
	public boolean isValidSignature()
	{
		String expectedSig = getExpectedSignature();
		return expectedSig.equals(signature);
	}

	@JsonIgnore
	public String getExpectedSignature()
	{
		return toSignature();
	}

	@SuppressWarnings("unchecked")
	private void populateAdditional(Map hashtable, String prefix)
	{
		SortedMap ht = new TreeMap(hashtable);
		for (Entry entry : ht.entrySet())
		{
			String key = entry.getKey();
			Object value = entry.getValue();

			if (value != null)
			{
				if (value instanceof Map)
				{
					populateAdditional((Map) value, prefix + key + ".");
				}
				else if (value instanceof List)
				{
					int i = 0;
					for (Object arrayItem : ((List) value))
					{
						if (arrayItem instanceof Map)
						{
							populateAdditional((Map) arrayItem, prefix + key + "." + i++ + ".");
						}
						else
						{
							additionalFields.put(prefix + key + "." + i++, arrayItem.toString());
						}
					}
				}
				else
					additionalFields.put(prefix + key, value.toString());
			}
		}
	}

	public void populate(Map hashtable)
	{
		populate(hashtable, null);
	}

	private void populate(Map hashtable, Object o)
	{
		populate(hashtable, o, "");
	}

	@SuppressWarnings("unchecked")
	private void populate(Map hashtable, Object o, String prefix)
	{
		if (o == null)
			o = this;

		Map properties = ReflectionUtil.getAllProperties(o);
		for (Entry entry : hashtable.entrySet())
		{
			String key = entry.getKey();
			Object value = entry.getValue();

			if (value == null || !properties.containsKey(key))
			{
				if (value != null)
				{
					if (value instanceof Map)
					{
						populateAdditional((Map) value, prefix + key + ".");
					}
					else if (value instanceof List)
					{
						int i = 0;
						for (Object arrayItem : ((List) value))
						{
							if (arrayItem instanceof Map)
							{
								populateAdditional((Map) arrayItem, prefix + key + "." + i++ + ".");
							}
							else
							{
								additionalFields.put(prefix + key + "." + i++, arrayItem.toString());
							}
						}
					}
					else
						additionalFields.put(prefix + key, value.toString());
				}
				continue;
			}
		}
	}
}