use of net.yacy.grid.http.APIHandler 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");
}
};
}
use of net.yacy.grid.http.APIHandler in project yacy_grid_mcp by yacy.
the class MCPIndexFactory method getIndex.
@Override
public Index getIndex() throws IOException {
final JSONObject params = new JSONObject(true);
return new Index() {
@Override
public IndexFactory checkConnection() throws IOException {
String protocolhostportstub = MCPIndexFactory.this.getConnectionURL();
APIHandler apiHandler = APIServer.getAPI(CheckService.NAME);
ServiceResponse sr = apiHandler.serviceImpl(protocolhostportstub, params);
if (!success(sr.getObject()))
throw new IOException("MCP does not respond properly");
return MCPIndexFactory.this;
}
private JSONObject getResponse(APIHandler handler) throws IOException {
String protocolhostportstub = MCPIndexFactory.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 elastic = response.getString(ObjectAPIHandler.SERVICE_KEY);
if (MCPIndexFactory.this.index.connectElasticsearch(elastic)) {
Data.logger.info("connected MCP index at " + elastic);
} else {
Data.logger.error("failed to connect MCP index at " + elastic);
}
}
}
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");
}
@Override
public IndexFactory add(String indexName, String typeName, String id, JSONObject object) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("id", id);
params.put("object", object.toString());
JSONObject response = getResponse(APIServer.getAPI(AddService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
return MCPIndexFactory.this;
} else {
throw handleError(response);
}
}
@Override
public boolean exist(String indexName, String typeName, String id) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("id", id);
JSONObject response = getResponse(APIServer.getAPI(ExistService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
return response.has("exists") && response.getBoolean("exists");
} else {
throw handleError(response);
}
}
@Override
public long count(String indexName, String typeName, QueryLanguage language, String query) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("language", language.name());
params.put("query", query);
JSONObject response = getResponse(APIServer.getAPI(CountService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
return response.has("count") ? response.getLong("count") : 0;
} else {
throw handleError(response);
}
}
@Override
public JSONObject query(String indexName, String typeName, String id) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("id", id);
JSONObject response = getResponse(APIServer.getAPI(QueryService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
if (!response.has("list"))
return null;
JSONArray list = response.getJSONArray("list");
if (list.length() == 0)
return null;
return list.getJSONObject(0);
} else {
throw handleError(response);
}
}
@Override
public JSONList query(String indexName, String typeName, QueryLanguage language, String query, int start, int count) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("language", language.name());
params.put("query", query);
JSONObject response = getResponse(APIServer.getAPI(QueryService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
JSONList list = new JSONList();
if (!response.has("list"))
return list;
JSONArray l = response.getJSONArray("list");
if (l.length() == 0)
return list;
for (int i = 0; i < l.length(); i++) list.add(l.getJSONObject(i));
return list;
} else {
throw handleError(response);
}
}
@Override
public boolean delete(String indexName, String typeName, String id) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("id", id);
JSONObject response = getResponse(APIServer.getAPI(DeleteService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
return response.has("deleted") && response.getBoolean("deleted");
} else {
throw handleError(response);
}
}
@Override
public long delete(String indexName, String typeName, QueryLanguage language, String query) throws IOException {
params.put("index", indexName);
params.put("type", typeName);
params.put("language", language.name());
params.put("query", query);
JSONObject response = getResponse(APIServer.getAPI(DeleteService.NAME));
// read the broker to store the service definition of the remote queue, if exists
if (success(response)) {
connectMCP(response);
return response.has("count") ? response.getLong("count") : 0;
} else {
throw handleError(response);
}
}
@Override
public void close() {
}
};
}
Aggregations