001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.webdav.impl;
018:
019: import org.apache.avalon.framework.configuration.Configurable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.parameters.ParameterException;
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.cocoon.caching.validity.Event;
026: import org.apache.cocoon.caching.validity.NameValueEvent;
027: import org.apache.cocoon.components.webdav.WebDAVEventFactory;
028: import org.apache.commons.httpclient.HttpURL;
029:
030: /**
031: * Default implementation
032: */
033: public class DefaultWebDAVEventFactory extends AbstractLogEnabled
034: implements WebDAVEventFactory, Configurable {
035:
036: protected static final String HOST_CONFIG_ELEM = "host";
037: protected static final String PORT_CONFIG_ELEM = "port";
038:
039: private String host = "localhost";
040: private int port = 60000;
041:
042: public void configure(Configuration config)
043: throws ConfigurationException {
044: host = config.getChild(HOST_CONFIG_ELEM).getValue(host);
045: port = config.getChild(PORT_CONFIG_ELEM)
046: .getValueAsInteger(port);
047: }
048:
049: public Event createEvent(String url) {
050:
051: // it might only be the path, supplement with host/port
052: if (url.startsWith("/")) {
053: return createEvent(host, port, url);
054: }
055:
056: try {
057: HttpURL newurl = new HttpURL(url);
058:
059: return createEvent(newurl);
060:
061: } catch (Exception e) {
062: if (getLogger().isErrorEnabled())
063: getLogger().error(
064: "Invalid URI, can't create event object!", e);
065: }
066: return null;
067: }
068:
069: // optimization for preparsed httpclient url
070: public Event createEvent(HttpURL url) {
071: Event event = null;
072: try {
073: String host = url.getHost();
074: int port = url.getPort();
075: String path = url.getEscapedPathQuery();
076:
077: event = createEvent(host, port, path);
078:
079: if (getLogger().isDebugEnabled())
080: getLogger().debug(
081: "Created event for url: " + event.toString());
082:
083: } catch (Exception e) {
084: if (getLogger().isErrorEnabled())
085: getLogger().error("could not create Event!", e);
086: }
087: return event;
088: }
089:
090: public Event createEvent(Parameters params)
091: throws ParameterException {
092: return createEvent(params.getParameter("host"), params
093: .getParameterAsInteger("port"), params
094: .getParameter("path"));
095: }
096:
097: protected Event createEvent(String host, int port, String path) {
098:
099: if (path.endsWith("/"))
100: path = path.substring(0, path.length() - 1);
101:
102: return new NameValueEvent("webdav", host + "|" + port + "|"
103: + path);
104: }
105:
106: }
|