Package org.apache.xindice.webadmin.viewer.components

Source Code of org.apache.xindice.webadmin.viewer.components.MetadataResourceViewer

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: MetadataResourceViewer.java 564043 2007-08-08 23:11:58Z vgritsenko $
*/

package org.apache.xindice.webadmin.viewer.components;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.meta.MetaData;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.webadmin.viewer.HtmlResourceViewer;
import org.apache.xindice.webadmin.viewer.HtmlViewerComponent;
import org.apache.xindice.xml.TextWriter;
import org.apache.xindice.xml.Xml2HtmlWriter;
import org.apache.xindice.xml.dom.DOMParser;
import org.apache.xindice.xml.dom.DocumentImpl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Iterator;

/**
* Xindice Html Viewer for Resource Metadata.
*
* @version $Revision: 564043 $, $Date: 2007-08-08 19:11:58 -0400 (Wed, 08 Aug 2007) $
*/
public class MetadataResourceViewer extends HtmlViewerComponent implements HtmlResourceViewer {

    private static final Log log = LogFactory.getLog(MetadataCollectionViewer.class);

    public void execute(HttpServletRequest req, HttpServletResponse res, Collection col, String name)
            throws ServletException, IOException {

        String path = col.getCanonicalDocumentName(name);
        String title = "Resource Metadata for " + path;

        ServletOutputStream output = startViewer(title, path, res);

        String custom = req.getParameter("custom");
        String deleteCustom = req.getParameter("deleteCustom");

        if (custom != null && custom.length() > 0 && col.isMetaEnabled()) {
            try {
                MetaData customMeta = col.getDocumentMeta(name);
                Document customDoc = DOMParser.toDocument(custom);
                customMeta.setCustomDocument(customDoc);
                col.setDocumentMeta(name, customMeta);
            } catch (DBException e) {
                log.error(e);
                printError("Cannot add/update Custom: " + e.getMessage(), output);
            } catch (XindiceException e) {
                log.error(e);
                printError("Cannot add/update Custom: " + e.getMessage(), output);
            }
        } else if (deleteCustom != null && deleteCustom.length() > 0 && col.isMetaEnabled()) {
            try {
                MetaData customMeta = col.getDocumentMeta(name);
                if (customMeta != null) {
                    customMeta.setCustomDocument(null);
                    col.setDocumentMeta(name, customMeta);
                }
            } catch (DBException e) {
                log.error(e);
                printError("Cannot delete Custom: " + e.getMessage(), output);
            }
        } else if (req.getMethod().equalsIgnoreCase("POST")) {
            byte[] content = new byte[0];
            try {
                //  Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload();
                // Set upload parameters
                upload.setSizeMax(-1);
                upload.setFileItemFactory(new DiskFileItemFactory());

                // Process the uploaded fields
                Iterator i = upload.parseRequest(req).iterator();
                while (i.hasNext()) {
                    FileItem item = (FileItem) i.next();
                    if (!item.isFormField()) {
                        if (item.getFieldName().equals("customfile")) {
                            content = item.get();
                        }
                    }
                    item.delete();
                }
            } catch (FileUploadException e) {
                log.error(e);
            }
           
            if (content.length > 0) {
                Document customDoc;
                try {
                    customDoc = DOMParser.toDocument(content);
                } catch (XindiceException e1) { // document fragment ?
                    log.error(e1);
                    StringBuffer newContent = new StringBuffer();
                    newContent.append("<custom>").append(new String(content)).append("</custom>");
                    try {
                        customDoc = DOMParser.toDocument(newContent.toString());
                    } catch (XindiceException e2) { // no xml content
                        log.error(e2);
                        printError("Cannot parse Custom: " + e2.getMessage(), output);
                        customDoc = null;
                    }
                }
                if (customDoc != null) {
                    if (!customDoc.getDocumentElement().getNodeName().equals("custom")) {
                        Document newCustomDoc = new DocumentImpl();
                        Element customEle = newCustomDoc.createElement("custom");
                        customEle.appendChild(customDoc.getDocumentElement());
                        newCustomDoc.appendChild(customEle);
                        customDoc = newCustomDoc;
                    }
                    try {
                        MetaData customMeta = col.getDocumentMeta(name);
                        customMeta.setCustomDocument(customDoc);
                        col.setDocumentMeta(name, customMeta);
                    } catch (DBException e2) {
                        log.error(e2);
                        printError("Cannot create Custom: " + e2.getMessage(), output);
                    }
                }
            }
        }

        if (col.isMetaEnabled()) {
            MetaData metaData;
            try {
                metaData = col.getDocumentMeta(name);
            } catch (DBException e) {
                log.error(e);
                metaData = null;
            }
            if (metaData != null) {
                printCustom(metaData, output);
            }
        }

        finishViewer(output);
    }

    private void printCustom(MetaData meta, ServletOutputStream output) throws IOException {
        Document custom = meta.getCustomDocument();
        printStartTable("Add/Update MetaData Custom", output);
        printStartSingleTableBox(output);
        output.print("<a href=\"./?viewer=default\">up to parent collection</a>");
        printEndSingleTableBox(output);
        printStartSingleTableBox(output);
        output.println(META_CUSTOM_UPDATE_FORM_START);
        if (custom != null && custom.getDocumentElement() != null) {
            Element customEle = custom.getDocumentElement();
            if (!customEle.hasChildNodes()) {
                Document helper = new DocumentImpl();
                customEle.appendChild(helper.createTextNode("\n\n"));
            }
            TextWriter.write(custom.getDocumentElement(), output);
        }
        output.println(META_CUSTOM_UPDATE_FORM_END);
        printEndSingleTableBox(output);
        printStartSingleTableBox(output);
        output.println("<a href=\"?viewer=" + getName() + "&deleteCustom=true\">");
        output.println("delete Custom</a>");
        printEndSingleTableBox(output);
        printStartSingleTableBox(output);
        output.println(META_CUSTOM_UPLOAD_FORM);
        printEndSingleTableBox(output);
        printEndTable(output);
        output.println("<br />");
        printStartTable("MetaData Custom", output);
        printStartSingleTableBox(output);
        if (custom != null && custom.getDocumentElement() != null) {
            Xml2HtmlWriter.write(custom.getDocumentElement(), output);
        } else {
            output.println("No Custom Document!");
        }
        printEndSingleTableBox(output);
        printEndTable(output);
    }

    private void printError(String message, ServletOutputStream output) throws IOException {
        printStartTable("Error", output);
        printStartSingleTableBox(output);
        output.println(message);
        printEndSingleTableBox(output);
        printEndTable(output);
        output.println("<br />\n");
    }

    private static final String META_CUSTOM_UPDATE_FORM_START =
            "<form action=\"\" method=\"get\"> \n" +
            "<input name=\"viewer\" value=\"meta\" type=\"hidden\" /> \n" +
            "<table class=\"upload-form\"> \n" +
            "<tr><td> \n" +
            "<textarea name=\"custom\" cols=\"80\" rows=\"8\">";

    private static final String META_CUSTOM_UPDATE_FORM_END =
            "</textarea> \n" +
            "</td></tr><tr><td> \n" +
            "<input type=\"submit\" name=\"submit\" value=\"add/update\" /> \n" +
            "</td></tr></table></form> \n";

    private static final String META_CUSTOM_UPLOAD_FORM =
            "<form action=\"?viewer=meta\" method=\"post\" " +
            "enctype=\"multipart/form-data\"> \n" +
            "<table class=\"upload-form\"> \n" +
            "<tr><td>Upload Custom file: </td> \n" +
            "<td><input name=\"customfile\" type=\"file\" /></td> \n" +
            "<td><input type=\"submit\" name=\"submit\" value=\"upload\" /> \n" +
            "</td></tr> " +
            "</table> \n" +
            "</form> \n";

    protected String getName() {
        return "meta";
    }
}
TOP

Related Classes of org.apache.xindice.webadmin.viewer.components.MetadataResourceViewer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.