/** * Sets the value of response headers after making sure that the response metadata is not already * sent. * * @param headerName The name of the header. * @param headerValue The intended value of the header. * @throws IllegalArgumentException if any of {@code headerName} or {@code headerValue} is null. * @throws IllegalStateException if response metadata has already been written to the channel. */ private void setResponseHeader(String headerName, Object headerValue) { if (headerName != null && headerValue != null) { long startTime = System.currentTimeMillis(); if (headerValue instanceof Date) { HttpHeaders.setDateHeader(responseMetadata, headerName, (Date) headerValue); } else { HttpHeaders.setHeader(responseMetadata, headerName, headerValue); } if (responseMetadataWritten.get()) { nettyMetrics.deadResponseAccessError.inc(); throw new IllegalStateException( "Response metadata changed after it has already been written to the channel"); } else { logger.trace( "Header {} set to {} for channel {}", headerName, responseMetadata.headers().get(headerName), ctx.channel()); nettyMetrics.headerSetTimeInMs.update(System.currentTimeMillis() - startTime); } } else { throw new IllegalArgumentException( "Header name [" + headerName + "] or header value [" + headerValue + "] null"); } }