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);
}
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);
}
}
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();
}
Aggregations