[Registry-dev] svn commit r17775 - in branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers: . utils

deepal at wso2.com deepal at wso2.com
Mon Jun 2 03:38:58 PDT 2008


Author: deepal
Date: Mon Jun  2 03:38:58 2008
New Revision: 17775
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=17775

Log:
seems like I have forgotten to add some files

Added:
   branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/WSIValidationHandler.java
   branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/utils/WSDLUtils.java

Added: branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/WSIValidationHandler.java
URL: http://wso2.org/svn/browse/wso2/branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/WSIValidationHandler.java?pathrev=17775
==============================================================================
--- (empty file)
+++ branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/WSIValidationHandler.java	Mon Jun  2 03:38:58 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.registry.handlers;
+
+import org.wso2.registry.jdbc.handlers.Handler;
+import org.wso2.registry.jdbc.handlers.RequestContext;
+import org.wso2.registry.Resource;
+import org.wso2.registry.handlers.utils.WSDLUtils;
+import org.wso2.registry.exceptions.RegistryException;
+
+public class WSIValidationHandler extends Handler {
+
+    public Resource get(RequestContext requestContext) throws RegistryException {
+        return null;
+    }
+
+    public void put(RequestContext requestContext) throws RegistryException {
+        WSDLUtils.validateWSI(requestContext);
+    }
+
+    public void importResource(RequestContext requestContext) throws RegistryException {
+        WSDLUtils.validateWSI(requestContext);
+    }
+
+    public void delete(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void putChild(RequestContext requestContext) throws RegistryException {
+    }
+
+    public void importChild(RequestContext requestContext) throws RegistryException {
+    }
+}

Added: branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/utils/WSDLUtils.java
URL: http://wso2.org/svn/browse/wso2/branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/utils/WSDLUtils.java?pathrev=17775
==============================================================================
--- (empty file)
+++ branches/registry/1_1/modules/extensions/src/org/wso2/registry/handlers/utils/WSDLUtils.java	Mon Jun  2 03:38:58 2008
@@ -0,0 +1,246 @@
+/*
+ * 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.registry.handlers.utils;
+
+import org.apache.xerces.parsers.DOMParser;
+import org.apache.xerces.parsers.StandardParserConfiguration;
+import org.apache.xerces.xs.XSModel;
+import org.eclipse.wsdl.validate.ValidationInfo;
+import org.eclipse.wsdl.validate.ValidationMessage;
+import org.eclipse.wsdl.validate.exception.ValidateWSDLException;
+import org.eclipse.wsdl.validate.wsdl11.internal.WSDL11ValidationInfoImpl;
+import org.eclipse.wsdl.validate.wsdl11.internal.WSDL11BasicValidator;
+import org.eclipse.wsdl.validate.wsdl11.internal.WSDL11ValidatorController;
+import org.eclipse.wsdl.validate.wsdl11.internal.WSDLDocument;
+import org.eclipse.wsdl.validate.internal.ValidationInfoImpl;
+import org.eclipse.wsdl.validate.internal.resolver.URIResolver;
+import org.eclipse.wsdl.validate.internal.util.MessageGenerator;
+import org.eclipse.wsdl.validate.internal.xml.LineNumberDOMParser;
+import org.eclipse.wsdl.validate.wsi.internal.bp10.WSIValidator;
+import org.w3c.dom.Document;
+import org.wso2.registry.exceptions.RegistryException;
+import org.wso2.registry.jdbc.handlers.RequestContext;
+import org.wso2.registry.Resource;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.wsdl.xml.WSDLReader;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.Definition;
+import javax.wsdl.WSDLException;
+import java.util.ResourceBundle;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Iterator;
+import java.io.*;
+import java.net.URL;
+
+public class WSDLUtils {
+
+    private static final String WSDL_VALIDATION_MESSAGE = "org.wso2.wsdl.validationMessage";
+    private static final String WSI_VALIDATION_MESSAGE = "org.wso2.wsi.validationMessage";
+
+    private static final String WSDL_STATUS = "org.wso2.wsdl.validationStatus ";
+    private static final String WSI_STATUS = "org.wso2.wsi.validationStatus ";
+    private static final String WSDL_VALID = "WSDL is valid";
+    private static final String WSDL_VALID_I = "WSDL is valid for WSI";
+    private static final String WSDL_INVALID = "WSDL is invalid ";
+    private static final String WSDL_INVALID_I = "WSDL is invalid for WSI ";
+
+    public static String[] validateForWSI(String uri) throws RegistryException {
+        ArrayList<String> errorMesage = new ArrayList();
+        try {
+            WSIValidator validator = new WSIValidator();
+            ResourceBundle rb = ResourceBundle.getBundle("validatewsdl");
+            MessageGenerator messagegenerator = new MessageGenerator(rb);
+
+            ValidationInfo info = new ValidationInfoImpl(uri, messagegenerator);
+            URIResolver uriResolver = new URIResolver();
+            ((ValidationInfoImpl) info).setURIResolver(uriResolver);
+            StandardParserConfiguration configuration = new StandardParserConfiguration();
+            DOMParser builder = new LineNumberDOMParser(configuration);
+            builder.parse(uri);
+
+            DocumentBuilder db;
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            dbf.setNamespaceAware(true);
+
+            try {
+                db = dbf.newDocumentBuilder();
+            } catch (Exception e) {
+                dbf = DocumentBuilderFactory.newInstance();
+                db = dbf.newDocumentBuilder();
+            }
+
+            Document doc = db.parse(uri);
+            validator.validate(doc, info);
+            ValidationMessage messages[] = info.getValidationMessages();
+            for (int i = 0; i < messages.length; i++) {
+                ValidationMessage message = messages[i];
+                errorMesage.add(message.getMessage());
+            }
+        } catch (Exception e) {
+            throw new RegistryException(e.getMessage());
+        }
+        return errorMesage.toArray(new String[errorMesage.size()]);
+    }
+
+    public static void validateWSI(RequestContext requestContext) throws RegistryException {
+        Resource resource = requestContext.getResource();
+        Object resourceContent = resource.getContent();
+        try {
+            if (resourceContent instanceof byte[]) {
+
+                InputStream in = new ByteArrayInputStream((byte[]) resourceContent);
+
+                File tempFile = File.createTempFile("reg", ".bin");
+                tempFile.deleteOnExit();
+
+                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();
+
+                String uri = tempFile.toURI().toString();
+                validateWSI(uri, resource);
+
+
+            } else if (requestContext.getSourceURL() != null) {
+                validateWSI(requestContext.getSourceURL(), resource);
+            }
+        } catch (Exception e) {
+            throw new RegistryException(e.getMessage());
+        }
+    }
+
+    private static void validateWSI(String uri, Resource resource) throws Exception {
+        String message[] = validateForWSI(uri);
+        if (message.length > 0) {
+            resource.setProperty(WSI_STATUS, WSDL_INVALID_I);
+        } else {
+            resource.setProperty(WSI_STATUS, WSDL_VALID_I);
+        }
+        for (int i = 0; i < message.length; i++) {
+            resource.addProperty(WSI_VALIDATION_MESSAGE, message[i]);
+        }
+    }
+
+    public static void validateWSDL(RequestContext requestContext) throws RegistryException {
+        Resource resource = requestContext.getResource();
+        Object resourceContent = resource.getContent();
+        try {
+            if (resourceContent instanceof byte[]) {
+
+                InputStream in = new ByteArrayInputStream((byte[]) resourceContent);
+
+                File tempFile = File.createTempFile("reg", ".bin");
+                tempFile.deleteOnExit();
+
+                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();
+
+                String uri = tempFile.toURI().toString();
+                validaWSDLFromURI(uri, resource);
+
+
+            } else if (requestContext.getSourceURL() != null) {
+                validaWSDLFromURI(requestContext.getSourceURL(), resource);
+            }
+        } catch (Exception e) {
+            throw new RegistryException(e.getMessage());
+        }
+        resource.setContentStream(null);
+    }
+
+    private static void validaWSDLFromURI(String uri, Resource resource) throws Exception {
+        InputStream inputStream = null;
+        try {
+            inputStream = new URL(uri).openStream();
+
+            ResourceBundle rb = ResourceBundle.getBundle("validatewsdl");
+            MessageGenerator messagegenerator = new MessageGenerator(rb);
+            DocumentBuilder db;
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            dbf.setNamespaceAware(true);
+
+            try {
+                db = dbf.newDocumentBuilder();
+            } catch (Exception e) {
+                dbf = DocumentBuilderFactory.newInstance();
+                db = dbf.newDocumentBuilder();
+            }
+
+            Document doc = db.parse(inputStream);
+            WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
+            reader.setFeature("javax.wsdl.importDocuments", true);
+            Definition wsdlDefinition = reader.readWSDL(uri);
+            ValidationInfo validateInfo = new ValidationInfoImpl(uri, messagegenerator);
+
+            URIResolver uriResolver = new URIResolver();
+            ((ValidationInfoImpl) validateInfo).setURIResolver(uriResolver);
+            java.util.Hashtable attributes = new java.util.Hashtable();
+            ((ValidationInfoImpl) validateInfo).setAttributes(attributes);
+
+            WSDL11ValidationInfoImpl info = new WSDL11ValidationInfoImpl(validateInfo);
+            info.setElementLocations(new java.util.Hashtable());
+            WSDL11BasicValidator validator = new WSDL11BasicValidator();
+            validator.setResourceBundle(rb);
+            ValidationMessage[] messages;
+
+            WSDL11ValidatorController wsdl11ValidatorController =
+                    new WSDL11ValidatorController();
+            WSDLDocument[] wsdlDocs = wsdl11ValidatorController.readWSDLDocument(doc,
+                    validateInfo.getFileURI(),
+                    messagegenerator,
+                    info);
+            WSDLDocument document = wsdlDocs[0];
+            List schema = document.getSchemas();
+            Iterator xsdIter = schema.iterator();
+            while (xsdIter.hasNext()) {
+                info.addSchema((XSModel) xsdIter.next());
+            }
+            // Set the element locations table.
+            info.setElementLocations(document.getElementLocations());
+            validator.validate(wsdlDefinition, new ArrayList(), info);
+            messages = validateInfo.getValidationMessages();
+            if (messages.length > 0) {
+                resource.setProperty(WSDL_STATUS, WSDL_INVALID);
+            } else {
+                resource.setProperty(WSDL_STATUS, WSDL_VALID);
+            }
+            for (ValidationMessage message : messages) {
+                String messageString =
+                        "[" + message.getLine() + "][" + message.getColumn() + "]"
+                                + message.getMessage();
+                resource.addProperty(WSDL_VALIDATION_MESSAGE, messageString);
+            }
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+        }
+    }
+}



More information about the Registry-dev mailing list