Search in sources :

Example 1 with CouchClient

use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.

the class FullSyncFilterListener method triggerSequence.

@Override
protected void triggerSequence(InternalHttpServletRequest request, JSONArray ids) throws EngineException {
    if (isEnabled()) {
        if (targetFilter == null || targetFilter.isEmpty()) {
            throw new EngineException("No target filter defined");
        }
        String filter = targetFilter;
        String ddoc = getTargetDocName();
        String filterName = getTargetFilterName();
        if (ddoc != null && filterName != null) {
            filter = ddoc + "/" + filterName;
        }
        int len = ids.length();
        Map<String, String> query = new HashMap<String, String>(3);
        query.put("filter", filter);
        query.put("include_docs", "true");
        query.put("conflicts", "true");
        String db = getDatabaseName();
        try {
            CouchClient client = getCouchClient();
            for (int i = 0; i < len; ) {
                JSONArray doc_ids = getChunk(ids, i);
                int ids_len = doc_ids.length();
                i += ids_len;
                Engine.logBeans.debug("(FullSyncFilterListener) Listener \"" + getName() + "\" : [" + db + "] post filter '" + ddoc + "/" + filter + "' for _id keys " + doc_ids);
                JSONObject json = client.postChanges(db, query, CouchKey.doc_ids.put(new JSONObject(), doc_ids));
                Engine.logBeans.debug("(FullSyncFilterListener) Listener \"" + getName() + "\" : [" + db + "] post filter '" + ddoc + "/" + filter + "' returned following documents :\n" + json.toString());
                if (json != null) {
                    if (CouchKey.error.has(json)) {
                        String error = CouchKey.error.String(json);
                        error = error == null ? "unknown" : error;
                        String reason = CouchKey.reason.String(json);
                        reason = reason == null ? "unknown" : reason;
                        throw new EngineException("Filter '" + db + "/" + ddoc + "/" + filter + "' returned error: " + error + ", reason: " + reason);
                    }
                    runDocs(request, CouchKey.results.JSONArray(json));
                }
            }
        } catch (Throwable t) {
            throw new EngineException("Query filter named '" + db + "/" + ddoc + "/" + filter + "' failed", t);
        }
    }
}
Also used : CouchClient(com.twinsoft.convertigo.engine.providers.couchdb.CouchClient) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONArray(org.codehaus.jettison.json.JSONArray)

Example 2 with CouchClient

use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.

the class FullSyncListener method triggerSequence.

@Override
protected void triggerSequence(InternalHttpServletRequest request, JSONArray ids) throws EngineException {
    if (isEnabled()) {
        if (targetView == null || targetView.isEmpty()) {
            throw new EngineException("No target view defined");
        }
        String ddoc = getTargetDocName();
        if (ddoc == null) {
            throw new EngineException("Target design document name is null");
        }
        String view = getTargetViewName();
        if (view == null) {
            throw new EngineException("Target view name is null");
        }
        int len = ids.length();
        Map<String, String> query = new HashMap<String, String>(5);
        query.put("reduce", "false");
        query.put("include_docs", "true");
        query.put("conflicts", "true");
        query.put("stable", "true");
        query.put("update", "true");
        String db = getDatabaseName();
        try {
            CouchClient client = getCouchClient();
            for (int i = 0; i < len; ) {
                JSONArray doc_ids = getChunk(ids, i);
                int ids_len = doc_ids.length();
                i += ids_len;
                Engine.logBeans.debug("(FullSyncListener) Listener \"" + getName() + "\" : [" + db + "] post view '" + ddoc + "/" + view + "' for _id keys " + doc_ids);
                JSONObject json = client.postView(db, ddoc, view, query, CouchKey.keys.put(new JSONObject(), doc_ids));
                Engine.logBeans.debug("(FullSyncListener) Listener \"" + getName() + "\" : [" + db + "] post view '" + ddoc + "/" + view + "' returned following documents :\n" + json.toString());
                if (json != null) {
                    if (CouchKey.error.has(json)) {
                        String error = CouchKey.error.String(json);
                        error = error == null ? "unknown" : error;
                        String reason = CouchKey.reason.String(json);
                        reason = reason == null ? "unknown" : reason;
                        throw new EngineException("View '" + db + "/" + ddoc + "/" + view + "' returned error: " + error + ", reason: " + reason);
                    }
                    runDocs(request, CouchKey.rows.JSONArray(json));
                }
            }
        } catch (Throwable t) {
            throw new EngineException("Query view named '" + db + "/" + ddoc + "/" + view + "' failed", t);
        }
    }
}
Also used : CouchClient(com.twinsoft.convertigo.engine.providers.couchdb.CouchClient) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONArray(org.codehaus.jettison.json.JSONArray)

Example 3 with CouchClient

use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.

the class PurgeDatabaseTransaction method invoke.

@Override
protected JSONObject invoke() throws Exception {
    String db = getTargetDatabase();
    JSONObject jsonBase;
    try {
        jsonBase = new JSONObject(getParameterStringValue(CouchParam.json_base));
    } catch (Throwable t) {
        jsonBase = new JSONObject();
    }
    JSONObject jsonDocument = jsonBase;
    JSONObject response = null;
    int length;
    int purged = 0;
    CouchClient couchClient = getCouchClient();
    String version = couchClient.getServerVersion();
    if (version.startsWith("2.0.") || version.startsWith("2.1.") || version.startsWith("2.2.")) {
        return new JSONObject("{\"error\": \"'_purge' is implemented since CouchDB 2.3.0 and you are using CouchDB " + version + "\"}");
    }
    boolean old = version != null && version.compareTo("2.") < 0;
    if (isPurgeAll()) {
        JSONObject body = null;
        Map<String, String> query = new HashMap<>(3);
        if (!old) {
            query.put("filter", "_selector");
            body = new JSONObject("{\"selector\":{\"_deleted\":true}}");
        }
        int limit = 50;
        query.put("limit", Integer.toString(limit));
        String since = "0";
        while (since != null) {
            query.put("since", since);
            jsonDocument = new JSONObject();
            JSONObject json = old ? couchClient.getChanges(db, query) : couchClient.postChanges(db, query, body);
            JSONArray changes = CouchKey.results.JSONArray(json);
            length = changes.length();
            since = length < limit ? null : CouchKey.last_seq.String(json);
            handleChanges(changes, old, jsonDocument);
            if (jsonDocument.length() > 0) {
                response = couchClient.postPurge(db, jsonDocument);
                if (CouchKey.purged.has(response)) {
                    purged += CouchKey.purged.JSONObject(response).length();
                } else {
                    return response;
                }
            }
        }
    } else {
        Object p = getParameterValue("_id");
        List<String> ids = null;
        if (p != null) {
            if (p instanceof String) {
                ids = Arrays.asList((String) p);
            } else if (p instanceof List) {
                ids = GenericUtils.cast(p);
            }
        }
        if (ids != null && !ids.isEmpty()) {
            List<String> revs = null;
            if ((p = getParameterValue("_rev")) != null) {
                if (p instanceof String) {
                    revs = Arrays.asList((String) p);
                } else if (p instanceof List) {
                    revs = GenericUtils.cast(p);
                }
            }
            Iterator<String> irev = revs != null ? revs.iterator() : Collections.emptyIterator();
            for (String id : ids) {
                JSONArray r = null;
                if (jsonDocument.has(id)) {
                    Object o = jsonDocument.get(id);
                    if (o instanceof JSONArray) {
                        r = (JSONArray) o;
                    }
                }
                if (r == null) {
                    jsonDocument.put(id, r = new JSONArray());
                }
                if (irev.hasNext()) {
                    r.put(irev.next());
                }
            }
        }
        if ((length = jsonDocument.length()) > 0) {
            Set<String> keys = new HashSet<>(length);
            for (Iterator<?> i = jsonDocument.keys(); i.hasNext(); ) {
                String key = (String) i.next();
                Object o = jsonDocument.get(key);
                if (!(o instanceof JSONArray)) {
                    jsonDocument.put(key, new JSONArray());
                    keys.add(key);
                } else if (((JSONArray) o).length() == 0) {
                    keys.add(key);
                }
            }
            if (!keys.isEmpty()) {
                JSONObject body = new JSONObject();
                CouchKey.doc_ids.put(body, new JSONArray(keys));
                JSONObject changes = couchClient.postChanges(db, null, body);
                JSONArray result = CouchKey.results.JSONArray(changes);
                handleChanges(result, false, jsonDocument);
            }
            if (jsonDocument.length() > 0) {
                response = couchClient.postPurge(db, jsonDocument);
                purged += CouchKey.purged.JSONObject(response).length();
            }
        }
    }
    if (response == null) {
        response = new JSONObject("{\"purge_seq\":0,\"purged\":{},\"_c8oMeta\":{\"statusCode\":200,\"status\":\"success\",\"reasonPhrase\":\"OK\",\"headers\":{\"Content-Type\":\"application\\/json\"}}}");
    }
    CouchKey.total_purged.put(response, purged);
    if (purged > 0) {
        try {
            JSONObject ddoc = couchClient.getDocument(db, DesignDocumentC8o.getId());
            JSONObject meta = CouchKey._c8oMeta.JSONObject(ddoc);
            if (meta.getInt("statusCode") == 200) {
                String dbVersion = Long.toString(System.currentTimeMillis(), Character.MAX_RADIX);
                CouchKey.c8oDbVersion.put(ddoc, dbVersion);
                couchClient.postDocument(db, ddoc, null, CouchPostDocumentPolicy.none, false);
                Engine.logBeans.info("(PurgeDatabaseTransaction) Database '" + db + "' version changed to '" + dbVersion + "'.");
            }
        } catch (Exception e) {
            Engine.logBeans.warn("(PurgeDatabaseTransaction) Failed to update database '" + db + "' version", e);
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) JSONObject(org.codehaus.jettison.json.JSONObject) CouchClient(com.twinsoft.convertigo.engine.providers.couchdb.CouchClient) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List) HashSet(java.util.HashSet)

Example 4 with CouchClient

use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.

the class PostSessionTransaction method invoke.

@Override
protected Object invoke() throws Exception {
    String name = getParameterStringValue(CouchParam.name);
    String password = getParameterStringValue(CouchParam.password);
    getConnector().setCouchClient(new CouchClient(getCouchClient().getServerUrl(), name, password));
    return getCouchClient().getSession();
}
Also used : CouchClient(com.twinsoft.convertigo.engine.providers.couchdb.CouchClient)

Example 5 with CouchClient

use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.

the class CouchDbConnector method getCouchClient.

public CouchClient getCouchClient() {
    if (couchClient == null) {
        if (!isOriginal()) {
            couchClient = getOriginal().getCouchClient();
        } else {
            String url = isHttps() ? "https" : "http";
            url += "://" + getServer() + ":" + getPort();
            couchClient = new CouchClient(url, couchUsername, couchPassword);
        }
    }
    return couchClient;
}
Also used : CouchClient(com.twinsoft.convertigo.engine.providers.couchdb.CouchClient)

Aggregations

CouchClient (com.twinsoft.convertigo.engine.providers.couchdb.CouchClient)8 JSONObject (org.codehaus.jettison.json.JSONObject)6 EngineException (com.twinsoft.convertigo.engine.EngineException)4 HashMap (java.util.HashMap)4 JSONArray (org.codehaus.jettison.json.JSONArray)4 JSONException (org.codehaus.jettison.json.JSONException)2 CouchDbConnector (com.twinsoft.convertigo.beans.connectors.CouchDbConnector)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1