001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.registry.url;
021:
022: import org.apache.axiom.om.OMNode;
023: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.synapse.SynapseException;
027: import org.apache.synapse.config.SynapseConfigUtils;
028: import org.apache.synapse.registry.AbstractRegistry;
029: import org.apache.synapse.registry.Registry;
030: import org.apache.synapse.registry.RegistryEntry;
031:
032: import javax.xml.stream.XMLInputFactory;
033: import javax.xml.stream.XMLStreamException;
034: import javax.xml.stream.XMLStreamReader;
035: import java.io.*;
036: import java.net.*;
037: import java.util.ArrayList;
038:
039: /**
040: * A Simple HTTP GET based registry which will work with a Web Server / WebDAV
041: * <p/>
042: * This saves the root server URL, and appends the a given key to construct the
043: * full URL to locate resources
044: */
045: public class SimpleURLRegistry extends AbstractRegistry implements
046: Registry {
047:
048: private static final Log log = LogFactory
049: .getLog(SimpleURLRegistry.class);
050:
051: private static final int MAX_KEYS = 200;
052:
053: public OMNode lookup(String key) {
054:
055: log.info("==> Repository fetch of resource with key : " + key);
056: URLConnection urlc = null;
057: try {
058: URL url = SynapseConfigUtils
059: .getURLFromPath(getRoot() + key);
060: if (url == null) {
061: return null;
062: }
063: urlc = url.openConnection();
064: urlc.connect();
065: } catch (IOException e) {
066: return null;
067: }
068:
069: try {
070: XMLStreamReader parser = XMLInputFactory.newInstance()
071: .createXMLStreamReader(urlc.getInputStream());
072: StAXOMBuilder builder = new StAXOMBuilder(parser);
073: return builder.getDocumentElement();
074:
075: } catch (MalformedURLException e) {
076: handleException("Invalid URL reference " + getRoot() + key,
077: e);
078: } catch (FileNotFoundException fnf) {
079: return null;
080: } catch (IOException e) {
081: handleException("IO Error reading from URL " + getRoot()
082: + key, e);
083: } catch (XMLStreamException e) {
084: handleException("XML Error reading from URL " + getRoot()
085: + key, e);
086: }
087: return null;
088: }
089:
090: public RegistryEntry getRegistryEntry(String key) {
091: if (log.isDebugEnabled()) {
092: log.debug("Perform RegistryEntry lookup for key : " + key);
093: }
094: try {
095: URL url = SynapseConfigUtils
096: .getURLFromPath(getRoot() + key);
097: if (url == null) {
098: return null;
099: }
100: URLConnection urlc = url.openConnection();
101: urlc.setReadTimeout(30000);
102: urlc.setRequestProperty("Connection", "Close");
103:
104: URLRegistryEntry wre = new URLRegistryEntry();
105: wre.setKey(key);
106: wre.setName(url.getFile());
107: wre.setType(new URI(urlc.getContentType()));
108: wre.setDescription("Resource at : " + url.toString());
109: wre.setLastModified(urlc.getLastModified());
110: wre.setVersion(urlc.getLastModified());
111: if (urlc.getExpiration() > 0) {
112: wre.setCachableDuration(urlc.getExpiration()
113: - System.currentTimeMillis());
114: } else {
115: wre.setCachableDuration(getCachableDuration());
116: }
117: return wre;
118:
119: } catch (MalformedURLException e) {
120: handleException("Invalid URL reference " + getRoot() + key,
121: e);
122: } catch (IOException e) {
123: handleException("IO Error reading from URL " + getRoot()
124: + key, e);
125: } catch (URISyntaxException e) {
126: handleException("URI Syntax error reading from URL "
127: + getRoot() + key, e);
128: }
129: return null;
130: }
131:
132: public void addConfigProperty(String name, String value) {
133:
134: if (name.equals("root")) {
135:
136: // if the root is folder, it should always end with '/'
137: // therefore, property keys do not have to begin with '/', which could be misleading
138: try {
139: URL url = new URL(value);
140: if (url.getProtocol().equals("file")) {
141: if (!value.endsWith("/")) {
142: value = value + "/";
143: }
144: }
145: } catch (MalformedURLException e) {
146: // don't do any thing if this is not a valid URL
147: }
148: }
149:
150: super .addConfigProperty(name, value);
151: }
152:
153: public String getRoot() {
154: String root = (String) properties.get("root");
155: if (root == null) {
156: return "";
157: } else {
158: return root;
159: }
160: }
161:
162: public long getCachableDuration() {
163: String cachableDuration = (String) properties
164: .get("cachableDuration");
165: return cachableDuration == null ? 1500 : Long
166: .parseLong(cachableDuration);
167: }
168:
169: public RegistryEntry[] getChildren(RegistryEntry entry) {
170: URL url;
171: if (entry == null) {
172: URLRegistryEntry urlEntry = new URLRegistryEntry();
173: urlEntry.setKey("");
174: entry = urlEntry;
175: }
176: url = SynapseConfigUtils.getURLFromPath(getRoot()
177: + entry.getKey());
178: if (url == null) {
179: return null;
180: }
181: if (url.getProtocol().equals("file")) {
182:
183: File file = new File(url.getFile());
184: if (!file.isDirectory()) {
185: return null;
186: }
187: InputStream inStream = null;
188: try {
189: inStream = (InputStream) url.getContent();
190:
191: BufferedReader reader = new BufferedReader(
192: new InputStreamReader(inStream));
193: ArrayList entryList = new ArrayList();
194: String key = "";
195: while ((key = reader.readLine()) != null) {
196: URLRegistryEntry registryEntry = new URLRegistryEntry();
197: if (entry.getKey().equals("")) {
198: registryEntry.setKey(key);
199: } else {
200: if (entry.getKey().endsWith("/")) {
201: registryEntry.setKey(entry.getKey() + key);
202: } else {
203: registryEntry.setKey(entry.getKey() + "/"
204: + key);
205: }
206: }
207:
208: entryList.add(registryEntry);
209: }
210:
211: RegistryEntry[] entries = new RegistryEntry[entryList
212: .size()];
213: for (int i = 0; i < entryList.size(); i++) {
214: entries[i] = (RegistryEntry) entryList.get(i);
215: }
216: return entries;
217:
218: } catch (Exception e) {
219: throw new SynapseException("Error in reading the URL.");
220: }
221:
222: } else {
223: throw new SynapseException("Invalid protocol.");
224: }
225: }
226:
227: public RegistryEntry[] getDescendants(RegistryEntry entry) {
228:
229: ArrayList list = new ArrayList();
230: RegistryEntry[] entries = getChildren(entry);
231: if (entries != null) {
232: for (int i = 0; i < entries.length; i++) {
233:
234: if (list.size() > MAX_KEYS) {
235: break;
236: }
237:
238: fillDescendants(entries[i], list);
239: }
240: }
241:
242: RegistryEntry[] descendants = new RegistryEntry[list.size()];
243: for (int i = 0; i < list.size(); i++) {
244: descendants[i] = (RegistryEntry) list.get(i);
245: }
246:
247: return descendants;
248: }
249:
250: private void fillDescendants(RegistryEntry parent, ArrayList list) {
251:
252: RegistryEntry[] entries = getChildren(parent);
253: if (entries != null) {
254: for (int i = 0; i < entries.length; i++) {
255:
256: if (list.size() > MAX_KEYS) {
257: break;
258: }
259:
260: fillDescendants(entries[i], list);
261: }
262: } else {
263: list.add(parent);
264: }
265: }
266:
267: private void handleException(String msg, Exception e) {
268: log.error(msg, e);
269: throw new SynapseException(msg, e);
270: }
271: }
|