001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.servlet;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.LocaleUtil;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.util.PortalInstances;
029: import com.liferay.util.servlet.ServletResponseUtil;
030:
031: import java.io.IOException;
032:
033: import java.util.Locale;
034:
035: import javax.servlet.ServletException;
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: /**
044: * <a href="LanguageServlet.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class LanguageServlet extends HttpServlet {
050:
051: public void service(HttpServletRequest req, HttpServletResponse res)
052: throws IOException, ServletException {
053:
054: String path = req.getPathInfo();
055:
056: if (_log.isDebugEnabled()) {
057: _log.debug("Path " + path);
058: }
059:
060: if (Validator.isNotNull(path)
061: && path.startsWith(StringPool.SLASH)) {
062: path = path.substring(1, path.length());
063: }
064:
065: String[] pathArray = StringUtil.split(path, StringPool.SLASH);
066:
067: if (pathArray.length == 0) {
068: _log.error("Language id is not specified");
069:
070: return;
071: }
072:
073: if (pathArray.length == 1) {
074: _log.error("Language key is not specified");
075:
076: return;
077: }
078:
079: Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
080: String key = pathArray[1];
081:
082: Object[] arguments = null;
083:
084: if (pathArray.length > 2) {
085: arguments = new Object[pathArray.length - 2];
086:
087: System.arraycopy(pathArray, 2, arguments, 0,
088: arguments.length);
089: }
090:
091: String value = key;
092:
093: try {
094: long companyId = PortalInstances.getCompanyId(req);
095:
096: if ((arguments == null) || (arguments.length == 0)) {
097: value = LanguageUtil.get(companyId, locale, key);
098: } else {
099: value = LanguageUtil.format(companyId, locale, key,
100: arguments);
101: }
102: } catch (Exception e) {
103: if (_log.isWarnEnabled()) {
104: _log.warn(e, e);
105: }
106: }
107:
108: res.setContentType(_CONTENT_TYPE);
109:
110: ServletResponseUtil.write(res, value.getBytes("UTF-8"));
111: }
112:
113: private static final String _CONTENT_TYPE = "text/plain; charset=UTF-8";
114:
115: private static Log _log = LogFactory.getLog(LanguageServlet.class);
116:
117: }
|