Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

Bucket created using Rest API cannot be access with Java Spy Memcached client

4 replies [Last post]
  • Login or register to post comments
Tue, 10/18/2011 - 04:29
robtindall
Offline
Joined: 08/01/2011
Groups: None

I wrote some code the delete and then re-create a bucket using the Rest API. The bucket it created was a membase type. It looked exactly the same as the bucket created through Membase console but I could not access the bucket using the spy memcache client or the Rest API.

The new bucket is shown in the console but can't be used elsewhere, am I missing a trick here ?

I have to provide a proxy port when I create the new bucket in the rest api, what is this for and what value should I set ?
Thanks !

Top
  • Login or register to post comments
Tue, 10/18/2011 - 13:44
mikew
Offline
Joined: 03/14/2011
Groups:

First off, Spymemcached should be able to connect to any bucket. There is not enough information here for me to know what the problem is though. Please answer the questions below to an I will help you solve this issue.

1. Does your bucket require a password?

2. What is the name of this bucket?

3. Can you copy the line of code that creates the connection in Spymemcached? (MembaseClient client = new ...)

Top
  • Login or register to post comments
Thu, 10/20/2011 - 03:14
robtindall
Offline
Joined: 08/01/2011
Groups: None

Here is an example of what I am doing:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
 
import net.spy.memcached.CouchbaseClient;
import sun.misc.BASE64Encoder;
 
public class ExampleForMikeOfRestApiIssues {
 
	String host = "localhost";
	String port = "8091";
	String bucketName = "default";
	String ramQuotaMB = "800";
 
	String user = "Administrator";
	String pass = "password";
 
	public static void main(String[] args) throws Exception {
 
		ExampleForMikeOfRestApiIssues example = new ExampleForMikeOfRestApiIssues();
 
		example.deleteBucketRestfully();
 
		example.createBucketRestfully();
 
		example.statusBucketsRestfully();
 
		example.test();
	}
 
	public void createBucketRestfully() throws Exception {
 
		System.out.println("Create bucket with Membase Rest API");
 
		URL url = new URL("http://" + host + ":" + port
				+ "/pools/default/buckets");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setAllowUserInteraction(false);
		conn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		addBasicHttpAuth(conn);
 
		String[] paramName = { "name", "ramQuotaMB", "authType",
				"replicaNumber", "proxyPort" };
		String[] paramVal = { bucketName, ramQuotaMB, "none", "0", "11215" };
 
		// Create the form content
		OutputStream out = conn.getOutputStream();
		Writer writer = new OutputStreamWriter(out, "UTF-8");
		for (int i = 0; i < paramName.length; i++) {
			writer.write(paramName[i]);
			writer.write("=");
			writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
			writer.write("&");
		}
		writer.close();
		out.close();
 
		System.out.println(conn.getResponseCode() + " : "
				+ conn.getResponseMessage());
		conn.disconnect();
 
		System.out.println("Create succeded");
	}
 
	public void deleteBucketRestfully() throws Exception {
 
		System.out.println("Deleting bucket '" + bucketName
				+ "' with Membase Rest API");
 
		URL url = new URL("http://" + host + ":" + port
				+ "/pools/default/buckets/" + bucketName);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("DELETE");
		addBasicHttpAuth(conn);
		System.out.println(conn.getResponseCode() + " : "
				+ conn.getResponseMessage());
		conn.disconnect();
 
		System.out.println("Deleting succeded");
	}
 
	public void statusBucketsRestfully() throws Exception {
 
		System.out.println("Deleting bucket '" + bucketName
				+ "' with Membase Rest API");
 
		URL url = new URL("http://" + host + ":" + port
				+ "/pools/default/buckets");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		addBasicHttpAuth(conn);
		System.out.println(conn.getResponseCode() + " : "
				+ conn.getResponseMessage());
		outputResult(conn);
		conn.disconnect();
 
		System.out.println("Status succeded");
	}
 
	private final void addBasicHttpAuth(HttpURLConnection conn) {
		BASE64Encoder enc = new sun.misc.BASE64Encoder();
		String userpassword = user + ":" + pass;
		String encodedAuthorization = enc.encode(userpassword.getBytes());
		conn.setRequestProperty("Authorization", "Basic "
				+ encodedAuthorization);
	}
 
	private final void outputResult(HttpURLConnection conn) throws IOException {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));
			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}
 
		} finally {
			if (br != null) {
				br.close();
			}
		}
	}
 
	private final void test() throws Exception {
 
		CouchbaseClient couchbaseClient = null;
		try {
			couchbaseClient = new CouchbaseClient(Arrays.asList(URI
					.create("http://" + host + ":" + port + "/pools")), bucketName, "");
 
			couchbaseClient.set("test", 0, "Hello World");
			System.out.println(couchbaseClient.get("test"));
 
		} finally {
			if (couchbaseClient != null) {
				couchbaseClient.shutdown(10L, TimeUnit.SECONDS);
			}
		}
	}
}

Top
  • Login or register to post comments
Thu, 10/20/2011 - 13:28
mikew
Offline
Joined: 03/14/2011
Groups:

Thanks for the code sample.

I took a look at the issue. The bad news is that there is a bug in our server, but the good news is that we have an easy work around. First off, let me explain what the server issue. You are creating a bucket using no authentication and it turns out that when you do this with the default bucket it makes the url http://localhost:8091/pools require authentication. As a result, when you try to connect to Membase with the java client you get a 401 Not Authorized error since the client isn't expecting to have to use authentication in order to get a valid response from that URL. We have filed a bug on this issue and will try to get it fixed as soon as possible. You can track the issue here: http://www.couchbase.org/issues/browse/MB-4371

The work around just requires you to change the "authType" to "sasl" and also add a parameter "saslPassword" and set it to "". I also added a sleep here:

example.statusBucketsRestfully();
Thread.sleep(1000); 
example.test();

in order to make sure that the bucket has some time to get set up on the server before you connect to it. Below is code I change to make sure your parameters are set properly.

String[] paramName = { "name", "ramQuotaMB", "authType", "saslPassword",
		"replicaNumber", "proxyPort" };
String[] paramVal = { bucketName, ramQuotaMB, "sasl", "", "0", "11215" };

Let me know if you run into any other issues.

Top
  • Login or register to post comments
Fri, 10/21/2011 - 01:03
robtindall
Offline
Joined: 08/01/2011
Groups: None

This workaround has got it working, thanks !

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker