java类com.google.protobuf.Message.Builder的实例源码

JXPathCopier.java 文件源码 项目:xpath_proto_builder 阅读 22 收藏 0 点赞 0 评论 0
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);
    }
}
Server.java 文件源码 项目:hadoop-oss 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 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;
}
Server.java 文件源码 项目:hadoop-oss 阅读 28 收藏 0 点赞 0 评论 0
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();
}
QueryResponseToProto.java 文件源码 项目:rejoiner 阅读 25 收藏 0 点赞 0 评论 0
@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();
}
QueryResponseToProto.java 文件源码 项目:rejoiner 阅读 36 收藏 0 点赞 0 评论 0
@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;
    }
  }
}
MessageMarshallerTest.java 文件源码 项目:curiostack 阅读 27 收藏 0 点赞 0 评论 0
@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());
}
MessageMarshallerTest.java 文件源码 项目:curiostack 阅读 25 收藏 0 点赞 0 评论 0
@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);
}
MessageMarshallerTest.java 文件源码 项目:curiostack 阅读 27 收藏 0 点赞 0 评论 0
@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());
}
MessageMarshallerTest.java 文件源码 项目:curiostack 阅读 24 收藏 0 点赞 0 评论 0
@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());
}
MessageMarshallerTest.java 文件源码 项目:curiostack 阅读 27 收藏 0 点赞 0 评论 0
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());
}
Server.java 文件源码 项目:hadoop 阅读 29 收藏 0 点赞 0 评论 0
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;
}
Server.java 文件源码 项目:hadoop 阅读 32 收藏 0 点赞 0 评论 0
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();
}
RpcClientImpl.java 文件源码 项目:ditb 阅读 24 收藏 0 点赞 0 评论 0
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();
}
Server.java 文件源码 项目:aliyun-oss-hadoop-fs 阅读 25 收藏 0 点赞 0 评论 0
/**
 * 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;
}
Server.java 文件源码 项目:aliyun-oss-hadoop-fs 阅读 33 收藏 0 点赞 0 评论 0
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();
}
Server.java 文件源码 项目:big-c 阅读 27 收藏 0 点赞 0 评论 0
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;
}
Server.java 文件源码 项目:big-c 阅读 26 收藏 0 点赞 0 评论 0
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();
}
VendorExtensionProtoConverter.java 文件源码 项目:api-compiler 阅读 23 收藏 0 点赞 0 评论 0
@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;
  }
}
Server.java 文件源码 项目:hadoop-2.6.0-cdh5.4.3 阅读 25 收藏 0 点赞 0 评论 0
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;
}
Server.java 文件源码 项目:hadoop-2.6.0-cdh5.4.3 阅读 23 收藏 0 点赞 0 评论 0
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();
}
Server.java 文件源码 项目:hadoop-plus 阅读 29 收藏 0 点赞 0 评论 0
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;
}
Server.java 文件源码 项目:hadoop-plus 阅读 26 收藏 0 点赞 0 评论 0
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();
}
RpcClientImpl.java 文件源码 项目:pbase 阅读 28 收藏 0 点赞 0 评论 0
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();
}
RpcClient.java 文件源码 项目:HIndex 阅读 50 收藏 0 点赞 0 评论 0
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();
}
Server.java 文件源码 项目:hadoop-TCP 阅读 27 收藏 0 点赞 0 评论 0
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;
}
Server.java 文件源码 项目:hadoop-TCP 阅读 27 收藏 0 点赞 0 评论 0
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();
}
RpcClient.java 文件源码 项目:PyroDB 阅读 36 收藏 0 点赞 0 评论 0
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();
}
RpcClient.java 文件源码 项目:c5 阅读 31 收藏 0 点赞 0 评论 0
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();
}
ServerChannelHandler.java 文件源码 项目:CmRaft 阅读 25 收藏 0 点赞 0 评论 0
@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);
  }
}
ServerChannelHandler.java 文件源码 项目:CmRaft 阅读 23 收藏 0 点赞 0 评论 0
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);
}


问题


面经


文章

微信
公众号

扫码关注公众号