Search in sources :

Example 1 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse in project yacy_grid_loader by yacy.

the class LoaderService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    // construct the same process as if it was submitted on a queue
    SusiThought process = ProcessService.queryToProcess(call);
    // extract call parameter here to enhance ability to debug
    SusiAction action = process.getActions().get(0);
    JSONArray data = process.getData();
    // construct a WARC
    byte[] b = ContentLoader.eval(action, data, true, "api call from " + call.getClientHost(), true);
    // store the WARC as asset if wanted
    return new ServiceResponse(b);
}
Also used : ServiceResponse(net.yacy.grid.http.ServiceResponse) SusiThought(ai.susi.mind.SusiThought) JSONArray(org.json.JSONArray) SusiAction(ai.susi.mind.SusiAction)

Example 2 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.

the class MCPQueueFactory method getQueue.

@Override
public Queue<byte[]> getQueue(String serviceQueueName) throws IOException {
    final int p = serviceQueueName.indexOf('_');
    if (p <= 0)
        return null;
    final JSONObject params = new JSONObject(true);
    params.put("serviceName", serviceQueueName.substring(0, p));
    params.put("queueName", serviceQueueName.substring(p + 1));
    return new AbstractQueue<byte[]>() {

        @Override
        public void checkConnection() throws IOException {
            String protocolhostportstub = MCPQueueFactory.this.getConnectionURL();
            APIHandler apiHandler = APIServer.getAPI(StatusService.NAME);
            ServiceResponse sr = apiHandler.serviceImpl(protocolhostportstub, params);
            if (!sr.getObject().has("system"))
                throw new IOException("MCP does not respond properly");
            // check on service level again
            available();
        }

        @Override
        public Queue<byte[]> send(byte[] message) throws IOException {
            params.put("message", new String(message, StandardCharsets.UTF_8));
            JSONObject response = getResponse(APIServer.getAPI(SendService.NAME));
            // read the broker to store the service definition of the remote queue, if exists
            if (success(response)) {
                connectMCP(response);
                return this;
            } else {
                throw handleError(response);
            }
        }

        @Override
        public byte[] receive(long timeout) throws IOException {
            params.put("timeout", Long.toString(timeout));
            JSONObject response = getResponse(APIServer.getAPI(ReceiveService.NAME));
            // read the broker to store the service definition of the remote queue, if exists
            if (success(response)) {
                connectMCP(response);
                if (response.has(ObjectAPIHandler.MESSAGE_KEY)) {
                    String message = response.getString(ObjectAPIHandler.MESSAGE_KEY);
                    return message == null ? null : message.getBytes(StandardCharsets.UTF_8);
                }
                throw new IOException("bad response from MCP: success but no message key");
            } else {
                throw handleError(response);
            }
        }

        @Override
        public int available() throws IOException {
            JSONObject response = getResponse(APIServer.getAPI(AvailableService.NAME));
            // read the broker to store the service definition of the remote queue, if exists
            if (success(response)) {
                connectMCP(response);
                if (response.has(ObjectAPIHandler.AVAILABLE_KEY)) {
                    int available = response.getInt(ObjectAPIHandler.AVAILABLE_KEY);
                    return available;
                }
                throw new IOException("bad response from MCP: success but no message key");
            } else {
                throw handleError(response);
            }
        }

        private JSONObject getResponse(APIHandler handler) throws IOException {
            String protocolhostportstub = MCPQueueFactory.this.getConnectionURL();
            ServiceResponse sr = handler.serviceImpl(protocolhostportstub, params);
            return sr.getObject();
        }

        private boolean success(JSONObject response) {
            return response.has(ObjectAPIHandler.SUCCESS_KEY) && response.getBoolean(ObjectAPIHandler.SUCCESS_KEY);
        }

        private void connectMCP(JSONObject response) {
            if (response.has(ObjectAPIHandler.SERVICE_KEY)) {
                String broker = response.getString(ObjectAPIHandler.SERVICE_KEY);
                if (MCPQueueFactory.this.broker.connectRabbitMQ(broker)) {
                    Data.logger.info("connected MCP broker at " + broker);
                } else {
                    Data.logger.error("failed to connect MCP broker at " + broker);
                }
            }
        }

        private IOException handleError(JSONObject response) {
            if (response.has(ObjectAPIHandler.COMMENT_KEY)) {
                return new IOException("cannot connect to MCP: " + response.getString(ObjectAPIHandler.COMMENT_KEY));
            }
            return new IOException("bad response from MCP: no success and no comment key");
        }
    };
}
Also used : APIHandler(net.yacy.grid.http.APIHandler) ObjectAPIHandler(net.yacy.grid.http.ObjectAPIHandler) ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 3 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse 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 4 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse 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)

Example 5 with ServiceResponse

use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.

the class CountService 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", "");
    if (typeName.length() == 0)
        typeName = null;
    QueryLanguage language = QueryLanguage.valueOf(call.get("language", "yacy"));
    String query = call.get("query", "");
    JSONObject json = new JSONObject(true);
    if (indexName.length() > 0 && query.length() > 0) {
        try {
            Index index = Data.gridIndex.getElasticIndex();
            String url = index.checkConnection().getConnectionURL();
            long count = index.count(indexName, typeName, language, query);
            json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            json.put("count", count);
            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());
        }
    } else {
        json.put(ObjectAPIHandler.SUCCESS_KEY, false);
        json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain an index, type, and a query");
    }
    return new ServiceResponse(json);
}
Also used : ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) QueryLanguage(net.yacy.grid.io.index.Index.QueryLanguage) Index(net.yacy.grid.io.index.Index) IOException(java.io.IOException)

Aggregations

ServiceResponse (net.yacy.grid.http.ServiceResponse)23 JSONObject (org.json.JSONObject)20 IOException (java.io.IOException)18 Index (net.yacy.grid.io.index.Index)6 GridQueue (net.yacy.grid.io.messages.GridQueue)6 JSONArray (org.json.JSONArray)5 Date (java.util.Date)4 SusiThought (ai.susi.mind.SusiThought)3 Map (java.util.Map)3 QueryLanguage (net.yacy.grid.io.index.Index.QueryLanguage)3 JSONList (net.yacy.grid.tools.JSONList)3 SusiAction (ai.susi.mind.SusiAction)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 APIHandler (net.yacy.grid.http.APIHandler)2 ObjectAPIHandler (net.yacy.grid.http.ObjectAPIHandler)2 ElasticsearchClient (net.yacy.grid.io.index.ElasticsearchClient)2 IndexFactory (net.yacy.grid.io.index.IndexFactory)2 Sort (net.yacy.grid.io.index.Sort)2 WebDocument (net.yacy.grid.io.index.WebDocument)2