Package org.apache.commons.vfs.provider.smb

Source Code of org.apache.commons.vfs.provider.smb.SmbFileNameParser

/*
* Copyright 2002-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs.provider.smb;

import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.provider.FileNameParser;
import org.apache.commons.vfs.provider.URLFileNameParser;
import org.apache.commons.vfs.provider.UriParser;
import org.apache.commons.vfs.provider.VfsComponentContext;

/**
* Implementation for sftp. set default port to 139
*/
public class SmbFileNameParser extends URLFileNameParser
{
    private final static SmbFileNameParser INSTANCE = new SmbFileNameParser();

    public SmbFileNameParser()
    {
        super(139);
    }

    public static FileNameParser getInstance()
    {
        return INSTANCE;
    }

    public FileName parseUri(final VfsComponentContext context, FileName base, final String filename) throws FileSystemException
    {
        final StringBuffer name = new StringBuffer();

        String actualFilename = filename;

        if (actualFilename.equals("smb://") || actualFilename.equals("smb:///")) {
          //return new SmbMasterBrowserFileName();
          actualFilename = "smb://_master_/_browser_/";
        }
        // Extract the scheme and authority parts
        final Authority auth = extractToPath(actualFilename, name);

        // extract domain
        String username = auth.userName;
        String domain = extractDomain(username);
        if (domain != null) {
          username = username.substring(domain.length() + 1);
        }

        // Decode and adjust separators
        UriParser.canonicalizePath(name, 0, name.length(), this);
        UriParser.fixSeparators(name);

        // Extract the share
        String share = UriParser.extractFirstElement(name);
        if (share == null || share.length() == 0) {
          share = "";
          //throw new FileSystemException("vfs.provider.smb/missing-share-name.error",
          //                              actualFilename);
        }

        // Normalise the path.  Do this after extracting the share name,
        // to deal with things like smb://hostname/share/..
        FileType fileType = UriParser.normalisePath(name);
        final String path = name.toString();

        return new SmbFileName(
            auth.scheme,
            auth.hostName,
            auth.port,
            username,
            auth.password,
            domain,
            share,
            path,
            fileType);

    }

    private String extractDomain(String username)
    {
        if (username == null)
        {
            return null;
        }

        for (int i = 0; i < username.length(); i++)
        {
            if (username.charAt(i) == '\\')
            {
                return username.substring(0, i);
            }
        }

        return null;
    }
}
TOP

Related Classes of org.apache.commons.vfs.provider.smb.SmbFileNameParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.