Skip to content

Instantly share code, notes, and snippets.

@kauanmocelin
Created November 6, 2024 11:33
Show Gist options
  • Save kauanmocelin/9a622fa1a4899f31245a708932b1be43 to your computer and use it in GitHub Desktop.
Save kauanmocelin/9a622fa1a4899f31245a708932b1be43 to your computer and use it in GitHub Desktop.
Java streams com checked exception usando Failable da apache commons
import org.apache.commons.lang3.function.Failable;
try {
final List<DadosTransacaoDTO> listaDocumentosProcessados = (List<DadosTransacaoDTO>) SessaoUtils.recuperarAtributoSessao(request, response, AtributosSessao.ATRIBUTO_SESSAO_LISTA_DOCUMENTOS);
final boolean possuiItemConfidencial = Optional.ofNullable(listaDocumentosProcessados)
.orElse(Collections.emptyList())
.stream()
.flatMap(Failable.asFunction(dados -> DocumentoFacade.obterDocumentosRelacionados(Utilitarios.formatarIdentificador(dados.getNumeroIdentificador())).stream()))
.anyMatch(item -> NivelAcessoDocumento.CONFIDENCIAL.getCodigo().equals(item.getCodigoRestricao()));
JSONObject respostaJson = new JSONObject()
.put("existeDocumentoConfidencial", possuiItemConfidencial);
response.getWriter().write(respostaJson.toString());
} catch(UndeclaredThrowableException ute) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().append(ute.getCause().getMessage());
throw new ApplicationException(ute.getCause());
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(e.getMessage());
throw new ApplicationException(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment