Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created August 7, 2012 21:38
Show Gist options
  • Save ingenthr/3289598 to your computer and use it in GitHub Desktop.
Save ingenthr/3289598 to your computer and use it in GitHub Desktop.
Example of adding a view to Couchbase Server 2.0 build 1495 and later
package com.couchbase.sample.designdlocloader;
import java.io.IOException;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
public class App {
public static void main(String[] args) throws IOException {
String view = "{\"language\":\"javascript\",\"views\":{\""
+ "dev_testview\":{\"map\":\"function (doc) { "
+ "emit(doc._id, 1)}\",\"reduce\":\"_sum\" }}}";
System.out.println("Result of load is " + loadDesignDoc(view, "endpoint", "dev_testview")); // do not use leading dev_ if you want a full cluster dataset view
}
public static boolean loadDesignDoc(String doc, String bucketname, String viewname) throws IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("localhost", 8092, "http");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("endpoint", "password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
HttpPut httpput = new HttpPut("http://localhost:8092/" + bucketname + "/_design/" + viewname);
httpput.setHeader("Content-type", "application/json");
StringEntity reqEntity = new StringEntity(doc);
httpput.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpput, localcontext);
System.err.println(response.getStatusLine());
StringWriter writer = new StringWriter();
IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8");
String theString = writer.toString();
System.err.println(theString);
if (response.getStatusLine().getStatusCode() < 300) {
return true;
} else {
return false;
}
}
}
@ingenthr
Copy link
Author

ingenthr commented Aug 7, 2012

Note that authentication needs to be asserted (or as HttpClient says, added preemptively). Therefore, the method of adding auth is a bit different.

Also note that the old port 8092 still exists, but won't accept any of these credentials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment