Search in sources :

Example 1 with URLRemoteFile

use of org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile in project ecf by eclipse.

the class HttpClientFileSystemBrowser method runRequest.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.provider.filetransfer.browse.AbstractFileSystemBrowser#runRequest()
	 */
protected void runRequest() throws Exception {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "runRequest");
    setupProxies();
    // set timeout
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
    String urlString = directoryOrFile.toString();
    // setup authentication
    setupAuthentication(urlString);
    headMethod = new HttpHead(urlString);
    // $NON-NLS-1$
    int maxAge = Integer.getInteger("org.eclipse.ecf.http.cache.max-age", 0).intValue();
    // fix the fix for bug 249990 with bug 410813
    if (maxAge == 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        headMethod.addHeader("Cache-Control", "max-age=0");
    } else if (maxAge > 0) {
        // $NON-NLS-1$//$NON-NLS-2$
        headMethod.addHeader("Cache-Control", "max-age=" + maxAge);
    }
    long lastModified = 0;
    long fileLength = -1;
    connectingSockets.clear();
    int code = -1;
    try {
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "browse=" + urlString);
        httpContext = new BasicHttpContext();
        httpResponse = httpClient.execute(headMethod, httpContext);
        code = httpResponse.getStatusLine().getStatusCode();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "browse resp=" + code);
        // Check for NTLM proxy in response headers
        // This check is to deal with bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252002
        boolean ntlmProxyFound = NTLMProxyDetector.detectNTLMProxy(httpContext);
        if (ntlmProxyFound && !hasForceNTLMProxyOption())
            // $NON-NLS-1$
            throw new BrowseFileTransferException("HttpClient Provider is not configured to support NTLM proxy authentication.", HttpClientOptions.NTLM_PROXY_RESPONSE_CODE);
        if (NTLMProxyDetector.detectSPNEGOProxy(httpContext))
            // $NON-NLS-1$
            throw new BrowseFileTransferException("HttpClient Provider does not support the use of SPNEGO proxy authentication.");
        if (code == HttpURLConnection.HTTP_OK) {
            Header contentLength = httpResponse.getLastHeader(CONTENT_LENGTH_HEADER);
            if (contentLength != null) {
                fileLength = Integer.parseInt(contentLength.getValue());
            }
            lastModified = getLastModifiedTimeFromHeader();
        } else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
            // $NON-NLS-1$
            throw new BrowseFileTransferException(NLS.bind("File not found: {0}", urlString), code);
        } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new BrowseFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code);
        } else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
            // $NON-NLS-1$
            throw new BrowseFileTransferException("Forbidden", code);
        } else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
            throw new BrowseFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code);
        } else {
            throw new BrowseFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(code)), code);
        }
        remoteFiles = new IRemoteFile[1];
        remoteFiles[0] = new URLRemoteFile(lastModified, fileLength, fileID);
    } catch (Exception e) {
        // $NON-NLS-1$
        Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, this.getClass(), "runRequest", e);
        BrowseFileTransferException ex = (BrowseFileTransferException) ((e instanceof BrowseFileTransferException) ? e : new BrowseFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code));
        throw ex;
    }
}
Also used : BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) Header(org.apache.http.Header) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) URLRemoteFile(org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile) HttpHead(org.apache.http.client.methods.HttpHead) BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) IOException(java.io.IOException) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException)

Example 2 with URLRemoteFile

use of org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile in project ecf by eclipse.

the class ScpFileSystemBrowser method createRemoteFile.

private IRemoteFile createRemoteFile(String string) throws URISyntaxException, FileCreateException, SecurityException {
    URLRemoteFile file = null;
    IFileID id = null;
    String[] parts = string.split("\\|");
    // Check to see if this string can be parsed
    if (parts.length < 4) {
        id = FileIDFactory.getDefault().createFileID(IDFactory.getDefault().getNamespaceByName(FileTransferNamespace.PROTOCOL), "scp://unknown");
        file = new URLRemoteFile(0, 0, id);
    } else {
        // Build the filename back up, since the filename may also contain
        // "|"
        // characters
        StringBuilder builder = new StringBuilder("scp://");
        for (int i = 3; i < parts.length; i++) {
            builder.append(parts[i]);
            // Put the | back into the name
            if (i > 3 && i < parts.length - 1) {
                builder.append("|");
            }
        }
        // If it's a directory, then make sure it ends with /
        if (parts[0].equals("directory") && !builder.toString().endsWith("/")) {
            builder.append("/");
        } else if (!parts[0].equals("directory") && builder.toString().endsWith("/")) {
            builder.deleteCharAt(builder.length() - 1);
        }
        String originalName = builder.toString();
        String formatedName = originalName.replace(" ", "%20");
        URI uri = new URI(formatedName);
        // Create the FileID
        id = FileIDFactory.getDefault().createFileID(IDFactory.getDefault().getNamespaceByName(FileTransferNamespace.PROTOCOL), new Object[] { uri });
        long size = Long.parseLong(parts[1]);
        long modification = Long.parseLong(parts[2]);
        file = new URLRemoteFile(modification, size, id);
    }
    return file;
}
Also used : URLRemoteFile(org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile)

Aggregations

URLRemoteFile (org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile)2 IOException (java.io.IOException)1 Header (org.apache.http.Header)1 HttpHead (org.apache.http.client.methods.HttpHead)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1 UnsupportedCallbackException (org.eclipse.ecf.core.security.UnsupportedCallbackException)1 BrowseFileTransferException (org.eclipse.ecf.filetransfer.BrowseFileTransferException)1