001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.workplace.tools.sites;
033
034import org.opencms.file.CmsObject;
035import org.opencms.file.CmsProperty;
036import org.opencms.file.types.CmsResourceTypeImage;
037import org.opencms.jsp.CmsJspActionElement;
038import org.opencms.main.CmsException;
039import org.opencms.main.CmsLog;
040import org.opencms.main.OpenCms;
041import org.opencms.workplace.administration.A_CmsImportFromHttp;
042
043import java.io.IOException;
044import java.io.RandomAccessFile;
045import java.util.ArrayList;
046import java.util.HashMap;
047import java.util.Map;
048
049import javax.servlet.ServletException;
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.servlet.jsp.PageContext;
053
054import org.apache.commons.logging.Log;
055
056/**
057 * Uploads and stores a favicon.<p>
058 *
059 * @since 9.0.0
060 */
061public class CmsSiteFaviconDialog extends A_CmsImportFromHttp {
062
063    /** The name for the favicon. */
064    public static final String ICON_NAME = "favicon.ico";
065
066    /** The URI for this dialog. */
067    private static final String DIALOG_URI = PATH_WORKPLACE + "admin/sites/favicon.jsp";
068
069    /** The log object for this class. */
070    private static final Log LOG = CmsLog.getLog(CmsSiteFaviconDialog.class);
071
072    /** The sites parameter. */
073    private String m_paramSites;
074
075    /**
076     * Public constructor with JSP action element.<p>
077     *
078     * @param jsp an initialized JSP action element
079     */
080    public CmsSiteFaviconDialog(CmsJspActionElement jsp) {
081
082        super(jsp);
083    }
084
085    /**
086     * Public constructor with JSP variables.<p>
087     *
088     * @param context the JSP page context
089     * @param req the JSP request
090     * @param res the JSP response
091     */
092    public CmsSiteFaviconDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
093
094        this(new CmsJspActionElement(context, req, res));
095    }
096
097    /**
098     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#actionCommit()
099     */
100    @Override
101    public void actionCommit() throws IOException, ServletException {
102
103        String site = getParamSites();
104        try {
105            // check if the site stored in the sites parameter exists and is readable
106            CmsObject cms = OpenCms.initCmsObject(getCms());
107            cms.getRequestContext().setSiteRoot("");
108            cms.existsResource(site + "/");
109
110            // copy the file to the server
111            copyFileToServer(OpenCms.getSystemInfo().getPackagesRfsPath());
112            if ((getParamImportfile() == null) || !getParamImportfile().equals(ICON_NAME)) {
113                // file null or name not valid
114                throw new CmsException(
115                    Messages.get().container(Messages.ERR_INVALID_FAVICON_FILE_1, getParamImportfile()));
116            }
117
118            // get the uploaded file content
119            String importpath = OpenCms.getSystemInfo().getPackagesRfsPath();
120            importpath = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(importpath + getParamImportfile());
121            RandomAccessFile f = new RandomAccessFile(importpath, "r");
122            byte[] content = new byte[(int)f.length()];
123            f.read(content);
124            f.close();
125
126            // check the existence of favicon
127            String favCreatePath = site + "/" + ICON_NAME;
128            int imageResId = CmsResourceTypeImage.getStaticTypeId();
129            if (cms.existsResource(favCreatePath)) {
130                // replace the existing favicon
131                cms.lockResource(favCreatePath);
132                cms.replaceResource(favCreatePath, imageResId, content, new ArrayList<CmsProperty>());
133                cms.unlockResource(favCreatePath);
134            } else {
135                // create the new favicon
136                cms.createResource(favCreatePath, imageResId, content, new ArrayList<CmsProperty>());
137            }
138
139            // set the dialog parameters
140            String title = OpenCms.getSiteManager().getSiteForSiteRoot(site).getTitle();
141            Map<String, String[]> params = new HashMap<String, String[]>();
142            params.put(CmsSitesOverviewList.PARAM_SITES, new String[] {getParamSites()});
143            params.put(CmsSitesOverviewList.PARAM_SITE_TITLE, new String[] {title});
144            params.put(PARAM_ACTION, new String[] {DIALOG_INITIAL});
145
146            // forward the request
147            getToolManager().jspForwardTool(this, "/sites/detail", params);
148
149        } catch (CmsException e) {
150            // error copying the file to the OpenCms server
151            if (LOG.isErrorEnabled()) {
152                LOG.error(e.getLocalizedMessage(getLocale()), e);
153            }
154            setException(e);
155            return;
156        }
157    }
158
159    /**
160     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#getDialogReturnUri()
161     */
162    @Override
163    public String getDialogReturnUri() {
164
165        return DIALOG_URI;
166    }
167
168    /**
169     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#getImportMessage()
170     */
171    @Override
172    public String getImportMessage() {
173
174        return key(Messages.GUI_SITES_IMPORT_FAV_0);
175    }
176
177    /**
178     * Returns the paramSites.<p>
179     *
180     * @return the paramSites
181     */
182    public String getParamSites() {
183
184        return m_paramSites;
185    }
186
187    /**
188     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#getStarttext()
189     */
190    @Override
191    public String getStarttext() {
192
193        return key(Messages.GUI_SITES_IMPORT_FAV_BLOCK_0);
194    }
195
196    /**
197     * Sets the paramSites.<p>
198     *
199     * @param paramSites the paramSites to set
200     */
201    public void setParamSites(String paramSites) {
202
203        m_paramSites = paramSites;
204    }
205
206    /**
207     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#defaultActionHtml()
208     */
209    @Override
210    protected String defaultActionHtml() {
211
212        return super.defaultActionHtml().replaceAll("application/zip", "image/x-icon");
213    }
214
215    /**
216     * @see org.opencms.workplace.administration.A_CmsImportFromHttp#initMessages()
217     */
218    @Override
219    protected void initMessages() {
220
221        // add specific dialog resource bundle
222        addMessages(Messages.get().getBundleName());
223        // add default resource bundles
224        addMessages(org.opencms.workplace.Messages.get().getBundleName());
225        addMessages(org.opencms.workplace.tools.Messages.get().getBundleName());
226
227    }
228}