View Javadoc
1   package uk.ac.ebi.intenz.webapp.controller;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.URL;
6   import java.net.URLConnection;
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServlet;
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  
12  import org.apache.log4j.Logger;
13  import uk.ac.ebi.intenz.webapp.IntEnzConfig;
14  
15  /**
16   * Simple servlet which reads an external sitemap file and writes it as is to
17   * the response. This is needed in order to have the sitemap decoupled from
18   * the war file, as the sitemap specs require it to be under the same base
19   * URL as the indexed URLs.
20   */
21  public class SitemapServlet extends HttpServlet {
22  
23  	private Logger LOGGER = Logger.getLogger(SitemapServlet.class);
24  
25      public void service(HttpServletRequest request, HttpServletResponse response)
26      throws ServletException, IOException {
27          response.setContentType("application/gzip");
28          LOGGER.info("Getting URL connection to sitemap...");
29          URL url = null;
30          IntEnzConfig config = (IntEnzConfig) request.getSession()
31                  .getServletContext().getAttribute("intenzConfig");
32          url = new URL(config.getSitemapUrl());
33          URLConnection con = url.openConnection(java.net.Proxy.NO_PROXY);
34          LOGGER.info("Connecting to sitemap...");
35          //con.connect();
36          LOGGER.info("Connected");
37          InputStream is = null;
38          try {
39              is = con.getInputStream();
40              int r = -1;
41              LOGGER.debug("Starting to read sitemap...");
42              while ((r = is.read()) != -1){
43                  response.getOutputStream().write(r);
44              }
45              response.getOutputStream().flush();
46              response.flushBuffer();
47              LOGGER.debug("... Read and served");
48          } finally {
49              if (is != null) is.close();
50          }
51      }
52  }