Search in sources :

Example 1 with JsonWriter

use of com.yahoo.document.json.JsonWriter in project vespa by vespa-engine.

the class OperationHandlerImpl method get.

@Override
public Optional<String> get(RestUri restUri, Optional<String> fieldSet) throws RestApiException {
    SyncSession syncSession = syncSessions.alloc();
    setRoute(syncSession, Optional.empty());
    try {
        DocumentId id = new DocumentId(restUri.generateFullId());
        final Document document = syncSession.get(id, fieldSet.orElse(restUri.getDocumentType() + ":[document]"), DocumentProtocol.Priority.NORMAL_1);
        if (document == null) {
            return Optional.empty();
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        JsonWriter jsonWriter = new JsonWriter(outputStream);
        jsonWriter.write(document);
        return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
    } catch (Exception e) {
        throw new RestApiException(Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED));
    } finally {
        syncSessions.free(syncSession);
    }
}
Also used : DocumentId(com.yahoo.document.DocumentId) SyncSession(com.yahoo.documentapi.SyncSession) MessageBusSyncSession(com.yahoo.documentapi.messagebus.MessageBusSyncSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.yahoo.document.Document) JsonWriter(com.yahoo.document.json.JsonWriter) DocumentAccessException(com.yahoo.documentapi.DocumentAccessException)

Aggregations

Document (com.yahoo.document.Document)1 DocumentId (com.yahoo.document.DocumentId)1 JsonWriter (com.yahoo.document.json.JsonWriter)1 DocumentAccessException (com.yahoo.documentapi.DocumentAccessException)1 SyncSession (com.yahoo.documentapi.SyncSession)1 MessageBusSyncSession (com.yahoo.documentapi.messagebus.MessageBusSyncSession)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1