Author: indika
Date: Thu Apr 17 04:14:05 2008
New Revision: 15743
Log:
Add non-xml text and binary content handling for ESBRegisrty
Modified:
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/WSO2RegistryAdapter.java
Modified:
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java
==============================================================================
---
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java
(original)
+++
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java
Thu Apr 17 04:14:05 2008
@@ -20,10 +20,12 @@
package org.wso2.esb.registry;
import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMException;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.SynapseConfigUtils;
import org.apache.synapse.registry.AbstractRegistry;
import org.apache.synapse.registry.Registry;
import org.apache.synapse.registry.RegistryEntry;
@@ -39,10 +41,7 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
+import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
@@ -113,44 +112,99 @@
log.info("==> Repository fetch of resource with key : " + key);
- URLConnection urlc = null;
+ URLConnection urlc;
+ URL url = null;
try {
- URL url = new URL(getRoot() + key);
- if ("file".equals(url.getProtocol())) {
+ url = new URL(getRoot() + key);
+ } catch (MalformedURLException e) {
+ handleException("Invalid path '" + getRoot() + key + "' for URL",
e);
+ }
+
+ if ("file".equals(url.getProtocol())) {
+ try {
+ url.openStream();
+ } catch (IOException ignored) {
+ if (!localRegistry.endsWith(URL_SEPARATOR)) {
+ localRegistry = localRegistry + URL_SEPARATOR;
+ }
try {
- url.openStream();
- } catch (IOException ignored) {
- if (!localRegistry.endsWith(URL_SEPARATOR)) {
- localRegistry = localRegistry + URL_SEPARATOR;
- }
url = new URL(url.getProtocol() + ":" + localRegistry +
key);
- try {
- url.openStream();
- } catch (IOException e) {
- return null;
- }
+ } catch (MalformedURLException e) {
+ handleException("Invalid path '" + url.getProtocol() + ":"
+
+ localRegistry + key + "' for URL", e);
+ }
+ try {
+ url.openStream();
+ } catch (IOException e) {
+ return null;
}
}
+ }
+
+ try {
urlc = url.openConnection();
urlc.connect();
} catch (IOException e) {
return null;
}
+ InputStream input = null;
+ try {
+ input = urlc.getInputStream();
+ } catch (IOException e) {
+ handleException("Error when getting a stream from the URL", e);
+ }
+
+ if (input == null) {
+ return null;
+ }
+
+ BufferedInputStream inputStream = new BufferedInputStream(input);
+ OMNode result = null;
try {
XMLStreamReader parser = XMLInputFactory.newInstance().
- createXMLStreamReader(urlc.getInputStream());
+ createXMLStreamReader(inputStream);
StAXOMBuilder builder = new StAXOMBuilder(parser);
- return builder.getDocumentElement();
+ result = builder.getDocumentElement();
+
+ } catch (OMException ignored) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("The resource at the provided URL isn't " +
+ "well-formed XML,So,takes it as a text");
+ }
+
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error("Error in closing the input stream. ", e);
+ }
+
+ result = SynapseConfigUtils.readNonXML(url);
+
+ } catch (XMLStreamException ignored) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("The resource at the provided URL isn't " +
+ "well-formed XML,So,takes it as a text");
+ }
+
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error("Error in closing the input stream. ", e);
+ }
+ result = SynapseConfigUtils.readNonXML(url);
+
+ } finally {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error("Error in closing the input stream.", e);
+ }
- } catch (MalformedURLException e) {
- handleException("Invalid URL reference " + getRoot() + key, e);
- } catch (IOException e) {
- handleException("IO Error reading from URL " + getRoot() + key, e);
- } catch (XMLStreamException e) {
- handleException("XML Error reading from URL " + getRoot() + key,
e);
}
- return null;
+ return result;
}
public RegistryEntry getRegistryEntry(String key) {
Modified:
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/WSO2RegistryAdapter.java
==============================================================================
---
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/WSO2RegistryAdapter.java
(original)
+++
trunk/esb/java/modules/core/src/main/java/org/wso2/esb/registry/WSO2RegistryAdapter.java
Thu Apr 17 04:14:05 2008
@@ -19,10 +19,14 @@
package org.wso2.esb.registry;
import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMException;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.SynapseException;
+import org.apache.synapse.util.SynapseBinaryDataSource;
import org.apache.synapse.config.Entry;
import org.apache.synapse.registry.AbstractRegistry;
import org.apache.synapse.registry.Registry;
@@ -35,8 +39,8 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
+import javax.activation.DataHandler;
+import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -86,20 +90,64 @@
log.info("==> Repository fetch of resource with key : " + key);
try {
+
Resource resource = getResource(key);
if (resource == null) {
return null;
}
+
+ InputStream input = resource.getContentStream();
+ if (input == null) {
+ return null;
+ }
+
+ BufferedInputStream inputStream = new BufferedInputStream(input);
+ OMNode result = null;
try {
XMLStreamReader parser = XMLInputFactory.newInstance().
- createXMLStreamReader(resource.getContentStream());
+ createXMLStreamReader(inputStream);
StAXOMBuilder builder = new StAXOMBuilder(parser);
- return builder.getDocumentElement();
+ result = builder.getDocumentElement();
+
+ } catch (OMException ignored) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("The resource at the provided URL isn't " +
+ "well-formed XML,So,takes it as a text");
+ }
+
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error("Error in closing the input stream. ", e);
+ }
+ resource = getResource(key);
+ result = readNonXML(resource.getContentStream(),
resource.getMediaType());
+
+ } catch (XMLStreamException ignored) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("The resource at the provided URL isn't " +
+ "well-formed XML,So,takes it as a text");
+ }
+
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error("Error in closing the input stream. ", e);
+ }
+ resource = getResource(key);
+ result = readNonXML(resource.getContentStream(),
resource.getMediaType());
+
+ } finally {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ log.error("Error in closing the input stream.", e);
+ }
- } catch (XMLStreamException e) {
- handleException("Error in converting resource (key :" + key +
" ) " +
- "content to a OMNode ");
}
+ return result;
} catch (RegistryException e) {
handleException("\"Error when fetching a resource with key : \"" +
key, e);
}
@@ -107,6 +155,28 @@
}
/**
+ * Helper method to handle non-XMl resources
+ *
+ * @param inputStream resource content as stream
+ * @param contentType content-type
+ * @return The content as an OMNode
+ */
+ private OMNode readNonXML(InputStream inputStream, String contentType) {
+
+ try {
+
+ OMFactory omFactory = OMAbstractFactory.getOMFactory();
+ return omFactory.createOMText(
+ new DataHandler(new SynapseBinaryDataSource(inputStream,
+ contentType)), true);
+
+ } catch (IOException e) {
+ handleException("Error when getting a stream from resource's
content ", e);
+ }
+ return null;
+ }
+
+ /**
* This is the publicly used interface to the registry. It will fetch
* the content from the registry and cache if required.
*
@@ -468,14 +538,14 @@
* @param toPath The root path for registry
*/
public void importResources(File file, String toPath) {
-
+
if (file == null) {
return;
}
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
-
+
try {
File child = new File(file, children[i]);
String path = removePathSeparator(toPath) + "/" +
child.getName();
_______________________________________________
Esb-java-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/esb-java-dev