📄 fetchftp.html
字号:
<a name="155" href="#155">155</a> + <span class="string">"completed in this number of seconds, give up (and retry later)."</span>;<a name="156" href="#156">156</a> <a name="157" href="#157">157</a> <em>/**<em>* The default value for the <code>timeout-seconds</code> attribute. */</em></em><a name="158" href="#158">158</a> <strong>final</strong> <strong>private</strong> <strong>static</strong> <strong>int</strong> DEFAULT_TIMEOUT = 1200;<a name="159" href="#159">159</a> <a name="160" href="#160">160</a> <a name="161" href="#161">161</a> <em>/**<em>*</em></em><a name="162" href="#162">162</a> <em> * Constructs a new <code>FetchFTP</code>.</em><a name="163" href="#163">163</a> <em> * </em><a name="164" href="#164">164</a> <em> * @param name the name of this processor</em><a name="165" href="#165">165</a> <em> */</em><a name="166" href="#166">166</a> <strong>public</strong> <a href="../../../../org/archive/crawler/fetcher/FetchFTP.html">FetchFTP</a>(String name) {<a name="167" href="#167">167</a> <strong>super</strong>(name, <span class="string">"FTP Fetcher."</span>);<a name="168" href="#168">168</a> add(ATTR_USERNAME, DESC_USERNAME, DEFAULT_USERNAME);<a name="169" href="#169">169</a> add(ATTR_PASSWORD, DESC_PASSWORD, DEFAULT_PASSWORD);<a name="170" href="#170">170</a> add(ATTR_EXTRACT, DESC_EXTRACT, DEFAULT_EXTRACT);<a name="171" href="#171">171</a> add(ATTR_EXTRACT_PARENT, DESC_EXTRACT_PARENT, DEFAULT_EXTRACT_PARENT);<a name="172" href="#172">172</a> add(ATTR_MAX_LENGTH, DESC_MAX_LENGTH, DEFAULT_MAX_LENGTH);<a name="173" href="#173">173</a> add(ATTR_BANDWIDTH, DESC_BANDWIDTH, DEFAULT_BANDWIDTH);<a name="174" href="#174">174</a> add(ATTR_TIMEOUT, DESC_TIMEOUT, DEFAULT_TIMEOUT);<a name="175" href="#175">175</a> }<a name="176" href="#176">176</a> <a name="177" href="#177">177</a> <a name="178" href="#178">178</a> <em>/**<em>*</em></em><a name="179" href="#179">179</a> <em> * Convenience method for adding an attribute.</em><a name="180" href="#180">180</a> <em> * </em><a name="181" href="#181">181</a> <em> * @param name The name of the attribute</em><a name="182" href="#182">182</a> <em> * @param desc The description of the attribute</em><a name="183" href="#183">183</a> <em> * @param def The default value for the attribute</em><a name="184" href="#184">184</a> <em> */</em><a name="185" href="#185">185</a> <strong>private</strong> <strong>void</strong> add(String name, String desc, Object def) {<a name="186" href="#186">186</a> <a href="../../../../org/archive/crawler/settings/SimpleType.html">SimpleType</a> st = <strong>new</strong> <a href="../../../../org/archive/crawler/settings/SimpleType.html">SimpleType</a>(name, desc, def);<a name="187" href="#187">187</a> addElementToDefinition(st);<a name="188" href="#188">188</a> }<a name="189" href="#189">189</a> <a name="190" href="#190">190</a> <a name="191" href="#191">191</a> <em>/**<em>*</em></em><a name="192" href="#192">192</a> <em> * Convenience method for extracting an attribute.</em><a name="193" href="#193">193</a> <em> * If a value for the specified name cannot be found,</em><a name="194" href="#194">194</a> <em> * a warning is written to the log and the specified</em><a name="195" href="#195">195</a> <em> * default value is returned instead.</em><a name="196" href="#196">196</a> <em> * </em><a name="197" href="#197">197</a> <em> * @param context The context for the attribute fetch</em><a name="198" href="#198">198</a> <em> * @param name The name of the attribute to fetch</em><a name="199" href="#199">199</a> <em> * @param def The value to return if the attribute isn't found</em><a name="200" href="#200">200</a> <em> * @return The value of that attribute</em><a name="201" href="#201">201</a> <em> */</em><a name="202" href="#202">202</a> <strong>private</strong> Object get(Object context, String name, Object def) {<a name="203" href="#203">203</a> <strong>try</strong> {<a name="204" href="#204">204</a> <strong>return</strong> getAttribute(context, name);<a name="205" href="#205">205</a> } <strong>catch</strong> (AttributeNotFoundException e) {<a name="206" href="#206">206</a> logger.warning(<span class="string">"Attribute not found (using default): "</span> + name);<a name="207" href="#207">207</a> <strong>return</strong> def;<a name="208" href="#208">208</a> }<a name="209" href="#209">209</a> }<a name="210" href="#210">210</a> <a name="211" href="#211">211</a> <a name="212" href="#212">212</a> <em>/**<em>*</em></em><a name="213" href="#213">213</a> <em> * Processes the given URI. If the given URI is not an FTP URI, then</em><a name="214" href="#214">214</a> <em> * this method does nothing. Otherwise an attempt is made to connect</em><a name="215" href="#215">215</a> <em> * to the FTP server.</em><a name="216" href="#216">216</a> <em> * </em><a name="217" href="#217">217</a> <em> * <p>If the connection is successful, an attempt will be made to CD to </em><a name="218" href="#218">218</a> <em> * the path specified in the URI. If the remote CD command succeeds, </em><a name="219" href="#219">219</a> <em> * then it is assumed that the URI represents a directory. If the</em><a name="220" href="#220">220</a> <em> * CD command fails, then it is assumed that the URI represents</em><a name="221" href="#221">221</a> <em> * a file.</em><a name="222" href="#222">222</a> <em> * </em><a name="223" href="#223">223</a> <em> * <p>For directories, the directory listing will be fetched using</em><a name="224" href="#224">224</a> <em> * the FTP LIST command, and saved to the HttpRecorder. If the</em><a name="225" href="#225">225</a> <em> * <code>extract.from.dirs</code> attribute is set to true, then</em><a name="226" href="#226">226</a> <em> * the files in the fetched list will be added to the curi as</em><a name="227" href="#227">227</a> <em> * extracted FTP links. (It was easier to do that here, rather</em><a name="228" href="#228">228</a> <em> * than writing a separate FTPExtractor.)</em><a name="229" href="#229">229</a> <em> * </em><a name="230" href="#230">230</a> <em> * <p>For files, the file will be fetched using the FTP RETR</em><a name="231" href="#231">231</a> <em> * command, and saved to the HttpRecorder.</em><a name="232" href="#232">232</a> <em> * </em><a name="233" href="#233">233</a> <em> * <p>All file transfers (including directory listings) occur using</em><a name="234" href="#234">234</a> <em> * Binary mode transfer. Also, the local passive transfer mode</em><a name="235" href="#235">235</a> <em> * is always used, to play well with firewalls.</em><a name="236" href="#236">236</a> <em> * </em><a name="237" href="#237">237</a> <em> * @param curi the curi to process</em><a name="238" href="#238">238</a> <em> * @throws InterruptedException if the thread is interrupted during</em><a name="239" href="#239">239</a> <em> * processing</em><a name="240" href="#240">240</a> <em> */</em><a name="241" href="#241">241</a> <strong>public</strong> <strong>void</strong> innerProcess(<a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi) throws InterruptedException {<a name="242" href="#242">242</a> <strong>if</strong> (!curi.getUURI().getScheme().equals(<span class="string">"ftp"</span>)) {<a name="243" href="#243">243</a> <strong>return</strong>;<a name="244" href="#244">244</a> }<a name="245" href="#245">245</a> <a name="246" href="#246">246</a> curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());<a name="247" href="#247">247</a> <a href="../../../../org/archive/util/HttpRecorder.html">HttpRecorder</a> recorder = HttpRecorder.getHttpRecorder();<a name="248" href="#248">248</a> <a href="../../../../org/archive/net/ClientFTP.html">ClientFTP</a> client = <strong>new</strong> <a href="../../../../org/archive/net/ClientFTP.html">ClientFTP</a>();<a name="249" href="#249">249</a> <a name="250" href="#250">250</a> <strong>try</strong> {<a name="251" href="#251">251</a> fetch(curi, client, recorder);<a name="252" href="#252">252</a> } <strong>catch</strong> (FTPException e) {<a name="253" href="#253">253</a> logger.log(Level.SEVERE, <span class="string">"FTP server reported problem."</span>, e);<a name="254" href="#254">254</a> curi.setFetchStatus(e.getReplyCode());<a name="255" href="#255">255</a> } <strong>catch</strong> (IOException e) {<a name="256" href="#256">256</a> logger.log(Level.SEVERE, <span class="string">"IO Error during FTP fetch."</span>, e);<a name="257" href="#257">257</a> curi.setFetchStatus(FetchStatusCodes.S_CONNECT_LOST);<a name="258" href="#258">258</a> } <strong>finally</strong> {<a name="259" href="#259">259</a> disconnect(client);<a name="260" href="#260">260</a> curi.setContentSize(recorder.getRecordedInput().getSize());<a name="261" href="#261">261</a> curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());<a name="262" href="#262">262</a> }<a name="263" href="#263">263</a> }<a name="264" href="#264">264</a> <a name="265" href="#265">265</a> <a name="266" href="#266">266</a> <em>/**<em>*</em></em><a name="267" href="#267">267</a> <em> * Fetches a document from an FTP server.</em><a name="268" href="#268">268</a> <em> * </em><a name="269" href="#269">269</a> <em> * @param curi the URI of the document to fetch</em><a name="270" href="#270">270</a> <em> * @param client the FTPClient to use for the fetch</em><a name="271" href="#271">271</a> <em> * @param recorder the recorder to preserve the document in</em><a name="272" href="#272">272</a> <em> * @throws IOException if a network or protocol error occurs</em><a name="273" href="#273">273</a> <em> * @throws InterruptedException if the thread is interrupted</em><a name="274" href="#274">274</a> <em> */</em><a name="275" href="#275">275</a> <strong>private</strong> <strong>void</strong> fetch(<a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi, <a href="../../../../org/archive/net/ClientFTP.html">ClientFTP</a> client, <a href="../../../../org/archive/util/HttpRecorder.html">HttpRecorder</a> recorder) <a name="276" href="#276">276</a> throws IOException, InterruptedException {<a name="277" href="#277">277</a> <em class="comment">// Connect to the FTP server.</em><a name="278" href="#278">278</a> <a href="../../../../org/archive/net/UURI.html">UURI</a> uuri = curi.getUURI();<a name="279" href="#279">279</a> <strong>int</strong> port = uuri.getPort();<a name="280" href="#280">280</a> <strong>if</strong> (port == -1) {<a name="281" href="#281">281</a> port = 21;<a name="282" href="#282">282</a> }<a name="283" href="#283">283</a> client.connectStrict(uuri.getHost(), port);<a name="284" href="#284">284</a> <a name="285" href="#285">285</a> <em class="comment">// Authenticate.</em><a name="286" href="#286">286</a> String[] auth = getAuth(curi);<a name="287" href="#287">287</a> client.loginStrict(auth[0], auth[1]);<a name="288" href="#288">288</a> <a name="289" href="#289">289</a> <em class="comment">// The given resource may or may not be a directory.</em><a name="290" href="#290">290</a> <em class="comment">// To figure out which is which, execute a CD command to</em><a name="291" href="#291">291</a> <em class="comment">// the UURI's path. If CD works, it's a directory.</em><a name="292" href="#292">292</a> <strong>boolean</strong> dir = client.changeWorkingDirectory(uuri.getPath());<a name="293" href="#293">293</a> <strong>if</strong> (dir) {<a name="294" href="#294">294</a> curi.setContentType(<span class="string">"text/plain"</span>);<a name="295" href="#295">295</a> }<a name="296" href="#296">296</a> <a name="297" href="#297">297</a> <em class="comment">// TODO: A future version of this class could use the system string to</em><a name="298" href="#298">298</a> <em class="comment">// set up custom directory parsing if the FTP server doesn't support </em><a name="299" href="#299">299</a> <em class="comment">// the nlist command.</em><a name="300" href="#300">300</a> <strong>if</strong> (logger.isLoggable(Level.FINE)) {<a name="301" href="#301">301</a> String system = client.getSystemName();<a name="302" href="#302">302</a> logger.fine(system);<a name="303" href="#303">303</a> }<a name="304" href="#304">304</a> <a name="305" href="#305">305</a> <em class="comment">// Get a data socket. This will either be the result of a NLIST</em><a name="306" href="#306">306</a> <em class="comment">// command for a directory, or a RETR command for a file.</em><a name="307" href="#307">307</a> <strong>int</strong> command = dir ? FTPCommand.NLST : FTPCommand.RETR;<a name="308" href="#308">308</a> String path = dir ? <span class="string">"."</span> : uuri.getPath();<a name="309" href="#309">309</a> client.enterLocalPassiveMode();<a name="310" href="#310">310</a> client.setBinary();<a name="311" href="#311">311</a> Socket socket = client.openDataConnection(command, path);<a name="312" href="#312">312</a> curi.setFetchStatus(client.getReplyCode());<a name="313" href="#313">313</a> <a name="314" href="#314">314</a> <em class="comment">// Save the streams in the CURI, where downstream processors</em><a name="315" href="#315">315</a> <em class="comment">// expect to find them.</em><a name="316" href="#316">316</a> <strong>try</strong> {<a name="317" href="#317">317</a> saveToRecorder(curi, socket, recorder);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -