Java Code Examples for org.elasticsearch.rest.RestStatus#OK
The following examples show how to use
org.elasticsearch.rest.RestStatus#OK .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSearchDataReader.java From pacbot with Apache License 2.0 | 6 votes |
/** * @param indexNameFromRuleParam * @param autoFixPlanType * @param resourceId * @return * @throws IOException */ public String searchDocument(String indexNameFromRuleParam, String autoFixPlanType, String resourceId) throws IOException { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termQuery(ESUtils.convertAttributetoKeyword("resourceId"), resourceId)); SearchRequest searchRequest = new SearchRequest(indexNameFromRuleParam); searchRequest.types(autoFixPlanType); searchRequest.source(sourceBuilder); logger.debug("searching auto fix plan with query " ,searchRequest.toString()); SearchResponse response = client.search(searchRequest); SearchHits hits = response.getHits(); if(RestStatus.OK==response.status() && hits.totalHits>0){ return hits.getAt(0).getSourceAsString(); }else{ throw new IOException(String.format("no plan found for resource %s",resourceId)); } }
Example 2
Source File: CreateRepositoryAnalyzer.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public CreateRepositoryAnalyzedStatement visitCreateRepository(CreateRepository node, Analysis context) { // Add SQL Authentication // GaoPan 2016/06/16 AuthResult authResult = AuthService.sqlAuthenticate(context.parameterContext().getLoginUserContext(), VirtualTableNames.sys.name(), VirtualTableNames.repositories.name(), PrivilegeType.READ_WRITE); if (authResult.getStatus() != RestStatus.OK) { throw new NoPermissionException(authResult.getStatus().getStatus(), authResult.getMessage()); } String repositoryName = node.repository(); if (repositoryService.getRepository(repositoryName) != null) { throw new RepositoryAlreadyExistsException(repositoryName); } Settings settings = repositoryParamValidator.convertAndValidate( node.type(), node.properties(), context.parameterContext()); return new CreateRepositoryAnalyzedStatement(repositoryName, node.type(), settings); }
Example 3
Source File: ShowCreateTableAnalyzer.java From Elasticsearch with Apache License 2.0 | 6 votes |
public ShowCreateTableAnalyzedStatement analyze(Table table, Analysis analysis) { TableInfo tableInfo = schemas.getTableInfo(TableIdent.of(table, analysis.parameterContext().defaultSchema())); if (!(tableInfo instanceof DocTableInfo)) { throw new UnsupportedOperationException("Table must be a doc table"); } // Add SQL Authentication // GaoPan 2016/06/16 AuthResult authResult = AuthService.sqlAuthenticate(analysis.parameterContext().getLoginUserContext(), tableInfo.ident().schema(), tableInfo.ident().name(), PrivilegeType.READ_ONLY); if (authResult.getStatus() != RestStatus.OK) { throw new NoPermissionException(authResult.getStatus().getStatus(), authResult.getMessage()); } return new ShowCreateTableAnalyzedStatement((DocTableInfo) tableInfo); }
Example 4
Source File: FactSearchManager.java From act-platform with ISC License | 6 votes |
/** * Search for Facts indexed in ElasticSearch by a given search criteria. Only Facts satisfying the search criteria * will be returned. Returns a result container which will stream out the results from ElasticSearch. It will not * limit the number of returned results. This must be done by the caller. Returns an empty result container if no * Fact satisfies the search criteria. * <p> * Both 'currentUserID' (identifying the calling user) and 'availableOrganizationID' (identifying the Organizations * the calling user has access to) must be set in the search criteria in order to apply access control to Facts. Only * Facts accessible to the calling user will be returned. * * @param criteria Search criteria to match against Facts * @return Facts satisfying search criteria wrapped inside a result container */ public ScrollingSearchResult<FactDocument> searchFacts(FactSearchCriteria criteria) { if (criteria == null) return ScrollingSearchResult.<FactDocument>builder().build(); SearchResponse response; try { response = clientFactory.getClient().search(buildFactsSearchRequest(criteria), RequestOptions.DEFAULT); } catch (ElasticsearchException | IOException ex) { throw logAndExit(ex, "Could not perform request to search for Facts."); } if (response.status() != RestStatus.OK) { LOGGER.warning("Could not search for Facts (response code %s).", response.status()); return ScrollingSearchResult.<FactDocument>builder().build(); } LOGGER.info("Successfully initiated streaming of search results. Start fetching data."); return ScrollingSearchResult.<FactDocument>builder() .setInitialBatch(createFactsBatch(response)) .setFetchNextBatch(this::fetchNextFactsBatch) .setCount((int) response.getHits().getTotalHits()) .build(); }
Example 5
Source File: FactSearchManager.java From act-platform with ISC License | 6 votes |
/** * Search for Objects indexed in ElasticSearch by a given search criteria. Only Objects satisfying the search criteria * will be returned. Returns an empty result container if no Object satisfies the search criteria. * <p> * First, the result will be reduced to only the Facts satisfying the search criteria. Then, for all matching Facts * the bound Objects will be reduced to the unique Objects satisfying the search criteria. * <p> * Both 'currentUserID' (identifying the calling user) and 'availableOrganizationID' (identifying the Organizations * the calling user has access to) must be set in the search criteria in order to apply access control to Facts. Only * Objects bound to Facts accessible to the calling user will be returned. * * @param criteria Search criteria to match against Facts and their bound Objects * @return Objects satisfying search criteria wrapped inside a result container */ public SearchResult<ObjectDocument> searchObjects(FactSearchCriteria criteria) { if (criteria == null) return SearchResult.<ObjectDocument>builder().build(); SearchResponse response; try { response = clientFactory.getClient().search(buildObjectsSearchRequest(criteria), RequestOptions.DEFAULT); } catch (ElasticsearchException | IOException ex) { throw logAndExit(ex, "Could not perform request to search for Objects."); } if (response.status() != RestStatus.OK) { LOGGER.warning("Could not search for Objects (response code %s).", response.status()); return SearchResult.<ObjectDocument>builder().setLimit(criteria.getLimit()).build(); } int count = retrieveSearchObjectsResultCount(response); List<ObjectDocument> result = retrieveSearchObjectsResultValues(response); LOGGER.info("Successfully retrieved %d Objects from a total of %d matching Objects.", result.size(), count); return SearchResult.<ObjectDocument>builder() .setLimit(criteria.getLimit()) .setCount(count) .setValues(result) .build(); }
Example 6
Source File: AuthService.java From Elasticsearch with Apache License 2.0 | 6 votes |
public static AuthResult checkWhiteList(String user, Set<String> addrs, Set<String> ipWhiteList) { for (String addr : addrs) { String userAndIp = user + "@" + addr; try { if (!userIpCache.get(userAndIp)) { boolean addrInWhiteList = false; for (String ip : ipWhiteList) { if (matchIP(addr, ip)) { addrInWhiteList = true; userIpCache.put(userAndIp, true); break; } } if (!addrInWhiteList) { return new AuthResult(RestStatus.UNAUTHORIZED, "proxy or source address is not in whitelist: " + addr); } } } catch (Exception e) { return new AuthResult(RestStatus.FORBIDDEN, "load cache occurs exceptions"); } } return new AuthResult(RestStatus.OK, null); }
Example 7
Source File: GetIndexRequestRestListener.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Override public RestResponse buildResponse(GetIndexResponse getIndexResponse, XContentBuilder builder) throws Exception { GetIndexRequest.Feature[] features = getIndexRequest.features(); String[] indices = getIndexResponse.indices(); builder.startObject(); for (String index : indices) { builder.startObject(index); for (GetIndexRequest.Feature feature : features) { switch (feature) { case ALIASES: writeAliases(getIndexResponse.aliases().get(index), builder, channel.request()); break; case MAPPINGS: writeMappings(getIndexResponse.mappings().get(index), builder, channel.request()); break; case SETTINGS: writeSettings(getIndexResponse.settings().get(index), builder, channel.request()); break; default: throw new IllegalStateException("feature [" + feature + "] is not valid"); } } builder.endObject(); } builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); }
Example 8
Source File: AlterTableAddColumnAnalyzer.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public AddColumnAnalyzedStatement visitAlterTableAddColumnStatement(AlterTableAddColumn node, Analysis analysis) { AddColumnAnalyzedStatement statement = new AddColumnAnalyzedStatement(schemas); setTableAndPartitionName(node.table(), statement, analysis.parameterContext()); // Add SQL Authentication // GaoPan 2016/06/16 AuthResult authResult = AuthService.sqlAuthenticate(analysis.parameterContext().getLoginUserContext(), statement.table().ident().schema(), statement.table().ident().name(), PrivilegeType.READ_WRITE); if (authResult.getStatus() != RestStatus.OK) { throw new NoPermissionException(authResult.getStatus().getStatus(), authResult.getMessage()); } statement.analyzedTableElements(TableElementsAnalyzer.analyze( node.tableElement(), analysis.parameterContext(), fulltextAnalyzerResolver )); for (AnalyzedColumnDefinition column : statement.analyzedTableElements().columns()) { ensureColumnLeafsAreNew(column, statement.table()); } addExistingPrimaryKeys(statement); ensureNoIndexDefinitions(statement.analyzedTableElements().columns()); statement.analyzedTableElements().finalizeAndValidate( statement.table().ident(), statement.table(), analysisMetaData, analysis.parameterContext()); int numCurrentPks = statement.table().primaryKey().size(); if (statement.table().primaryKey().contains(DocSysColumns.ID)) { numCurrentPks -= 1; } statement.newPrimaryKeys(statement.analyzedTableElements().primaryKeys().size() > numCurrentPks); statement.hasNewGeneratedColumns(statement.analyzedTableElements().hasGeneratedColumns()); return statement; }
Example 9
Source File: ElasticJoinExecutor.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
public void sendResponse(RestChannel channel){ try { XContentBuilder builder = ElasticUtils.hitsAsXContentBuilder(results,metaResults); BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, builder); channel.sendResponse(bytesRestResponse); } catch (IOException e) { e.printStackTrace(); } }
Example 10
Source File: AuthActionFilter.java From Elasticsearch with Apache License 2.0 | 5 votes |
public void apply(Task task, String action, ActionRequest request, ActionListener listener, ActionFilterChain chain) { try { // authentication ActionSourceType sourceType = ActionSourceType.getActionSourceType(action); LoginUserContext userInfoContext = request.getHeader(LoginUserContext.USER_INFO_KEY); // authentication and authorization only happened on the proxy node(the node user connected to) // for sql, the authorized value will be set to true // for rest, if (sourceType == ActionSourceType.OTHERS && userInfoContext != null && !userInfoContext.authorized() && userInfoContext.getUserProperty() != null) { AuthResult authResult = authService.authenticate(request, action); if (authResult.getStatus() != RestStatus.OK) { throw new NoPermissionException(authResult.getStatus().getStatus(), authResult.getMessage()); } userInfoContext.setAuthorized(true); } // filter other tenants in clusterState if necessary AuthService.setStateFilter(request, action); chain.proceed(task, action, request, listener); } catch (Throwable t) { logger.error("errors while check auth", t); throw t; } }
Example 11
Source File: RestDataAction.java From elasticsearch-dataformat with Apache License 2.0 | 5 votes |
private void writeResponse(final RestRequest request, final RestChannel channel, final File outputFile, final long limit, final DataContent dataContent) { if (outputFile.length() > limit) { onFailure(new ElasticsearchException("Content size is too large " + outputFile.length())); return; } try (FileInputStream fis = new FileInputStream(outputFile)) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final byte[] bytes = new byte[1024]; int len; while ((len = fis.read(bytes)) > 0) { out.write(bytes, 0, len); } final ContentType contentType = dataContent.getContentType(); final BytesRestResponse response = new BytesRestResponse( RestStatus.OK, contentType.contentType(), out.toByteArray()); response.addHeader("Content-Disposition", "attachment; filename=\"" + contentType.fileName(request) + "\""); channel.sendResponse(response); } catch (final Throwable e) { throw new ElasticsearchException("Failed to render the content.", e); } }
Example 12
Source File: JobRestHandler.java From elasticsearch-rest-command with The Unlicense | 5 votes |
@Override public RestResponse buildResponse(SearchResponse result, XContentBuilder builder) throws Exception { Response resp = new Response(); resp.type = ResponseType.SearchResponse; resp.search = result; resp.fieldNames = search.tableFieldNames; queryResultCache.put(retId, resp); Search.buildQuery(from, builder, result, logger, search.tableFieldNames, showMeta); return new BytesRestResponse(RestStatus.OK, builder); }
Example 13
Source File: ElasticsearchIndexer.java From datashare with GNU Affero General Public License v3.0 | 5 votes |
private boolean tagUntag(Project prj, String documentId, String rootDocument, Script untagScript) throws IOException { UpdateRequest update = new UpdateRequest(prj.getId(), esCfg.indexType, documentId).routing(rootDocument); update.script(untagScript); update.setRefreshPolicy(esCfg.refreshPolicy); UpdateResponse updateResponse = client.update(update); return updateResponse.status() == RestStatus.OK && updateResponse.getResult() == DocWriteResponse.Result.UPDATED; }
Example 14
Source File: ESUtils.java From Stargraph with MIT License | 5 votes |
static void check(SearchResponse response) { if (response.status() != RestStatus.OK) { throw new StarGraphException("ES failure. RestStatus is " + response.status()); } if (response.getFailedShards() > 0) { throw new StarGraphException("Shard Search Failure."); } }
Example 15
Source File: XmlFilter.java From elasticsearch-xml with Apache License 2.0 | 5 votes |
@Override public void sendResponse(RestResponse response) { if (!response.status().equals(RestStatus.OK)) { channel.sendResponse(response); } if (isXml(request)) { XContentParser parser = null; try { String string = response.content().toUtf8(); // takes some space ... :( XContentType xContentType = XContentFactory.xContentType(string); parser = XContentFactory.xContent(xContentType).createParser(string); parser.nextToken(); XmlXContentBuilder builder = XmlXContentFactory.xmlBuilder(params); if (request.paramAsBoolean("pretty", false)) { builder.prettyPrint(); } builder.copyCurrentStructure(parser); BytesRestResponse restResponse = new BytesRestResponse(RestStatus.OK, "text/xml; charset=UTF-8", builder.bytes()); channel.sendResponse(restResponse); return; } catch (Throwable e) { logger.error(e.getMessage(), e); channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, e.getMessage())); return; } finally { if (parser != null) { parser.close(); } } } channel.sendResponse(response); }
Example 16
Source File: RestoreBackupManager.java From Raigad with Apache License 2.0 | 4 votes |
public void runRestore(String sourceRepositoryName, String repositoryType, String snapshotName, String indices, String renamePattern, String renameReplacement) throws Exception { Client esTransportClient = ElasticsearchTransportClient.instance(config).getTransportClient(); // Get Repository Name : This will serve as BasePath Suffix String sourceRepoName = StringUtils.isBlank(sourceRepositoryName) ? config.getRestoreRepositoryName() : sourceRepositoryName; if (StringUtils.isBlank(sourceRepoName)) throw new RestoreBackupException("Repository Name is Null or Empty"); //Attach suffix to the repository name so that it does not conflict with Snapshot Repository name String restoreRepositoryName = sourceRepoName + SUFFIX_SEPARATOR_TAG + config.getRestoreSourceClusterName(); String repoType = StringUtils.isBlank(repositoryType) ? config.getRestoreRepositoryType().toLowerCase() : repositoryType; if (StringUtils.isBlank(repoType)) { logger.info("RepositoryType is empty, hence Defaulting to <s3> type"); repoType = AbstractRepository.RepositoryType.s3.name(); } if (!repository.doesRepositoryExists(restoreRepositoryName, AbstractRepository.RepositoryType.valueOf(repoType.toLowerCase()))) { //If repository does not exist, create new one repository.createRestoreRepository(restoreRepositoryName, sourceRepoName); } // Get Snapshot Name String snapshotN = StringUtils.isBlank(snapshotName) ? config.getRestoreSnapshotName() : snapshotName; if (StringUtils.isBlank(snapshotN)) { //Pick the last Snapshot from the available Snapshots List<String> snapshots = ElasticsearchUtils.getAvailableSnapshots(esTransportClient, restoreRepositoryName); if (snapshots.isEmpty()) throw new RestoreBackupException("No available snapshots in <" + restoreRepositoryName + "> repository."); //Sorting Snapshot names in Reverse Order Collections.sort(snapshots, Collections.reverseOrder()); //Use the Last available snapshot snapshotN = snapshots.get(0); } logger.info("Snapshot Name : <" + snapshotN + ">"); // Get Names of Indices String commaSeparatedIndices = StringUtils.isBlank(indices) ? config.getCommaSeparatedIndicesToRestore() : indices; if (StringUtils.isBlank(commaSeparatedIndices) || commaSeparatedIndices.equalsIgnoreCase(ALL_INDICES_TAG)) { commaSeparatedIndices = null; logger.info("Restoring all Indices."); } logger.info("Indices param : <" + commaSeparatedIndices + ">"); RestoreSnapshotResponse restoreSnapshotResponse = getRestoreSnapshotResponse(esTransportClient, commaSeparatedIndices, restoreRepositoryName, snapshotN, renamePattern, renameReplacement); logger.info("Restore Status = " + restoreSnapshotResponse.status().toString()); if (restoreSnapshotResponse.status() == RestStatus.OK) { printRestoreDetails(restoreSnapshotResponse); } else if (restoreSnapshotResponse.status() == RestStatus.INTERNAL_SERVER_ERROR) logger.info("Restore Completely Failed"); }
Example 17
Source File: MysqlProto.java From Elasticsearch with Apache License 2.0 | 4 votes |
private static boolean authenticate(ConnectContext context, byte[] scramble, byte[] randomString, String user) { String usePass = scramble.length == 0 ? "NO" : "YES"; if (user == null || user.isEmpty()) { ErrorReport.report(ErrorCode.ERR_ACCESS_DENIED_ERROR, "", usePass); logger.warn(" user == null"); return false; } Client nodeClient = BootstrapProxy.getNodeClient(); ShowUserPropertyRequest request = new ShowUserPropertyRequest(user); ShowUserPropertyResponse userPropertyResponse = nodeClient.execute(ShowUserPropertyAction.INSTANCE, request).actionGet(); String userPassword = userPropertyResponse.getUserProperty().getPassword(); if (userPassword == null) { ErrorReport.report(ErrorCode.ERR_ACCESS_DENIED_ERROR, user, usePass); return false; } byte[] mysqlPass = MysqlPassword.getSaltFromPassword(userPassword.getBytes()); // when the length of password is zero, the user has no password if ((scramble.length == mysqlPass.length) && (scramble.length == 0 || MysqlPassword.checkScramble(scramble, randomString, mysqlPass))) { // authenticate success context.setUser(user); } else { // password check failed. ErrorReport.report(ErrorCode.ERR_ACCESS_DENIED_ERROR, user, usePass); return false; } String sourceAddrs = context.getMysqlChannel().getRemote().split(":")[0]; long startTimestamp = System.currentTimeMillis(); if (!UserProperty.getUsernameWithoutTenantFromFullUsername(user).equalsIgnoreCase(UserProperty.ROOT_NAME)) { Set<String> addrs = new HashSet<>(); addrs.add(sourceAddrs); AuthResult whiteListResult = AuthService.checkWhiteList(user, addrs, userPropertyResponse.getUserProperty().getHostIpWhiteList()); if (whiteListResult.getStatus() != RestStatus.OK) { // whitelist check failed. ErrorReport.report(ErrorCode.ERR_IP_ACCESS_DENIED, sourceAddrs, usePass); String message = "Login denied, ip not in whitelist:" + userPropertyResponse.getUserProperty().getHostIpWhiteList().toString(); auditLog.log(startTimestamp, user, sourceAddrs, "login", 0, message); return false; } } auditLog.log(startTimestamp, user, sourceAddrs, "login", 0, "mysql authenticate successfully"); return true; }
Example 18
Source File: ClusterHealthResponse.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public RestStatus status() { return isTimedOut() ? RestStatus.REQUEST_TIMEOUT : RestStatus.OK; }
Example 19
Source File: InsertFromValuesAnalyzer.java From Elasticsearch with Apache License 2.0 | 4 votes |
public AnalyzedStatement analyze(InsertFromValues node, Analysis analysis) { analysis.expectsAffectedRows(true); DocTableInfo tableInfo = analysisMetaData.schemas().getWritableTable( TableIdent.of(node.table(), analysis.parameterContext().defaultSchema())); DocTableRelation tableRelation = new DocTableRelation(tableInfo); // Add SQL Authentication // GaoPan 2016/06/16 AuthResult authResult = AuthService.sqlAuthenticate(analysis.parameterContext().getLoginUserContext(), tableInfo.ident().schema(), tableInfo.ident().name(), PrivilegeType.READ_WRITE); if (authResult.getStatus() != RestStatus.OK) { throw new NoPermissionException(authResult.getStatus().getStatus(), authResult.getMessage()); } FieldProvider fieldProvider = new NameFieldProvider(tableRelation); ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(analysisMetaData, analysis.parameterContext(), fieldProvider, tableRelation); ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext(); expressionAnalyzer.resolveWritableFields(true); ValuesResolver valuesResolver = new ValuesResolver(tableRelation); ExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer( analysisMetaData, analysis.parameterContext(), fieldProvider, valuesResolver); InsertFromValuesAnalyzedStatement statement = new InsertFromValuesAnalyzedStatement( tableInfo, analysis.parameterContext().hasBulkParams()); handleInsertColumns(node, node.maxValuesLength(), statement); Set<ReferenceInfo> allReferencedReferences = new HashSet<>(); for (GeneratedReferenceInfo generatedReferenceInfo : tableInfo.generatedColumns()) { allReferencedReferences.addAll(generatedReferenceInfo.referencedReferenceInfos()); } ReferenceToLiteralConverter.Context referenceToLiteralContext = new ReferenceToLiteralConverter.Context( statement.columns(), allReferencedReferences); ValueNormalizer valuesNormalizer = new ValueNormalizer(analysisMetaData.schemas(), new EvaluatingNormalizer( analysisMetaData.functions(), RowGranularity.CLUSTER, analysisMetaData.referenceResolver(), tableRelation, false)); for (ValuesList valuesList : node.valuesLists()) { analyzeValues( tableRelation, valuesNormalizer, expressionAnalyzer, expressionAnalysisContext, valuesResolver, valuesAwareExpressionAnalyzer, valuesList, node.onDuplicateKeyAssignments(), statement, analysis.parameterContext(), referenceToLiteralContext); } return statement; }
Example 20
Source File: RestoreInfo.java From crate with Apache License 2.0 | 2 votes |
/** * REST status of the operation * * @return REST status */ public RestStatus status() { return RestStatus.OK; }