Search in sources :

Example 1 with IndexFactory

use of net.yacy.grid.io.index.IndexFactory in project yacy_grid_mcp by yacy.

the class AddService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    // String indexName, String typeName, final String id, JSONObject object
    String indexName = call.get("index", "");
    String typeName = call.get("type", "");
    String id = call.get("id", "");
    // this contains the JSON object
    byte[] object = call.get("object", EMPTY_OBJECT);
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && typeName.length() > 0 && id.length() > 0 && object.length > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            JSONObject payload = new JSONObject(new JSONTokener(new String(object, StandardCharsets.UTF_8)));
            IndexFactory factory = index.add(indexName, typeName, id, payload);
            String url = factory.getConnectionURL();
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            if (url != null)
                json.put(ObjectAPIHandler.SERVICE_KEY, url);
        } catch (IOException | JSONException e) {
            json.put(ObjectAPIHandler.SUCCESS_KEY, false);
            json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
        }
    } else {
        json.put(ObjectAPIHandler.SUCCESS_KEY, false);
        json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain an index, type, id and json object");
    }
    return new ServiceResponse(json);
}
Also used : JSONTokener(org.json.JSONTokener) ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) IndexFactory(net.yacy.grid.io.index.IndexFactory) JSONException(org.json.JSONException) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Example 2 with IndexFactory

use of net.yacy.grid.io.index.IndexFactory in project yacy_grid_mcp by yacy.

the class CheckService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    JSONObject json = new JSONObject(true);
    try {
        Index index = Data.gridIndex.getElasticIndex();
        IndexFactory factory = index.checkConnection();
        String url = factory.getConnectionURL();
        json.put(ObjectAPIHandler.SUCCESS_KEY, true);
        if (url != null)
            json.put(ObjectAPIHandler.SERVICE_KEY, url);
    } catch (IOException e) {
        json.put(ObjectAPIHandler.SUCCESS_KEY, false);
        json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
    }
    return new ServiceResponse(json);
}
Also used : ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) IndexFactory(net.yacy.grid.io.index.IndexFactory) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ServiceResponse (net.yacy.grid.http.ServiceResponse)2 Index (net.yacy.grid.io.index.Index)2 IndexFactory (net.yacy.grid.io.index.IndexFactory)2 JSONObject (org.json.JSONObject)2 JSONException (org.json.JSONException)1 JSONTokener (org.json.JSONTokener)1