/*
 *  Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.twosigma.beaker;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Form;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.map.ObjectMapper;

public class NamespaceClient {

    private Base64 encoder;
    private String session;
    private ObjectMapper mapper;
    private String auth;
    private String urlBase;

    public NamespaceClient(String session) {
	this.encoder = new Base64();
	this.mapper = new ObjectMapper();
	this.session = session;
	String account = "beaker:" + System.getenv("beaker_core_password");
	this.auth = "Basic " + encoder.encodeBase64String(account.getBytes());
	this.urlBase = "http://127.0.0.1:" + System.getenv("beaker_core_port") +
	    "/rest/namespace";
    }

    public void set(String name, Object value)
	throws ClientProtocolException, IOException
    {
	
	
	List<NameValuePair> form = Form.form()
	    .add("name", name)
	    .add("value", mapper.writeValueAsString(value))
	    .add("sync", "false")
	    .add("session", this.session).build();
	Request.Post(urlBase + "/set")
	    .addHeader("Authorization", auth).bodyForm(form).execute();
    }
    public Object get(String name)
	throws ClientProtocolException, IOException
    {
	String args = "name=" + URLEncoder.encode(name, "ISO-8859-1") +
	    "&session=" + URLEncoder.encode(this.session, "ISO-8859-1");
	String valueString = Request.Get(urlBase + "/get?" + args)
	    .addHeader("Authorization", auth)
	    .execute().returnContent().asString();;
	return mapper.readValue(valueString, Object.class);
    }
}

@@ -951,24 +951,77 @@
 lException;%0A
+import org.codehaus.jackson.annotate.JsonAutoDetect;%0A
 import org.c
@@ -1055,16 +1055,17 @@
 apper;%0A%0A
+%0A
 public c
@@ -2371,64 +2371,1171 @@
 ;;%0A%09
-return mapper.readValue(valueString, Object.class)
+Binding binding = mapper.readValue(valueString, Binding.class);%0A%09if (!binding.defined) %7B%0A%09    throw new RuntimeException(%22name not defined: %22 + name);%0A%09%7D%0A%09return binding.value;%0A    %7D%0A%0A    // XXX duplicated code with NamespaceService, move to shared module.%0A  @JsonAutoDetect%0A  public static class Binding %7B%0A%0A    private String name;%0A    private String session;%0A    private Object value;%0A    private Boolean defined;%0A%0A    public String getName() %7B%0A      return this.name;%0A    %7D%0A    public String getSession() %7B%0A      return this.session;%0A    %7D%0A    public Object getValue() %7B%0A      return this.value;%0A    %7D%0A    public Boolean getDefined() %7B%0A      return this.defined;%0A    %7D%0A    public void setName(String s) %7B%0A      this.name = s;%0A    %7D%0A    public void setSession(String s) %7B%0A      this.session = s;%0A    %7D%0A    public void setValue(Object o) %7B%0A      this.value = o;%0A    %7D%0A    public void setDefined(Boolean b) %7B%0A      this.defined = b;%0A    %7D%0A    public Binding() %7B%0A    %7D%0A    public Binding(String session, String name, Object value, Boolean defined) %7B%0A      this.session = session;%0A      this.name = name;%0A      this.value = value;%0A      this.defined = defined
 ;%0A    %7D%0A
 %7D%0A
@@ -3532,10 +3532,15 @@
 ;%0A    %7D%0A
+  %7D%0A%0A
 %7D%0A

copy Binding class from NamespaceService. should move this to shared module.