private boolean rangeOk(String url) { try { try (HTTPMethod method = HTTPFactory.Get(session, url)) { method.setRange(0,0); doConnect(method); int code = method.getStatusCode(); if(code != 206) throw new IOException("Server does not support Range requests, code= " + code); Header head = method.getResponseHeader("Content-Range"); total_length = Long.parseLong(head.getValue().substring(head.getValue().lastIndexOf("/") + 1)); // clear stream method.close(); return true; } } catch (IOException e) { return false; } }
method.setRange(pos,end); doConnect(method);
static private boolean checkIfRemoteNcml(String location) throws IOException { if (decodePathExtension(location)==ServiceType.NCML) { // just because location ends with ncml does not mean it's ncml // if the ncml file is being served up via http by a remote server, // we should be able to read the first bit of it and see if it even // looks like an ncml file. try (HTTPMethod method = HTTPFactory.Get(location)) { method.setRange(0, NUM_BYTES_TO_DETERMINE_NCML); method.setRequestHeader("accept-encoding", "identity"); int statusCode = method.execute(); if (statusCode >= 300) { if (statusCode == 401) { throw new IOException("Unauthorized to open dataset " + location); } else if (statusCode == 406) { String msg = location + " - this server does not support returning content without any encoding."; msg = msg + " Please download the file locally. Return status=" + statusCode; throw new IOException(msg); } else { throw new IOException(location + " is not a valid URL, return status=" + statusCode); } } return checkIfNcml(method.getResponseAsString()); } } return false; }
@Test public void testByteRange() { String file = "http://" + TestDir.remoteTestServer + "/thredds/fileServer/scanLocal/testData.nc"; try { try (HTTPMethod m = HTTPFactory.Get(file)) { m.setRange(0, 9); int statusCode = m.execute(); System.err.printf("status = %d%n", statusCode); Assert.assertTrue("Unexpected return code: " + statusCode, statusCode == 206); byte[] result = m.getResponseAsBytes(); Assert.assertTrue("Wrong size result", result.length == 10); } } catch (Exception e) { } }