link = request.getContextPath()+ "/file" + collectionName + name ;
link = request.getContextPath()+  collectionName + name ;
    public static void sendDIR(HttpServletRequest request,                               HttpServletResponse response)            throws Exception {        if (Debug.isSet("showResponse"))            System.out.println("sendDIR request = " + request);        String name;        String size;        String lastModified;        String link;        String responseLinks;        PrintWriter pw = new PrintWriter(response.getOutputStream());        Iterator it;        Element childDataset;        String collectionName  = ReqInfo.getCollectionName(request);        String targetURL = request.getContextPath() + request.getServletPath() + collectionName;        if(Debug.isSet("S4Dir")) System.out.println("S4Dir - targetURL:                  "+targetURL);        if(Debug.isSet("S4Dir")) System.out.println("S4Dir - collectionName:        "+collectionName);        boolean isTopLevel = collectionName.equals("/");        // Get the catalog for this collection        Element dataset = BesAPI.showCatalog(collectionName).getRootElement();        // Compute White Space required for correct formating        int headerSpace = 0;        it = dataset.getChildren("dataset").iterator();        while (it.hasNext()) {            childDataset = (Element) it.next();            name = childDataset.getChildTextTrim("name");            if (headerSpace < name.length())                headerSpace = name.length();        }        headerSpace += 10;        String baseName; // The last item on the Collection path/name thingy        // Strip off a trailing / from the basename.        if (collectionName.endsWith("/"))            baseName = collectionName.substring(0, collectionName.length() - 1);        else            baseName = collectionName;        // Strip off any prefixed path (made of "/"'s)        if (baseName.lastIndexOf("/") > 0)            baseName = baseName.substring(baseName.lastIndexOf("/"), baseName.length());        if(Debug.isSet("S4Dir")) System.out.println("S4Dir - baseName: "+baseName);        // Strip basename from the end of the targetURL to make the link to the parent directory        link = targetURL.substring(0, targetURL.lastIndexOf(baseName))+"/contents.html";        // Set up the page.        printHTMLHeader(collectionName, headerSpace, link, pw);        // Build a line in the page for each child dataset/collection        it = dataset.getChildren("dataset").iterator();        while (it.hasNext()) {            childDataset = (Element) it.next();            boolean isData = childDataset.getAttributeValue("isData").equalsIgnoreCase("true");            name = childDataset.getChildTextTrim("name");            size = childDataset.getChildTextTrim("size");            lastModified = childDataset.getChild("lastmodified").getChildTextTrim("date") + " " +                    childDataset.getChild("lastmodified").getChildTextTrim("time");            // Is it a collection?            if (childDataset.getAttributeValue("thredds_collection").equalsIgnoreCase("true")) {                link = targetURL +  name + "/contents.html";                responseLinks = "        " +                        " -  " +                        " -  " +                        " -  " +                        " -   " +                        " -   ";                name += "/";                size = "  -";            } else { /// It must be a dataset                if(isData){                    link = targetURL + name + ".html";                    // Build response links                    responseLinks = "      " +                            "<a href=\"" + targetURL + name + ".ddx" + "\">ddx</a> " +                            "<a href=\"" + targetURL + name + ".dds" + "\">dds</a> " +                            "<a href=\"" + targetURL + name + ".das" + "\">das</a> " +                            "<a href=\"" + targetURL + name + ".info" + "\">info</a> " +                            "<a href=\"" + targetURL + name + ".html" + "\">html</a> ";                }                else{                    link = request.getContextPath()+ "/file" + collectionName + name ;                    responseLinks = "      " +                            " -  " +                            " -  " +                            " -  " +                            " -   " +                            " -   ";                }                size = computeSizeString(size);            }            pw.print("<A HREF=\"");            pw.print(link);            pw.print("\">" + name + "</a>" + getWhiteSpacePadding(name, headerSpace));            pw.print(lastModified);            pw.print("      " + size);            pw.print(responseLinks);            pw.print("\n");        }        printHTMLFooter(pw,targetURL,isTopLevel);        pw.flush();    }