Java Code Examples for com.cedarsoftware.util.io.JsonWriter#formatJson()

The following examples show how to use com.cedarsoftware.util.io.JsonWriter#formatJson() . 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: IndexesResource.java    From lumongo with Apache License 2.0 6 votes vote down vote up
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

	try {
		Lumongo.GetIndexesResponse getIndexesResponse = indexManager.getIndexes(Lumongo.GetIndexesRequest.newBuilder().build());

		Document mongoDocument = new org.bson.Document();
		mongoDocument.put("indexes", getIndexesResponse.getIndexNameList());
		String docString = JSONSerializers.getStrict().serialize(mongoDocument);

		if (pretty) {
			docString = JsonWriter.formatJson(docString);
		}

		return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

	}
	catch (Exception e) {
		return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get index names: " + e.getMessage()).build();
	}

}
 
Example 2
Source File: IndexResource.java    From lumongo with Apache License 2.0 5 votes vote down vote up
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.INDEX) String index, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

	try {
		StringBuilder responseBuilder = new StringBuilder();

		IndexConfig indexConfig = indexManager.getIndexConfig(index);

		responseBuilder.append("{");
		responseBuilder.append("\"indexName\": ");
		responseBuilder.append("\"");
		responseBuilder.append(indexConfig.getIndexName());
		responseBuilder.append("\"");
		responseBuilder.append(",");
		responseBuilder.append("\"numberOfSegments\": ");
		responseBuilder.append(indexConfig.getNumberOfSegments());
		responseBuilder.append(",");
		responseBuilder.append("\"indexSettings\": ");
		JsonFormat.Printer printer = JsonFormat.printer();
		responseBuilder.append(printer.print(indexConfig.getIndexSettings()));
		responseBuilder.append("}");

		String docString = responseBuilder.toString();

		if (pretty) {
			docString = JsonWriter.formatJson(docString);
		}

		return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

	}
	catch (Exception e) {
		return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get index names: " + e.getMessage()).build();
	}

}
 
Example 3
Source File: FieldsResource.java    From lumongo with Apache License 2.0 5 votes vote down vote up
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.INDEX) final String indexName,
		@QueryParam(LumongoConstants.PRETTY) boolean pretty) {

	if (indexName != null) {

		Lumongo.GetFieldNamesRequest fieldNamesRequest = Lumongo.GetFieldNamesRequest.newBuilder().setIndexName(indexName).build();

		Lumongo.GetFieldNamesResponse fieldNamesResponse;

		try {
			fieldNamesResponse = indexManager.getFieldNames(fieldNamesRequest);

			Document mongoDocument = new Document();
			mongoDocument.put("index", indexName);
			mongoDocument.put("fields", fieldNamesResponse.getFieldNameList());

			String docString = JSONSerializers.getStrict().serialize(mongoDocument);

			if (pretty) {
				docString = JsonWriter.formatJson(docString);
			}

			return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

		}
		catch (Exception e) {
			return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to fetch fields for index <" + indexName + ">: " + e.getMessage())
					.build();
		}
	}
	else {
		return Response.status(LumongoConstants.INTERNAL_ERROR).entity("No index defined").build();
	}

}
 
Example 4
Source File: StatsResource.java    From lumongo with Apache License 2.0 5 votes vote down vote up
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

	try {

		Document mongoDocument = new Document();

		mongoDocument.put("indexBlockSize", indexManager.getClusterConfig().getIndexBlockSize());
		mongoDocument.put("maxIndexBlockCount", indexManager.getClusterConfig().getMaxIndexBlocks());
		mongoDocument.put("currentIndexBlockCount", MongoFile.getCacheSize());

		Runtime runtime = Runtime.getRuntime();

		mongoDocument.put("jvmUsedMemoryMB", (runtime.totalMemory() - runtime.freeMemory()) / MB);
		mongoDocument.put("jvmFreeMemoryMB", runtime.freeMemory() / MB);
		mongoDocument.put("jvmTotalMemoryMB", runtime.totalMemory() / MB);
		mongoDocument.put("jvmMaxMemoryMB", runtime.maxMemory() / MB);

		String docString = JSONSerializers.getStrict().serialize(mongoDocument);

		if (pretty) {
			docString = JsonWriter.formatJson(docString);
		}

		return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

	}
	catch (Exception e) {
		return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get cluster membership: " + e.getMessage()).build();
	}

}
 
Example 5
Source File: ETL.java    From WhiteRabbit with Apache License 2.0 4 votes vote down vote up
public String toJson() {
	return JsonWriter.formatJson(JsonWriter.objectToJson(this));
}
 
Example 6
Source File: MembersResource.java    From lumongo with Apache License 2.0 4 votes vote down vote up
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

	try {
		Lumongo.GetMembersResponse getMembersResponse = indexManager.getMembers(Lumongo.GetMembersRequest.newBuilder().build());

		org.bson.Document mongoDocument = new org.bson.Document();

		List<Document> memberObjList = new ArrayList<>();
		for (Lumongo.LMMember lmMember : getMembersResponse.getMemberList()) {
			Document memberObj = new Document();
			memberObj.put("serverAddress", lmMember.getServerAddress());
			memberObj.put("hazelcastPort", lmMember.getHazelcastPort());
			memberObj.put("internalPort", lmMember.getInternalPort());
			memberObj.put("externalPort", lmMember.getExternalPort());

			Document indexMappingObj = new Document();
			for (Lumongo.IndexMapping indexMapping : getMembersResponse.getIndexMappingList()) {

				TreeSet<Integer> segments = new TreeSet<>();
				for (Lumongo.SegmentMapping segmentMapping : indexMapping.getSegmentMappingList()) {
					if (segmentMapping.getMember().equals(lmMember)) {
						segments.add(segmentMapping.getSegmentNumber());
					}
				}

				indexMappingObj.put(indexMapping.getIndexName(), segments);
			}
			memberObj.put("indexMapping", indexMappingObj);


			memberObjList.add(memberObj);

		}

		mongoDocument.put("members", memberObjList);

		String docString = JSONSerializers.getStrict().serialize(mongoDocument);

		if (pretty) {
			docString = JsonWriter.formatJson(docString);
		}

		return Response.status(LumongoConstants.SUCCESS).entity(docString).build();

	}
	catch (Exception e) {
		return Response.status(LumongoConstants.INTERNAL_ERROR).entity("Failed to get cluster membership: " + e.getMessage()).build();
	}

}
 
Example 7
Source File: FetchResource.java    From lumongo with Apache License 2.0 4 votes vote down vote up
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response get(@Context Response response, @QueryParam(LumongoConstants.ID) final String uniqueId,
		@QueryParam(LumongoConstants.INDEX) final String indexName, @QueryParam(LumongoConstants.PRETTY) boolean pretty) {

	Lumongo.FetchRequest.Builder fetchRequest = Lumongo.FetchRequest.newBuilder();
	fetchRequest.setIndexName(indexName);
	fetchRequest.setUniqueId(uniqueId);

	Lumongo.FetchResponse fetchResponse;

	try {
		fetchResponse = indexManager.fetch(fetchRequest.build());

		if (fetchResponse.hasResultDocument()) {
			Document document = ResultHelper.getDocumentFromResultDocument(fetchResponse.getResultDocument());
			String docString;
			if (pretty) {
				docString = JSONSerializers.getLegacy().serialize(document);
			}
			else {
				docString = JSONSerializers.getStrict().serialize(document);
			}

			if (pretty) {
				docString = JsonWriter.formatJson(docString);
			}

			return Response.status(LumongoConstants.SUCCESS).entity(docString).build();
		}
		else {
			return Response.status(LumongoConstants.NOT_FOUND).entity("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">").build();
		}

	}
	catch (Exception e) {
		return Response.status(LumongoConstants.INTERNAL_ERROR)
				.entity("Failed to fetch uniqueId <" + uniqueId + "> for index <" + indexName + ">: " + e.getMessage()).build();
	}

}