if (cs != null && csType != null)
if (auth && cs != null && csType != null)
    private void writeManifest(Context context, Item item,                               PackageParameters params, OutputStream out)        throws PackageValidationException, CrosswalkException, AuthorizeException, SQLException, IOException    {        try        {            // Create the METS file            Mets mets = new Mets();                     // Top-level stuff            mets.setID(gensym("mets"));            mets.setOBJID("hdl:" + item.getHandle());            mets.setLABEL("DSpace Item");            mets.setPROFILE(getProfile());                     // MetsHdr            MetsHdr metsHdr = new MetsHdr();            metsHdr.setCREATEDATE(new Date()); // FIXME: CREATEDATE is now:                                               // maybe should be item create            // date?            // Agent            Agent agent = new Agent();            agent.setROLE(Role.CUSTODIAN);            agent.setTYPE(Type.ORGANIZATION);            Name name = new Name();            name.getContent()                    .add(new PCData(ConfigurationManager                                    .getProperty("dspace.name")));            agent.getContent().add(name);            metsHdr.getContent().add(agent);            mets.getContent().add(metsHdr);                     // add DMD sections            // Each type element MAY be either just a MODS-and-crosswalk name, OR            // a combination "MODS-name:crosswalk-name" (e.g. "DC:qDC").            String dmdTypes[] = getDmdTypes(params);            // record of ID of each dmdsec to make DMDID in structmap.            String dmdGroup = gensym("dmd_group");            String dmdId[] = new String[dmdTypes.length];            for (int i = 0; i < dmdTypes.length; ++i)            {                dmdId[i] = gensym("dmd");                XmlData xmlData = new XmlData();                String xwalkName, metsName;                String parts[] = dmdTypes[i].split(":", 2);                if (parts.length > 1)                {                    metsName = parts[0];                    xwalkName = parts[1];                }                else                    xwalkName = metsName = dmdTypes[i];                DisseminationCrosswalk xwalk = (DisseminationCrosswalk)                  PluginManager.getNamedPlugin(DisseminationCrosswalk.class, xwalkName);                if (xwalk == null)                    throw new PackageValidationException("Cannot find "+dmdTypes[i]+" crosswalk plugin!");                else                    crosswalkToMets(xwalk, item, xmlData);                DmdSec dmdSec = new DmdSec();                dmdSec.setID(dmdId[i]);                dmdSec.setGROUPID(dmdGroup);                MdWrap mdWrap = new MdWrap();                setMdType(mdWrap, metsName);                mdWrap.getContent().add(xmlData);                dmdSec.getContent().add(mdWrap);                mets.getContent().add(dmdSec);            }                     // only add license AMD section if there are any licenses.            String licenseID = null;            AmdSec amdSec = new AmdSec();            addRightsMd(context, item, amdSec);            if (amdSec.getContent().size() > 0)            {                licenseID = gensym("license");                amdSec.setID(licenseID);                mets.getContent().add(amdSec);            }            // FIXME: History data???? Nooooo!!!!            // fileSec - all non-metadata bundles go into fileGrp,            // and each bitstream therein into a file.            // Create the bitstream-level techMd and div's for structmap            // at the same time so we can connec the IDREFs to IDs.            FileSec fileSec = new FileSec();                     String techMdType = getTechMdType(params);            String parts[] = techMdType.split(":", 2);            String xwalkName, metsName;            if (parts.length > 1)            {                metsName = parts[0];                xwalkName = parts[1];            }            else                xwalkName = metsName = techMdType;            DisseminationCrosswalk xwalk = (DisseminationCrosswalk)              PluginManager.getNamedPlugin(DisseminationCrosswalk.class, xwalkName);            if (xwalk == null)                throw new PackageValidationException("Cannot find "+xwalkName+" crosswalk plugin!");            // log the primary bitstream for structmap            String primaryBitstreamFileID = null;            // accumulate content DIV items to put in structMap later.            List contentDivs = new ArrayList();            Bundle[] bundles = item.getBundles();            for (int i = 0; i < bundles.length; i++)            {                if (PackageUtils.isMetaInfoBundle(bundles[i]))                    continue;                Bitstream[] bitstreams = bundles[i].getBitstreams();                // Create a fileGrp                FileGrp fileGrp = new FileGrp();                         // Bundle name for USE attribute                String bName = bundles[i].getName();                if ((bName != null) && !bName.equals(""))                    fileGrp.setUSE(bundleToFileGrp(bName));                         // watch for primary bitstream                int primaryBitstreamID = -1;                boolean isContentBundle = false;                if ((bName != null) && bName.equals("ORIGINAL"))                {                    isContentBundle = true;                    primaryBitstreamID = bundles[i].getPrimaryBitstreamID();                }                for (int bits = 0; bits < bitstreams.length; bits++)                {                    String sid = String.valueOf(bitstreams[bits].getSequenceID());                             edu.harvard.hul.ois.mets.File file = new edu.harvard.hul.ois.mets.File();                             String xmlIDstart = "bitstream_";                    String fileID = xmlIDstart + sid;                    file.setID(fileID);                    // log primary bitstream for later (structMap)                    if (bitstreams[bits].getID() == primaryBitstreamID)                        primaryBitstreamFileID = fileID;                    // if this is content, add to structmap too:                    if (isContentBundle)                    {                        Div div = new Div();                        div.setID(gensym("div"));                        div.setTYPE("DSpace Content Bitstream");                        Fptr fptr = new Fptr();                        fptr.setFILEID(fileID);                        div.getContent().add(fptr);                        contentDivs.add(div);                    }                    file.setSEQ(bitstreams[bits].getSequenceID());                             String groupID = "GROUP_" + xmlIDstart + sid;                             /*                     * If we're in THUMBNAIL or TEXT bundles, the bitstream is                     * extracted text or a thumbnail, so we use the name to work                     * out which bitstream to be in the same group as                     */                    if ((bundles[i].getName() != null)                            && (bundles[i].getName().equals("THUMBNAIL") ||                                bundles[i].getName().startsWith("TEXT")))                    {                        // Try and find the original bitstream, and chuck the                        // derived bitstream in the same group                        Bitstream original = findOriginalBitstream(item,                                bitstreams[bits]);                                 if (original != null)                        {                            groupID = "GROUP_" + xmlIDstart                                    + original.getSequenceID();                        }                    }                             file.setGROUPID(groupID);                    file.setMIMETYPE(bitstreams[bits].getFormat().getMIMEType());                             // FIXME: CREATED: no date                    file.setSIZE(bitstreams[bits].getSize());                    // translate checksum and type to METS, if available.                    String csType = bitstreams[bits].getChecksumAlgorithm();                    String cs = bitstreams[bits].getChecksum();                    if (cs != null && csType != null)                    {                        try                        {                            file.setCHECKSUMTYPE(Checksumtype.parse(csType));                            file.setCHECKSUM(cs);                        }                        catch (MetsException e)                        {                            log.warn("Cannot set bitstream checksum type="+csType+" in METS.");                        }                    }                             // FLocat: filename is MD5 checksum                    FLocat flocat = new FLocat();                    flocat.setLOCTYPE(Loctype.URL);                    flocat.setXlinkHref(makeBitstreamName(bitstreams[bits]));                    // Make bitstream techMD metadata, add to file.                    String techID = "techMd_for_bitstream_"+bitstreams[bits].getSequenceID();                    AmdSec fAmdSec = new AmdSec();                    fAmdSec.setID(techID);                    TechMD techMd = new TechMD();                    techMd.setID(gensym("tech"));                    MdWrap mdWrap = new MdWrap();                    setMdType(mdWrap, metsName);                    XmlData xmlData = new XmlData();                    mdWrap.getContent().add(xmlData);                    techMd.getContent().add(mdWrap);                    fAmdSec.getContent().add(techMd);                    mets.getContent().add(fAmdSec);                    crosswalkToMets(xwalk, bitstreams[bits], xmlData);                    file.setADMID(techID);                    // Add FLocat to File, and File to FileGrp                    file.getContent().add(flocat);                    fileGrp.getContent().add(file);                }                         // Add fileGrp to fileSec                fileSec.getContent().add(fileGrp);            }                     // Add fileSec to document            mets.getContent().add(fileSec);                     // Create simple structMap: initial div represents the Item,            // and user-visible content bitstreams are in its child divs.            StringBuffer dmdIds = new StringBuffer();            for (int i = 0; i < dmdId.length; ++i)                dmdIds.append(" "+dmdId[i]);            StructMap structMap = new StructMap();            structMap.setID(gensym("struct"));            structMap.setTYPE("LOGICAL");            structMap.setLABEL("DSpace");            Div div0 = new Div();            div0.setID(gensym("div"));            div0.setTYPE("DSpace Item");            div0.setDMDID(dmdIds.substring(1));            if (licenseID != null)                div0.setADMID(licenseID);            // if there is a primary bitstream, add FPTR to it.            if (primaryBitstreamFileID != null)            {                Fptr fptr = new Fptr();                fptr.setFILEID(primaryBitstreamFileID);                div0.getContent().add(fptr);            }            // add DIV for each content bitstream            div0.getContent().addAll(contentDivs);            structMap.getContent().add(div0);            // Does subclass have something to add to structMap?            addStructMap(context, item, params, mets);            mets.getContent().add(structMap);            mets.validate(new MetsValidator());                     mets.write(new MetsWriter(out));        }        catch (MetsException e)        {            // We don't pass up a MetsException, so callers don't need to            // know the details of the METS toolkit            // e.printStackTrace();            throw new PackageValidationException(e);        }    }