This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Database.java
148 lines (124 loc) · 5.27 KB
/
Database.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.github.rnewson.couchdb.lucene;
/**
* Copyright 2009 Robert Newson
*
* 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.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
/**
* Communication with couchdb.
*
* @author rnewson
*
*/
public final class Database {
static final HttpClient CLIENT = new HttpClient();
private static final String[] EMPTY_ARR = new String[0];
static {
CLIENT.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
if (Config.DB_USER != null && Config.DB_PASSWORD != null) {
CLIENT.getParams().setAuthenticationPreemptive(true);
final Credentials creds = new UsernamePasswordCredentials(Config.DB_USER, Config.DB_PASSWORD);
CLIENT.getState().setCredentials(AuthScope.ANY, creds);
Utils.LOG.debug("Authenticating to couchdb as " + Config.DB_USER);
}
}
private final String url;
public Database(final String url) {
if (url.endsWith("/"))
this.url = url.substring(0, url.length() - 1);
else
this.url = url;
}
public String[] getAllDatabases() throws HttpException, IOException {
return (String[]) JSONArray.fromObject(get("_all_dbs")).toArray(EMPTY_ARR);
}
public JSONObject getAllDocsBySeq(final String dbname, final long startkey) throws HttpException, IOException {
return JSONObject.fromObject(get(String.format("%s/_all_docs_by_seq?startkey=%s&include_docs=true",
encode(dbname), startkey)));
}
public JSONObject getAllDocsBySeq(final String dbname, final long startkey, final int limit) throws HttpException,
IOException {
return JSONObject.fromObject(get(String.format("%s/_all_docs_by_seq?startkey=%d&limit=%d&include_docs=true",
encode(dbname), startkey, limit)));
}
public JSONObject getDoc(final String dbname, final String id) throws HttpException, IOException {
return JSONObject.fromObject(get(String.format("%s/%s", encode(dbname), id)));
}
public JSONObject getDocs(final String dbname, final String... ids) throws HttpException, IOException {
final JSONArray keys = new JSONArray();
for (final String id : ids) {
keys.add(id);
}
final JSONObject req = new JSONObject();
req.element("keys", keys);
return JSONObject.fromObject(post(String.format("%s/_all_docs?include_docs=true", encode(dbname)), req
.toString()));
}
public JSONObject getInfo(final String dbname) throws HttpException, IOException {
return JSONObject.fromObject(get(encode(dbname)));
}
private String get(final String path) throws HttpException, IOException {
return execute(new GetMethod(url(path)));
}
String url(final String path) {
return String.format("%s/%s", url, path);
}
String encode(final String path) {
try {
return URLEncoder.encode(path, "UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new Error("UTF-8 support missing!");
}
}
private String post(final String path, final String body) throws HttpException, IOException {
final PostMethod post = new PostMethod(url(path));
post.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
return execute(post);
}
private synchronized String execute(final HttpMethodBase method) throws HttpException, IOException {
try {
final int sc = CLIENT.executeMethod(method);
if (sc == 401) {
throw new HttpException("Unauthorized.");
}
final InputStream in = method.getResponseBodyAsStream();
try {
final StringWriter writer = new StringWriter(2048);
IOUtils.copy(in, writer, method.getResponseCharSet());
return writer.toString();
} finally {
in.close();
}
} finally {
method.releaseConnection();
}
}
}