/** * Create an index * @param index index name * @param ignoreErrors don't fail if the index already exists * @param indexSettings index settings if any * @throws IOException In case of error */ public void createIndex(String index, boolean ignoreErrors, String indexSettings) throws IOException { logger.debug("create index [{}]", index); logger.trace("index settings: [{}]", indexSettings); try { Response response = lowLevelClient.performRequest("PUT", "/" + index, Collections.emptyMap(), createEntity(indexSettings)); logger.trace("create index response: {}", asMap(response)); } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 400 && (e.getMessage().contains("index_already_exists_exception") || // ES 5.x e.getMessage().contains("IndexAlreadyExistsException") )) { // ES 1.x and 2.x if (!ignoreErrors) { throw new RuntimeException("index already exists"); } logger.trace("index already exists. Ignoring error..."); return; } throw e; } }
public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } // get the POST body String postBody = session.getQueryParameterString(); // or you can access the POST request's parameters String postParameter = session.getParms().get("parameter"); return new Response(postBody); // Or postParameter. }
/** * Converts a {@link ResponseException} obtained from the low level REST client into an {@link ElasticsearchException}. * If a response body was returned, tries to parse it as an error returned from Elasticsearch. * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link ElasticsearchStatusException} * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing. */ protected final ElasticsearchStatusException parseResponseException(ResponseException responseException) { Response response = responseException.getResponse(); HttpEntity entity = response.getEntity(); ElasticsearchStatusException elasticsearchException; if (entity == null) { elasticsearchException = new ElasticsearchStatusException( responseException.getMessage(), RestStatus.fromCode(response.getStatusLine().getStatusCode()), responseException); } else { try { elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent); elasticsearchException.addSuppressed(responseException); } catch (Exception e) { RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode()); elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException); elasticsearchException.addSuppressed(e); } } return elasticsearchException; }
/** * Wrap a {@linkplain ResponseException} with another one with the current * stack trace. This is used during synchronous calls so that the caller * ends up in the stack trace of the exception thrown. */ ResponseException(ResponseException e) throws IOException { super(e.getMessage(), e); this.response = e.getResponse(); }
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage());
private void handleResponseException(ResponseException e) throws DataException { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug(e.getMessage()); } else { throw new DataException(e); } } }
return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); Log.e("UnsupportedEncodingException", re.getMessage());
/** * Create an index * @param index index name * @param ignoreErrors don't fail if the index already exists * @param indexSettings index settings if any * @throws IOException In case of error */ public void createIndex(String index, boolean ignoreErrors, String indexSettings) throws IOException { logger.debug("create index [{}]", index); logger.trace("index settings: [{}]", indexSettings); try { Response response = lowLevelClient.performRequest("PUT", "/" + index, Collections.emptyMap(), createEntity(indexSettings)); logger.trace("create index response: {}", asMap(response)); } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 400 && (e.getMessage().contains("index_already_exists_exception") || // ES 5.x e.getMessage().contains("IndexAlreadyExistsException") )) { // ES 1.x and 2.x if (!ignoreErrors) { throw new RuntimeException("index already exists"); } logger.trace("index already exists. Ignoring error..."); return; } throw e; } }
return getResponse("Internal Error IO Exception: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } catch (NotFoundException nfe) { return new NanoHTTPD.Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found");
/** * Delete document from the index. * * @param id * of the document * @param forceRefresh * force index refresh - if true, time of execution is longer but * object is right after that available for display */ void deleteDocument(Integer id, boolean forceRefresh) throws IOException, CustomResponseException { try { restClient.performRequest(HttpMethod.DELETE, "/" + this.getIndex() + "/" + this.getType() + "/" + id, getParameters(forceRefresh)); } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug(e.getMessage()); } else { throw new CustomResponseException(e.getMessage()); } } }
return getResponse("Internal Error IO Exception: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
/** * Converts a {@link ResponseException} obtained from the low level REST client into an {@link ElasticsearchException}. * If a response body was returned, tries to parse it as an error returned from Elasticsearch. * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link ElasticsearchStatusException} * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing. */ protected final ElasticsearchStatusException parseResponseException(ResponseException responseException) { Response response = responseException.getResponse(); HttpEntity entity = response.getEntity(); ElasticsearchStatusException elasticsearchException; if (entity == null) { elasticsearchException = new ElasticsearchStatusException( responseException.getMessage(), RestStatus.fromCode(response.getStatusLine().getStatusCode()), responseException); } else { try { elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent); elasticsearchException.addSuppressed(responseException); } catch (Exception e) { RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode()); elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException); elasticsearchException.addSuppressed(e); } } return elasticsearchException; }