gt:       catch (NoRouteToHostException e) {
context: package com.cloudera.server.web.cmf;
  
  import com.cloudera.cmf.model.DbHost;
  import com.cloudera.cmf.model.DbHostHeartbeat;
  import com.cloudera.cmf.model.DbRole;
  import com.cloudera.cmf.model.DbService;
  import com.cloudera.cmf.persist.CmfEntityManager;
  import com.cloudera.cmf.protocol.HostStatus;
  import com.cloudera.cmf.service.HandlerUtil;
  import com.cloudera.cmf.service.RoleHandler;
  import com.cloudera.cmf.service.ServiceDataProvider;
  import com.cloudera.cmf.service.ServiceHandler;
  import com.cloudera.cmf.service.ServiceHandlerRegistry;
  import com.cloudera.cmf.service.config.CommonParamSpecs;
  import com.cloudera.cmf.service.config.ConfigSpec;
  import com.cloudera.cmf.service.config.NumericParamSpec;
  import com.cloudera.cmf.service.config.ParamParseException;
  import com.cloudera.cmf.service.config.StacksLogFileEvaluator;
  import com.cloudera.enterprise.ThrottlingLogger;
  import com.cloudera.server.common.ConnectionUtils;
  import com.cloudera.server.common.ConnectionUtils.AgentResponse;
  import com.cloudera.server.web.cmf.config.CommonConfigOperation;
  import com.cloudera.server.web.common.I18n;
  import com.google.common.base.Preconditions;
  import com.google.common.collect.ImmutableMap;
  import com.google.common.collect.Maps;
  import com.google.common.io.ByteStreams;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.ConnectException;
  import java.net.HttpURLConnection;
  import java.net.NoRouteToHostException;
  import java.net.URL;
  import java.nio.charset.Charset;
  import java.util.Map;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.io.IOUtils;
  import org.jamon.Renderer;
  import org.joda.time.Duration;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  @Controller
  @RequestMapping({"/*"})
  public class StacksController extends WebController
  {
    protected static final Logger LOG = LoggerFactory.getLogger(StacksController.class);
  
    protected static final Logger THROTTLED_LOG = new ThrottlingLogger(LOG, Duration.standardMinutes(5L));
  
    private static String getStacksLogFilePath(ServiceDataProvider sdp, DbRole role)
    {
      Preconditions.checkNotNull(sdp);
      Preconditions.checkNotNull(role);
      DbService service = role.getService();
      RoleHandler roleHandler = sdp.getServiceHandlerRegistry().getRoleHandler(role);
  
      ServiceHandler serviceHandler = sdp.getServiceHandlerRegistry().get(service);
      try
      {
        Map configs = HandlerUtil.getConfigs(serviceHandler, service, role, roleHandler);
  
        return StacksLogFileEvaluator.getStacksLogFile(sdp, service, role, roleHandler, configs);
      }
      catch (Exception e)
      {
        THROTTLED_LOG.error("Could not determine stacks log file for " + role.getName(), e);
      }
      return null;
    }
  
    public static String stacksLogDownloadUrl(ServiceDataProvider sdp, DbRole role)
    {
      Preconditions.checkNotNull(sdp);
      Preconditions.checkNotNull(role);
      String path = getStacksLogFilePath(sdp, role);
      if (path == null)
      {
        return null;
      }
      DbHostHeartbeat hostHeartbeat = role.getHost().getHeartbeat();
      if (hostHeartbeat == null) {
        THROTTLED_LOG.info("No heartbeat for host " + role.getHost().getName());
        return null;
      }
      ImmutableMap params = ImmutableMap.of("path", path, "onlyTail", Boolean.toString(false), "compress", Boolean.toString(true));
  
      HostStatus status = hostHeartbeat.getHostStatus();
      return CmfPath.buildGetUrl(status.getAgentUrl() + "download_log", params);
    }
  
    @RequestMapping({"stacks/{roleId}/viewLog"})
    public void roleStacksViewLog(@PathVariable long roleId, HttpServletResponse response)
      throws Exception
    {
      CmfEntityManager cmfEM = createCmfEntityManager();
      try {
        cmfEM.beginForRollbackAndReadonly();
        DbRole role = validateRole(cmfEM, roleId);
        DbHost host = role.getHost();
        if (!validateHostAndAgentInfo(response, host))
          return;
        String url = stacksLogDownloadUrl(getServiceDataProvider(), role);
  
        if (url == null) {
          throw new MessageException("Could not determine stacks log download URL.");
        }
  
        ConnectionUtils.AgentResponse agentResponse = ConnectionUtils.readAgentUrl(url, true, host.getHeartbeat());
        try
        {
          if (agentResponse.succeeded()) {
            response.setContentType("text/plain");
            ByteStreams.copy(agentResponse.inputStream, response.getOutputStream());
          }
          else if (agentResponse.fileNotFound()) {
            response.setContentType("text/html");
            String page = new NoStacksLog().makeRenderer(getURLForStacksCollectionConfigs(role), CmfPath.Host.buildAgentLogURL(role.getHost().getId())).asString();
  
            response.getOutputStream().write(page.getBytes(Charset.forName("UTF-8")));
          } else {
            response.setContentType("text/plain");
            response.sendError(agentResponse.responseCode, agentResponse.message);
          }
        }
        finally {
          IOUtils.closeQuietly(agentResponse.inputStream);
          response.setStatus(200);
          response.flushBuffer();
        }
      } finally {
        cmfEM.close();
      }
    }
  
    public static StacksLogBundleDownloadData stacksLogsBundleDownload(ServiceDataProvider sdp, DbRole role)
    {
      Preconditions.checkNotNull(sdp);
      Preconditions.checkNotNull(role);
      String path = getStacksLogFilePath(sdp, role);
      if (path == null)
      {
        return null;
      }RoleHandler roleHandler = sdp.getServiceHandlerRegistry().getRoleHandler(role);
      Long retainedBytes;
      try {
        retainedBytes = (Long)((NumericParamSpec)roleHandler.getConfigSpec().getParam(CommonParamSpecs.STACKS_COLLECTION_DATA_RETENTION)).extract(role);
      }
      catch (ParamParseException e) {
        throw new RuntimeException(e);
      }
      Map params = Maps.newHashMap();
      params.put("max_bytes", retainedBytes);
      params.put("stacks_filename", path);
      HostStatus status = role.getHost().getHeartbeat().getHostStatus();
      return new StacksLogBundleDownloadData(CmfPath.buildGetUrl(status.getAgentUrl() + "compressed_stacks_logs", params), path + ".zip", "application/zip");
    }
  
    @RequestMapping({"stacks/{roleId}/downloadLogs"})
    public void roleStacksDownloadLogs(@PathVariable long roleId, HttpServletResponse response)
      throws Exception
    {
      CmfEntityManager cmfEM = createCmfEntityManager();
      try {
        cmfEM.beginForRollbackAndReadonly();
        DbRole role = validateRole(cmfEM, roleId);
        DbHost host = role.getHost();
        if (!validateHostAndAgentInfo(response, host))
          return;
        StacksLogBundleDownloadData data = stacksLogsBundleDownload(getServiceDataProvider(), role);
  
        if (data == null) {
          throw new MessageException("Could not determine stacks log bundle download URL.");
        }
  
        URL url = new URL(data.agentUrl);
        InputStream is = null;
        try {
          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  
          DbHostHeartbeat hb = host.getHeartbeat();
          ConnectionUtils.setAgentTokenCookie(connection, hb);
          if (connection.getResponseCode() == 200) {
            response.setContentType(data.contentType);
            response.addHeader("Content-disposition:", "attachment; filename=\"" + data.filename + "\"");
  
            response.setStatus(200);
            is = connection.getInputStream();
            ByteStreams.copy(is, response.getOutputStream());
          } else {
            response.sendError(502);
          }
        } catch (ConnectException e) {
          String message = I18n.t("message.noConnectionToHost");
          response.sendError(502, e.getMessage() + "\n" + message);
        }
  