Skip to content
Browse files

added methods for putting and getting attachments

  • Loading branch information...
1 parent 1039eeb commit 254e545d0c5bb94b3bc860bbd7db0a75ffe0da15 @theaspect theaspect committed with Feb 22, 2010
View
62 src/java/com/fourspaces/couchdb/AdHocView.java
@@ -1,31 +1,31 @@
-/*
- Copyright 2007 Fourspaces Consulting, 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.fourspaces.couchdb;
-
-/**
- * Creates an AdHoc view... this basically just creates a new View with a name
- * of "_temp_view" and a defined function.
- *
- * @author mbreese
- *
- */
-public class AdHocView extends View {
- public AdHocView(String function) {
- super("_temp_view");
- this.function=function;
- }
-}
+/*
+ Copyright 2007 Fourspaces Consulting, 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.fourspaces.couchdb;
+
+/**
+ * Creates an AdHoc view... this basically just creates a new View with a name
+ * of "_temp_view" and a defined function.
+ *
+ * @author mbreese
+ *
+ */
+public class AdHocView extends View {
+ public AdHocView(String function) {
+ super("_temp_view");
+ this.function=function;
+ }
+}
View
7 src/java/com/fourspaces/couchdb/CouchResponse.java
@@ -53,7 +53,6 @@
private Header[] headers;
private int statusCode;
private String methodName;
-
boolean ok = false;
private String error_id;
@@ -72,7 +71,7 @@
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
-
+
path = req.getURI().getPath();
statusCode = response.getStatusLine().getStatusCode();
@@ -186,4 +185,8 @@ public String getHeader(String key) {
}
return null;
}
+
+ public String getBody() {
+ return body;
+ }
}
View
39 src/java/com/fourspaces/couchdb/Database.java
@@ -101,6 +101,19 @@ public ViewResults getAllDocuments() {
}
/**
+ * Gets all design documents
+ *
+ * @return ViewResults - all design docs
+ */
+ public ViewResults getAllDesignDocuments() {
+ View v = new View("_all_docs");
+ v.startKey = "%22_design%2F%22";
+ v.endKey = "%22_design0%22";
+ v.includeDocs = Boolean.TRUE;
+ return view(v, false);
+ }
+
+ /**
* Runs the standard "_all_docs" view on this database, with count
*
* @return ViewResults - the results of the view... this can be iterated over to get each document.
@@ -381,4 +394,30 @@ public boolean deleteDocument(Document d) throws IOException {
}
}
+
+ /**
+ * Gets attachment
+ *
+ * @param id
+ * @param attachment attachment body
+ * @return attachment body
+ */
+ public String getAttachment(String id, String attachment) throws IOException {
+ CouchResponse resp = session.get(name + "/" + urlEncodePath(id) + "/" + attachment);
+ return resp.getBody();
+ }
+
+ /**
+ * Puts attachment to the doc
+ *
+ * @param id
+ * @param fname attachment name
+ * @param ctype content type
+ * @param attachment attachment body
+ * @return was the delete successful?
+ */
+ public String putAttachment(String id, String fname, String ctype, String attachment) throws IOException {
+ CouchResponse resp = session.put(name + "/" + urlEncodePath(id) + "/" + fname, ctype, attachment);
+ return resp.getBody();
+ }
}
View
840 src/java/com/fourspaces/couchdb/Document.java
@@ -1,420 +1,420 @@
-/*
- Copyright 2007 Fourspaces Consulting, 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.fourspaces.couchdb;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
-import com.fourspaces.couchdb.util.JSONUtils;
-import net.sf.json.*;
-
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-
-/**
- * Everything in CouchDB is a Document. In this case, the document is an object backed by a
- * JSONObject. The Document is also aware of the database that it is connected to. This allows
- * the Document to reload it's properties when needed. The only special fields are "_id", "_rev",
- * "_revisions", and "_view_*".
- * <p>
- * All document have an _id and a _rev. If this is a new document those fields are populated when
- * they are saved to the CouchDB server.
- * <p>
- * _revisions is only populated if the document has been retrieved via database.getDocumentWithRevisions();
- * So, if this document wasn't, then when you call document.getRevisions(), it will go back to the server
- * to reload itself via database.getDocumentWithRevisions().
- * <p>
- * The Document can be treated like a JSONObject, eventhough it doesn't extend JSONObject (it's final).
- * <p>
- * You can also get/set values by calling document.get(key), document.put(key,value), just like a Map.
- * <p>
- * You can get a handle on the backing JSONObject by calling document.getJSONObject(); If this hasn't
- * been loaded yet, it will load the data itself using the given database connection.
- * <p>
- * If you got this Document from a view, you are likely missing elements. To load them you can call
- * document.load().
- *
- * @author mbreese
- *
- */
-@SuppressWarnings("unchecked")
-public class Document implements Map {
- Log log = LogFactory.getLog(Document.class);
-
- public static final String REVISION_HISTORY_PROP = "_revisions";
-
- protected Database database=null;
- protected JSONObject object;
-
- boolean loaded = false;
-
- /**
- * Create a new Document
- *
- */
- public Document () {
- this.object = new JSONObject();
- }
- /**
- * Create a new Document from a JSONObject
- * @param obj
- */
- public Document (JSONObject obj) {
- this.object = obj;
- loaded=true;
- }
-
- /**
- * Load data into this document from a differing JSONObject
- * <p>
- * This is mainly for reloading data for an object that was retrieved from a view. This version
- * doesn't overwrite any unsaved data that is currently present in this object.
- *
- * @param object2
- */
- protected void load(JSONObject object2) {
- if (!loaded) {
- object.putAll(object2);
- loaded=true;
- }
- }
-
- /**
- * This document's id (if saved)
- * @return
- */
- public String getId() {
- if (StringUtils.isNotBlank(object.optString("_id"))) {
- return object.optString("_id");
- } else {
- return object.optString("id");
- }
- }
- public void setId(String id) {
- object.put("_id",id);
- }
-
- /**
- * This strips _design from the document id
- */
- public String getViewDocumentId() {
- String id = getId();
- int pos = id.lastIndexOf("/");
- if (pos == -1) {
- return id;
- } else {
- return id.substring(pos+1);
- }
- }
-
- /**
- * This document's Revision (if saved)
- * @return
- */
- public String getRev() {
- if (StringUtils.isNotBlank(object.optString("_rev"))) {
- return object.optString("_rev");
- } else {
- return object.optString("rev");
- }
- }
- public void setRev(String rev) {
- object.put("_rev", rev);
- }
-
- /**
- * A list of the revision numbers that this document has. If this hasn't been
- * populated with a "full=true" query, then the database will be re-queried
- * @return
- */
- public String[] getRevisions() throws IOException {
- String[] revs = null;
- if (!object.has("_revs")) {
- populateRevisions();
- }
- //System.out.println(object);
- JSONArray ar = object.getJSONObject(REVISION_HISTORY_PROP).getJSONArray("ids");
- if (ar!=null) {
- revs = new String[ar.size()];
- for (int i=0 ; i< ar.size(); i++) {
- revs[i]=ar.getString(i);
- }
- }
- return revs;
- }
-
- /**
- * Get a named view that is stored in the document.
- * @param name
- * @return
- */
- public View getView(String name) {
- if (object.has("views")) {
- JSONObject views = object.getJSONObject("views");
- if (views.has(name)) {
- return new View(this,name);
- }
- }
- return null;
- }
-
- /**
- * Add a view to this document. If a view function already exists with the given viewName
- * it is overwritten.
- * <p>
- * This isn't persisted until the document is saved.
- *
- * @param designDoc document name
- * @param viewName
- * @param function
- * @return
- */
- public View addView(String designDoc, String viewName, String function) {
- object.put("_id", "_design/"+ designDoc); //Not sure if _id or id should be used
- object.put("language", "javascript"); //FIXME specify language
-
- JSONObject funcs = new JSONObject();
-// System.err.println("JSON String: " + JSONUtils.stringSerializedFunction(function));
-// funcs.put("map", JSONUtils.stringSerializedFunction(function));
- funcs.accumulate("map", JSONUtils.stringSerializedFunction(function));
-
- System.err.println("FUNCS: " + funcs.toString());
-
- JSONObject viewMap = new JSONObject();
- viewMap.put(viewName, funcs);
-
- object.put("views", viewMap);
-
- return new View(this, viewName, function);
-
- }
-
- /**
- * Removes a view from this document.
- * <p>
- * This isn't persisted until the document is saved.
- * @param viewName
- */
- public void deleteView(String viewName) {
- object.remove("_design/"+viewName);
- }
-
- void setDatabase(Database database) {
- this.database=database;
- }
-
- /**
- * Loads data from the server for this document. Actually requests a new copy of data from the
- * server and uses that to populate this document. This doesn't overwrite any unsaved data.
- */
- public void refresh() throws IOException {
- if (database!=null) {
- Document doc = database.getDocument(getId());
- log.info("Loading: "+doc.getJSONObject());
- load(doc.getJSONObject());
- }
- }
-
- protected void populateRevisions() throws IOException {
- if (database!=null) {
- Document doc = database.getDocumentWithRevisions(getId());
- log.info("Loading: "+doc.getJSONObject());
- load(doc.getJSONObject());
- }
- }
-
- /**
- * Retrieves the backing JSONObject
- * @return
- */
- public JSONObject getJSONObject() {
- if (!loaded && database!=null && getId()!=null && !getId().equals("")) {
- try {
- refresh();
- } catch (IOException e) {
- throw new RuntimeException("error in refreshing Document", e);
- }
- }
- return object;
- }
-
- public String toString() {
- return object.toString();
- }
-
- /*
- * Delegate methods to the JSON Object.
- */
- public JSONObject accumulate(String arg0, boolean arg1) {
- return getJSONObject().accumulate(arg0, arg1);
- }
- public JSONObject accumulate(String arg0, double arg1) {
- return getJSONObject().accumulate(arg0, arg1);
- }
- public JSONObject accumulate(String arg0, int arg1) {
- return getJSONObject().accumulate(arg0, arg1);
- }
- public JSONObject accumulate(String arg0, long arg1) {
- return getJSONObject().accumulate(arg0, arg1);
- }
- public JSONObject accumulate(String arg0, Object arg1) {
- return getJSONObject().accumulate(arg0, arg1);
- }
- public void accumulateAll(Map arg0) {
- getJSONObject().accumulateAll(arg0);
- }
- public void clear() {
- getJSONObject().clear();
- }
- public boolean containsKey(Object arg0) {
- return getJSONObject().containsKey(arg0);
- }
- public boolean containsValue(Object arg0) {
- return getJSONObject().containsValue(arg0);
- }
- public JSONObject element(String arg0, boolean arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject element(String arg0, Collection arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject element(String arg0, double arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject element(String arg0, int arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject element(String arg0, long arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject element(String arg0, Map arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject element(String arg0, Object arg1) {
- return getJSONObject().element(arg0, arg1);
- }
- public JSONObject elementOpt(String arg0, Object arg1) {
- return getJSONObject().elementOpt(arg0, arg1);
- }
- public Set entrySet() {
- return getJSONObject().entrySet();
- }
- public Object get(Object arg0) {
- return getJSONObject().get(arg0);
- }
- public Object get(String arg0) {
- return getJSONObject().get(arg0);
- }
- public boolean getBoolean(String arg0) {
- return getJSONObject().getBoolean(arg0);
- }
- public double getDouble(String arg0) {
- return getJSONObject().getDouble(arg0);
- }
- public int getInt(String arg0) {
- return getJSONObject().getInt(arg0);
- }
- public JSONArray getJSONArray(String arg0) {
- return getJSONObject().getJSONArray(arg0);
- }
- public JSONObject getJSONObject(String arg0) {
- return getJSONObject().getJSONObject(arg0);
- }
- public long getLong(String arg0) {
- return getJSONObject().getLong(arg0);
- }
- public String getString(String arg0) {
- return getJSONObject().getString(arg0);
- }
- public boolean has(String arg0) {
- return getJSONObject().has(arg0);
- }
- public Iterator keys() {
- return getJSONObject().keys();
- }
- public Set keySet() {
- return getJSONObject().keySet();
- }
- public JSONArray names() {
- return getJSONObject().names();
- }
- public Object opt(String arg0) {
- return getJSONObject().opt(arg0);
- }
- public boolean optBoolean(String arg0, boolean arg1) {
- return getJSONObject().optBoolean(arg0, arg1);
- }
- public boolean optBoolean(String arg0) {
- return getJSONObject().optBoolean(arg0);
- }
- public double optDouble(String arg0, double arg1) {
- return getJSONObject().optDouble(arg0, arg1);
- }
- public double optDouble(String arg0) {
- return getJSONObject().optDouble(arg0);
- }
- public int optInt(String arg0, int arg1) {
- return getJSONObject().optInt(arg0, arg1);
- }
- public int optInt(String arg0) {
- return getJSONObject().optInt(arg0);
- }
- public JSONArray optJSONArray(String arg0) {
- return getJSONObject().optJSONArray(arg0);
- }
- public JSONObject optJSONObject(String arg0) {
- return getJSONObject().optJSONObject(arg0);
- }
- public long optLong(String arg0, long arg1) {
- return getJSONObject().optLong(arg0, arg1);
- }
- public long optLong(String arg0) {
- return getJSONObject().optLong(arg0);
- }
- public String optString(String arg0, String arg1) {
- return getJSONObject().optString(arg0, arg1);
- }
- public String optString(String arg0) {
- return getJSONObject().optString(arg0);
- }
- public Object put(Object arg0, Object arg1) {
- return getJSONObject().put(arg0, arg1);
- }
- public void putAll(Map arg0) {
- getJSONObject().putAll(arg0);
- }
- public Object remove(Object arg0) {
- return getJSONObject().remove(arg0);
- }
- public Object remove(String arg0) {
- return getJSONObject().remove(arg0);
- }
- public int size() {
- return getJSONObject().size();
- }
- public Collection values() {
- return getJSONObject().values();
- }
- public boolean isEmpty() {
- return getJSONObject().isEmpty();
- }
-}
+/*
+ Copyright 2007 Fourspaces Consulting, 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.fourspaces.couchdb;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import com.fourspaces.couchdb.util.JSONUtils;
+import net.sf.json.*;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Everything in CouchDB is a Document. In this case, the document is an object backed by a
+ * JSONObject. The Document is also aware of the database that it is connected to. This allows
+ * the Document to reload it's properties when needed. The only special fields are "_id", "_rev",
+ * "_revisions", and "_view_*".
+ * <p>
+ * All document have an _id and a _rev. If this is a new document those fields are populated when
+ * they are saved to the CouchDB server.
+ * <p>
+ * _revisions is only populated if the document has been retrieved via database.getDocumentWithRevisions();
+ * So, if this document wasn't, then when you call document.getRevisions(), it will go back to the server
+ * to reload itself via database.getDocumentWithRevisions().
+ * <p>
+ * The Document can be treated like a JSONObject, eventhough it doesn't extend JSONObject (it's final).
+ * <p>
+ * You can also get/set values by calling document.get(key), document.put(key,value), just like a Map.
+ * <p>
+ * You can get a handle on the backing JSONObject by calling document.getJSONObject(); If this hasn't
+ * been loaded yet, it will load the data itself using the given database connection.
+ * <p>
+ * If you got this Document from a view, you are likely missing elements. To load them you can call
+ * document.load().
+ *
+ * @author mbreese
+ *
+ */
+@SuppressWarnings("unchecked")
+public class Document implements Map {
+ Log log = LogFactory.getLog(Document.class);
+
+ public static final String REVISION_HISTORY_PROP = "_revisions";
+
+ protected Database database=null;
+ protected JSONObject object;
+
+ boolean loaded = false;
+
+ /**
+ * Create a new Document
+ *
+ */
+ public Document () {
+ this.object = new JSONObject();
+ }
+ /**
+ * Create a new Document from a JSONObject
+ * @param obj
+ */
+ public Document (JSONObject obj) {
+ this.object = obj;
+ loaded=true;
+ }
+
+ /**
+ * Load data into this document from a differing JSONObject
+ * <p>
+ * This is mainly for reloading data for an object that was retrieved from a view. This version
+ * doesn't overwrite any unsaved data that is currently present in this object.
+ *
+ * @param object2
+ */
+ protected void load(JSONObject object2) {
+ if (!loaded) {
+ object.putAll(object2);
+ loaded=true;
+ }
+ }
+
+ /**
+ * This document's id (if saved)
+ * @return
+ */
+ public String getId() {
+ if (StringUtils.isNotBlank(object.optString("_id"))) {
+ return object.optString("_id");
+ } else {
+ return object.optString("id");
+ }
+ }
+ public void setId(String id) {
+ object.put("_id",id);
+ }
+
+ /**
+ * This strips _design from the document id
+ */
+ public String getViewDocumentId() {
+ String id = getId();
+ int pos = id.lastIndexOf("/");
+ if (pos == -1) {
+ return id;
+ } else {
+ return id.substring(pos+1);
+ }
+ }
+
+ /**
+ * This document's Revision (if saved)
+ * @return
+ */
+ public String getRev() {
+ if (StringUtils.isNotBlank(object.optString("_rev"))) {
+ return object.optString("_rev");
+ } else {
+ return object.optString("rev");
+ }
+ }
+ public void setRev(String rev) {
+ object.put("_rev", rev);
+ }
+
+ /**
+ * A list of the revision numbers that this document has. If this hasn't been
+ * populated with a "full=true" query, then the database will be re-queried
+ * @return
+ */
+ public String[] getRevisions() throws IOException {
+ String[] revs = null;
+ if (!object.has("_revs")) {
+ populateRevisions();
+ }
+ //System.out.println(object);
+ JSONArray ar = object.getJSONObject(REVISION_HISTORY_PROP).getJSONArray("ids");
+ if (ar!=null) {
+ revs = new String[ar.size()];
+ for (int i=0 ; i< ar.size(); i++) {
+ revs[i]=ar.getString(i);
+ }
+ }
+ return revs;
+ }
+
+ /**
+ * Get a named view that is stored in the document.
+ * @param name
+ * @return
+ */
+ public View getView(String name) {
+ if (object.has("views")) {
+ JSONObject views = object.getJSONObject("views");
+ if (views.has(name)) {
+ return new View(this,name);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Add a view to this document. If a view function already exists with the given viewName
+ * it is overwritten.
+ * <p>
+ * This isn't persisted until the document is saved.
+ *
+ * @param designDoc document name
+ * @param viewName
+ * @param function
+ * @return
+ */
+ public View addView(String designDoc, String viewName, String function) {
+ object.put("_id", "_design/"+ designDoc); //Not sure if _id or id should be used
+ object.put("language", "javascript"); //FIXME specify language
+
+ JSONObject funcs = new JSONObject();
+// System.err.println("JSON String: " + JSONUtils.stringSerializedFunction(function));
+// funcs.put("map", JSONUtils.stringSerializedFunction(function));
+ funcs.accumulate("map", JSONUtils.stringSerializedFunction(function));
+
+ System.err.println("FUNCS: " + funcs.toString());
+
+ JSONObject viewMap = new JSONObject();
+ viewMap.put(viewName, funcs);
+
+ object.put("views", viewMap);
+
+ return new View(this, viewName, function);
+
+ }
+
+ /**
+ * Removes a view from this document.
+ * <p>
+ * This isn't persisted until the document is saved.
+ * @param viewName
+ */
+ public void deleteView(String viewName) {
+ object.remove("_design/"+viewName);
+ }
+
+ void setDatabase(Database database) {
+ this.database=database;
+ }
+
+ /**
+ * Loads data from the server for this document. Actually requests a new copy of data from the
+ * server and uses that to populate this document. This doesn't overwrite any unsaved data.
+ */
+ public void refresh() throws IOException {
+ if (database!=null) {
+ Document doc = database.getDocument(getId());
+ log.info("Loading: "+doc.getJSONObject());
+ load(doc.getJSONObject());
+ }
+ }
+
+ protected void populateRevisions() throws IOException {
+ if (database!=null) {
+ Document doc = database.getDocumentWithRevisions(getId());
+ log.info("Loading: "+doc.getJSONObject());
+ load(doc.getJSONObject());
+ }
+ }
+
+ /**
+ * Retrieves the backing JSONObject
+ * @return
+ */
+ public JSONObject getJSONObject() {
+ if (!loaded && database!=null && getId()!=null && !getId().equals("")) {
+ try {
+ refresh();
+ } catch (IOException e) {
+ throw new RuntimeException("error in refreshing Document", e);
+ }
+ }
+ return object;
+ }
+
+ public String toString() {
+ return object.toString();
+ }
+
+ /*
+ * Delegate methods to the JSON Object.
+ */
+ public JSONObject accumulate(String arg0, boolean arg1) {
+ return getJSONObject().accumulate(arg0, arg1);
+ }
+ public JSONObject accumulate(String arg0, double arg1) {
+ return getJSONObject().accumulate(arg0, arg1);
+ }
+ public JSONObject accumulate(String arg0, int arg1) {
+ return getJSONObject().accumulate(arg0, arg1);
+ }
+ public JSONObject accumulate(String arg0, long arg1) {
+ return getJSONObject().accumulate(arg0, arg1);
+ }
+ public JSONObject accumulate(String arg0, Object arg1) {
+ return getJSONObject().accumulate(arg0, arg1);
+ }
+ public void accumulateAll(Map arg0) {
+ getJSONObject().accumulateAll(arg0);
+ }
+ public void clear() {
+ getJSONObject().clear();
+ }
+ public boolean containsKey(Object arg0) {
+ return getJSONObject().containsKey(arg0);
+ }
+ public boolean containsValue(Object arg0) {
+ return getJSONObject().containsValue(arg0);
+ }
+ public JSONObject element(String arg0, boolean arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject element(String arg0, Collection arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject element(String arg0, double arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject element(String arg0, int arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject element(String arg0, long arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject element(String arg0, Map arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject element(String arg0, Object arg1) {
+ return getJSONObject().element(arg0, arg1);
+ }
+ public JSONObject elementOpt(String arg0, Object arg1) {
+ return getJSONObject().elementOpt(arg0, arg1);
+ }
+ public Set entrySet() {
+ return getJSONObject().entrySet();
+ }
+ public Object get(Object arg0) {
+ return getJSONObject().get(arg0);
+ }
+ public Object get(String arg0) {
+ return getJSONObject().get(arg0);
+ }
+ public boolean getBoolean(String arg0) {
+ return getJSONObject().getBoolean(arg0);
+ }
+ public double getDouble(String arg0) {
+ return getJSONObject().getDouble(arg0);
+ }
+ public int getInt(String arg0) {
+ return getJSONObject().getInt(arg0);
+ }
+ public JSONArray getJSONArray(String arg0) {
+ return getJSONObject().getJSONArray(arg0);
+ }
+ public JSONObject getJSONObject(String arg0) {
+ return getJSONObject().getJSONObject(arg0);
+ }
+ public long getLong(String arg0) {
+ return getJSONObject().getLong(arg0);
+ }
+ public String getString(String arg0) {
+ return getJSONObject().getString(arg0);
+ }
+ public boolean has(String arg0) {
+ return getJSONObject().has(arg0);
+ }
+ public Iterator keys() {
+ return getJSONObject().keys();
+ }
+ public Set keySet() {
+ return getJSONObject().keySet();
+ }
+ public JSONArray names() {
+ return getJSONObject().names();
+ }
+ public Object opt(String arg0) {
+ return getJSONObject().opt(arg0);
+ }
+ public boolean optBoolean(String arg0, boolean arg1) {
+ return getJSONObject().optBoolean(arg0, arg1);
+ }
+ public boolean optBoolean(String arg0) {
+ return getJSONObject().optBoolean(arg0);
+ }
+ public double optDouble(String arg0, double arg1) {
+ return getJSONObject().optDouble(arg0, arg1);
+ }
+ public double optDouble(String arg0) {
+ return getJSONObject().optDouble(arg0);
+ }
+ public int optInt(String arg0, int arg1) {
+ return getJSONObject().optInt(arg0, arg1);
+ }
+ public int optInt(String arg0) {
+ return getJSONObject().optInt(arg0);
+ }
+ public JSONArray optJSONArray(String arg0) {
+ return getJSONObject().optJSONArray(arg0);
+ }
+ public JSONObject optJSONObject(String arg0) {
+ return getJSONObject().optJSONObject(arg0);
+ }
+ public long optLong(String arg0, long arg1) {
+ return getJSONObject().optLong(arg0, arg1);
+ }
+ public long optLong(String arg0) {
+ return getJSONObject().optLong(arg0);
+ }
+ public String optString(String arg0, String arg1) {
+ return getJSONObject().optString(arg0, arg1);
+ }
+ public String optString(String arg0) {
+ return getJSONObject().optString(arg0);
+ }
+ public Object put(Object arg0, Object arg1) {
+ return getJSONObject().put(arg0, arg1);
+ }
+ public void putAll(Map arg0) {
+ getJSONObject().putAll(arg0);
+ }
+ public Object remove(Object arg0) {
+ return getJSONObject().remove(arg0);
+ }
+ public Object remove(String arg0) {
+ return getJSONObject().remove(arg0);
+ }
+ public int size() {
+ return getJSONObject().size();
+ }
+ public Collection values() {
+ return getJSONObject().values();
+ }
+ public boolean isEmpty() {
+ return getJSONObject().isEmpty();
+ }
+}
View
19 src/java/com/fourspaces/couchdb/Session.java
@@ -350,7 +350,24 @@ CouchResponse put(String url, String content) {
}
return http(put);
}
-
+
+ /**
+ * Overloaded Put using by attachments
+ */
+ CouchResponse put(String url, String ctype, String content) {
+ HttpPut put = new HttpPut(buildUrl(url));
+ if (content!=null) {
+ HttpEntity entity;
+ try {
+ entity = new StringEntity(content, DEFAULT_CHARSET);
+ put.setEntity(entity);
+ put.setHeader(new BasicHeader("Content-Type", ctype));
+ } catch (UnsupportedEncodingException e) {
+ log.error(ExceptionUtils.getStackTrace(e));
+ }
+ }
+ return http(put);
+ }
/**
* Send a GET request
* @param url
View
400 src/java/com/fourspaces/couchdb/View.java
@@ -1,131 +1,145 @@
-/*
- Copyright 2007 Fourspaces Consulting, 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.fourspaces.couchdb;
-
-/**
- * The View is the mechanism for performing Querys on a CouchDB instance.
- * The view can be named or ad-hoc (see AdHocView). (Currently [14 Sept 2007] named view aren't working in the
- * mainline CouchDB code... but this _should_ work.)
- *<p>
- * The View object exists mainly to apply filtering to the view. Otherwise, views can be
- * called directly from the database object by using their names (or given an ad-hoc query).
- *
- * @author mbreese
- *
- */
-public class View {
- protected String startKey;
- protected String endKey;
- protected Integer limit;
- protected Boolean update;
- protected Boolean reverse;
- protected String skip;
- protected Boolean group;
-
- protected String name;
- protected Document document;
- protected String function;
-
- /**
- * Build a view given a document and a name
- *
- * @param doc
- * @param name
- */
- public View(Document doc, String name) {
- this.document=doc;
- this.name=name;
- }
-
- /**
- * Build a view given only a fullname ex: ("_add_docs", "_temp_view")
- * @param fullname
- */
- public View(String fullname) {
- this.name=fullname;
- this.document=null;
- }
-
- /**
- * Builds a new view for a document, a given name, and the function definition.
- * This <i>does not actually add it to the document</i>. That is handled by
- * Document.addView()
- * <p>
- * This constructor should only be called by Document.addView();
- *
- * @param doc
- * @param name
- * @param function
- */
- View(Document doc, String name, String function) {
- this.name=name;
- this.document=doc;
- this.function=function;
- }
-
- /**
- * Based upon settings, builds the queryString to add to the URL for this view.
- *
- *
- * @return
- */
- public String getQueryString() {
- String queryString = "";
- if (startKey!=null) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="startkey="+startKey;
- }
- if (endKey!=null) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="endkey="+endKey;
- }
- if (skip!=null) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="skip="+skip;
- }
- if (limit!=null) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="limit="+limit;
- }
- if (update!=null && update.booleanValue()) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="update=true";
- }
- if (reverse!=null && reverse.booleanValue()) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="descending=true";
- }
- if (group!=null && group.booleanValue()) {
- if (!queryString.equals("")) { queryString+="&"; }
- queryString+="group=true";
- }
- return queryString.equals("") ? null : queryString;
-
- }
-
- /**
- * The number of entries to return
- * @param count
+/*
+ Copyright 2007 Fourspaces Consulting, 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.fourspaces.couchdb;
+
+/**
+ * The View is the mechanism for performing Querys on a CouchDB instance.
+ * The view can be named or ad-hoc (see AdHocView). (Currently [14 Sept 2007] named view aren't working in the
+ * mainline CouchDB code... but this _should_ work.)
+ *<p>
+ * The View object exists mainly to apply filtering to the view. Otherwise, views can be
+ * called directly from the database object by using their names (or given an ad-hoc query).
+ *
+ * @author mbreese
+ *
+ */
+public class View {
+ protected String key;
+ protected String startKey;
+ protected String endKey;
+ protected Integer limit;
+ protected Boolean update;
+ protected Boolean reverse;
+ protected String skip;
+ protected Boolean group;
+ protected Boolean includeDocs;
+
+ protected String name;
+ protected Document document;
+ protected String function;
+
+ /**
+ * Build a view given a document and a name
+ *
+ * @param doc
+ * @param name
+ */
+ public View(Document doc, String name) {
+ this.document=doc;
+ this.name=name;
+ }
+
+ /**
+ * Build a view given only a fullname ex: ("_add_docs", "_temp_view")
+ * @param fullname
+ */
+ public View(String fullname) {
+ this.name=fullname;
+ this.document=null;
+ }
+
+ /**
+ * Builds a new view for a document, a given name, and the function definition.
+ * This <i>does not actually add it to the document</i>. That is handled by
+ * Document.addView()
+ * <p>
+ * This constructor should only be called by Document.addView();
+ *
+ * @param doc
+ * @param name
+ * @param function
+ */
+ View(Document doc, String name, String function) {
+ this.name=name;
+ this.document=doc;
+ this.function=function;
+ }
+
+ /**
+ * Based upon settings, builds the queryString to add to the URL for this view.
+ *
+ *
+ * @return
+ */
+ public String getQueryString() {
+ String queryString = "";
+ if (key!=null) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="key="+key;
+ }
+ if (startKey!=null) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="startkey="+startKey;
+ }
+ if (endKey!=null) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="endkey="+endKey;
+ }
+ if (skip!=null) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="skip="+skip;
+ }
+ if (limit!=null) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="limit="+limit;
+ }
+ if (update!=null && update.booleanValue()) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="update=true";
+ }
+ if (includeDocs!=null && includeDocs.booleanValue()) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="include_docs=true";
+ }
+ if (reverse!=null && reverse.booleanValue()) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="descending=true";
+ }
+ if (group!=null && group.booleanValue()) {
+ if (!queryString.equals("")) { queryString+="&"; }
+ queryString+="group=true";
+ }
+ return queryString.equals("") ? null : queryString;
+
+ }
+
+ /**
+ * The number of entries to return
+ * @param count
* @deprecated CouchDB 0.9 uses limit instead
- */
- public void setCount(Integer count) {
- //this.count = count;
- setLimit(count);
- }
+ */
+ public void setCount(Integer count) {
+ //this.count = count;
+ setLimit(count);
+ }
+
+ public void setKey(String key) {
+ this.key = key;
+ }
public void setLimit(Integer limit) {
this.limit = limit;
@@ -135,72 +149,76 @@ public void setGroup(Boolean group) {
this.group = group;
}
- /**
- * Stop listing at this key
- * @param endKey
- */
- public void setEndKey(String endKey) {
- this.endKey = endKey;
- }
- /**
- * Reverse the listing
- * @param reverse
+ /**
+ * Stop listing at this key
+ * @param endKey
+ */
+ public void setEndKey(String endKey) {
+ this.endKey = endKey;
+ }
+ /**
+ * Reverse the listing
+ * @param reverse
* @deprecated CouchDB 0.9 uses "descending" instead
- */
- public void setReverse(Boolean reverse) {
- this.reverse = reverse;
- }
+ */
+ public void setReverse(Boolean reverse) {
+ this.reverse = reverse;
+ }
public void setDescending(Boolean descending) {
this.reverse = descending;
}
- /**
- * Skip listing these keys (not sure if this works, or the format)
- * @param skip
- */
- public void setSkip(String skip) {
- this.skip = skip;
- }
- /**
- * Start listing at this key
- * @param startKey
- */
- public void setStartKey(String startKey) {
- this.startKey = startKey;
- }
- /**
- * Not sure... might be for batch updates, but not sure.
- * @param update
- */
- public void setUpdate(Boolean update) {
- this.update = update;
- }
-
- /**
- * The name for this view (w/o doc id)
- * @return
- */
- public String getName() {
- return name;
- }
- /**
- * the full name for this view (w/ doc id, if avail)
- * in the form of :
- * "docid:name"
- * or
- * "name"
- * @return
- */
- public String getFullName() {
- return (document==null) ? name: document.getViewDocumentId()+"/"+name;
- }
-
- /**
- * The function definition for this view, if it is available.
- * @return
- */
- public String getFunction() {
- return function;
- }
-
-}
+ /**
+ * Skip listing these keys (not sure if this works, or the format)
+ * @param skip
+ */
+ public void setSkip(String skip) {
+ this.skip = skip;
+ }
+ /**
+ * Start listing at this key
+ * @param startKey
+ */
+ public void setStartKey(String startKey) {
+ this.startKey = startKey;
+ }
+ /**
+ * Not sure... might be for batch updates, but not sure.
+ * @param update
+ */
+ public void setUpdate(Boolean update) {
+ this.update = update;
+ }
+
+ public void setWithDocs(Boolean withDocs) {
+ this.includeDocs = includeDocs;
+ }
+
+ /**
+ * The name for this view (w/o doc id)
+ * @return
+ */
+ public String getName() {
+ return name;
+ }
+ /**
+ * the full name for this view (w/ doc id, if avail)
+ * in the form of :
+ * "docid:name"
+ * or
+ * "name"
+ * @return
+ */
+ public String getFullName() {
+ return (document==null) ? name: document.getViewDocumentId()+"/"+name;
+ }
+
+ /**
+ * The function definition for this view, if it is available.
+ * @return
+ */
+ public String getFunction() {
+ return function;
+ }
+
+}
View
50 src/test/com/fourspaces/couchdb/test/TestSession.java
@@ -1,25 +1,25 @@
-package com.fourspaces.couchdb.test;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
-import org.junit.Test;
-
-import com.fourspaces.couchdb.Session;
-
-public class TestSession {
- public static Session getTestSession() {
- Properties props = new Properties();
- try {
- InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("couchdb-test.properties");
- props.load(is);
- return new Session(props.getProperty("host"),Integer.parseInt(props.getProperty("port")));
- } catch (Exception e) {
- return new Session("localhost",5984);
- //throw new RuntimeException(e);
- }
- }
- @Test
- public void noop() {} // just to avoid junit warning
-}
+package com.fourspaces.couchdb.test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.junit.Test;
+
+import com.fourspaces.couchdb.Session;
+
+public class TestSession {
+ public static Session getTestSession() {
+ Properties props = new Properties();
+ try {
+ InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("couchdb-test.properties");
+ props.load(is);
+ return new Session(props.getProperty("host"),Integer.parseInt(props.getProperty("port")));
+ } catch (Exception e) {
+ return new Session("localhost",5984);
+ //throw new RuntimeException(e);
+ }
+ }
+ @Test
+ public void noop() {} // just to avoid junit warning
+}
View
2 src/test/couchdb-test.properties.default
@@ -1,2 +1,2 @@
-host=localhost
+host=localhost
port=5984
View
20 src/test/log4j.properties
@@ -1,11 +1,11 @@
-log4j.rootCategory=DEBUG, A1
-
-log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
-log4j.appender.A1.File=test.log
-log4j.appender.A1.DatePattern = '.'yyyy-MM-dd
-log4j.appender.A1.layout=org.apache.log4j.PatternLayout
-log4j.appender.A1.layout.ConversionPattern=%-6r %-5p %30.30c %x - %m%n
-
-log4j.appender.A2 = org.apache.log4j.ConsoleAppender
-log4j.appender.A2.layout=org.apache.log4j.PatternLayout
+log4j.rootCategory=DEBUG, A1
+
+log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
+log4j.appender.A1.File=test.log
+log4j.appender.A1.DatePattern = '.'yyyy-MM-dd
+log4j.appender.A1.layout=org.apache.log4j.PatternLayout
+log4j.appender.A1.layout.ConversionPattern=%-6r %-5p %30.30c %x - %m%n
+
+log4j.appender.A2 = org.apache.log4j.ConsoleAppender
+log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-6r %-5p %45.45c:%-5L %x - %m%n

0 comments on commit 254e545

Please sign in to comment.
Something went wrong with that request. Please try again.