@Override public void withHttpURLConnection(OutputStream outputStream) throws IOException { IOUtils.copy(inputStream, outputStream); inputStream.close(); outputStream.close(); } };
@Override public T handleResponse(HttpRequest<?> httpRequest, InputStream is, Integer statusCode, IOException e) throws IOException { if (statusCode != null && statusCode >= 400) { logger.warn(httpRequest.getSafeUrl() + ": " + statusCode + " " + IOUtils.toString(is)); } else { IOUtils.consumeAndClose(is); } return null; } }
public static void consumeAndClose(InputStream is) { if (is == null) { return; } try { while (is.read() != EOF) {} } catch (IOException e) { logger.warn(e.getMessage(), e); } finally { closeQuietly(is); } }
public static String getResourceAsString(String name) { try { return toString(getResourceAsStream(name)); } catch (IOException e) { throw new RuntimeException(e); } }
private void onFlushError(Integer responseCode, InputStream inputStream, @Nullable IOException e) { // TODO read accepted, dropped and invalid onConnectionError(responseCode, currentlyTransmitting, 0); if (e != null) { logger.error("Error sending data to APM server: {}, response code is {}", e.getMessage(), responseCode); logger.debug("Sending payload to APM server failed", e); } if (logger.isWarnEnabled()) { try { logger.warn(IOUtils.toString(inputStream)); } catch (IOException e1) { logger.warn(e1.getMessage(), e); } finally { IOUtils.closeQuietly(inputStream); } } else { // in order to be able to reuse the underlying TCP connections, // the input stream must be consumed and closed // see also https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html IOUtils.consumeAndClose(inputStream); } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { IOUtils.copy(IOUtils.getResourceAsStream("wsdl.xml"), response.getOutputStream()); baseRequest.setHandled(true); } });
@Override public T handleResponse(HttpRequest<?> httpRequest, InputStream is, Integer statusCode, IOException e) throws IOException { // we have to read the whole response, otherwise bad things happen IOUtils.consumeAndClose(is); return null; }
ObjectNode getDashboardForElasticsearch(String dashboardPath) throws IOException { final ObjectMapper mapper = JsonUtils.getMapper(); final ObjectNode dashboard = (ObjectNode) mapper.readTree(IOUtils.getResourceAsStream(dashboardPath)); dashboard.put("editable", false); ObjectNode dashboardElasticsearchFormat = mapper.createObjectNode(); dashboardElasticsearchFormat.put("user", "guest"); dashboardElasticsearchFormat.put("group", "guest"); dashboardElasticsearchFormat.set(TITLE, dashboard.get(TITLE)); dashboardElasticsearchFormat.set("tags", dashboard.get("tags")); dashboardElasticsearchFormat.put("dashboard", dashboard.toString()); return dashboardElasticsearchFormat; }
private String buildWidgetTemplate(String contextPath) { return IOUtils.getResourceAsString("stagemonitorWidget.html") .replace("@@CONTEXT_PREFIX_PATH@@", contextPath) .replace("@@OPEN_IMMEDIATELY@@", Boolean.toString(openImmediately)) .replace("@@OVERLAY_DISPLAY@@", openImmediately ? "block" : "none"); }
logger.warn("Error handling error response for {} request to url {}: {}", request.getMethod(), request.getSafeUrl(), e1.getMessage(), e1); try { logger.trace(new String(IOUtils.readToBytes(inputStream), "UTF-8")); } catch (IOException e2) { logger.trace("Could not read error stream: {}", e2.getMessage(), e2); IOUtils.closeQuietly(inputStream);
@Test public void testToString() throws IOException { final ByteArrayInputStream bios = new ByteArrayInputStream("test".getBytes()); Assert.assertEquals("test", IOUtils.toString(bios)); }
public static void sendConfigurationMapping(ElasticsearchClient elasticsearchClient) { final String mappingJson; try { mappingJson = IOUtils.toString(IOUtils.getResourceAsStream("stagemonitor-configuration-elasticsearch-mapping.json")); elasticsearchClient.sendMappingTemplate(mappingJson, "stagemonitor-configuration"); } catch (IOException e) { logger.warn("Suppressed exception:", e); } }
@Override public JsonNode handleResponse(HttpRequest<?> httpRequest, InputStream is, Integer statusCode, IOException e) throws IOException { if (statusCode == 200) { return JsonUtils.getMapper().readTree(is); } else { IOUtils.consumeAndClose(is); return null; } } }
private void createKibana5IndexAndMappings(ElasticsearchClient elasticsearchClient) { // makes sure the .kibana index is present and has the right mapping. // otherwise it leads to problems if stagemonitor sends the dashboards to the // .kibana index before it has been properly created by kibana final String resourcePath = elasticsearchClient.getElasticsearchResourcePath(); elasticsearchClient.createIndexAndSendMapping(".kibana", "index-pattern", IOUtils.getResourceAsStream(resourcePath + "kibana-index-index-pattern.json")); elasticsearchClient.createIndexAndSendMapping(".kibana", "search", IOUtils.getResourceAsStream(resourcePath + "kibana-index-search.json")); elasticsearchClient.createIndexAndSendMapping(".kibana", "dashboard", IOUtils.getResourceAsStream(resourcePath + "kibana-index-dashboard.json")); elasticsearchClient.createIndexAndSendMapping(".kibana", "visualization", IOUtils.getResourceAsStream(resourcePath + "kibana-index-visualization.json")); }
@Override public String getClientTraceExtensionScriptStaticPart() { String eumJs; if (servletPlugin.getDebugClientSpanScript()) { eumJs = IOUtils.getResourceAsString("eum.debug.js"); } else { eumJs = IOUtils.getResourceAsString("eum.min.js"); } eumJs += "\nineum('sampleRate', " + tracingPlugin.getDefaultRateLimitSpansPercent() + ")"; return eumJs; }
public static byte[] readToBytes(InputStream inputStream) throws IOException { if (inputStream == null) { return new byte[0]; } final ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(inputStream, output); return output.toByteArray(); }
private void onFlushSuccess(InputStream inputStream) { errorCount = 0; reported += currentlyTransmitting; // in order to be able to reuse the underlying TCP connections, // the input stream must be consumed and closed // see also https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html IOUtils.consumeAndClose(inputStream); }
private String getFields(String fieldsJsonPath) throws IOException { final JsonNode fields = JsonUtils.getMapper().readTree(IOUtils.getResourceAsStream(fieldsJsonPath)); for (JsonNode field : fields) { if (!field.has("readFromDocValues")) { // if a field mapping does not contain readFromDocValues (previously named doc_values) // kibana refreshes the mapping based on the contents of the index // if the index is empty or contains documents which don't have all possible properties set (like username) // the value for readFromDocValues can't be determined // thus these field mappings are "deleted" (or rather can't be recreated) logger.warn("Field {} in {} does not have property readFromDocValues", field.get("name"), fieldsJsonPath); } } return JsonUtils.getMapper().writeValueAsString(fields); }
@Test public void testGetResourceAsString() throws Exception { Assert.assertEquals("foo=bar", IOUtils.getResourceAsString("test.properties")); }
private static File createTempDispatcherJar() throws IOException { final InputStream input = AgentAttacher.class.getClassLoader() .getResourceAsStream("stagemonitor-dispatcher.jar.gradlePleaseDontExtract"); if (input == null) { throw new IllegalStateException("If you see this exception inside you IDE, you have to execute gradle " + "processResources before running the tests."); } final File tempDispatcherJar = File.createTempFile("stagemonitor-dispatcher", ".jar"); tempDispatcherJar.deleteOnExit(); IOUtils.copy(input, new FileOutputStream(tempDispatcherJar)); return tempDispatcherJar; }