/* * SMBResponse.java * * Created on 02 January 2007, 12:12 * */ package org.apache.nutch.protocol.smb; import java.io.File; import java.io.IOException; import java.net.URL; import jcifs.smb.SmbException; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.nutch.crawl.CrawlDatum; import org.apache.nutch.metadata.Metadata; import org.apache.nutch.net.protocols.Response; import org.apache.nutch.protocol.Content; /** * * @author Armel T. Nene */ public class SMBResponse { private String orig; private String base; private byte[] content; private int code; private Metadata headers = new Metadata(); private final SMB file; private Configuration conf; public static final Log LOG = LogFactory.getLog(SMB.class); /** Creates a new instance of SMBResponse * @param the url, the crawlDatum, the file, configuration for Nutch * @throws SMBException, IOException */ public SMBResponse(URL url, CrawlDatum datum, SMB file, Configuration conf) throws SMBException, IOException { this.orig = url.toString(); this.base = url.toString(); this.file = file; this.conf = conf; // check if protocol is a window file share protocol if(!"smb".equals(url.getProtocol())) throw new SMBException("Not an SMB url: "+url); try { this.content = null; // for debugging purpose only // System.out.println("creating an smb file from the following url: "+url.toString()); SmbFile f = new SmbFile(url); // // for debugging purpose only // boolean b = f.exists(); // System.out.println("Connected to: "+f.getName()+" successfully: "+b); // SmbFile[] list = f.listFiles(); // // for (int i = 0; i Integer.MAX_VALUE) throw new SMBException("file is too large, size: "+size); // capture content int len = (int)size; byte[] bytes = new byte[len]; java.io.InputStream is = new SmbFileInputStream(f); int offset = 0; int n = 0; while(offset < size && (n = is.read(this.content, offset, len-offset)) >= 0){ offset += n; } if(offset < len){ if(LOG.isWarnEnabled()) LOG.warn("not enough bytes read from file: "+f.getCanonicalPath()); } is.close(); // set headers headers.set(Response.CONTENT_LENGTH, new Long(size).toString()); headers.set(Response.LAST_MODIFIED, String.valueOf(f.lastModified())); headers.set(Response.CONTENT_TYPE, ""); // response code this.code = 200; // http OK } /** * This method generate an html file from a list of SmbFile * @param the smbFile, path, boolean value to include dotdot in the file path * @return html content as byte */ private byte[] list2html(SmbFile[] smbFile, String path, boolean includeDotDot) throws SmbException { StringBuffer x = new StringBuffer(""); x.append("Index of "+path+"\n"); x.append("

Index of "+path+"

"); if(includeDotDot){ x.append("../\t-\t-\n"); } SmbFile f; for(int i=0; i"+name+"/\t"); x.append(time+"\t-\n"); } else{} // ignore any other } x.append("\n"); return new String(x).getBytes(); } }