Search in sources :

Example 1 with MyCoReWebPageProvider

use of org.mycore.tools.MyCoReWebPageProvider in project mycore by MyCoRe-Org.

the class MCRWCMSContentManager method getContent.

/**
 * Return a json object with the content of a MyCoRe webpage.
 * <p>
 * {
 *  type: "content",
 *  content: @see {@link MCRWCMSDefaultSectionProvider}
 * }
 * </p>
 * <p>
 * If an error occur (e.g. file not exist) the returning json
 * looks like:<br>
 * {
 *  type: "error",
 *  errorType: "invalidFile"
 *  webpageId: "myfolder/webpage1.xml"
 * }
 * </p>
 *
 * @param webpageId id of the webpage
 * @return json object
 * @see ErrorType
 */
public JsonObject getContent(String webpageId) throws IOException, JDOMException, SAXException {
    boolean isXML = webpageId.endsWith(".xml");
    URL resourceURL = null;
    try {
        resourceURL = MCRWebPagesSynchronizer.getURL(webpageId);
    } catch (MalformedURLException e) {
        throwError(ErrorType.invalidDirectory, webpageId);
    }
    // file is not in web application directory
    if (!isXML) {
        throwError(ErrorType.invalidFile, webpageId);
    }
    Document doc = null;
    if (resourceURL == null) {
        MyCoReWebPageProvider wpp = new MyCoReWebPageProvider();
        wpp.addSection("neuer Eintrag", new Element("p").setText("TODO"), "de");
        doc = wpp.getXML();
    } else {
        doc = new MCRURLContent(resourceURL).asXML();
    }
    Element rootElement = doc.getRootElement();
    if (!rootElement.getName().equals("MyCoReWebPage")) {
        throwError(ErrorType.notMyCoReWebPage, webpageId);
    }
    // return content
    return getContent(rootElement);
}
Also used : MalformedURLException(java.net.MalformedURLException) MyCoReWebPageProvider(org.mycore.tools.MyCoReWebPageProvider) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) MCRURLContent(org.mycore.common.content.MCRURLContent) Document(org.jdom2.Document) URL(java.net.URL)

Example 2 with MyCoReWebPageProvider

use of org.mycore.tools.MyCoReWebPageProvider in project mycore by MyCoRe-Org.

the class MCRWCMSContentManager method move.

public void move(String from, String to) {
    try {
        // get from
        URL fromURL = MCRWebPagesSynchronizer.getURL(from);
        Document document;
        if (fromURL == null) {
            // if the from resource couldn't be found we assume its not created yet.
            MyCoReWebPageProvider wpp = new MyCoReWebPageProvider();
            wpp.addSection("neuer Eintrag", new Element("p").setText("TODO"), "de");
            document = wpp.getXML();
        } else {
            SAXBuilder builder = new SAXBuilder();
            document = builder.build(fromURL);
        }
        // save
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8"));
        try (OutputStream fout = MCRWebPagesSynchronizer.getOutputStream(to)) {
            out.output(document, fout);
        }
        // delete old
        if (fromURL != null) {
            Files.delete(Paths.get(fromURL.toURI()));
        }
    } catch (Exception exc) {
        LOGGER.error("Error moving {} to {}", from, to, exc);
        throwError(ErrorType.couldNotMove, to);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) MyCoReWebPageProvider(org.mycore.tools.MyCoReWebPageProvider) JsonElement(com.google.gson.JsonElement) Element(org.jdom2.Element) OutputStream(java.io.OutputStream) Document(org.jdom2.Document) URL(java.net.URL) JDOMException(org.jdom2.JDOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 3 with MyCoReWebPageProvider

use of org.mycore.tools.MyCoReWebPageProvider in project mycore by MyCoRe-Org.

the class MCRWCMSDefaultSectionProvider method fromJSON.

@Override
public Element fromJSON(JsonArray jsonSectionArray) {
    // create new document
    MyCoReWebPageProvider wp = new MyCoReWebPageProvider();
    // parse sections
    for (JsonElement sectionElement : jsonSectionArray) {
        if (!sectionElement.isJsonObject()) {
            LOGGER.warn("Invalid json element in content array! {}", sectionElement);
            continue;
        }
        JsonObject sectionObject = sectionElement.getAsJsonObject();
        String title = null;
        String lang = null;
        if (sectionObject.has(JSON_TITLE)) {
            title = sectionObject.get(JSON_TITLE).getAsJsonPrimitive().getAsString();
        }
        if (sectionObject.has(JSON_LANG)) {
            lang = sectionObject.get(JSON_LANG).getAsJsonPrimitive().getAsString();
        }
        String xmlAsString = sectionObject.get(JSON_DATA).getAsJsonPrimitive().getAsString();
        try {
            wp.addSection(title, xmlAsString, lang);
        } catch (Exception exc) {
            throw new RuntimeException("unable to add section " + title, exc);
        }
    }
    wp.updateMeta(MCRSessionMgr.getCurrentSession().getUserInformation().getUserID(), null);
    return wp.getXML().detachRootElement();
}
Also used : JsonElement(com.google.gson.JsonElement) MyCoReWebPageProvider(org.mycore.tools.MyCoReWebPageProvider) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Aggregations

JsonElement (com.google.gson.JsonElement)3 MyCoReWebPageProvider (org.mycore.tools.MyCoReWebPageProvider)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Document (org.jdom2.Document)2 Element (org.jdom2.Element)2 JsonObject (com.google.gson.JsonObject)1 OutputStream (java.io.OutputStream)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JDOMException (org.jdom2.JDOMException)1 SAXBuilder (org.jdom2.input.SAXBuilder)1 XMLOutputter (org.jdom2.output.XMLOutputter)1 MCRURLContent (org.mycore.common.content.MCRURLContent)1 SAXException (org.xml.sax.SAXException)1