001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.xml.xml2json.handler; 029 030import org.opencms.file.types.CmsResourceTypeXmlContainerPage; 031import org.opencms.file.types.CmsResourceTypeXmlContent; 032import org.opencms.json.JSONException; 033import org.opencms.json.JSONObject; 034import org.opencms.main.CmsLog; 035import org.opencms.xml.xml2json.CmsJsonRequest; 036import org.opencms.xml.xml2json.CmsJsonResult; 037import org.opencms.xml.xml2json.document.CmsJsonDocumentXmlContent; 038 039import javax.servlet.http.HttpServletResponse; 040 041import org.apache.commons.logging.Log; 042 043/** 044 * Sub-handler for converting XML contents to JSON, either as a whole or just specific locales or paths. 045 */ 046public class CmsJsonHandlerXmlContent implements I_CmsJsonHandler { 047 048 /** 049 * Exception thrown when path lookup fails. 050 */ 051 public static class PathNotFoundException extends Exception { 052 053 /** Serial version id. */ 054 private static final long serialVersionUID = 1L; 055 056 /** 057 * Creates a new exception for a given message.<p> 058 * 059 * @param string the message 060 */ 061 public PathNotFoundException(String string) { 062 063 super(string); 064 } 065 } 066 067 /** Request parameter name. */ 068 public static final String PARAM_LOCALE = "locale"; 069 070 /** Request parameter name. */ 071 public static final String PARAM_PATH = "path"; 072 073 /** The logger instance for this class. */ 074 private static final Log LOG = CmsLog.getLog(CmsJsonHandlerXmlContent.class); 075 076 /** 077 * Creates an empty JSON object. 078 * 079 * @return the empty JSON object 080 */ 081 public static JSONObject empty() { 082 083 try { 084 return new JSONObject("{}"); 085 } catch (JSONException e) { 086 return null; 087 } 088 } 089 090 /** 091 * @see org.opencms.xml.xml2json.handler.I_CmsJsonHandler#getOrder() 092 */ 093 public double getOrder() { 094 095 return 100.0; 096 097 } 098 099 /** 100 * @see org.opencms.xml.xml2json.handler.I_CmsJsonHandler#matches(org.opencms.xml.xml2json.handler.CmsJsonHandlerContext) 101 */ 102 public boolean matches(CmsJsonHandlerContext context) { 103 104 return CmsResourceTypeXmlContent.isXmlContent(context.getResource()) 105 && !CmsResourceTypeXmlContainerPage.isContainerPage(context.getResource()); 106 } 107 108 /** 109 * @see org.opencms.xml.xml2json.handler.I_CmsJsonHandler#renderJson(org.opencms.xml.xml2json.handler.CmsJsonHandlerContext) 110 */ 111 public CmsJsonResult renderJson(CmsJsonHandlerContext context) { 112 113 return renderJson(context, true); 114 } 115 116 /** 117 * Renders a JSON representation of an XML content.<p> 118 * 119 * @param context the handler context 120 * @param throwError whether to throw an error if a locale or path selection fails 121 * @return the XML content as JSON 122 */ 123 public CmsJsonResult renderJson(CmsJsonHandlerContext context, boolean throwError) { 124 125 try { 126 CmsJsonRequest jsonRequest = new CmsJsonRequest(context, this); 127 jsonRequest.validate(); 128 if (jsonRequest.hasErrors()) { 129 return new CmsJsonResult(jsonRequest.getErrorsAsJson(), HttpServletResponse.SC_BAD_REQUEST); 130 } 131 CmsJsonDocumentXmlContent jsonDocument = new CmsJsonDocumentXmlContent(jsonRequest, context.getContent()); 132 return new CmsJsonResult(jsonDocument.getJson(), HttpServletResponse.SC_OK); 133 } catch (JSONException | PathNotFoundException e) { 134 LOG.info(e.getLocalizedMessage(), e); 135 return new CmsJsonResult(e.getLocalizedMessage(), HttpServletResponse.SC_NOT_FOUND); 136 } catch (CmsJsonHandlerException e) { 137 LOG.debug(e.getLocalizedMessage(), e); 138 return new CmsJsonResult(e.getLocalizedMessage(), HttpServletResponse.SC_BAD_REQUEST); 139 } catch (Exception e) { 140 LOG.error(e.getLocalizedMessage(), e); 141 return new CmsJsonResult(e.getLocalizedMessage(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 142 } 143 } 144 145}