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.document;
029
030import org.opencms.file.CmsResource;
031import org.opencms.file.types.CmsResourceTypeXmlContent;
032import org.opencms.json.JSONException;
033import org.opencms.json.JSONObject;
034import org.opencms.main.CmsException;
035import org.opencms.xml.xml2json.CmsJsonRequest;
036import org.opencms.xml.xml2json.CmsResourceDataJsonHelper;
037import org.opencms.xml.xml2json.handler.CmsJsonHandlerException;
038import org.opencms.xml.xml2json.handler.CmsJsonHandlerXmlContent.PathNotFoundException;
039
040/**
041 * Class representing a JSON document for a CMS resource.<p>
042 */
043public class CmsJsonDocumentResource extends A_CmsJsonDocument implements I_CmsJsonDocument {
044
045    /** The resource data helper. */
046    protected CmsResourceDataJsonHelper m_helper;
047
048    /** The resource. */
049    protected CmsResource m_resource;
050
051    /**
052     * Creates a new JSON document.<p>
053     *
054     * @param jsonRequest the JSON request
055     * @param resource the resource
056     */
057    public CmsJsonDocumentResource(CmsJsonRequest jsonRequest, CmsResource resource) {
058
059        super(jsonRequest);
060        m_resource = resource;
061        initHelper();
062    }
063
064    /**
065     * @see org.opencms.xml.xml2json.document.I_CmsJsonDocument#getJson()
066     */
067    public Object getJson()
068    throws JSONException, CmsException, CmsJsonHandlerException, PathNotFoundException, Exception {
069
070        insertJsonResource();
071        return m_json;
072    }
073
074    /**
075     * If the request parameter "wrapper" is set, inserts information about this resource into the JSON document.<p>
076     * @throws JSONException
077     * @throws CmsException
078     */
079    protected void insertJsonResource() throws JSONException, CmsException {
080
081        insertJsonResourceAttributes();
082        insertJsonResourceFile();
083        insertJsonResourceParams();
084        insertJsonResourcePathAndLink();
085        insertJsonResourceProperties();
086    }
087
088    /**
089     * Inserts the resource attributes into this JSON document.<p>
090     *
091     * @throws JSONException if JSON rendering fails
092     */
093    protected void insertJsonResourceAttributes() throws JSONException {
094
095        m_json.put("attributes", m_helper.attributes());
096    }
097
098    /**
099     * Inserts file type information into this JSON document.<p>
100     *
101     * @throws JSONException if JSON rendering fails
102     */
103    protected void insertJsonResourceFile() throws JSONException {
104
105        boolean isContent = !m_resource.isFolder() && CmsResourceTypeXmlContent.isXmlContent(m_resource);
106        m_json.put("isFolder", m_resource.isFolder());
107        m_json.put("isXmlContent", Boolean.valueOf(isContent));
108    }
109
110    /**
111     * Inserts information about the effective request parameters into this JSON document.<p>
112     *
113     * @throws JSONException if JSON rendering fails
114     */
115    protected void insertJsonResourceParams() throws JSONException {
116
117        JSONObject json = new JSONObject(true);
118        for (String key : m_context.getParameters().keySet()) {
119            json.put(key, m_context.getParameters().get(key));
120        }
121        m_json.put("requestParams", json);
122    }
123
124    /**
125     * Inserts path and link information about this resource into this JSON document.<p>
126     *
127     * @throws JSONException if JSON rendering fails
128     */
129    protected void insertJsonResourcePathAndLink() throws JSONException {
130
131        m_helper.addPathAndLink(m_json);
132    }
133
134    /**
135     * Inserts the properties of this resource into the JSON document.<p>
136     *
137     * @throws JSONException if JSON rendering fails
138     * @throws CmsException if reading the properties fails
139     */
140    protected void insertJsonResourceProperties() throws JSONException, CmsException {
141
142        m_helper.addProperties(m_json);
143    }
144
145    /**
146     * Initializes the resource data helper.<p>
147     */
148    private void initHelper() {
149
150        m_helper = new CmsResourceDataJsonHelper(
151            m_context.getCms(),
152            m_resource,
153            m_context.getAccessPolicy()::checkPropertyAccess);
154    }
155}