Created
November 6, 2024 11:33
-
-
Save kauanmocelin/9a622fa1a4899f31245a708932b1be43 to your computer and use it in GitHub Desktop.
Java streams com checked exception usando Failable da apache commons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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