Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Fixed bugs occurring when opening cdmremote datasets
Browse files Browse the repository at this point in the history
* CdmRemote.sendQuery() correctly leaves the InputStream it returns open.
* CdmrFeatureDataset handles cases where the cdmremote capabilites document lacks an /cdmRemoteCapabilities/featureDataset/@type attribute.
  • Loading branch information
cwardgar committed Jul 31, 2015
1 parent 284e748 commit b144c17
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
48 changes: 25 additions & 23 deletions cdm/src/main/java/ucar/nc2/stream/CdmRemote.java
Expand Up @@ -130,6 +130,7 @@ public CdmRemote(String _remoteURI) throws IOException {
if (showRequest) System.out.printf(" took %d msecs %n", took);
}

// Closes is.
public CdmRemote(InputStream is, String location ) throws IOException {
long start = System.currentTimeMillis();
remoteURI = location;
Expand All @@ -140,6 +141,7 @@ public CdmRemote(InputStream is, String location ) throws IOException {
this.location = SCHEME + remoteURI;

} finally {
is.close();
}
long took = System.currentTimeMillis() - start;
if (showRequest) System.out.printf(" took %d msecs %n", took);
Expand All @@ -164,7 +166,8 @@ protected Array readData(ucar.nc2.Variable v, Section section) throws IOExceptio
// sbuff.append( URLEncoder.encode(f.toString(), "UTF-8")); // LOOK dont % escape query; entire thing varname and section

if (showRequest)
System.out.println(" CdmRemote data request for variable: " + v.getFullName() + " section= " + section + " url=" + f);
System.out.println(" CdmRemote data request for variable: " + v.getFullName() + " section= " + section + " " +
"url=" + f);

try (
HTTPMethod method = HTTPFactory.Get(httpClient,f.toString())) {
Expand All @@ -187,7 +190,7 @@ protected Array readData(ucar.nc2.Variable v, Section section) throws IOExceptio
}
}

InputStream is = method.getResponseAsStream();
InputStream is = method.getResponseAsStream(); // Closed by HTTPMethod.close().
NcStreamReader reader = new NcStreamReader();
NcStreamReader.DataResult result = reader.readData(is, this);

Expand All @@ -209,35 +212,34 @@ protected StructureDataIterator getStructureIterator(Structure s, int bufferSize
}
}

public static InputStream sendQuery(String remoteURI, String query) throws IOException
{
public static InputStream sendQuery(String remoteURI, String query) throws IOException {
long start = System.currentTimeMillis();

InputStream stream = null;
int statusCode = 0;

StringBuilder sbuff = new StringBuilder(remoteURI);
sbuff.append("?");
sbuff.append(query);

if(showRequest)
System.out.printf(" CdmRemote sendQuery= %s", sbuff);
if (showRequest) System.out.printf(" CdmRemote sendQuery= %s", sbuff);

HTTPMethod method = HTTPFactory.Get(sbuff.toString());
try {
try (
HTTPMethod method = HTTPFactory.Get(sbuff.toString()) // use temp session
) {
statusCode = method.execute();
if(statusCode == 404)
throw new FileNotFoundException(method.getPath() + " " + method.getStatusLine());
if(statusCode >= 300)
throw new IOException(method.getPath() + " " + method.getStatusLine());
stream = method.getResponseBodyAsStream();
if(showRequest) System.out.printf(" took %d msecs %n", System.currentTimeMillis() - start);
return stream;
}
} catch (HTTPException he) {
throw new IOException(he);
int statusCode = method.execute();
if (statusCode == 404) {
throw new FileNotFoundException(method.getPath() + " " + method.getStatusLine());
} else if (statusCode >= 400) {
throw new IOException(method.getPath() + " " + method.getStatusLine());
}

InputStream stream = method.getResponseBodyAsStream();
if (showRequest) System.out.printf(" took %d msecs %n", System.currentTimeMillis() - start);

// Leave the stream open. We must also leave the HTTPMethod open because the two are linked:
// calling close() on one object causes the other object to be closed as well.
return stream;
} catch (IOException e) {
// Close the HTTPMethod if there was an exception; otherwise leave it open.
method.close();
throw e;
}
}

Expand Down
64 changes: 32 additions & 32 deletions cdm/src/main/java/ucar/nc2/stream/CdmrFeatureDataset.java
Expand Up @@ -32,13 +32,22 @@

package ucar.nc2.stream;

import ucar.nc2.ft.*;
import ucar.nc2.ft.point.remote.PointDatasetRemote;
import ucar.nc2.ft.point.writer.FeatureDatasetPointXML;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.jdom2.output.XMLOutputter;
import ucar.nc2.VariableSimpleIF;
import ucar.nc2.constants.FeatureType;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dt.grid.GridDataset;
import ucar.nc2.VariableSimpleIF;
import ucar.nc2.ft.FeatureCollection;
import ucar.nc2.ft.FeatureDataset;
import ucar.nc2.ft.FeatureDatasetPoint;
import ucar.nc2.ft.point.remote.PointDatasetRemote;
import ucar.nc2.ft.point.writer.FeatureDatasetPointXML;
import ucar.nc2.time.CalendarDateRange;
import ucar.nc2.units.DateUnit;
import ucar.unidata.geoloc.LatLonRect;
Expand All @@ -47,11 +56,6 @@
import java.io.InputStream;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.jdom2.input.SAXBuilder;

/**
* Factory for FeatureDataset using CdmrRemote protocol. GRID, POINT, STATION so far
*
Expand All @@ -71,12 +75,16 @@ static public FeatureDataset factory(FeatureType wantFeatureType, String endpoin
Document doc = getCapabilities(endpoint);
Element root = doc.getRootElement();
Element elem = root.getChild("featureDataset");
String fType = elem.getAttribute("type").getValue(); // LOOK, may be multiple types
String uri = elem.getAttribute("url").getValue();

if (debug) System.out.printf("CdmrFeatureDataset endpoint %s%n ftype= %s url=%s%n", endpoint, fType, uri);
// Often, CdmRemoteController won't be able to figure out the FeatureType of a dataset and as a result,
// the capabilites document it returns won't contain an /cdmRemoteCapabilities/featureDataset/@type attribute.
Attribute typeAttrib = elem.getAttribute("type"); // Could be null if attribute doesn't exist.

FeatureType ft = FeatureType.getType(fType);
// If the "type" attribute exists, use it; otherwise use wantFeatureType.
FeatureType ft = (typeAttrib != null) ? FeatureType.getType(typeAttrib.getValue()) : wantFeatureType;

if (debug) System.out.printf("CdmrFeatureDataset endpoint %s%n ftype= %s url=%s%n", endpoint, ft, uri);

if (ft == null || ft == FeatureType.NONE || ft == FeatureType.GRID) {
CdmRemote ncremote = new CdmRemote(uri);
Expand All @@ -95,28 +103,20 @@ static public FeatureDataset factory(FeatureType wantFeatureType, String endpoin
}

static private org.jdom2.Document getCapabilities(String endpoint) throws IOException {
org.jdom2.Document doc;
InputStream in = null;
try {
in = CdmRemote.sendQuery(endpoint, "req=capabilities");
SAXBuilder builder = new SAXBuilder(false);
doc = builder.build(in); // closes in when done ??

} catch (Throwable t) {
throw new IOException(t);

} finally {
//if (in != null)
// in.close();
}
try (InputStream in = CdmRemote.sendQuery(endpoint, "req=capabilities")) {
SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
org.jdom2.Document doc = builder.build(in);

if (showXML) {
System.out.printf("*** endpoint = %s %n", endpoint);
XMLOutputter xmlOut = new XMLOutputter();
System.out.printf("*** CdmrFeatureDataset/showParsedXML = %n %s %n", xmlOut.outputString(doc));
}

if (showXML) {
System.out.printf("*** endpoint = %s %n", endpoint);
XMLOutputter xmlOut = new XMLOutputter();
System.out.printf("*** CdmrFeatureDataset/showParsedXML = %n %s %n", xmlOut.outputString(doc));
return doc;
} catch (JDOMException e) {
throw new IOException(e);
}

return doc;
}

public static void main(String args[]) throws IOException {
Expand Down

0 comments on commit b144c17

Please sign in to comment.