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.NoSuchLayoutSetException;
024: import com.liferay.portal.kernel.util.ContentTypes;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.model.LayoutSet;
027: import com.liferay.portal.service.LayoutSetLocalServiceUtil;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portal.util.SitemapUtil;
030:
031: import java.io.IOException;
032: import java.io.OutputStreamWriter;
033:
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServlet;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * <a href="SitemapServlet.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Jorge Ferrer
046: *
047: */
048: public class SitemapServlet extends HttpServlet {
049:
050: public void service(HttpServletRequest req, HttpServletResponse res)
051: throws IOException, ServletException {
052:
053: OutputStreamWriter out = null;
054:
055: try {
056: String host = PortalUtil.getHost(req);
057:
058: long groupId = ParamUtil.getLong(req, "groupId");
059: boolean privateLayout = ParamUtil.getBoolean(req,
060: "privateLayout");
061:
062: LayoutSet layoutSet = null;
063:
064: if (groupId > 0) {
065: layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
066: groupId, privateLayout);
067: } else {
068: layoutSet = LayoutSetLocalServiceUtil
069: .getLayoutSet(host);
070: }
071:
072: String portalURL = PortalUtil.getPortalURL(host, req
073: .getServerPort(), req.isSecure());
074:
075: String mainPath = PortalUtil.PATH_MAIN;
076:
077: String sitemap = SitemapUtil.getSitemap(layoutSet
078: .getGroupId(), layoutSet.isPrivateLayout(),
079: portalURL + mainPath);
080:
081: if (!res.isCommitted()) {
082: res.setContentType(ContentTypes.TEXT_XML_UTF8);
083:
084: out = new OutputStreamWriter(res.getOutputStream());
085:
086: out.write(sitemap);
087: }
088: } catch (NoSuchLayoutSetException e) {
089: PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND, e,
090: req, res);
091: } catch (Exception e) {
092: if (_log.isWarnEnabled()) {
093: _log.warn(e, e);
094: }
095:
096: PortalUtil.sendError(
097: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e,
098: req, res);
099: } finally {
100: if (out != null) {
101: out.flush();
102: out.close();
103: }
104: }
105: }
106:
107: private static Log _log = LogFactory.getLog(SitemapServlet.class);
108:
109: }
|