Advertisement
Guest User

Untitled

a guest
Jul 15th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.26 KB | None | 0 0
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5.  
  6. package sun.net.www.protocol.ftp;
  7.  
  8. import java.io.BufferedInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.FilterInputStream;
  11. import java.io.FilterOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.net.InetSocketAddress;
  16. import java.net.Proxy;
  17. import java.net.ProxySelector;
  18. import java.net.SocketPermission;
  19. import java.net.URI;
  20. import java.net.URL;
  21. import java.net.UnknownHostException;
  22. import java.net.Proxy.Type;
  23. import java.security.AccessController;
  24. import java.security.Permission;
  25. import java.security.PrivilegedAction;
  26. import java.util.Iterator;
  27. import java.util.StringTokenizer;
  28. import sun.net.ProgressMonitor;
  29. import sun.net.ProgressSource;
  30. import sun.net.ftp.FtpClient;
  31. import sun.net.ftp.FtpLoginException;
  32. import sun.net.ftp.FtpProtocolException;
  33. import sun.net.www.MessageHeader;
  34. import sun.net.www.MeteredStream;
  35. import sun.net.www.ParseUtil;
  36. import sun.net.www.URLConnection;
  37. import sun.net.www.protocol.http.HttpURLConnection;
  38. import sun.security.action.GetPropertyAction;
  39.  
  40. public class FtpURLConnection extends URLConnection {
  41. HttpURLConnection http;
  42. private Proxy instProxy;
  43. InputStream is;
  44. OutputStream os;
  45. FtpClient ftp;
  46. Permission permission;
  47. String password;
  48. String user;
  49. String host;
  50. String pathname;
  51. String filename;
  52. String fullpath;
  53. int port;
  54. static final int NONE = 0;
  55. static final int ASCII = 1;
  56. static final int BIN = 2;
  57. static final int DIR = 3;
  58. int type;
  59. private int connectTimeout;
  60. private int readTimeout;
  61.  
  62. public FtpURLConnection(URL var1) {
  63. this(var1, (Proxy)null);
  64. }
  65.  
  66. FtpURLConnection(URL var1, Proxy var2) {
  67. super(var1);
  68. this.http = null;
  69. this.is = null;
  70. this.os = null;
  71. this.ftp = null;
  72. this.type = 0;
  73. this.connectTimeout = -1;
  74. this.readTimeout = -1;
  75. this.instProxy = var2;
  76. this.host = var1.getHost();
  77. this.port = var1.getPort();
  78. String var3 = var1.getUserInfo();
  79. if(var3 != null) {
  80. int var4 = var3.indexOf(58);
  81. if(var4 == -1) {
  82. this.user = ParseUtil.decode(var3);
  83. this.password = null;
  84. } else {
  85. this.user = ParseUtil.decode(var3.substring(0, var4++));
  86. this.password = ParseUtil.decode(var3.substring(var4));
  87. }
  88. }
  89.  
  90. }
  91.  
  92. private void setTimeouts() {
  93. if(this.ftp != null) {
  94. if(this.connectTimeout >= 0) {
  95. this.ftp.setConnectTimeout(this.connectTimeout);
  96. }
  97.  
  98. if(this.readTimeout >= 0) {
  99. this.ftp.setReadTimeout(this.readTimeout);
  100. }
  101. }
  102.  
  103. }
  104.  
  105. public synchronized void connect() throws IOException {
  106. if(!this.connected) {
  107. Proxy var1 = null;
  108. if(this.instProxy != null) {
  109. var1 = this.instProxy;
  110. if(var1.type() == Type.HTTP) {
  111. this.http = new HttpURLConnection(this.url, this.instProxy);
  112. this.http.setDoInput(this.getDoInput());
  113. this.http.setDoOutput(this.getDoOutput());
  114. if(this.connectTimeout >= 0) {
  115. this.http.setConnectTimeout(this.connectTimeout);
  116. }
  117.  
  118. if(this.readTimeout >= 0) {
  119. this.http.setReadTimeout(this.readTimeout);
  120. }
  121.  
  122. this.http.connect();
  123. this.connected = true;
  124. return;
  125. }
  126. } else {
  127. ProxySelector var2 = (ProxySelector)AccessController.doPrivileged(new PrivilegedAction() {
  128. public ProxySelector run() {
  129. return ProxySelector.getDefault();
  130. }
  131. });
  132. if(var2 != null) {
  133. URI var3 = ParseUtil.toURI(this.url);
  134. Iterator var4 = var2.select(var3).iterator();
  135.  
  136. label100:
  137. while(true) {
  138. while(true) {
  139. if(!var4.hasNext()) {
  140. break label100;
  141. }
  142.  
  143. var1 = (Proxy)var4.next();
  144. if(var1 == null || var1 == Proxy.NO_PROXY || var1.type() == Type.SOCKS) {
  145. break label100;
  146. }
  147.  
  148. if(var1.type() == Type.HTTP && var1.address() instanceof InetSocketAddress) {
  149. InetSocketAddress var5 = (InetSocketAddress)var1.address();
  150.  
  151. try {
  152. this.http = new HttpURLConnection(this.url, var1);
  153. this.http.setDoInput(this.getDoInput());
  154. this.http.setDoOutput(this.getDoOutput());
  155. if(this.connectTimeout >= 0) {
  156. this.http.setConnectTimeout(this.connectTimeout);
  157. }
  158.  
  159. if(this.readTimeout >= 0) {
  160. this.http.setReadTimeout(this.readTimeout);
  161. }
  162.  
  163. this.http.connect();
  164. this.connected = true;
  165. return;
  166. } catch (IOException var10) {
  167. var2.connectFailed(var3, var5, var10);
  168. this.http = null;
  169. }
  170. } else {
  171. var2.connectFailed(var3, var1.address(), new IOException("Wrong proxy type"));
  172. }
  173. }
  174. }
  175. }
  176. }
  177.  
  178. if(this.user == null) {
  179. this.user = "anonymous";
  180. String var11 = (String)AccessController.doPrivileged(new GetPropertyAction("java.version"));
  181. this.password = (String)AccessController.doPrivileged(new GetPropertyAction("ftp.protocol.user", "Java" + var11 + "@"));
  182. }
  183.  
  184. try {
  185. this.ftp = FtpClient.create();
  186. if(var1 != null) {
  187. this.ftp.setProxy(var1);
  188. }
  189.  
  190. this.setTimeouts();
  191. if(this.port != -1) {
  192. this.ftp.connect(new InetSocketAddress(this.host, this.port));
  193. } else {
  194. this.ftp.connect(new InetSocketAddress(this.host, FtpClient.defaultPort()));
  195. }
  196. } catch (UnknownHostException var8) {
  197. throw var8;
  198. } catch (FtpProtocolException var9) {
  199. throw new IOException(var9);
  200. }
  201.  
  202. try {
  203. this.ftp.login(this.user, this.password == null?null:this.password.toCharArray());
  204. } catch (FtpProtocolException var7) {
  205. this.ftp.close();
  206. throw new FtpLoginException("Invalid username/password");
  207. }
  208.  
  209. this.connected = true;
  210. }
  211. }
  212.  
  213. private void decodePath(String var1) {
  214. int var2 = var1.indexOf(";type=");
  215. if(var2 >= 0) {
  216. String var3 = var1.substring(var2 + 6, var1.length());
  217. if("i".equalsIgnoreCase(var3)) {
  218. this.type = 2;
  219. }
  220.  
  221. if("a".equalsIgnoreCase(var3)) {
  222. this.type = 1;
  223. }
  224.  
  225. if("d".equalsIgnoreCase(var3)) {
  226. this.type = 3;
  227. }
  228.  
  229. var1 = var1.substring(0, var2);
  230. }
  231.  
  232. if(var1 != null && var1.length() > 1 && var1.charAt(0) == 47) {
  233. var1 = var1.substring(1);
  234. }
  235.  
  236. if(var1 == null || var1.length() == 0) {
  237. var1 = "./";
  238. }
  239.  
  240. if(!var1.endsWith("/")) {
  241. var2 = var1.lastIndexOf(47);
  242. if(var2 > 0) {
  243. this.filename = var1.substring(var2 + 1, var1.length());
  244. this.filename = ParseUtil.decode(this.filename);
  245. this.pathname = var1.substring(0, var2);
  246. } else {
  247. this.filename = ParseUtil.decode(var1);
  248. this.pathname = null;
  249. }
  250. } else {
  251. this.pathname = var1.substring(0, var1.length() - 1);
  252. this.filename = null;
  253. }
  254.  
  255. if(this.pathname != null) {
  256. this.fullpath = this.pathname + "/" + (this.filename != null?this.filename:"");
  257. } else {
  258. this.fullpath = this.filename;
  259. }
  260.  
  261. }
  262.  
  263. private void cd(String var1) throws FtpProtocolException, IOException {
  264. if(var1 != null && !var1.isEmpty()) {
  265. if(var1.indexOf(47) == -1) {
  266. this.ftp.changeDirectory(ParseUtil.decode(var1));
  267. } else {
  268. StringTokenizer var2 = new StringTokenizer(var1, "/");
  269.  
  270. while(var2.hasMoreTokens()) {
  271. this.ftp.changeDirectory(ParseUtil.decode(var2.nextToken()));
  272. }
  273.  
  274. }
  275. }
  276. }
  277.  
  278. public InputStream getInputStream() throws IOException {
  279. if(!this.connected) {
  280. this.connect();
  281. }
  282.  
  283. if(this.http != null) {
  284. return this.http.getInputStream();
  285. } else if(this.os != null) {
  286. throw new IOException("Already opened for output");
  287. } else if(this.is != null) {
  288. return this.is;
  289. } else {
  290. MessageHeader var1 = new MessageHeader();
  291. boolean var2 = false;
  292.  
  293. try {
  294. this.decodePath(this.url.getPath());
  295. if(this.filename != null && this.type != 3) {
  296. if(this.type == 1) {
  297. this.ftp.setAsciiType();
  298. } else {
  299. this.ftp.setBinaryType();
  300. }
  301.  
  302. this.cd(this.pathname);
  303. this.is = new FtpURLConnection.FtpInputStream(this.ftp, this.ftp.getFileStream(this.filename));
  304. } else {
  305. this.ftp.setAsciiType();
  306. this.cd(this.pathname);
  307. if(this.filename == null) {
  308. this.is = new FtpURLConnection.FtpInputStream(this.ftp, this.ftp.list((String)null));
  309. } else {
  310. this.is = new FtpURLConnection.FtpInputStream(this.ftp, this.ftp.nameList(this.filename));
  311. }
  312. }
  313.  
  314. try {
  315. long var3 = this.ftp.getLastTransferSize();
  316. var1.add("content-length", Long.toString(var3));
  317. if(var3 > 0L) {
  318. boolean var5 = ProgressMonitor.getDefault().shouldMeterInput(this.url, "GET");
  319. ProgressSource var6 = null;
  320. if(var5) {
  321. var6 = new ProgressSource(this.url, "GET", var3);
  322. var6.beginTracking();
  323. }
  324.  
  325. this.is = new MeteredStream(this.is, var6, var3);
  326. }
  327. } catch (Exception var9) {
  328. var9.printStackTrace();
  329. }
  330.  
  331. if(var2) {
  332. var1.add("content-type", "text/plain");
  333. var1.add("access-type", "directory");
  334. } else {
  335. var1.add("access-type", "file");
  336. String var12 = guessContentTypeFromName(this.fullpath);
  337. if(var12 == null && this.is.markSupported()) {
  338. var12 = guessContentTypeFromStream(this.is);
  339. }
  340.  
  341. if(var12 != null) {
  342. var1.add("content-type", var12);
  343. }
  344. }
  345. } catch (FileNotFoundException var10) {
  346. try {
  347. this.cd(this.fullpath);
  348. this.ftp.setAsciiType();
  349. this.is = new FtpURLConnection.FtpInputStream(this.ftp, this.ftp.list((String)null));
  350. var1.add("content-type", "text/plain");
  351. var1.add("access-type", "directory");
  352. } catch (IOException var7) {
  353. throw new FileNotFoundException(this.fullpath);
  354. } catch (FtpProtocolException var8) {
  355. throw new FileNotFoundException(this.fullpath);
  356. }
  357. } catch (FtpProtocolException var11) {
  358. throw new IOException(var11);
  359. }
  360.  
  361. this.setProperties(var1);
  362. return this.is;
  363. }
  364. }
  365.  
  366. public OutputStream getOutputStream() throws IOException {
  367. if(!this.connected) {
  368. this.connect();
  369. }
  370.  
  371. if(this.http != null) {
  372. OutputStream var1 = this.http.getOutputStream();
  373. this.http.getInputStream();
  374. return var1;
  375. } else if(this.is != null) {
  376. throw new IOException("Already opened for input");
  377. } else if(this.os != null) {
  378. return this.os;
  379. } else {
  380. this.decodePath(this.url.getPath());
  381. if(this.filename != null && this.filename.length() != 0) {
  382. try {
  383. if(this.pathname != null) {
  384. this.cd(this.pathname);
  385. }
  386.  
  387. if(this.type == 1) {
  388. this.ftp.setAsciiType();
  389. } else {
  390. this.ftp.setBinaryType();
  391. }
  392.  
  393. this.os = new FtpURLConnection.FtpOutputStream(this.ftp, this.ftp.putFileStream(this.filename, false));
  394. } catch (FtpProtocolException var2) {
  395. throw new IOException(var2);
  396. }
  397.  
  398. return this.os;
  399. } else {
  400. throw new IOException("illegal filename for a PUT");
  401. }
  402. }
  403. }
  404.  
  405. String guessContentTypeFromFilename(String var1) {
  406. return guessContentTypeFromName(var1);
  407. }
  408.  
  409. public Permission getPermission() {
  410. if(this.permission == null) {
  411. int var1 = this.url.getPort();
  412. var1 = var1 < 0?FtpClient.defaultPort():var1;
  413. String var2 = this.host + ":" + var1;
  414. this.permission = new SocketPermission(var2, "connect");
  415. }
  416.  
  417. return this.permission;
  418. }
  419.  
  420. public void setRequestProperty(String var1, String var2) {
  421. super.setRequestProperty(var1, var2);
  422. if("type".equals(var1)) {
  423. if("i".equalsIgnoreCase(var2)) {
  424. this.type = 2;
  425. } else if("a".equalsIgnoreCase(var2)) {
  426. this.type = 1;
  427. } else {
  428. if(!"d".equalsIgnoreCase(var2)) {
  429. throw new IllegalArgumentException("Value of \'" + var1 + "\' request property was \'" + var2 + "\' when it must be either \'i\', \'a\' or \'d\'");
  430. }
  431.  
  432. this.type = 3;
  433. }
  434. }
  435.  
  436. }
  437.  
  438. public String getRequestProperty(String var1) {
  439. String var2 = super.getRequestProperty(var1);
  440. if(var2 == null && "type".equals(var1)) {
  441. var2 = this.type == 1?"a":(this.type == 3?"d":"i");
  442. }
  443.  
  444. return var2;
  445. }
  446.  
  447. public void setConnectTimeout(int var1) {
  448. if(var1 < 0) {
  449. throw new IllegalArgumentException("timeouts can\'t be negative");
  450. } else {
  451. this.connectTimeout = var1;
  452. }
  453. }
  454.  
  455. public int getConnectTimeout() {
  456. return this.connectTimeout < 0?0:this.connectTimeout;
  457. }
  458.  
  459. public void setReadTimeout(int var1) {
  460. if(var1 < 0) {
  461. throw new IllegalArgumentException("timeouts can\'t be negative");
  462. } else {
  463. this.readTimeout = var1;
  464. }
  465. }
  466.  
  467. public int getReadTimeout() {
  468. return this.readTimeout < 0?0:this.readTimeout;
  469. }
  470.  
  471. protected class FtpInputStream extends FilterInputStream {
  472. FtpClient ftp;
  473.  
  474. FtpInputStream(FtpClient var2, InputStream var3) {
  475. super(new BufferedInputStream(var3));
  476. this.ftp = var2;
  477. }
  478.  
  479. public void close() throws IOException {
  480. super.close();
  481. if(this.ftp != null) {
  482. this.ftp.close();
  483. }
  484.  
  485. }
  486. }
  487.  
  488. protected class FtpOutputStream extends FilterOutputStream {
  489. FtpClient ftp;
  490.  
  491. FtpOutputStream(FtpClient var2, OutputStream var3) {
  492. super(var3);
  493. this.ftp = var2;
  494. }
  495.  
  496. public void close() throws IOException {
  497. super.close();
  498. if(this.ftp != null) {
  499. this.ftp.close();
  500. }
  501.  
  502. }
  503. }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement