Re: Nutch, samba and urls...

2006-08-17 Thread Sami Siren

Hi,

Could you please submit a JIRA issue and attach this (or perhaps the 
diff for whole plugin exluding the jcifs .jar because it is lgpl) in it.



René Treffer wrote:

Hi,

I've just written an protocol-smb, it's really simple (code attached). 
It uses the jcifs lib and seems to work - but there is some stuff I'd 
like to discuss...


Nutch is glued to URL, which works if you write an URLHandler. No 
Problem so far, but you can't install an URLHandler everywhere - have a 
look at the jcifs FAQ ( http://jcifs.samba.org/src/docs/faq.html ). Most 
important: It won't work in you war - so protocol plugins will be 
useless in a web context! Might cause a lot of trouble.
Moreover Nutch will never be able to handle \\192.168.0.1\ correctly 
with URL


Perhaps a custom URL parser (nutch currently uses URL class only for 
parsing urls) could do the job here. I have seen custom implementations 
at least in tomcat which we could perhaps borrow and extend if required.




Converting directories into html lists suck. And reproducing the code is 
even worse. Perhaps a virtual mime-type could be added (e.g. 
nutch/dir). Almost forgotten: tell my how I should index files with  
and ' in there name (currently I check for ' and change the href 
quotes). Same problem for file://


There could perhaps be a different crawler implementation to crawl local 
filesystem and these shared windows resources (and perhaps webdav too) 
efficiently.


--
 Sami Siren


Nutch, samba and urls...

2006-08-16 Thread René Treffer

Hi,

I've just written an protocol-smb, it's really simple (code attached). 
It uses the jcifs lib and seems to work - but there is some stuff I'd 
like to discuss...


Nutch is glued to URL, which works if you write an URLHandler. No 
Problem so far, but you can't install an URLHandler everywhere - have a 
look at the jcifs FAQ ( http://jcifs.samba.org/src/docs/faq.html ). Most 
important: It won't work in you war - so protocol plugins will be 
useless in a web context! Might cause a lot of trouble.
Moreover Nutch will never be able to handle \\192.168.0.1\ correctly 
with URL


Converting directories into html lists suck. And reproducing the code is 
even worse. Perhaps a virtual mime-type could be added (e.g. 
nutch/dir). Almost forgotten: tell my how I should index files with  
and ' in there name (currently I check for ' and change the href 
quotes). Same problem for file://


Most protocols are not mime-type aware (e.g. file:// - indexed my mp3 
collection with the text parser, great fun!). I've added a simple 
mime-type guess, but this shouldn't be part of the protocol handler.


Anyway, feel free to use the smb code, it's rather simple/basic.
There is still a multithreading issue left :( but the very basic 
crawling process seems to works (-threads 1). I've not yet tested the 
generated index (= I've not yet indexed my hd and I've not yet tried to 
search)


I've added the apache header, hope this is ok.
/**
 * 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.protocol.smb;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.regex.Pattern;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.UTF8;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.metadata.Metadata;
import org.apache.nutch.net.protocols.HttpDateFormat;
import org.apache.nutch.net.protocols.Response;
import org.apache.nutch.protocol.Content;
import org.apache.nutch.protocol.Protocol;
import org.apache.nutch.protocol.ProtocolOutput;
import org.apache.nutch.protocol.ProtocolStatus;
import org.apache.nutch.util.mime.MimeType;
import org.apache.nutch.util.mime.MimeTypes;

/**
 * Smb.java handles smb data access.
 * 
 * @author treffer
 */
public class Smb implements Protocol {

	static {
		try {
			jcifs.Config.registerSmbURLHandler();
		} catch (Throwable t) {}
	}

	private static Pattern nonTopLevel = Pattern.compile(smb://[^/]+/.+);

	//Don't download anything unless specified!
	private int maxContentLength;

	public static final Log LOG = LogFactory.getLog(Smb.class);

	private Configuration conf;

	private Metadata header = new Metadata();

	private MimeTypes mimeTypes;

	public ProtocolOutput getProtocolOutput(UTF8 url, CrawlDatum datum) {
		System.out.println(url.toString());
		/*
		 * Most of the code here was inspired from FtpResponse
		 */
		try {
			String urlString = url.toString();
			//BTW: how can I handle non smb: urls?
			urlString.replace('\\', '/');
			if (!urlString.startsWith(smb:)) {
urlString = (
		urlString.startsWith(//)
		? smb:
		: ((urlString.startsWith(/))
			? smb:/
			: smb://)
		)
	+ urlString;
			}
			SmbFile smbFile = new SmbFile(urlString);
			byte data[];
			if (!smbFile.isFile()) {
data = dir2html(smbFile, urlString);
			} else {
data = file2html(smbFile, urlString);
			}
			return new ProtocolOutput(new Content(urlString, urlString, data, header.get(Response.CONTENT_TYPE), header, conf));
		} catch (Exception e) {
			return new ProtocolOutput(null, new ProtocolStatus(e));
		}
	}

	private byte[] dir2html(SmbFile smbFile, String urlString) throws SmbException {
		header.set(Response.LAST_MODIFIED, Long.toString(smbFile.getLastModified()));

		SmbFile[] subs = smbFile.listFiles();
		//Try to guess buffer size
		header.set(Response.CONTENT_TYPE, text/html);

		int bufferSize = subs.length * (urlString.length() + 100) + 2048;
		StringBuffer sb = new StringBuffer(bufferSize);
		sb.append(htmlhead);
		sb.append(titleIndex of +smbFile.toString()+/title/head\n);
		sb.append(bodyh1Index of +smbFile.toString()+/h1pre\n);
		if (!smbFile.getParent().equals(smb://)) {
			//Not toplevel, Host + Directory = reference to parent directory
		sb.append(a href=);