public void convertToImage(GraphConfiguration config) throws IOException { File outputDirectory = new File(config.getOutputDirectory()); if (!outputDirectory.exists()) { LOGGER.info( "Output directory does not exist yet. Creating it: " + outputDirectory.getAbsolutePath()); FileUtils.forceMkdir(outputDirectory); } File imageFile = new File(outputDirectory, config.getGraphName() + ".png"); File dotFile = new File(outputDirectory, config.getGraphName() + ".dot"); StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dotFile))); String line = null; while ((line = reader.readLine()) != null) { builder.append(line); } postDotCode(config, builder.toString(), imageFile); }
private int postDotCode(GraphConfiguration config, String dotCode, File imageFile) throws IOException { HttpClient httpclient = new DefaultHttpClient(); try { HttpPost post = new HttpPost("https://chart.googleapis.com/chart"); List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("cht", "gv")); postParameters.add(new BasicNameValuePair("chl", dotCode)); String width = config.getGraphWidth(); String height = config.getGraphHeight(); if (StringUtils.isNotEmpty(width) && StringUtils.isNotEmpty(height)) { String dimensions = config.getGraphWidth() + "x" + config.getGraphHeight(); LOGGER.info("Adding dimensions: " + dimensions); // postParameters.add(new BasicNameValuePair("chs", dimensions)); } post.setEntity(new UrlEncodedFormEntity(postParameters)); LOGGER.info("Sending request.."); HttpResponse response = httpclient.execute(post); LOGGER.info("Response: " + response); HttpEntity responseEntity = response.getEntity(); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(imageFile)); responseEntity.writeTo(outputStream); outputStream.close(); return response.getStatusLine().getStatusCode(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }