private static String readErrorResponseBodyDoNotDie(HttpURLConnection http, int status, String charset) { InputStream errorStream = http.getErrorStream(); if (errorStream != null) { String error = charset == null ? IO.read(errorStream) : IO.read(errorStream, charset); return error; } else { return ""; } }
private static URLConnection doPost(String url, Map<String, ?> headers, String contentType, String charset, String body ) throws IOException { HttpURLConnection connection;/* Handle output. */ connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(DEFAULT_TIMEOUT_SECONDS * 1000); connection.setDoOutput(true); manageContentTypeHeaders(contentType, charset, connection); manageHeaders(headers, connection); IO.write(connection.getOutputStream(), body, IO.DEFAULT_CHARSET); return connection; }
private static byte[] readResponseBodyAsBytes(HttpURLConnection http) { try { return IO.input(http.getInputStream()); } catch (IOException e) { return Exceptions.handle(byte[].class, e); } }
@Override public void servicesRemoved(String serviceName, int count) { puts("servicesRemoved", serviceName, count); } };
public static List<URI> readDnsConf() { final Logger logger = LoggerFactory.getLogger(DnsUtil.class); final boolean debug = logger.isDebugEnabled(); final File file = new File(Sys.sysProp(QBIT_DNS_RESOLV_CONF, "/etc/resolv.conf")); if (file.exists()) { final List<String> lines = IO.readLines(file); if (debug) logger.debug("file contents {}", lines); return lines.stream().filter(line -> line.startsWith("nameserver")) .map(line -> { if (debug) logger.debug("file content line = {}", line); final String uriToParse = line.replace("nameserver ", "").trim(); final String[] split = Str.split(uriToParse, ':'); try { if (split.length == 1) { return new URI("dns", "", split[0], 53, "", "", ""); } else if (split.length >= 2) { return new URI("dns", "", split[0], Integer.parseInt(split[1]), "", "", ""); } else { throw new IllegalStateException("Unable to parse URI from /etc/resolv.conf"); } } catch (URISyntaxException e) { throw new IllegalStateException("failed to convert to URI"); } }) .collect(Collectors.toList()); } else { throw new IllegalStateException("" + file + " not found"); } }
private static List<String> readLines( String location, URI uri ) throws Exception { try { String path = location; path = getWindowsPathIfNeeded( path ); FileSystem fileSystem = FileSystems.getFileSystem( uri ); Path fsPath = fileSystem.getPath( path ); //Paths.get() return Files.readAllLines( fsPath, DEFAULT_CHARSET ); } catch ( ProviderNotFoundException ex ) { return readLines( uri.toURL().openStream() ); } }
@Override public Path apply( String s ) { return path(s); } }
public static String read( final String location ) { final URI uri = createURI( location ); return Exceptions.tryIt( String.class, new Exceptions.TrialWithReturn<String>() { @Override public String tryIt() throws Exception { String path = location; path = getWindowsPathIfNeeded( path ); if ( uri.getScheme() == null ) { Path thePath = FileSystems.getDefault().getPath( path ); return read( Files.newBufferedReader( thePath, DEFAULT_CHARSET ) ); } else if ( uri.getScheme().equals( FILE_SCHEMA ) ) { return readFromFileSchema( uri ); } else { return read( location, uri ); } } } ); }
public static Path createDirectory( String dir ) { try { final Path newDir = path(dir); createDirectory( newDir ); return newDir; } catch ( Exception ex ) { return Exceptions.handle( Path.class, ex ); } }
public static List<String> readLines( final String location ) { final String path = getWindowsPathIfNeeded( location ); final URI uri = createURI( path ); return ( List<String> ) Exceptions.tryIt( Typ.list, new Exceptions.TrialWithReturn<List>() { @Override public List<String> tryIt() throws Exception { if ( uri.getScheme() == null ) { Path thePath = FileSystems.getDefault().getPath( path ); return Files.readAllLines( thePath, DEFAULT_CHARSET ); } else if ( uri.getScheme().equals( FILE_SCHEMA ) ) { Path thePath = FileSystems.getDefault().getPath( uri.getPath() ); return Files.readAllLines( thePath, DEFAULT_CHARSET ); } else { return readLines( location, uri ); } } } ); }
public static List<String> listByExt( final String path, String ext ) { final List<String> list = list( path ); final List<String> newList = new ArrayList<>(); for ( String file : list ) { if ( file.endsWith( ext ) ) { newList.add( file ); } } return newList; }
@Override public void accept(Long integer) { puts("Last ten second count for abc.count", integer); } }, "abc.count");