Package org.wso2.carbon.registry.resource.services.utils

Source Code of org.wso2.carbon.registry.resource.services.utils.ImportResourceUtil

/*
* Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* 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.wso2.carbon.registry.resource.services.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;

import java.sql.SQLException;

public class ImportResourceUtil {

    private static final Log log = LogFactory.getLog(ImportResourceUtil.class);

    public static void importResource(
                String parentPath,
                String resourceName,
                String mediaType,
                String description,
                String fetchURL,
                String symlinkLocation,
                UserRegistry userRegistry) throws Exception {

        String resourcePath;
        if (RegistryConstants.ROOT_PATH.equals(parentPath)) {
            resourcePath = RegistryConstants.ROOT_PATH + resourceName;
        } else {
            resourcePath = parentPath + RegistryConstants.PATH_SEPARATOR + resourceName;
        }

        try {
            Resource metadataResource = userRegistry.newResource();

            metadataResource.setMediaType(mediaType);
            metadataResource.setDescription(description);

            if (symlinkLocation != null) {
                if (!symlinkLocation.endsWith(RegistryConstants.PATH_SEPARATOR)) {
                    symlinkLocation += RegistryConstants.PATH_SEPARATOR;
                }
                metadataResource.setProperty(RegistryConstants.SYMLINK_PROPERTY_NAME, symlinkLocation);
            }

            userRegistry.importResource(resourcePath, fetchURL, metadataResource);
            metadataResource.discard();

        } catch (RegistryException e) {
            String msg = "Failed to import resource from the URL " + fetchURL + " to path " +
                    resourcePath + ". " + ((e.getCause() instanceof SQLException) ?
                    "" : e.getMessage());
            log.error(msg, e);
            throw new RegistryException(msg, e);
        }

    }
}
TOP

Related Classes of org.wso2.carbon.registry.resource.services.utils.ImportResourceUtil

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.