public ContextException setContext(String key, @Nullable Object value) { clearContext(key); return addContext(key, value); }
/** * Returns the message contained in {@code file}. Throws an unchecked exception * if the file does not exist, is empty or does not contain message with the * expected type. */ public static <MSG extends Message> MSG read(File file, Parser<MSG> parser) { InputStream input = null; try { input = new BufferedInputStream(new FileInputStream(file)); return parser.parseFrom(input); } catch (Exception e) { throw ContextException.of("Unable to read message", e).addContext("file", file); } finally { IOUtils.closeQuietly(input); } }
/** * Writes a single message to {@code file}. Existing content is replaced, the message is not * appended. */ public static void write(Message message, File toFile) { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(toFile, false)); message.writeTo(out); } catch (Exception e) { throw ContextException.of("Unable to write message", e).addContext("file", toFile); } finally { IOUtils.closeQuietly(out); } }
/** * Reads a stream of messages. This method returns an empty iterator if there are no messages. An * exception is raised on IO error, if file does not exist or if messages have a * different type than {@code parser}. */ public static <MSG extends Message> CloseableIterator<MSG> readStream(File file, Parser<MSG> parser) { try { // the input stream is closed by the CloseableIterator BufferedInputStream input = new BufferedInputStream(new FileInputStream(file)); return readStream(input, parser); } catch (Exception e) { throw ContextException.of("Unable to read messages", e).addContext("file", file); } }
public static ContextException of(Throwable t) { if (t instanceof ContextException) { return new ContextException(t.getCause()).addContext((ContextException) t); } return new ContextException(t); }
public static ContextException of(String message, Throwable t) { if (t instanceof ContextException) { return new ContextException(message, t.getCause()).addContext((ContextException) t); } return new ContextException(message, t); }
public void appendComponentExternalIssue(int componentRef, ScannerReport.ExternalIssue issue) { File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { issue.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write external issue", e).addContext("file", file); } }
public void appendComponentIssue(int componentRef, ScannerReport.Issue issue) { File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { issue.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write issue", e).addContext("file", file); } }
public void appendAdHocRule(ScannerReport.AdHocRule adHocRule) { File file = fileStructure.adHocRules(); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { adHocRule.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write ad hoc rule", e).addContext("file", file); } }
/** * Streams multiple messages to {@code file}. Reading the messages back requires to * call methods {@code readStream(...)}. * <p> * See https://developers.google.com/protocol-buffers/docs/techniques#streaming * </p> */ public static <MSG extends Message> void writeStream(Iterable<MSG> messages, File toFile, boolean append) { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(toFile, append)); writeStream(messages, out); } catch (Exception e) { throw ContextException.of("Unable to write messages", e).addContext("file", toFile); } finally { IOUtils.closeQuietly(out); } }
@Test public void addContext() { ContextException e = ContextException.of(LABEL) .addContext("K1", "V1") .addContext("K2", "V2"); assertThat(e).hasMessage(LABEL + " | K1=V1 | K2=V2"); }
@Test public void multiple_context_values() { ContextException e = ContextException.of(LABEL) .addContext("K1", "V1") .addContext("K1", "V2"); assertThat(e).hasMessage(LABEL + " | K1=[V1,V2]"); }
@Test public void merge_ContextException() { ContextException cause = ContextException.of("cause").addContext("K1", "V1"); ContextException e = ContextException.of(cause) .addContext("K1", "V11") .addContext("K2", "V2"); assertThat(e.getContext("K1")).containsExactly("V1", "V11"); assertThat(e.getContext("K2")).containsOnly("V2"); assertThat(e).hasMessage("K1=[V1,V11] | K2=V2"); }
@Test public void setContext() { ContextException e = ContextException.of(LABEL) .addContext("K1", "V1") .setContext("K1", "V2"); assertThat(e).hasMessage(LABEL + " | K1=V2"); }
@Test public void merge_ContextException_with_new_message() { ContextException cause = ContextException.of("cause").addContext("K1", "V1"); ContextException e = ContextException.of(LABEL, cause).addContext("K2", "V2"); assertThat(e.getContext("K1")).containsOnly("V1"); assertThat(e.getContext("K2")).containsOnly("V2"); assertThat(e).hasMessage(LABEL + " | K1=V1 | K2=V2"); } }
public static ContextException of(String message, Throwable t) { if (t instanceof ContextException) { return new ContextException(message, t.getCause()).addContext((ContextException) t); } return new ContextException(message, t); }
public static ContextException of(Throwable t) { if (t instanceof ContextException) { return new ContextException(t.getCause()).addContext((ContextException) t); } return new ContextException(t); }
public void appendAdHocRule(ScannerReport.AdHocRule adHocRule) { File file = fileStructure.adHocRules(); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { adHocRule.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write ad hoc rule", e).addContext("file", file); } }
public void appendComponentExternalIssue(int componentRef, ScannerReport.ExternalIssue issue) { File file = fileStructure.fileFor(FileStructure.Domain.EXTERNAL_ISSUES, componentRef); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { issue.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write external issue", e).addContext("file", file); } }
public void appendComponentIssue(int componentRef, ScannerReport.Issue issue) { File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { issue.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write issue", e).addContext("file", file); } }