Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Add servlet Cached.java that serves cached Content of any mime type.

John Xing55943eae044c
diff --git a/CHANGES.txt b/CHANGES.txt
index cfb82516c..af63d7b5a 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -39,6 +39,9 @@ Release 0.7
for anything but anchors. (Piotr Kosiorowski via Doug Cutting
20050323)
+ 7. Add servlet Cached.java that serves cached Content of any mime type.
+ Slightly modified are web.xml and cached.jsp.
+ (John Xing, 20050401)
Release 0.6
diff --git a/src/java/org/apache/nutch/servlet/Cached.java b/src/java/org/apache/nutch/servlet/Cached.java
new file mode 100644
index 000000000..dba6f64f5
--- /dev/null
+++ b/src/java/org/apache/nutch/servlet/Cached.java
@@ -0,0 +1,105 @@
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nutch.servlet;
+
+import org.apache.nutch.searcher.NutchBean;
+import org.apache.nutch.searcher.Hit;
+import org.apache.nutch.searcher.HitDetails;
+
+import org.apache.nutch.util.NutchConf;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.util.Properties;
+
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * A servlet that serves raw Content of any mime type.
+ *
+ * If it fails with java.lang.OutOfMemoryError,
+ * you may have to increase heap size when starting container,
+ * such as -Xms1024M -Xmx1024M
+ *
+ * @author John Xing
+ */
+
+public class Cached extends HttpServlet {
+
+ NutchBean bean = null;
+
+ public void init() {
+ try {
+ bean = NutchBean.get(this.getServletContext());
+ } catch (IOException e) {
+ // nothing
+ }
+ }
+
+ public void destroy() {
+ // maybe clean bean?
+ // nothing now
+ }
+
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws IOException {
+
+ // quit if no bean
+ if (bean == null)
+ return;
+
+ bean.LOG.info("request from " + request.getRemoteAddr());
+
+ Hit hit = new Hit(Integer.parseInt(request.getParameter("idx")),
+ Integer.parseInt(request.getParameter("id")), 0.0f, null);
+ HitDetails details = bean.getDetails(hit);
+
+ // raw bytes
+ byte[] bytes = bean.getContent(details);
+
+ // pass all original headers? only these for now.
+ Properties metaData = bean.getParseData(details).getMetadata();
+ String contentType = (String) metaData.get("Content-Type");
+ //String lastModified = (String) metaData.get("Last-Modified");
+ //String contentLength = (String) metaData.get("Content-Length");
+ // better use this, since it may have been truncated during fetch
+ // or give warning if they don't match?
+ int contentLength = bytes.length;
+
+ // response
+ response.setContentType(contentType);
+ response.setContentLength(contentLength);
+
+ OutputStream os = response.getOutputStream();
+ os.write(bytes);
+ // need this or flush more frequently?
+ //os.flush();
+ os.close();
+
+ return;
+ }
+
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws IOException {
+ doGet(request, response);
+ }
+
+}
diff --git a/src/web/jsp/cached.jsp b/src/web/jsp/cached.jsp
index 3b7fb6bb3..b492b61bc 100644
--- a/src/web/jsp/cached.jsp
+++ b/src/web/jsp/cached.jsp
@@ -13,6 +13,8 @@
Hit hit = new Hit(Integer.parseInt(request.getParameter("idx")),
Integer.parseInt(request.getParameter("id")), 0.0f, null);
HitDetails details = bean.getDetails(hit);
+ String id = "idx=" + hit.getIndexNo() + "&id=" + hit.getIndexDocNo();
+
String language =
ResourceBundle.getBundle("org.nutch.jsp.cached", request.getLocale())
.getLocale().getLanguage();
@@ -40,7 +42,10 @@
else
content = new String(bean.getContent(details));
}
-%><base href="<%=details.getValue("url")%>">
+%>
+<!--
+<base href="<%=details.getValue("url")%>">
+-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
out.flush();
@@ -58,8 +63,17 @@
FIXME: have to sanitize 'content' : e.g. removing unncessary part
of head elememt
-->
+<% if (contentType.startsWith("text/html")) {%>
+
<% if (content != null && !content.equals("")) {%>
<%= content %>
<% } else { %>
<i18n:message key="noContent"/>
<% } %>
+
+<% } else { %>
+
+The cached content has mime type "<%=contentType%>",
+click this <a href="/servlet/cached?<%=id%>">link</a> to download it directly.
+
+<% } %>
diff --git a/src/web/web.xml b/src/web/web.xml
index cce75125f..347f1bb2c 100755
--- a/src/web/web.xml
+++ b/src/web/web.xml
@@ -1,20 +1,32 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
-
-<!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
-
-<web-app>
-
-<welcome-file-list>
- <welcome-file>search.html</welcome-file>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.jsp</welcome-file>
-</welcome-file-list>
-
-<taglib>
- <taglib-uri>http://jakarta.apache.org/taglibs/i18n</taglib-uri>
- <taglib-location>/WEB-INF/taglibs-i18n.tld</taglib-location>
- </taglib>
-
-</web-app>
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<!DOCTYPE web-app
+ PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+ "http://java.sun.com/dtd/web-app_2_3.dtd">
+
+<web-app>
+
+<!-- order is very important here -->
+
+<servlet>
+ <servlet-name>Cached</servlet-name>
+ <servlet-class>org.apache.nutch.servlet.Cached</servlet-class>
+</servlet>
+
+<servlet-mapping>
+ <servlet-name>Cached</servlet-name>
+ <url-pattern>/servlet/cached</url-pattern>
+</servlet-mapping>
+
+<welcome-file-list>
+ <welcome-file>search.html</welcome-file>
+ <welcome-file>index.html</welcome-file>
+ <welcome-file>index.jsp</welcome-file>
+</welcome-file-list>
+
+<taglib>
+ <taglib-uri>http://jakarta.apache.org/taglibs/i18n</taglib-uri>
+ <taglib-location>/WEB-INF/taglibs-i18n.tld</taglib-location>
+ </taglib>
+
+</web-app>