KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > i18n > CmsDefaultLocaleHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsDefaultLocaleHandler.java,v $
3  * Date : $Date: 2006/03/27 14:53:01 $
4  * Version: $Revision: 1.23 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.i18n;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsUser;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.main.OpenCms;
42
43 import java.io.UnsupportedEncodingException JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Locale JavaDoc;
46
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48
49 import org.apache.commons.logging.Log;
50
51 /**
52  * Default implementation of the locale handler.<p>
53  *
54  * @author Carsten Weinholz
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.23 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsDefaultLocaleHandler implements I_CmsLocaleHandler {
62
63     /** The log object for this class. */
64     private static final Log LOG = CmsLog.getLog(CmsDefaultLocaleHandler.class);
65
66     /** A cms object that has been initialized with Admin permissions. */
67     private CmsObject m_adminCmsObject;
68
69     /**
70      * Constructor, no action is required.<p>
71      */

72     public CmsDefaultLocaleHandler() {
73
74         // noop
75
}
76
77     /**
78      * @see org.opencms.i18n.I_CmsLocaleHandler#getI18nInfo(javax.servlet.http.HttpServletRequest, org.opencms.file.CmsUser, org.opencms.file.CmsProject, java.lang.String)
79      */

80     public CmsI18nInfo getI18nInfo(HttpServletRequest JavaDoc req, CmsUser user, CmsProject project, String JavaDoc resourceName) {
81
82         CmsLocaleManager localeManager = OpenCms.getLocaleManager();
83         List JavaDoc defaultLocales = null;
84         String JavaDoc encoding = null;
85
86         CmsObject adminCms = null;
87         try {
88             // create a copy of the Admin context to avoid concurrent modification
89
adminCms = OpenCms.initCmsObject(m_adminCmsObject);
90         } catch (CmsException e) {
91             // unable to copy Admin context - this should never happen
92
}
93
94         if (adminCms != null) {
95
96             // must switch project id in stored Admin context to match current project
97
adminCms.getRequestContext().setCurrentProject(project);
98             adminCms.getRequestContext().setUri(resourceName);
99
100             // now get default m_locale names
101
CmsResource res = null;
102             try {
103                 res = adminCms.readResource(resourceName);
104             } catch (CmsException e) {
105                 // unable to read the resource - maybe we need the init handlers
106
}
107             if (res == null) {
108                 try {
109                     res = OpenCms.initResource(adminCms, resourceName, req, null);
110                 } catch (CmsException e) {
111                     // unable to resolve the resource, use default locale
112
}
113             }
114
115             String JavaDoc defaultNames = null;
116
117             if (res != null) {
118                 // the resource may not exist at all (e.g. if an unknown resource was requested by the user in the browser)
119
try {
120                     defaultNames = adminCms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_LOCALE, true).getValue();
121                 } catch (CmsException e) {
122                     LOG.warn(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e);
123                 }
124                 if (defaultNames != null) {
125                     defaultLocales = localeManager.getAvailableLocales(defaultNames);
126                 }
127
128                 // get the encoding
129
try {
130                     encoding = adminCms.readPropertyObject(res, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue(
131                         OpenCms.getSystemInfo().getDefaultEncoding());
132                 } catch (CmsException e) {
133                     if (LOG.isInfoEnabled()) {
134                         LOG.info(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e);
135                     }
136                 }
137             }
138         }
139
140         if ((defaultLocales == null) || (defaultLocales.isEmpty())) {
141             // no default locales could be determined
142
defaultLocales = localeManager.getDefaultLocales();
143         }
144         if (encoding == null) {
145             // no special encoding could be determined
146
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
147         }
148
149         // set the request character encoding
150
if (req != null) {
151             try {
152                 req.setCharacterEncoding(encoding);
153             } catch (UnsupportedEncodingException JavaDoc e) {
154                 LOG.error(Messages.get().getBundle().key(Messages.ERR_UNSUPPORTED_REQUEST_ENCODING_1, encoding), e);
155             }
156         }
157
158         Locale JavaDoc locale;
159         // return the first default locale name
160
if ((defaultLocales != null) && (defaultLocales.size() > 0)) {
161             locale = (Locale JavaDoc)defaultLocales.get(0);
162         } else {
163             locale = CmsLocaleManager.getDefaultLocale();
164         }
165
166         return new CmsI18nInfo(locale, encoding);
167     }
168
169     /**
170      * @see org.opencms.i18n.I_CmsLocaleHandler#initHandler(org.opencms.file.CmsObject)
171      */

172     public void initHandler(CmsObject cms) {
173
174         m_adminCmsObject = cms;
175     }
176 }
Popular Tags