Search in sources :

Example 1 with RegistryEntryImpl

use of org.apache.synapse.registry.RegistryEntryImpl in project wso2-synapse by wso2.

the class SimpleURLRegistry method getChildren.

public RegistryEntry[] getChildren(RegistryEntry entry) {
    URL url;
    if (entry == null) {
        RegistryEntryImpl entryImpl = new RegistryEntryImpl();
        entryImpl.setKey("");
        entry = entryImpl;
    }
    url = SynapseConfigUtils.getURLFromPath(root + entry.getKey(), properties.get(SynapseConstants.SYNAPSE_HOME) != null ? properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
    if (url == null) {
        return null;
    }
    if (url.getProtocol().equals("file")) {
        File file = new File(url.getFile());
        if (!file.isDirectory()) {
            return null;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader((InputStream) url.getContent()));
            ArrayList<RegistryEntry> entryList = new ArrayList<RegistryEntry>();
            String key;
            while ((key = reader.readLine()) != null) {
                RegistryEntryImpl registryEntryImpl = new RegistryEntryImpl();
                if (entry.getKey().equals("")) {
                    registryEntryImpl.setKey(key);
                } else {
                    if (entry.getKey().endsWith("/")) {
                        registryEntryImpl.setKey(entry.getKey() + key);
                    } else {
                        registryEntryImpl.setKey(entry.getKey() + "/" + key);
                    }
                }
                entryList.add(registryEntryImpl);
            }
            RegistryEntry[] entries = new RegistryEntry[entryList.size()];
            for (int i = 0; i < entryList.size(); i++) {
                entries[i] = entryList.get(i);
            }
            return entries;
        } catch (Exception e) {
            throw new SynapseException("Error in reading the URL.");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignored) {
                }
            }
        }
    } else {
        throw new SynapseException("Invalid protocol.");
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) ArrayList(java.util.ArrayList) RegistryEntryImpl(org.apache.synapse.registry.RegistryEntryImpl) RegistryEntry(org.apache.synapse.registry.RegistryEntry) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) SynapseException(org.apache.synapse.SynapseException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with RegistryEntryImpl

use of org.apache.synapse.registry.RegistryEntryImpl in project wso2-synapse by wso2.

the class SimpleURLRegistry method getRegistryEntry.

public RegistryEntry getRegistryEntry(String key) {
    if (log.isDebugEnabled()) {
        log.debug("Perform RegistryEntry lookup for key : " + key);
    }
    URL url = SynapseConfigUtils.getURLFromPath(root + key, properties.get(SynapseConstants.SYNAPSE_HOME) != null ? properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
    if (url == null) {
        return null;
    }
    URLConnection connection = SynapseConfigUtils.getURLConnection(url);
    if (connection == null) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot create a URLConnection for given URL : " + url);
        }
        return null;
    }
    RegistryEntryImpl wre = new RegistryEntryImpl();
    wre.setKey(key);
    wre.setName(url.getFile());
    wre.setType(connection.getContentType());
    wre.setDescription("Resource at : " + url.toString());
    wre.setLastModified(connection.getLastModified());
    wre.setVersion(connection.getLastModified());
    if (connection.getExpiration() > 0) {
        wre.setCachableDuration(connection.getExpiration() - System.currentTimeMillis());
    } else {
        wre.setCachableDuration(getCachableDuration());
    }
    return wre;
}
Also used : RegistryEntryImpl(org.apache.synapse.registry.RegistryEntryImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

URL (java.net.URL)2 RegistryEntryImpl (org.apache.synapse.registry.RegistryEntryImpl)2 MalformedURLException (java.net.MalformedURLException)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 SynapseException (org.apache.synapse.SynapseException)1 RegistryEntry (org.apache.synapse.registry.RegistryEntry)1