/** * Construct a header to be prepended to the actual content. This implementation writes * {@code Content-Length} and {@code Content-Type} attributes according to the LSP specification. */ protected String getHeader(int contentLength) { StringBuilder headerBuilder = new StringBuilder(); appendHeader(headerBuilder, CONTENT_LENGTH_HEADER, contentLength).append(CRLF); if (!StandardCharsets.UTF_8.name().equals(encoding)) { appendHeader(headerBuilder, CONTENT_TYPE_HEADER, JSON_MIME_TYPE); headerBuilder.append("; charset=").append(encoding).append(CRLF); } headerBuilder.append(CRLF); return headerBuilder.toString(); }
@Override public void consume(Message message) { try { String content = jsonHandler.serialize(message); byte[] contentBytes = content.getBytes(encoding); int contentLength = contentBytes.length; String header = getHeader(contentLength); byte[] headerBytes = header.getBytes(StandardCharsets.US_ASCII); synchronized (outputLock) { output.write(headerBytes); output.write(contentBytes); output.flush(); } } catch (IOException exception) { throw new JsonRpcException(exception); } }
/** * Create the remote endpoint that communicates with the local services. */ protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { MessageConsumer outgoingMessageStream = new StreamMessageConsumer(output, jsonHandler); outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); RemoteEndpoint remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, ServiceEndpoints.toEndpoint(localServices)); jsonHandler.setMethodProvider(remoteEndpoint); return remoteEndpoint; } }
/** * Create the remote endpoint that communicates with the local services. */ protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { MessageConsumer outgoingMessageStream = new StreamMessageConsumer(output, jsonHandler); outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); RemoteEndpoint remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, ServiceEndpoints.toEndpoint(localServices)); jsonHandler.setMethodProvider(remoteEndpoint); return remoteEndpoint; } }
@Override protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { MessageConsumer outgoingMessageStream = new StreamMessageConsumer(output, jsonHandler); outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); RemoteEndpoint remoteEndpoint = new DebugRemoteEndpoint(outgoingMessageStream, ServiceEndpoints.toEndpoint(localServices)); jsonHandler.setMethodProvider(remoteEndpoint); return remoteEndpoint; }
/** * Construct a header to be prepended to the actual content. This implementation writes * {@code Content-Length} and {@code Content-Type} attributes according to the LSP specification. */ protected String getHeader(int contentLength) { StringBuilder headerBuilder = new StringBuilder(); appendHeader(headerBuilder, CONTENT_LENGTH_HEADER, contentLength).append(CRLF); if (!StandardCharsets.UTF_8.name().equals(encoding)) { appendHeader(headerBuilder, CONTENT_TYPE_HEADER, JSON_MIME_TYPE); headerBuilder.append("; charset=").append(encoding).append(CRLF); } headerBuilder.append(CRLF); return headerBuilder.toString(); }
@Override public void consume(Message message) { try { String content = jsonHandler.serialize(message); byte[] contentBytes = content.getBytes(encoding); int contentLength = contentBytes.length; String header = getHeader(contentLength); byte[] headerBytes = header.getBytes(StandardCharsets.US_ASCII); synchronized (outputLock) { output.write(headerBytes); output.write(contentBytes); output.flush(); } } catch (IOException exception) { throw new JsonRpcException(exception); } }