コード例 #1
0
  @Override
  public String put(
      String itemId,
      InputStream dataStream,
      long dataSize,
      HashMap<String, String> params,
      HashMap<String, String> metas,
      XMLConfig extra) {

    if (coll == null) return null;
    if (maxDocSize > 0 && dataSize > 0 && dataSize > maxDocSize) return null;

    BasicDBObject docsearch = new BasicDBObject();
    docsearch.put("item_id", itemId);

    BasicDBObject doc = new BasicDBObject();
    doc.put("item_id", itemId);
    doc.put("item_extra", extra.asXml());

    Document xml_params = DocumentFactory.getInstance().createDocument("utf-8");
    xml_params.setXMLEncoding("utf-8");
    Element xml_params_items = xml_params.addElement("params");

    for (Map.Entry<String, String> item : params.entrySet()) {
      String key = item.getKey();
      if (item.getValue() != null) xml_params_items.addElement(key).addText(item.getValue());
    }
    doc.put("item_params", xml_params.asXML());

    Document xml_metas = DocumentFactory.getInstance().createDocument("utf-8");
    xml_metas.setXMLEncoding("utf-8");
    Element xml_metas_items = xml_metas.addElement("metas");

    for (Map.Entry<String, String> item : metas.entrySet()) {
      String key = item.getKey();
      if (item.getKey().startsWith("meta_")) {
        key = key.replace(':', '_').replace('-', '_').replace('.', '_').replace('/', '_');
      }
      if (item.getValue() != null) xml_metas_items.addElement(key).addText(item.getValue());
    }
    doc.put("item_metas", xml_metas.asXML());

    if (dataStream != null) {
      String contentBase64 = "";
      try {
        // dataStream.reset();
        contentBase64 = Base64.inputStreamToStringBase64(dataStream);
        doc.put("content_base64", contentBase64);

      } catch (IOException e) {
        System.out.println(itemId);
        e.printStackTrace();
      }
    }
    remove(itemId);
    return coll.add(doc);
  }
コード例 #2
0
  public static DocumentCacheItem doc2DocumentCacheItem(BasicDBObject doc) {
    try {
      String itemId = doc.get("item_id").toString();

      XMLConfig extra = null;
      if (doc.get("item_extra") != null) {
        extra = new XMLConfig();
        extra.loadString(doc.get("item_extra").toString());
      }

      HashMap<String, String> params = new HashMap<String, String>();
      HashMap<String, String> metas = new HashMap<String, String>();

      Document document = null;
      SAXReader reader = new SAXReader();
      reader.setValidation(false);
      try {
        document = reader.read(new StringReader(doc.get("item_params").toString()));

        Element elmParams = (Element) document.selectSingleNode("params");
        if (elmParams != null) {
          List<Element> items = elmParams.elements();
          Iterator<Element> iterItems = items.iterator();
          while (iterItems.hasNext()) {
            Element item = (Element) iterItems.next();
            params.put(item.getName(), item.getText());
          }
        }
      } catch (DocumentException de) {
        IOException ioe = new IOException();
        ioe.initCause(de);
        throw ioe;
      }

      reader = new SAXReader();
      reader.setValidation(false);
      try {
        document = reader.read(new StringReader(doc.get("item_metas").toString()));

        Element elmMetas = (Element) document.selectSingleNode("metas");
        if (elmMetas != null) {
          List<Element> items = elmMetas.elements();
          Iterator<Element> iterItems = items.iterator();
          while (iterItems.hasNext()) {
            Element item = (Element) iterItems.next();
            metas.put(
                item.getName()
                    .replace(':', '_')
                    .replace('-', '_')
                    .replace('.', '_')
                    .replace('/', '_'),
                item.getText());
          }
        }
      } catch (DocumentException de) {
        IOException ioe = new IOException();
        ioe.initCause(de);
        throw ioe;
      }

      InputStream inputSource =
          new ByteArrayInputStream(doc.get("content_base64").toString().getBytes());
      InputStream streamData = new Base64.InputStream(inputSource, Base64.DECODE);

      return new DocumentCacheItem(itemId, streamData, params, metas, extra);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }