ServerCallImpl extends ServerCall<RespT> { @Override public void sendHeaders(Metadata headers) { checkState(!sendHeadersCalled, "sendHeaders has already been called"); checkState(!closeCalled, "call is closed"); headers.removeAll(MESSAGE_ENCODING_KEY); if (compressor == null) { compressor = Codec.Identity.NONE; if (inboundHeaders.containsKey(MESSAGE_ACCEPT_ENCODING_KEY)) { String acceptEncodings = inboundHeaders.get(MESSAGE_ACCEPT_ENCODING_KEY); for (String acceptEncoding : ACCEPT_ENCODING_SPLITER.split(acceptEncodings)) { Compressor c = compressorRegistry.lookupCompressor(acceptEncoding); if (c != null) { compressor = c; break; } } } } else { if (inboundHeaders.containsKey(MESSAGE_ACCEPT_ENCODING_KEY)) { String acceptEncodings = inboundHeaders.get(MESSAGE_ACCEPT_ENCODING_KEY); List<String> acceptedEncodingsList = ACCEPT_ENCODING_SPLITER.splitToList(acceptEncodings); if (!acceptedEncodingsList.contains(compressor.getMessageEncoding())) { compressor = Codec.Identity.NONE; } } else { compressor = Codec.Identity.NONE; } } inboundHeaders = null; if (compressor != Codec.Identity.NONE) { headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding()); } stream.setCompressor(compressor); headers.removeAll(MESSAGE_ACCEPT_ENCODING_KEY); Set<String> acceptEncodings = decompressorRegistry.getAdvertisedMessageEncodings(); if (!acceptEncodings.isEmpty()) { headers.put(MESSAGE_ACCEPT_ENCODING_KEY, ACCEPT_ENCODING_JOINER.join(acceptEncodings)); } sendHeadersCalled = true; stream.writeHeaders(headers); } ServerCallImpl(ServerStream stream, MethodDescriptor<ReqT, RespT> method,
      Metadata inboundHeaders, Context.CancellableContext context,
      DecompressorRegistry decompressorRegistry, CompressorRegistry compressorRegistry); @Override void request(int numMessages); @Override void sendHeaders(Metadata headers); @Override void sendMessage(RespT message); @Override void setMessageCompression(boolean enable); @Override void setCompression(String compressorName); @Override boolean isReady(); @Override void close(Status status, Metadata trailers); @Override boolean isCancelled(); @Override Attributes attributes();  }
@Test public void sendHeader_firstCall() { Metadata headers = new Metadata(); call.sendHeaders(headers); verify(stream).writeHeaders(headers); }
@Test public void sendHeader_failsOnSecondCall() { call.sendHeaders(new Metadata()); thrown.expect(IllegalStateException.class); thrown.expectMessage("sendHeaders has already been called"); call.sendHeaders(new Metadata()); }