PageRenderTime 134ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.ecf.provider.filetransfer.source_3.2.0.v20110531-2218/org/eclipse/ecf/provider/filetransfer/browse/URLFileSystemBrowser.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 204 lines | 146 code | 22 blank | 36 comment | 38 complexity | 4fff04ef2308aebc77bfc8bedcd165d3 MD5 | raw file
  1. /****************************************************************************
  2. * Copyright (c) 2008 Composent, Inc. and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Composent, Inc. - initial API and implementation
  10. *****************************************************************************/
  11. package org.eclipse.ecf.provider.filetransfer.browse;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.net.Authenticator;
  16. import java.net.HttpURLConnection;
  17. import java.net.PasswordAuthentication;
  18. import java.net.URL;
  19. import java.net.URLConnection;
  20. import org.eclipse.ecf.core.security.Callback;
  21. import org.eclipse.ecf.core.security.CallbackHandler;
  22. import org.eclipse.ecf.core.security.IConnectContext;
  23. import org.eclipse.ecf.core.security.NameCallback;
  24. import org.eclipse.ecf.core.security.ObjectCallback;
  25. import org.eclipse.ecf.core.security.UnsupportedCallbackException;
  26. import org.eclipse.ecf.core.util.Proxy;
  27. import org.eclipse.ecf.filetransfer.BrowseFileTransferException;
  28. import org.eclipse.ecf.filetransfer.IRemoteFile;
  29. import org.eclipse.ecf.filetransfer.IRemoteFileSystemListener;
  30. import org.eclipse.ecf.filetransfer.IncomingFileTransferException;
  31. import org.eclipse.ecf.filetransfer.identity.IFileID;
  32. import org.eclipse.ecf.internal.provider.filetransfer.Activator;
  33. import org.eclipse.ecf.internal.provider.filetransfer.IURLConnectionModifier;
  34. import org.eclipse.ecf.internal.provider.filetransfer.Messages;
  35. import org.eclipse.ecf.provider.filetransfer.util.JREProxyHelper;
  36. import org.eclipse.osgi.util.NLS;
  37. /**
  38. *
  39. */
  40. public class URLFileSystemBrowser extends AbstractFileSystemBrowser {
  41. private static final String USERNAME_PREFIX = Messages.UrlConnectionRetrieveFileTransfer_USERNAME_PROMPT;
  42. private static final String JRE_CONNECT_TIMEOUT_PROPERTY = "sun.net.client.defaultConnectTimeout"; //$NON-NLS-1$
  43. // 10/26/2009: Added being able to set with system property with name org.eclipse.ecf.provider.filetransfer.browse.connectTimeout
  44. // for https://bugs.eclipse.org/bugs/show_bug.cgi?id=292995
  45. private static final String DEFAULT_CONNECT_TIMEOUT = System.getProperty("org.eclipse.ecf.provider.filetransfer.browse.connectTimeout", "30000"); //$NON-NLS-1$ //$NON-NLS-2$
  46. private static final String JRE_READ_TIMEOUT_PROPERTY = "sun.net.client.defaultReadTimeout"; //$NON-NLS-1$
  47. // 10/26/2009: Added being able to set with system property with name org.eclipse.ecf.provider.filetransfer.browse.readTimeout
  48. // for https://bugs.eclipse.org/bugs/show_bug.cgi?id=292995
  49. private static final String DEFAULT_READ_TIMEOUT = System.getProperty("org.eclipse.ecf.provider.filetransfer.browse.readTimeout", "30000"); //$NON-NLS-1$ //$NON-NLS-2$
  50. private JREProxyHelper proxyHelper = null;
  51. protected String username = null;
  52. protected String password = null;
  53. /**
  54. * @param directoryOrFileID
  55. * @param listener
  56. */
  57. public URLFileSystemBrowser(IFileID directoryOrFileID, IRemoteFileSystemListener listener, URL directoryOrFileURL, IConnectContext connectContext, Proxy proxy) {
  58. super(directoryOrFileID, listener, directoryOrFileURL, connectContext, proxy);
  59. proxyHelper = new JREProxyHelper();
  60. }
  61. private void setupTimeouts() {
  62. String existingTimeout = System.getProperty(JRE_CONNECT_TIMEOUT_PROPERTY);
  63. if (existingTimeout == null) {
  64. System.setProperty(JRE_CONNECT_TIMEOUT_PROPERTY, DEFAULT_CONNECT_TIMEOUT);
  65. }
  66. existingTimeout = System.getProperty(JRE_READ_TIMEOUT_PROPERTY);
  67. if (existingTimeout == null) {
  68. System.setProperty(JRE_READ_TIMEOUT_PROPERTY, DEFAULT_READ_TIMEOUT);
  69. }
  70. }
  71. /* (non-Javadoc)
  72. * @see org.eclipse.ecf.provider.filetransfer.browse.AbstractFileSystemBrowser#runRequest()
  73. */
  74. protected void runRequest() throws Exception {
  75. int code = -1;
  76. try {
  77. setupProxies();
  78. setupAuthentication();
  79. setupTimeouts();
  80. URLConnection urlConnection = directoryOrFile.openConnection();
  81. // set cache to off if using jar protocol
  82. // this is for addressing bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=235933
  83. if (directoryOrFile.getProtocol().equalsIgnoreCase("jar")) { //$NON-NLS-1$
  84. urlConnection.setUseCaches(false);
  85. }
  86. // Add http 1.1 'Connection: close' header in order to potentially avoid
  87. // server issue described here https://bugs.eclipse.org/bugs/show_bug.cgi?id=234916#c13
  88. // See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247197
  89. // also see http 1.1 rfc section 14-10 in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  90. urlConnection.setRequestProperty("Connection", "close"); //$NON-NLS-1$ //$NON-NLS-2$
  91. IURLConnectionModifier connectionModifier = Activator.getDefault().getURLConnectionModifier();
  92. if (connectionModifier != null) {
  93. connectionModifier.setSocketFactoryForConnection(urlConnection);
  94. }
  95. if (urlConnection instanceof HttpURLConnection) {
  96. HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
  97. httpConnection.setRequestMethod("HEAD"); //$NON-NLS-1$
  98. httpConnection.connect();
  99. } else {
  100. InputStream ins = urlConnection.getInputStream();
  101. ins.close();
  102. }
  103. code = getResponseCode(urlConnection);
  104. if (isHTTP()) {
  105. if (code == HttpURLConnection.HTTP_OK) {
  106. // do nothing
  107. } else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
  108. throw new BrowseFileTransferException(NLS.bind("File not found: {0}", directoryOrFile.toString()), code); //$NON-NLS-1$
  109. } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
  110. throw new BrowseFileTransferException("Unauthorized", code); //$NON-NLS-1$
  111. } else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
  112. throw new BrowseFileTransferException("Forbidden", code); //$NON-NLS-1$
  113. } else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
  114. throw new BrowseFileTransferException("Proxy auth required", code); //$NON-NLS-1$
  115. } else {
  116. throw new BrowseFileTransferException(NLS.bind("General connection error with response code={0}", new Integer(code)), code); //$NON-NLS-1$
  117. }
  118. }
  119. remoteFiles = new IRemoteFile[1];
  120. remoteFiles[0] = new URLRemoteFile(urlConnection.getLastModified(), urlConnection.getContentLength(), fileID);
  121. } catch (final FileNotFoundException e) {
  122. throw new IncomingFileTransferException(NLS.bind("File not found: {0}", directoryOrFile.toString()), 404); //$NON-NLS-1$
  123. } catch (Exception e) {
  124. Exception except = (e instanceof BrowseFileTransferException) ? e : new BrowseFileTransferException(NLS.bind("Could not connect to {0}", directoryOrFile), e, code); //$NON-NLS-1$
  125. throw except;
  126. }
  127. }
  128. private boolean isHTTP() {
  129. final String protocol = directoryOrFile.getProtocol();
  130. if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) //$NON-NLS-1$ //$NON-NLS-2$
  131. return true;
  132. return false;
  133. }
  134. private int getResponseCode(URLConnection urlConnection) {
  135. int responseCode = -1;
  136. String response = urlConnection.getHeaderField(0);
  137. if (response == null) {
  138. responseCode = -1;
  139. return responseCode;
  140. }
  141. if (!response.startsWith("HTTP/")) //$NON-NLS-1$
  142. return -1;
  143. response = response.trim();
  144. final int mark = response.indexOf(" ") + 1; //$NON-NLS-1$
  145. if (mark == 0)
  146. return -1;
  147. int last = mark + 3;
  148. if (last > response.length())
  149. last = response.length();
  150. responseCode = Integer.parseInt(response.substring(mark, last));
  151. return responseCode;
  152. }
  153. protected void setupAuthentication() throws IOException, UnsupportedCallbackException {
  154. if (connectContext == null)
  155. return;
  156. final CallbackHandler callbackHandler = connectContext.getCallbackHandler();
  157. if (callbackHandler == null)
  158. return;
  159. final NameCallback usernameCallback = new NameCallback(USERNAME_PREFIX);
  160. final ObjectCallback passwordCallback = new ObjectCallback();
  161. // Call callback with username and password callbacks
  162. callbackHandler.handle(new Callback[] {usernameCallback, passwordCallback});
  163. username = usernameCallback.getName();
  164. Object o = passwordCallback.getObject();
  165. if (!(o instanceof String))
  166. throw new UnsupportedCallbackException(passwordCallback, Messages.UrlConnectionRetrieveFileTransfer_UnsupportedCallbackException);
  167. password = (String) passwordCallback.getObject();
  168. // Now set authenticator to our authenticator with user and password
  169. Authenticator.setDefault(new UrlConnectionAuthenticator());
  170. }
  171. class UrlConnectionAuthenticator extends Authenticator {
  172. /* (non-Javadoc)
  173. * @see java.net.Authenticator#getPasswordAuthentication()
  174. */
  175. protected PasswordAuthentication getPasswordAuthentication() {
  176. return new PasswordAuthentication(username, password.toCharArray());
  177. }
  178. }
  179. protected void setupProxy(final Proxy proxy2) {
  180. proxyHelper.setupProxy(proxy2);
  181. }
  182. }