public NetcdfFile getNetcdfFile(HttpServletRequest req, HttpServletResponse res) throws IOException { StringBuffer jnl = new StringBuffer("DEFINE ALIAS letdeq1 let/d=1\nDEFINE ALIAS ATTRCMD SET ATT/LIKE=\n"); StringBuffer urlbuf = req.getRequestURL(); String url = URLDecoder.decode(urlbuf.toString(), "UTF-8"); if (url.length() == urlbuf.length()) { // Length unchanged, decoding was unnecessary. url = urlbuf.toString(); } log.debug("building netcdf file from " + url); String base = getBaseURL(url); ArrayList<String> expressions = getExpressions(url); FerretIOServiceProvider fiosp = new FerretIOServiceProvider(); String data_path = fiosp.getDataDir(); if (expressions.size() == 2) { if (!expressions.get(0).trim().equals("")) { List<String> expression_one = new ArrayList<String>(); String od = URLDecoder.decode(expressions.get(0), "UTF-8"); String[] urls = od.split(","); for (int i = 0; i < urls.length; i++) { String[] cmds; if (urls[i].contains("_cr_")) { cmds = urls[i].split("_cr_"); for (int j = 0; j < cmds.length; j++) { expression_one.add(cmds[j]); } } else if (urls[i].contains(";")) { cmds = urls[i].split(";"); for (int j = 0; j < cmds.length; j++) { expression_one.add(cmds[j]); } } else { expression_one.add(urls[i]); } } for (int i = 0; i < expression_one.size(); i++) { String dataURL = URLDecoder.decode(expression_one.get(i), "UTF-8"); if (FerretCommands.containsCommand(dataURL)) { // This is actually a command, not a data URL. Treat it as such... if (!FerretCommands.containsForbiddenCommand(dataURL)) { jnl.append(dataURL + "\n"); } } else { if (!FerretCommands.containsForbiddenCommand(dataURL)) { int ds = i + 2; jnl.append("use \"" + dataURL + "\"\n"); } } } } // Everything in the first bracket goes before the dataset. jnl.append("use \"" + base + "\"\n"); if (!expressions.get(1).trim().equals("")) { // Decode the expression because it might contain an encode URL. String expr_two = URLDecoder.decode(expressions.get(1), "UTF-8"); String[] cmds; if (expr_two.contains("_cr_")) { cmds = expr_two.split("_cr_"); } else { cmds = expr_two.split(";"); } for (int i = 0; i < cmds.length; i++) { cmds[i] = cmds[i].replaceAll("_q-t_", "\""); if (!FerretCommands.containsForbiddenCommand(cmds[i])) { jnl.append(cmds[i].replace("_qt_", "\"") + "\n"); } } } } else if (expressions.size() == 1) { if (!expressions.get(0).trim().equals("")) { String od = URLDecoder.decode(expressions.get(0), "UTF-8"); String[] urls = od.split(","); for (int i = 0; i < urls.length; i++) { String dataURL = URLDecoder.decode(urls[i], "UTF-8"); if (FerretCommands.containsCommand(dataURL)) { // This is actually a command, not a data URL. Treat it as such... if (!FerretCommands.containsForbiddenCommand(dataURL)) { jnl.append(dataURL + "\n"); } } else { if (!FerretCommands.containsForbiddenCommand(dataURL)) { int ds = i + 2; jnl.append("use \"" + dataURL.replace("_qt_", "\"") + "\"\n"); } } } } } else if (expressions.size() == 0) { throw new IOException( "Expression parsing failed for this URL. " + url + " Now expressions found inside the curly brackets."); } else if (expressions.size() > 2) { throw new IOException( "Expression parsing failed for this URL. " + url + " Too many expressions found."); } String key = JDOMUtils.MD5Encode(jnl.toString()); String script = data_path + File.separator + "data_expr_" + key + ".jnl"; log.debug("using " + script + " for temporary script file."); if (script.contains("../") || script.contains("/..")) { throw new IOException("Illegal script file name."); } File script_file = new File(script); if (!script_file.exists()) { PrintWriter data_script = new PrintWriter(new FileOutputStream(script_file)); data_script.println(jnl); data_script.close(); } RandomAccessFile raf = new RandomAccessFile(script, "r"); return new FerretNetcdfFile(raf, req.getRequestURI()); }
public void read(String src) { // addXML ax = new addXML(); InvCatalogFactory factory = new InvCatalogFactory("default", false); InvCatalog catalog = (InvCatalog) factory.readXML(src); StringBuilder buff = new StringBuilder(); int count = catalog.getDatasets().size(); if (!catalog.check(buff, true)) { log.error("Invalid catalog <" + src + ">\n" + buff.toString()); } else { for (int index = 0; index < count; index++) { factory = new InvCatalogFactory("default", false); catalog = (InvCatalog) factory.readXML(src); InvDataset invDataset = catalog.getDatasets().get(index); System.out.println(invDataset.getName()); String file = "/home/rhs/NCAR/las_categories_"; try { file = file + JDOMUtils.MD5Encode(invDataset.getName()) + ".xml"; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Element las_categories = new Element("las_categories"); // CategoryBean cb = new CategoryBean(); // cb.setName(invDataset.getName()); // cb.setID(invDataset.getID()); // // This is the top level... // //cb.setContributors(getContributors(invDataset)); // Vector topCats = new Vector(); for (Iterator topLevelIt = invDataset.getDatasets().iterator(); topLevelIt.hasNext(); ) { InvDataset topDS = (InvDataset) topLevelIt.next(); // CategoryBean topCB = new CategoryBean(); // topCB.setName(topDS.getName()); String id = topDS.getID(); if (id == null) { try { id = "id_" + JDOMUtils.MD5Encode(topDS.getName()); } catch (UnsupportedEncodingException e) { id = "id_" + String.valueOf(Math.random()); } } System.out.println("top: " + topDS.getName() + ", " + topDS.getID()); // topCB.setID(id); // for (Iterator subDatasetsIt = topDS.getDatasets().iterator(); // subDatasetsIt.hasNext(); ) { // InvDataset subDataset = (InvDataset) subDatasetsIt.next(); // topCB.addCatID(subDataset.getID()); // // These will be the catalog containers that will contain the aggregations... // for (Iterator grandChildrenIt = subDataset.getDatasets().iterator(); // grandChildrenIt.hasNext(); ) { // InvDataset grandChild = (InvDataset) grandChildrenIt.next(); // if ( grandChild.hasAccess() && grandChild.getName().contains("aggregation")) { // // We are done. // String url = null; // InvAccess access = null; // for (Iterator ait = grandChild.getAccess().iterator(); ait.hasNext(); ) { // access = (InvAccess) ait.next(); // if (access.getService().getServiceType() == ServiceType.DODS || // access.getService().getServiceType() == ServiceType.OPENDAP || // access.getService().getServiceType() == ServiceType.NETCDF) { // url = access.getStandardUrlName(); // } // } // if ( url != null && url.contains("aggregation") ){ // FilterBean filter = new FilterBean(); // filter.setAction("apply-variable"); // String tag = grandChild.getID(); // filter.setContainstag(tag); // topCB.addFilter(filter); // topCB.addCatID(grandChild.getID()); // } // } // } // } // if ( topCB.getFilters().size() > 0 ) { // topCats.add(topCB); // } } // if ( topCats.size() > 0 ) { // cb.setCategories(topCats); // } // las_categories.addContent(cb.toXml()); // ax.processESGCategories(invDataset, las_categories); // LASDocument document = new LASDocument(); // document.setRootElement(las_categories); // System.out.println("Writing "+file+" for "+invDataset.getName()); // document.write(file); // document = null; } } }