in alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/ConnectionSocket.java [216:259]
private void metadataExchange(SSLSocket socket) throws IOException {
logger.debug("Metadata exchange initiated.");
MetadataExchangeRequest.AuthType authType = MetadataExchangeRequest.AuthType.DB_NATIVE;
if (connectionConfig.getAuthType().equals(AuthType.IAM)) {
authType = MetadataExchangeRequest.AuthType.AUTO_IAM;
}
String tokenValue = accessTokenSupplier.getTokenValue();
MetadataExchangeRequest request =
MetadataExchangeRequest.newBuilder()
.setAuthType(authType)
.setOauth2Token(tokenValue)
.setUserAgent(userAgents)
.build();
// Write data to the server.
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
out.writeInt(request.getSerializedSize());
out.write(request.toByteArray());
out.flush();
// Set timeout for read.
socket.setSoTimeout(IO_TIMEOUT_MS);
// Read data from the server.
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
int respSize = in.readInt();
byte[] respData = new byte[respSize];
in.readFully(respData);
// Clear the timeout.
socket.setSoTimeout(0);
// Parse the response and raise a RuntimeException if it is not OK.
MetadataExchangeResponse response = MetadataExchangeResponse.parseFrom(respData);
if (response == null || !response.getResponseCode().equals(ResponseCode.OK)) {
throw new RuntimeException(
response != null ? response.getError() : "Metadata exchange response is null.");
}
logger.debug("Metadata exchange completed successfully.");
}