/**
 * Copyright (c) 2011-2014 SmeshLink Technology Corporation.
 * All rights reserved.
 * 
 * This file is part of the Misty, a sensor cloud for WSN.
 */
package com.smeshlink.misty.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;

import com.smeshlink.misty.formatter.JSONFormatter;

/**
 * Represents a JSON request to a service.
 * 
 * @author smeshlink
 *
 */
public class JsonResponse implements IServiceResponse {
	private JSONObject json;
	private JSONObject headers;
	private int status;
	private Object body;
	private String resource;
	
	public JsonResponse(JSONObject json) {
		this.json = json;
		status = json.optInt("status", 200);
		resource = json.optString("resource", null);
		headers = json.optJSONObject("headers");
	}

	public String getHeader(String name) {
		return headers == null ? null : headers.optString(name, null);
	}
	
	public Map getHeaders() {
		Map map = new HashMap();
		if (headers != null) {
			Iterator it = headers.keys();
			while (it.hasNext()) {
				String name = (String) it.next();
				map.put(name, headers.optString(name, null));
			}
		}
		return map;
	}

	public String getResource() {
		return resource;
	}

	public void setBody(Object body) {
		this.body = body;
	}

	public Object getBody() {
		if (body == null) {
			Object obj = json.opt("body");
			if (obj != null)
				body = (new JSONFormatter()).parseObject(obj);
		}
		return body;
	}

	public int getStatus() {
		return status;
	}

	public void setHeader(String name, String value) {
		 if (headers == null) {
			 headers = new JSONObject();
			 json.put("headers", headers);
         }
		 headers.put(name, value);
	}
	
	public String getToken() {
		return json.optString("token", null);
	}

	public void setToken(String token) {
		json.put("token", token);
	}

	public InputStream getResponseStream() throws IOException {
		return null;
	}

	public void dispose() {
		// do nothing
	}
}

@@ -352,61 +352,8 @@
 t;%0A%0A
-import com.smeshlink.misty.formatter.JSONFormatter;%0A%0A
 /**%0A
@@ -798,16 +798,43 @@
 ders%22);%0A
+%09%09body = json.opt(%22body%22);%0A
 %09%7D%0A%0A%09pub
@@ -1298,32 +1298,32 @@
 ody = body;%0A%09%7D%0A%0A
+
 %09public Object g
@@ -1337,139 +1337,8 @@
 ) %7B%0A
-%09%09if (body == null) %7B%0A%09%09%09Object obj = json.opt(%22body%22);%0A%09%09%09if (obj != null)%0A%09%09%09%09body = (new JSONFormatter()).parseObject(obj);%0A%09%09%7D%0A
 %09%09re

return original body in JsonResponse