private static void setTargetField(final Builder target, final Object sourceObject, final String targetField) throws IllegalArgumentException { Descriptors.FieldDescriptor fieldDescriptor = target.getDescriptorForType().findFieldByName(targetField); if (null == fieldDescriptor) { throw new RuntimeException("Unknown target field in protobuf: " + targetField); } if (fieldDescriptor.isRepeated()) { target.addRepeatedField(fieldDescriptor, sourceObject); } else { target.setField(fieldDescriptor, sourceObject); } }
/** * Process the Sasl's Negotiate request, including the optimization of * accelerating token negotiation. * @return the response to Negotiate request - the list of enabled * authMethods and challenge if the TOKENS are supported. * @throws SaslException - if attempt to generate challenge fails. * @throws IOException - if it fails to create the SASL server for Tokens */ private RpcSaslProto buildSaslNegotiateResponse() throws InterruptedException, SaslException, IOException { RpcSaslProto negotiateMessage = negotiateResponse; // accelerate token negotiation by sending initial challenge // in the negotiation response if (enabledAuthMethods.contains(AuthMethod.TOKEN)) { saslServer = createSaslServer(AuthMethod.TOKEN); byte[] challenge = saslServer.evaluateResponse(new byte[0]); RpcSaslProto.Builder negotiateBuilder = RpcSaslProto.newBuilder(negotiateResponse); negotiateBuilder.getAuthsBuilder(0) // TOKEN is always first .setChallenge(ByteString.copyFrom(challenge)); negotiateMessage = negotiateBuilder.build(); } sentNegotiate = true; return negotiateMessage; }
private RpcSaslProto buildNegotiateResponse(List<AuthMethod> authMethods) throws IOException { RpcSaslProto.Builder negotiateBuilder = RpcSaslProto.newBuilder(); if (authMethods.contains(AuthMethod.SIMPLE) && authMethods.size() == 1) { // SIMPLE-only servers return success in response to negotiate negotiateBuilder.setState(SaslState.SUCCESS); } else { negotiateBuilder.setState(SaslState.NEGOTIATE); for (AuthMethod authMethod : authMethods) { SaslRpcServer saslRpcServer = new SaslRpcServer(authMethod); SaslAuth.Builder builder = negotiateBuilder.addAuthsBuilder() .setMethod(authMethod.toString()) .setMechanism(saslRpcServer.mechanism); if (saslRpcServer.protocol != null) { builder.setProtocol(saslRpcServer.protocol); } if (saslRpcServer.serverId != null) { builder.setServerId(saslRpcServer.serverId); } } } return negotiateBuilder.build(); }
@SuppressWarnings("unchecked") private static Object buildMessage(Builder builder, Map<String, Object> fields) { Descriptor descriptor = builder.getDescriptorForType(); for (Map.Entry<String, Object> entry : fields.entrySet()) { if (entry.getValue() == null) { continue; } FieldDescriptor field = getField(descriptor, entry.getKey()); if (entry.getValue() instanceof List<?>) { List<Object> values = (List<Object>) entry.getValue(); for (Object value : values) { builder.addRepeatedField(field, buildValue(builder, field, value)); } } else { builder.setField(field, buildValue(builder, field, entry.getValue())); } } return builder.build(); }
@SuppressWarnings("unchecked") private static Object buildValue( Message.Builder parentBuilder, FieldDescriptor field, Object value) { if (field.getType() == FieldDescriptor.Type.MESSAGE) { if (field.isRepeated()) {} Message.Builder fieldBuilder = parentBuilder.newBuilderForField(field); return buildMessage(fieldBuilder, (Map<String, Object>) value); } else if (field.getType() == FieldDescriptor.Type.ENUM) { return field.getEnumType().findValueByName((String) value); } else { switch (field.getType()) { case FLOAT: // float is a special case return Float.valueOf(value.toString()); default: return value; } } }
@Test public void parserAcceptsStringForNumericField() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); mergeFromJson( "{\n" + " \"optionalInt32\": \"1234\",\n" + " \"optionalUint32\": \"5678\",\n" + " \"optionalSint32\": \"9012\",\n" + " \"optionalFixed32\": \"3456\",\n" + " \"optionalSfixed32\": \"7890\",\n" + " \"optionalFloat\": \"1.5\",\n" + " \"optionalDouble\": \"1.25\",\n" + " \"optionalBool\": \"true\"\n" + "}", builder); TestAllTypes message = builder.build(); assertEquals(1234, message.getOptionalInt32()); assertEquals(5678, message.getOptionalUint32()); assertEquals(9012, message.getOptionalSint32()); assertEquals(3456, message.getOptionalFixed32()); assertEquals(7890, message.getOptionalSfixed32()); assertEquals(1.5f, message.getOptionalFloat(), 0.000001); assertEquals(1.25, message.getOptionalDouble(), 0.000001); assertEquals(true, message.getOptionalBool()); }
@Test public void mapNullValueIsRejected() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); assertThatThrownBy( () -> mergeFromJson( "{\n" + " \"int32ToInt32Map\": {null: 1},\n" + " \"int32ToMessageMap\": {null: 2}\n" + "}", builder)) .isInstanceOf(InvalidProtocolBufferException.class); TestMap.Builder builder2 = TestMap.newBuilder(); assertThatThrownBy( () -> mergeFromJson( "{\n" + " \"int32ToInt32Map\": {\"1\": null},\n" + " \"int32ToMessageMap\": {\"2\": null}\n" + "}", builder2)) .isInstanceOf(InvalidProtocolBufferException.class); }
@Test public void anyInMaps() throws Exception { TestAny.Builder testAny = TestAny.newBuilder(); testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build())); testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build())); testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"))); testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s"))); testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz"))); Value numberValue = Value.newBuilder().setNumberValue(1.125).build(); Struct.Builder struct = Struct.newBuilder(); struct.putFields("number", numberValue); testAny.putAnyMap("struct", Any.pack(struct.build())); Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(); testAny.putAnyMap( "list_value", Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build())); testAny.putAnyMap("number_value", Any.pack(numberValue)); testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue))); testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance())); testAny.putAnyMap("default", Any.getDefaultInstance()); assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance()); }
@Test public void preservingProtoFieldNames() throws Exception { TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build(); assertMatchesUpstream(message); assertMatchesUpstream(message, false, true, false); // The json_name field option is ignored when configured to use original proto field names. TestCustomJsonName messageWithCustomJsonName = TestCustomJsonName.newBuilder().setValue(12345).build(); assertMatchesUpstream(message, false, true, false); // Parsers accept both original proto field names and lowerCamelCase names. TestAllTypes.Builder builder = TestAllTypes.newBuilder(); mergeFromJson("{\"optionalInt32\": 12345}", builder); assertEquals(12345, builder.getOptionalInt32()); builder.clear(); mergeFromJson("{\"optional_int32\": 54321}", builder); assertEquals(54321, builder.getOptionalInt32()); }
private void mergeFromJson( String json, boolean ignoringUnknownFields, Builder builder, Message... additionalTypes) throws IOException { MessageMarshaller.Builder marshallerBuilder = MessageMarshaller.builder() .register(builder.getDefaultInstanceForType()) .ignoringUnknownFields(ignoringUnknownFields); for (Message prototype : additionalTypes) { marshallerBuilder.register(prototype); } MessageMarshaller marshaller = marshallerBuilder.build(); marshaller.mergeValue(json, builder); Message.Builder builder2 = builder.build().newBuilderForType(); marshaller.mergeValue(json.getBytes(StandardCharsets.UTF_8), builder2); assertThat(builder2.build()).isEqualTo(builder.build()); Message.Builder builder3 = builder.build().newBuilderForType(); try (ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) { marshaller.mergeValue(bis, builder3); } assertThat(builder3.build()).isEqualTo(builder.build()); }
private RpcSaslProto buildSaslNegotiateResponse() throws IOException, InterruptedException { RpcSaslProto negotiateMessage = negotiateResponse; // accelerate token negotiation by sending initial challenge // in the negotiation response if (enabledAuthMethods.contains(AuthMethod.TOKEN)) { saslServer = createSaslServer(AuthMethod.TOKEN); byte[] challenge = saslServer.evaluateResponse(new byte[0]); RpcSaslProto.Builder negotiateBuilder = RpcSaslProto.newBuilder(negotiateResponse); negotiateBuilder.getAuthsBuilder(0) // TOKEN is always first .setChallenge(ByteString.copyFrom(challenge)); negotiateMessage = negotiateBuilder.build(); } sentNegotiate = true; return negotiateMessage; }
private synchronized UserInformation getUserInfo(UserGroupInformation ugi) { if (ugi == null || authMethod == AuthMethod.DIGEST) { // Don't send user for token auth return null; } UserInformation.Builder userInfoPB = UserInformation.newBuilder(); if (authMethod == AuthMethod.KERBEROS) { // Send effective user for Kerberos auth userInfoPB.setEffectiveUser(ugi.getUserName()); } else if (authMethod == AuthMethod.SIMPLE) { //Send both effective user and real user for simple auth userInfoPB.setEffectiveUser(ugi.getUserName()); if (ugi.getRealUser() != null) { userInfoPB.setRealUser(ugi.getRealUser().getUserName()); } } return userInfoPB.build(); }
@SuppressWarnings("unchecked") public <T extends Message> T convertJsonToProto(T prototype, String json, String extensionName) { try { Builder builder = prototype.newBuilderForType(); JsonFormat.parser().merge(json, builder); return (T) builder.build(); } catch (InvalidProtocolBufferException ex) { diagCollector.addDiag( Diag.error( new SimpleLocation(extensionName), "Extension %s cannot be converted into proto type %s. Details: %s", extensionName, prototype.getDescriptorForType().getFullName(), ex.getMessage())); return prototype; } }
private UserInformation getUserInfo(UserGroupInformation ugi) { if (ugi == null || authMethod == AuthMethod.DIGEST) { // Don't send user for token auth return null; } UserInformation.Builder userInfoPB = UserInformation.newBuilder(); if (authMethod == AuthMethod.KERBEROS) { // Send effective user for Kerberos auth userInfoPB.setEffectiveUser(ugi.getUserName()); } else if (authMethod == AuthMethod.SIMPLE) { //Send both effective user and real user for simple auth userInfoPB.setEffectiveUser(ugi.getUserName()); if (ugi.getRealUser() != null) { userInfoPB.setRealUser(ugi.getRealUser().getUserName()); } } return userInfoPB.build(); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { //System.out.println("channelRead"); RpcCall call = (RpcCall)msg; if(call == null) { return; } LOG.debug("RpcServer read, call ID: " + call.getCallId() + ", local server:" + ctx.channel().localAddress().toString()); try { Message response = service.callBlockingMethod(call.getMd(), null, call.getMessage()); if(response != null) { ResponseHeader.Builder builder = ResponseHeader.newBuilder(); builder.setId(call.getCallId()); builder.setResponseName(call.getMd().getName()); ResponseHeader header = builder.build(); call.setHeader(header); call.setMessage(response); ctx.writeAndFlush(call); callCounter.getAndIncrement(); } } catch(ServiceException e) { LOG.error("Rpc Server channelRead exception:" + e.getMessage(), e); } }
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { ByteBufInputStream in = new ByteBufInputStream(msg); RequestHeader.Builder hbuilder = RequestHeader.newBuilder(); hbuilder.mergeDelimitedFrom(in); RequestHeader header = hbuilder.build(); BlockingService service = RaftRpcService.create().getService(); MethodDescriptor md = service.getDescriptorForType().findMethodByName(header.getRequestName()); Builder builder = service.getRequestPrototype(md).newBuilderForType(); Message body = null; if (builder != null) { if(builder.mergeDelimitedFrom(in)) { body = builder.build(); } else { LOG.error("Parsing packet failed!"); } } RpcCall call = new RpcCall(header.getId(), header, body, md); out.add(call); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { ByteBufInputStream in = new ByteBufInputStream(msg); ResponseHeader.Builder hbuilder = ResponseHeader.newBuilder(); hbuilder.mergeDelimitedFrom(in); ResponseHeader header = hbuilder.build(); BlockingService service = RaftRpcService.create().getService(); MethodDescriptor md = service.getDescriptorForType().findMethodByName(header.getResponseName()); Builder builder = service.getResponsePrototype(md).newBuilderForType(); Message body = null; if (builder != null) { if(builder.mergeDelimitedFrom(in)) { body = builder.build(); } else { LOG.error("Parse packet failed!!"); } } RpcCall call = new RpcCall(header.getId(), header, body, md); out.add(call); }
public RpcCall buildResponse(RpcCall requestCall) { ResponseHeader.Builder builder = ResponseHeader.newBuilder(); builder.setId(requestCall.getCallId()); builder.setResponseName(requestCall.getMd().getName()); ResponseHeader header = builder.build(); requestCall.setHeader(header); //call.setMessage(response); TestRpcResponse.Builder tbuilder = TestRpcResponse.newBuilder(); byte[] bytes = new byte[50]; tbuilder.setResult( ByteString.copyFrom(bytes)); requestCall.setMessage(tbuilder.build()); //RpcCall call = new RpcCall(); return requestCall; }
/** * Generates a random protocol buffer, filling in all required fields but * with a p chance of not setting an optional field and p chance of having * an empty repeated field. */ @SuppressWarnings("unchecked") public E generate(double p) { Builder builder = instance.newBuilderForType(); Descriptor descriptor = instance.getDescriptorForType(); for (FieldDescriptor field : descriptor.getFields()) { if (!field.isRequired() && random.nextDouble() < p) { continue; } builder.setField(field, getRandomValue(field, p)); } return (E) builder.build(); }
private UserInformation getUserInfoPB(UserGroupInformation ugi) { if (ugi == null || authMethod == AuthMethod.DIGEST) { // Don't send user for token auth return null; } UserInformation.Builder userInfoPB = UserInformation.newBuilder(); if (authMethod == AuthMethod.KERBEROS) { // Send effective user for Kerberos auth userInfoPB.setEffectiveUser(ugi.getUserName()); } else if (authMethod == AuthMethod.SIMPLE) { //Send both effective user and real user for simple auth userInfoPB.setEffectiveUser(ugi.getUserName()); if (ugi.getRealUser() != null) { userInfoPB.setRealUser(ugi.getRealUser().getUserName()); } } return userInfoPB.build(); }
public T buildKey(T data) { Builder b = data.newBuilderForType(); for (FieldDescriptor field : fields) { if (!data.hasField(field)) { if (requireFields) { throw new IllegalStateException("Field not set: " + field.getFullName()); } else { continue; } } Object value = data.getField(field); b.setField(field, value); } return (T) b.build(); }
/** * Make a call, passing <code>param</code>, to the IPC server running at <code>address</code> * which is servicing the <code>protocol</code> protocol, with the <code>ticket</code> * credentials, returning the value. Throws exceptions if there are network problems or if the * remote code threw an exception. */ public Message call(RpcRequestBody requestBody, Class<? extends VersionedProtocol> protocol, int rpcTimeout, Callback<Message> callback) throws Exception { ConnectionHeader.Builder builder = ConnectionHeader.newBuilder(); builder.setProtocol(protocol == null ? "" : protocol.getName()); ConnectionHeader connectionHeader = builder.build(); RpcRequestHeader.Builder headerBuilder = RPCProtos.RpcRequestHeader.newBuilder(); RpcRequestHeader rpcHeader = headerBuilder.build(); ByteBufferOutputStream bbo = new ByteBufferOutputStream(); connectionHeader.writeDelimitedTo(bbo); rpcHeader.writeDelimitedTo(bbo); requestBody.writeDelimitedTo(bbo); CallFuture<Message> future = new CallFuture<Message>(callback); if (LOG.isDebugEnabled()) { LOG.debug("send message, " + requestBody.getMethodName() + " , channel: " + channel); } transceive(bbo.getBufferList(), new TransceiverCallback<Message>(requestBody, protocol, future)); if (callback == null) { try { return future.get(conf.getLong(AngelConf.ANGEL_READ_TIMEOUT_SEC, AngelConf.DEFAULT_ANGEL_READ_TIMEOUT_SEC), TimeUnit.SECONDS); } catch (java.util.concurrent.TimeoutException e) { if (LOG.isDebugEnabled()) { LOG.debug("timeout for: send message, " + requestBody.getMethodName() + " , channel: " + channel); } disconnect(this.channel, true, true, e); throw e; } } return null; }