001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.layoutsadmin.action;
016    
017    import com.liferay.portal.LayoutSetVirtualHostException;
018    import com.liferay.portal.NoSuchLayoutSetException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.LayoutSet;
025    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.RobotsUtil;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    import org.apache.struts.action.Action;
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author David Truong
039     */
040    public class RobotsAction extends Action {
041    
042            @Override
043            public ActionForward execute(
044                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
045                            HttpServletResponse response)
046                    throws Exception {
047    
048                    try {
049                            String virtualHostname = PortalUtil.getHost(request);
050    
051                            LayoutSet layoutSet = null;
052    
053                            try {
054                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
055                                            virtualHostname);
056                            }
057                            catch (LayoutSetVirtualHostException lsvhe) {
058                            }
059                            catch (NoSuchLayoutSetException nslse) {
060                            }
061    
062                            String robots = RobotsUtil.getRobots(layoutSet);
063    
064                            ServletResponseUtil.sendFile(
065                                    request, response, null, robots.getBytes(StringPool.UTF8),
066                                    ContentTypes.TEXT_PLAIN_UTF8);
067                    }
068                    catch (Exception e) {
069                            if (_log.isWarnEnabled()) {
070                                    _log.warn(e, e);
071                            }
072    
073                            PortalUtil.sendError(
074                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
075                                    response);
076                    }
077    
078                    return null;
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(RobotsAction.class);
082    
083    }