java类com.google.protobuf.Empty的实例源码

_EnvironmentServiceIntTest.java 文件源码 项目:generator-jhipster-grpc 阅读 27 收藏 0 点赞 0 评论 0
@Test
public void testEnvironment() throws IOException {
    Environment Environment = stub.getEnv(Empty.newBuilder().build());
    ObjectMapper mapper = new ObjectMapper();
    TypeReference<HashMap<String,Object>> typeRef
        = new TypeReference<HashMap<String,Object>>() {};
    // String value should represent a Json map
    HashMap<String,Object> env = mapper.readValue(Environment.getValue(), typeRef);
    assertThat(env).isNotEmpty();
}
_EnvironmentService.java 文件源码 项目:generator-jhipster-grpc 阅读 30 收藏 0 点赞 0 评论 0
@Override
public Mono<Environment> getEnv(Mono<Empty> request) {
    return request.map( empty -> {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return Environment.newBuilder()
                .setValue(mapper.writeValueAsString(endpoint.invoke()))
                .build();
        } catch (JsonProcessingException e) {
            throw Status.INTERNAL.withCause(e).asRuntimeException();
        }
    });
}
_UserGrpcService.java 文件源码 项目:generator-jhipster-grpc 阅读 24 收藏 0 点赞 0 评论 0
@Override
public Mono<Empty> deleteUser(Mono<StringValue> request) {
    return request
        .map(StringValue::getValue)
        .doOnSuccess(login -> log.debug("gRPC request to delete User: {}", login))
        .doOnSuccess(userService::deleteUser)
        .map(l -> Empty.newBuilder().build());
}
_UserGrpcService.java 文件源码 项目:generator-jhipster-grpc 阅读 30 收藏 0 点赞 0 评论 0
@Override
public Flux<StringValue> getAllAuthorities(Mono<Empty> request) {
    return request
        .doOnSuccess(e -> log.debug("gRPC request to gat all authorities"))
        .filter(e -> SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN))
        .switchIfEmpty(Mono.error(Status.PERMISSION_DENIED.asRuntimeException()))
        .flatMapIterable(e -> userService.getAuthorities())
        .map(authority -> StringValue.newBuilder().setValue(authority).build());
}
_AccountService.java 文件源码 项目:generator-jhipster-grpc 阅读 22 收藏 0 点赞 0 评论 0
@Override
public Mono<StringValue> isAuthenticated(Mono<Empty> request) {
    return request.map(e -> {
        log.debug("gRPC request to check if the current user is authenticated");
        Authentication principal = SecurityContextHolder.getContext().getAuthentication();
        StringValue.Builder builder = StringValue.newBuilder();
        if (principal != null) {
            builder.setValue(principal.getName());
        }
        return builder.build();
    });
}
_AccountService.java 文件源码 项目:generator-jhipster-grpc 阅读 18 收藏 0 点赞 0 评论 0
@Override
public Mono<Empty> registerAccount(Mono<UserProto> request) {
    return request
        .doOnSuccess(userProto -> log.debug("gRPC request to register account {}", userProto.getLogin()))
        .filter(userProto -> checkPasswordLength(userProto.getPassword()))
        .switchIfEmpty(Mono.error(Status.INVALID_ARGUMENT.withDescription("Incorrect password").asRuntimeException()))
        .filter(userProto -> !userRepository.findOneByLogin(userProto.getLogin().toLowerCase()).isPresent())
        .switchIfEmpty(Mono.error(Status.ALREADY_EXISTS.withDescription("Login already in use").asRuntimeException()))
        .filter(userProto -> !userRepository.findOneByEmailIgnoreCase(userProto.getEmail()).isPresent())
        .switchIfEmpty(Mono.error(Status.ALREADY_EXISTS.withDescription("Email already in use").asRuntimeException()))
        .map(userProto -> Pair.of(userProtoMapper.userProtoToUserDTO(userProto), userProto.getPassword()))
        .map(pair -> {
            try {
                return userService.registerUser(pair.getFirst(), pair.getSecond());
            <%_ if (databaseType === 'sql') { _%>
            } catch (TransactionSystemException e) {
                if (e.getOriginalException().getCause() instanceof ConstraintViolationException) {
                    log.info("Invalid user", e);
                    throw Status.INVALID_ARGUMENT.withDescription("Invalid user").asRuntimeException();
                } else {
                    throw e;
                }
            <%_ } _%>
            } catch (ConstraintViolationException e) {
                log.error("Invalid user", e);
                throw Status.INVALID_ARGUMENT.withDescription("Invalid user").asRuntimeException();
            }
        })
        .doOnSuccess(mailService::sendCreationEmail)
        .map(u -> Empty.newBuilder().build());
}
_AccountService.java 文件源码 项目:generator-jhipster-grpc 阅读 22 收藏 0 点赞 0 评论 0
@Override
public Mono<Empty> saveAccount(Mono<UserProto> request) {
    String currentLogin = SecurityUtils.getCurrentUserLogin().orElseThrow(Status.INTERNAL::asRuntimeException);
    return request
        .filter(user -> !userRepository.findOneByEmailIgnoreCase(user.getEmail())
            .map(User::getLogin)
            .map(login -> !login.equalsIgnoreCase(currentLogin))
            .isPresent()
        )
        .switchIfEmpty(Mono.error(Status.ALREADY_EXISTS.withDescription("Email already in use").asRuntimeException()))
        .filter(user -> userRepository.findOneByLogin(currentLogin).isPresent())
        .switchIfEmpty(Mono.error(Status.INTERNAL.asRuntimeException()))
        .doOnSuccess(user -> {
            try {
                userService.updateUser(
                    user.getFirstName().isEmpty() ? null : user.getFirstName(),
                    user.getLastName().isEmpty() ? null : user.getLastName(),
                    user.getEmail().isEmpty() ? null : user.getEmail(),
                    user.getLangKey().isEmpty() ? null : user.getLangKey()<% if (databaseType === 'mongodb' || databaseType === 'sql') { %>,
                    user.getImageUrl().isEmpty() ? null : user.getImageUrl()<% } %>
                );
            <%_ if (databaseType === 'sql') { _%>
            } catch (TransactionSystemException e) {
                if (e.getOriginalException().getCause() instanceof ConstraintViolationException) {
                    log.info("Invalid user", e);
                    throw Status.INVALID_ARGUMENT.withDescription("Invalid user").asRuntimeException();
                } else {
                    throw e;
                }
            <%_ } _%>
            } catch (ConstraintViolationException e) {
                log.error("Invalid user", e);
                throw Status.INVALID_ARGUMENT.withDescription("Invalid user").asRuntimeException();
            }
        })
        .map(u -> Empty.newBuilder().build());
}
_AccountService.java 文件源码 项目:generator-jhipster-grpc 阅读 20 收藏 0 点赞 0 评论 0
@Override
public Mono<Empty> changePassword(Mono<StringValue> request) {
    return request
        .map(StringValue::getValue)
        .filter(AccountService::checkPasswordLength)
        .switchIfEmpty(Mono.error(Status.INVALID_ARGUMENT.withDescription("Incorrect password").asRuntimeException()))
        .doOnSuccess(userService::changePassword)
        .map(p -> Empty.newBuilder().build());
}
_AccountService.java 文件源码 项目:generator-jhipster-grpc 阅读 20 收藏 0 点赞 0 评论 0
@Override
public Flux<PersistentToken> getCurrentSessions(Mono<Empty> request) {
    return request
        .map(e-> SecurityUtils.getCurrentUserLogin()
            .flatMap(userRepository::findOneByLogin)
            .orElseThrow(Status.INTERNAL::asRuntimeException)
        )
        .flatMapIterable(persistentTokenRepository::findByUser)
        .map(ProtobufMappers::persistentTokenToPersistentTokenProto);
}
_AccountService.java 文件源码 项目:generator-jhipster-grpc 阅读 26 收藏 0 点赞 0 评论 0
@Override
public Mono<Empty> invalidateSession(Mono<StringValue> request) {
    return request
        .map(StringValue::getValue)
        .doOnSuccess(series -> SecurityUtils.getCurrentUserLogin()
            .flatMap(userRepository::findOneByLogin)
            .map(persistentTokenRepository::findByUser)
            .orElse(new ArrayList<>())
            .stream()
            .filter(persistentToken -> StringUtils.equals(persistentToken.getSeries(), series))
            .forEach(persistentTokenRepository::delete)
        )
        .map(s -> Empty.newBuilder().build());
}


问题


面经


文章

微信
公众号

扫码关注公众号