Package org.wso2.carbon.registry.extensions.handlers

Source Code of org.wso2.carbon.registry.extensions.handlers.WSDLMediaTypeHandler

/*
* Copyright (c) 2008, 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.extensions.handlers;

import org.apache.axiom.om.OMElement;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.registry.core.*;
import org.wso2.carbon.registry.core.config.RegistryContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.jdbc.handlers.Handler;
import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext;
import org.wso2.carbon.registry.core.utils.AuthorizationUtils;
import org.wso2.carbon.registry.extensions.beans.BusinessServiceInfo;
import org.wso2.carbon.registry.extensions.handlers.utils.UDDIPublisher;
import org.wso2.carbon.registry.extensions.handlers.utils.WSDLInfo;
import org.wso2.carbon.registry.extensions.handlers.utils.WSDLProcessor;
import org.wso2.carbon.registry.extensions.utils.CommonConstants;
import org.wso2.carbon.registry.extensions.utils.CommonUtil;
import org.wso2.carbon.user.mgt.UserMgtConstants;

import javax.xml.namespace.QName;
import java.io.*;
import java.util.*;

@SuppressWarnings("unused")
public class WSDLMediaTypeHandler extends Handler {
    private static final Log log = LogFactory.getLog(WSDLMediaTypeHandler.class);
    private String locationTag = "location";
   
    private String wsdlLocation = "/wsdls/";        // location will always has a leading '/' and trailing '/'
    private OMElement wsdlLocationConfiguration;
    protected String schemaLocation = "/schema/";     // location will always has a leading '/' and trailing '/'
    private OMElement schemaLocationConfiguration;

    protected String policyLocation = "/policy/";     // location will always has a leading '/' and trailing '/'
    private OMElement policyLocationConfiguration;

    private boolean disableWSDLValidation = false;

    public OMElement getWsdlLocationConfiguration() {
        return wsdlLocationConfiguration;
    }

    public void setWsdlLocationConfiguration(OMElement locationConfiguration) throws RegistryException {
        Iterator confElements = locationConfiguration.getChildElements();
        while (confElements.hasNext()) {
            OMElement confElement = (OMElement)confElements.next();
            if (confElement.getQName().equals(new QName(locationTag))) {
                wsdlLocation = confElement.getText();
                if (!wsdlLocation.startsWith(RegistryConstants.PATH_SEPARATOR)) {
                    wsdlLocation = RegistryConstants.PATH_SEPARATOR + wsdlLocation;
                }
                if (!wsdlLocation.endsWith(RegistryConstants.PATH_SEPARATOR)) {
                    wsdlLocation = wsdlLocation + RegistryConstants.PATH_SEPARATOR;
                }
            }
        }
        WSDLProcessor.setCommonWSDLLocation(wsdlLocation);
        AuthorizationUtils.addAuthorizeRoleListener(
                RegistryConstants.ADD_WSDL_AUTHORIZE_ROLE_LISTENER_EXECUTION_ORDER_ID,
                WSDLProcessor.getChrootedWSDLLocation(RegistryContext.getBaseInstance()),
                UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "manage/resources/govern/metadata/add",
                UserMgtConstants.EXECUTE_ACTION);
        AuthorizationUtils.addAuthorizeRoleListener(
                RegistryConstants.LIST_WSDL_AUTHORIZE_ROLE_LISTENER_EXECUTION_ORDER_ID,
                WSDLProcessor.getChrootedWSDLLocation(RegistryContext.getBaseInstance()),
                UserMgtConstants.UI_ADMIN_PERMISSION_ROOT + "manage/resources/govern/metadata/list",
                UserMgtConstants.EXECUTE_ACTION, new String[]{ActionConstants.GET});
        this.wsdlLocationConfiguration = locationConfiguration;
    }

    public OMElement getSchemaLocationConfiguration() {
        return schemaLocationConfiguration;
    }
   
    public void setSchemaLocationConfiguration(OMElement locationConfiguration) throws RegistryException {
        Iterator confElements = locationConfiguration.getChildElements();
        while (confElements.hasNext()) {
            OMElement confElement = (OMElement)confElements.next();
            if (confElement.getQName().equals(new QName(locationTag))) {
                schemaLocation = confElement.getText();
                if (!schemaLocation.startsWith(RegistryConstants.PATH_SEPARATOR)) {
                    schemaLocation = RegistryConstants.PATH_SEPARATOR + schemaLocation;
                }
                if (!schemaLocation.endsWith(RegistryConstants.PATH_SEPARATOR)) {
                    schemaLocation = schemaLocation + RegistryConstants.PATH_SEPARATOR;
                }
            }
        }
       
        WSDLProcessor.setCommonSchemaLocation(schemaLocation);
        this.schemaLocationConfiguration = locationConfiguration;
    }

     public OMElement getPolicyLocationConfiguration() {
        return policyLocationConfiguration;
    }
    public void setPolicyLocationConfiguration(OMElement locationConfiguration) throws RegistryException {
           Iterator confElements = locationConfiguration.getChildElements();
           while (confElements.hasNext()) {
               OMElement confElement = (OMElement)confElements.next();
               if (confElement.getQName().equals(new QName(locationTag))) {
                   policyLocation = confElement.getText();
                   if (!policyLocation.startsWith(RegistryConstants.PATH_SEPARATOR)) {
                       policyLocation = RegistryConstants.PATH_SEPARATOR + policyLocation;
                   }
                   if (!policyLocation.endsWith(RegistryConstants.PATH_SEPARATOR)) {
                       policyLocation = policyLocation + RegistryConstants.PATH_SEPARATOR;
                   }
               }
           }

           WSDLProcessor.setCommonPolicyLocation(policyLocation);
           this.policyLocationConfiguration = locationConfiguration;
       }



    public void makeDir(File file) throws IOException {
        if (file != null && !file.exists() && !file.mkdir()) {
            log.warn("Failed to create directory at path: " + file.getAbsolutePath());
        }
    }

    public void makeDirs(File file) throws IOException {
        if (file != null && !file.exists() && !file.mkdirs()) {
            log.warn("Failed to create directories at path: " + file.getAbsolutePath());
        }
    }

    public void delete(File file) throws IOException {
        if (file != null && file.exists() && !file.delete()) {
            log.warn("Failed to delete file/directory at path: " + file.getAbsolutePath());
        }
    }

    /**
     * Method that will executed after the put operation has been done.
     *
     * @param path the path of the resource.
     * @param addedResources the resources that have been added to the registry.
     * @param otherResources the resources that have not been added to the registry.
     * @param requestContext the request context for the put operation.
     * @throws RegistryException if the operation failed.
     */
    @SuppressWarnings("unused")
    protected void onPutCompleted(String path, Map<String, String> addedResources,
                                  List<String> otherResources, RequestContext requestContext)
            throws RegistryException {
    }

    public void put(RequestContext requestContext) throws RegistryException {
        if (!CommonUtil.isUpdateLockAvailable()) {
            return;
        }
        CommonUtil.acquireUpdateLock();
                WSDLProcessor wsdlProcessor=null;
        try {
            Resource metadata = requestContext.getResource();
            String path = requestContext.getResourcePath().getPath();
            try {
                // If the WSDL is already there, we don't need to re-run this handler unless the content is changed.
                // Re-running this handler causes issues with downstream handlers and other behaviour (ex:- lifecycles).
                // If you need to do a replace pragmatically, delete-then-replace.
                if (metadata == null) {
                    // will go with the default processing
                    return;
                }
                Registry registry = requestContext.getRegistry();

                // This is to distinguish operations on xsd and wsdl on remote mounting.
                String remotePut = metadata.getProperty(RegistryConstants.REMOTE_MOUNT_OPERATION);
                if (remotePut != null) {
                    metadata.removeProperty(RegistryConstants.REMOTE_MOUNT_OPERATION);
                    registry.put(path, metadata);
                    requestContext.setProcessingComplete(true);
                    return;
                }

                if (registry.resourceExists(path)) {
                    // logic to compare content, and return only if the content didn't change.
                    Object newContent = metadata.getContent();

                    Resource oldResource = registry.get(path);
                    Object oldContent = oldResource.getContent();
                    String newContentString = null;
                    String oldContentString = null;
                    if (newContent != null) {
                        if (newContent instanceof String) {
                            newContentString = (String) newContent;
                        } else {
                            newContentString = new String((byte[]) newContent);
                        }
                    }
                    if (oldContent != null) {
                        if (oldContent instanceof String) {
                            oldContentString = (String) oldContent;
                        } else {
                            oldContentString = new String((byte[]) oldContent);
                        }
                    }
                    if ((newContent == null && oldContent == null) ||
                            (newContentString != null && newContentString.equals(oldContentString))) {
                        // this will continue adding from the default path.
                        return;
                    }

                    // so we creating temp files for the wsdl and all the dependencies.
                    Set<String> registryPaths = new LinkedHashSet<String>();
                    // the first path is the current resource path.
                    registryPaths.add(path);
                    // get the associations.
                    Association[] dependencies = registry.getAssociations(path, CommonConstants.DEPENDS);
                    if (dependencies != null) {
                        for (Association dependency: dependencies) {
                            String targetPath = dependency.getDestinationPath();
                            if (targetPath.startsWith(RegistryConstants.ROOT_PATH)) {
                                registryPaths.add(targetPath);
                            }
                        }
                    }

                    File referenceTempFile = File.createTempFile("wsdl", ".ref");
                    File tempDir = new File(referenceTempFile.getAbsolutePath().substring(0,
                            referenceTempFile.getAbsolutePath().length() - ".ref".length()));
                    String tempDirPath = tempDir.getAbsolutePath();
                    // now add each of the registry paths to the the tempDir
                    List<File> tempFiles = new ArrayList<File>();
                    for (String registryPath: registryPaths) {
                        if (!registryPath.startsWith(RegistryConstants.ROOT_PATH)) {
                            continue;
                        }
                        String filePath = tempDirPath + registryPath;
                        File tempFile = new File(filePath);
                        makeDirs(tempFile.getParentFile());

                        Object resourceContent;
                        if (registryPath.equals(path)) {
                            // this is the wsdl we want to update.
                            resourceContent = metadata.getContent();
                        } else {
                            Resource r = registry.get(registryPath);
                            if (r == null) {
                                continue;
                            }
                            resourceContent = r.getContent();
                        }
                        byte[] resourceContentBytes;

                        if (resourceContent == null) {
                            resourceContentBytes = new byte[0];
                        } else if (resourceContent instanceof byte[]) {
                            resourceContentBytes = (byte[])resourceContent;
                        } else if (resourceContent instanceof String) {
                            resourceContentBytes = ((String)resourceContent).getBytes();
                        } else {
                            String msg = "Unknown type for the content path: " + path + ", content type: " +
                                    resourceContent.getClass().getName() + ".";
                            log.error(msg);
                            throw new RegistryException(msg);
                        }
                        InputStream in = new ByteArrayInputStream(resourceContentBytes);

                        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
                        byte[] contentChunk = new byte[1024];
                        int byteCount;
                        while ((byteCount = in.read(contentChunk)) != -1) {
                            out.write(contentChunk, 0, byteCount);
                        }
                        out.flush();
                        out.close();
                        tempFiles.add(tempFile);
                    }

                    if (tempFiles.size() == 0) {
                        // unreachable state, anyway better log and return.
                        String msg = "Temporary files count is zero, when updating a wsdl. " +
                                "wsdl path: " + path + ".";
                        log.error(msg);
                        // we are just returning, as the put operation will continue in its default path.
                        return;
                    }

                    File tempFile = tempFiles.get(0);
                    String uri = tempFile.toURI().toString();
                    if (uri.startsWith("file:")) {
                        uri = uri.substring(5);
                    }
                    while (uri.startsWith("/")) {
                        uri = uri.substring(1);
                    }
                    uri = "file:///" + uri;
                    String wsdlPath = null;
                    if (uri != null) {
                        requestContext.setSourceURL(uri);
                        requestContext.setResource(metadata);

                        wsdlProcessor = new WSDLProcessor(requestContext);
                        wsdlPath = processWSDLImport(requestContext, wsdlProcessor, metadata, uri);
                    }

                    // now we will delete each temp files, ref file and the temp directory.
                    for (File temp : tempFiles) {
                        FileUtils.forceDelete(temp);
                    }
                    FileUtils.deleteDirectory(tempDir);
                    FileUtils.forceDelete(referenceTempFile);

                    if (wsdlPath != null) {
                        onPutCompleted(path, Collections.singletonMap(uri, wsdlPath),
                                Collections.<String>emptyList(), requestContext);
                    }
                    requestContext.setProcessingComplete(true);
                    return;
                }
            } catch (IOException e) {
                String msg = "Error in updating the wsdl. wsdl path: " + path + ".";
                log.error(msg, e);
                throw new RegistryException(msg, e);
            }
            try {
                Object resourceContent = metadata.getContent();
                byte[] resourceContentBytes;

                if (resourceContent == null) {
                    resourceContentBytes = new byte[0];
                } else if (resourceContent instanceof byte[]) {
                    resourceContentBytes = (byte[])resourceContent;
                } else if (resourceContent instanceof String) {
                    resourceContentBytes = ((String)resourceContent).getBytes();
                } else {
                    String msg = "Unknown type for the content path: " + path + ", content type: " +
                            resourceContent.getClass().getName() + ".";
                    log.error(msg);
                    throw new RegistryException(msg);
                }
                InputStream in = new ByteArrayInputStream(resourceContentBytes);

                File tempFile = File.createTempFile("wsdl", ".wsdl");

                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
                byte[] contentChunk = new byte[1024];
                int byteCount;
                while ((byteCount = in.read(contentChunk)) != -1) {
                    out.write(contentChunk, 0, byteCount);
                }
                out.flush();
                out.close();
                String uri = tempFile.toURI().toString();
                if (uri.startsWith("file:")) {
                    uri = uri.substring(5);
                }
                while (uri.startsWith("/")) {
                    uri = uri.substring(1);
                }
                uri = "file:///" + uri;
                String wsdlPath = null;
                if (uri != null) {
                    requestContext.setSourceURL(uri);
                    requestContext.setResource(metadata);

                    wsdlProcessor = new WSDLProcessor(requestContext);
                    wsdlPath = processWSDLImport(requestContext, wsdlProcessor, metadata, uri);
                }
                delete(tempFile);
                if (wsdlPath != null) {
                    onPutCompleted(path, Collections.singletonMap(uri, wsdlPath),
                            Collections.<String>emptyList(), requestContext);
                }
                /*WSDLProcessor wsdlProcessor = new WSDLProcessor(requestContext.getRegistry());
                wsdlProcessor
                       .addWSDLToRegistry(
                               requestContext, null,
                               metadata, true, true);*/
            } catch (IOException e) {
                throw new RegistryException("An error occurred while uploading WSDL file", e);
            }

            requestContext.setProcessingComplete(true);

            if (wsdlProcessor != null && CommonConstants.ENABLE.equals(System.getProperty(CommonConstants.UDDI_SYSTEM_PROPERTY))) {
                BusinessServiceInfo businessServiceInfo = new BusinessServiceInfo();
                WSDLInfo wsdlInfo = wsdlProcessor.getMasterWSDLInfo();
                businessServiceInfo.setServiceWSDLInfo(wsdlInfo);
                UDDIPublisher publisher = new UDDIPublisher(businessServiceInfo);
                publisher.publishBusinessService();
            }
        } finally {
            CommonUtil.releaseUpdateLock();
        }
    }

    public void importResource(RequestContext requestContext) throws RegistryException {
        if (!CommonUtil.isUpdateLockAvailable()) {
            return;
        }
        CommonUtil.acquireUpdateLock();
        WSDLProcessor wsdlProcessor=null;
        try {
            Resource metadata = requestContext.getResource();
            String sourceURL = requestContext.getSourceURL();
            if (requestContext.getSourceURL() != null &&
                    requestContext.getSourceURL().toLowerCase().startsWith("file:")) {
                String msg = "The source URL must not be file in the server's local file system";
                throw new RegistryException(msg);
            }
            try {
                wsdlProcessor = new WSDLProcessor(requestContext);
                String wsdlPath =
                        processWSDLImport(requestContext, wsdlProcessor, metadata, sourceURL);
                ResourcePath resourcePath = requestContext.getResourcePath();
                String path = null;
                if (resourcePath != null) {
                    path = resourcePath.getPath();
                }
                onPutCompleted(path,
                        Collections.singletonMap(sourceURL, wsdlPath),
                        Collections.<String>emptyList(), requestContext);
            } catch (Exception e) {
                throw new RegistryException(e.getMessage(), e);
            }

            requestContext.setProcessingComplete(true);

            if (wsdlProcessor != null && CommonConstants.ENABLE.equals(System.getProperty(CommonConstants.UDDI_SYSTEM_PROPERTY))) {
                BusinessServiceInfo businessServiceInfo = new BusinessServiceInfo();
                businessServiceInfo.setServiceWSDLInfo(wsdlProcessor.getMasterWSDLInfo());
                UDDIPublisher publisher = new UDDIPublisher(businessServiceInfo);
                publisher.publishBusinessService();
            }
        } finally {
            CommonUtil.releaseUpdateLock();
        }
    }

    /**
     * Method that runs the WSDL import/upload procedure.
     *
     * @param requestContext the request context for the import/put operation
     * @param metadata the resource metadata
     * @param sourceURL the URL from which the WSDL is imported
     * @param wsdlProcessor the WSDL Processor instance, used for upload and validation
     *
     * @return the path at which the WSDL was uploaded to
     *
     * @throws RegistryException if the operation failed.
     */
    protected String processWSDLImport(RequestContext requestContext, WSDLProcessor wsdlProcessor,
                                       Resource metadata, String sourceURL)
            throws RegistryException {
        return wsdlProcessor.addWSDLToRegistry(requestContext, sourceURL, metadata, false, true,
                disableWSDLValidation);
    }

    public void setDisableWSDLValidation(String disableWSDLValidation) {
        this.disableWSDLValidation = Boolean.toString(true).equals(disableWSDLValidation);
    }
}
TOP

Related Classes of org.wso2.carbon.registry.extensions.handlers.WSDLMediaTypeHandler

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.