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 GmbH & Co. KG, 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.repository;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.file.wrapper.CmsObjectWrapper;
035import org.opencms.loader.CmsResourceManager;
036import org.opencms.main.CmsException;
037import org.opencms.main.OpenCms;
038
039/**
040 * Represents a single entry in the repository. In the context of OpenCms
041 * this means a single {@link CmsResource}.<p>
042 *
043 * @since 6.5.6
044 */
045public class CmsRepositoryItem implements I_CmsRepositoryItem {
046
047    /** The actual {@link CmsObjectWrapper}. */
048    private CmsObjectWrapper m_cms;
049
050    /** The content of the item as a byte array. */
051    private byte[] m_content;
052
053    /** The MIME type of the item. */
054    private String m_mimeType;
055
056    /** The {@link CmsResource} the CmsRepositoryItem is for. */
057    private CmsResource m_resource;
058
059    /**
060     * Construct a new CmsRepositoryItem initialized with the {@link CmsResource}
061     * to use and the {@link CmsObjectWrapper} needed for some operations.<p>
062     *
063     * @param res the CmsResource this CmsRepositoryItem is used for
064     * @param cms the actual CmsObjectWrapper
065     */
066    public CmsRepositoryItem(CmsResource res, CmsObjectWrapper cms) {
067
068        m_resource = res;
069        m_cms = cms;
070    }
071
072    /**
073     * @see org.opencms.repository.I_CmsRepositoryItem#getContent()
074     */
075    public byte[] getContent() {
076
077        if (!m_resource.isFile()) {
078            return null;
079        }
080
081        if (m_content == null) {
082            try {
083                String filename = m_cms.getSitePath(m_resource);
084
085                // read and return the file
086                CmsFile file = m_cms.readFile(filename, CmsResourceFilter.IGNORE_EXPIRATION);
087
088                m_content = file.getContents();
089            } catch (CmsException ex) {
090                // noop
091            }
092        }
093
094        return m_content;
095    }
096
097    /**
098     * @see org.opencms.repository.I_CmsRepositoryItem#getContentLength()
099     */
100    public long getContentLength() {
101
102        return m_resource.getLength();
103    }
104
105    /**
106     * @see org.opencms.repository.I_CmsRepositoryItem#getCreationDate()
107     */
108    public long getCreationDate() {
109
110        return m_resource.getDateCreated();
111    }
112
113    /**
114     * @see org.opencms.repository.I_CmsRepositoryItem#getLastModifiedDate()
115     */
116    public long getLastModifiedDate() {
117
118        return m_resource.getDateLastModified();
119    }
120
121    /**
122     * @see org.opencms.repository.I_CmsRepositoryItem#getMimeType()
123     */
124    public String getMimeType() {
125
126        if (!m_resource.isFile()) {
127            return null;
128        }
129
130        if (m_mimeType == null) {
131            try {
132                String encoding = m_cms.readPropertyObject(
133                    m_resource,
134                    CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
135                    true).getValue(OpenCms.getSystemInfo().getDefaultEncoding());
136
137                m_mimeType = OpenCms.getResourceManager().getMimeType(
138                    m_resource.getRootPath(),
139                    encoding,
140                    CmsResourceManager.MIMETYPE_TEXT);
141
142            } catch (CmsException ex) {
143                // noop
144            }
145        }
146
147        return m_mimeType;
148    }
149
150    /**
151     * @see org.opencms.repository.I_CmsRepositoryItem#getName()
152     */
153    public String getName() {
154
155        return m_cms.getRequestContext().removeSiteRoot(m_resource.getRootPath());
156    }
157
158    /**
159     * @see org.opencms.repository.I_CmsRepositoryItem#isCollection()
160     */
161    public boolean isCollection() {
162
163        return m_resource.isFolder();
164    }
165
166}