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.repository.impl;
018:
019: import java.io.IOException;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Properties;
023: import java.util.Set;
024:
025: import javax.xml.transform.OutputKeys;
026:
027: import org.apache.avalon.framework.activity.Disposable;
028: import org.apache.avalon.framework.component.Component;
029: import org.apache.avalon.framework.logger.AbstractLogEnabled;
030: import org.apache.avalon.framework.service.ServiceException;
031: import org.apache.avalon.framework.service.ServiceManager;
032: import org.apache.avalon.framework.service.Serviceable;
033: import org.apache.cocoon.ProcessingException;
034: import org.apache.cocoon.components.repository.helpers.CredentialsToken;
035: import org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper;
036: import org.apache.cocoon.components.source.helpers.SourceProperty;
037: import org.apache.cocoon.components.webdav.WebDAVUtil;
038: import org.apache.cocoon.xml.XMLUtils;
039: import org.apache.commons.httpclient.HttpException;
040: import org.w3c.dom.Node;
041:
042: /**
043: * A property helper class for the WebDAV repository
044: * intended to be used by flowscripts or corresponding wrapper components.
045: */
046: public class WebDAVRepositoryPropertyHelper extends AbstractLogEnabled
047: implements RepositoryPropertyHelper, Serviceable, Disposable,
048: Component {
049:
050: /* The ServiceManager */
051: private ServiceManager manager;
052:
053: /* The repository component */
054: private WebDAVRepository repo;
055:
056: /* The credentials to be used against the WebDAV repository */
057: private CredentialsToken credentials;
058:
059: /* (non-Javadoc)
060: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
061: */
062: public void service(ServiceManager manager) throws ServiceException {
063: this .manager = manager;
064: }
065:
066: /* (non-Javadoc)
067: * @see org.apache.avalon.framework.activity.Disposable#dispose()
068: */
069: public void dispose() {
070: this .manager = null;
071: }
072:
073: /**
074: * create a WebDAVRepositoryPropertyHelper
075: *
076: * @param credentials the user credentials to be used against the WebDAV repository.
077: * @param repo a reference to the WebDAVRepository object.
078: */
079: public WebDAVRepositoryPropertyHelper(CredentialsToken credentials,
080: WebDAVRepository repo) {
081: this .credentials = credentials;
082: this .repo = repo;
083: }
084:
085: /* (non-Javadoc)
086: * @see org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper#getProperty(java.lang.String, java.lang.String, java.lang.String)
087: */
088: public SourceProperty getProperty(String uri, String name,
089: String namespace) {
090:
091: try {
092: return WebDAVUtil.getProperty(
093: this .repo.getAbsoluteURI(uri), name, namespace);
094:
095: } catch (HttpException he) {
096: this .getLogger().error(
097: "HTTP Error getting property " + namespace + ":"
098: + name + " for " + uri, he);
099:
100: } catch (IOException ioe) {
101: this .getLogger().error(
102: "IO Error getting property " + namespace + ":"
103: + name + " for " + uri, ioe);
104: }
105:
106: return null;
107: }
108:
109: /* (non-Javadoc)
110: * @see org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper#getProperties(java.lang.String, java.util.Set)
111: */
112: public Map getProperties(String uri, Set propNames) {
113:
114: try {
115: return WebDAVUtil.getProperties(this .repo
116: .getAbsoluteURI(uri), propNames);
117:
118: } catch (HttpException he) {
119: this .getLogger().error(
120: "HTTP Error getting properties for " + uri, he);
121:
122: } catch (IOException ioe) {
123: this .getLogger().error(
124: "IO Error getting properties for " + uri, ioe);
125: }
126:
127: return null;
128: }
129:
130: /* (non-Javadoc)
131: * @see org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper#getAllProperties(java.lang.String)
132: */
133: public List getAllProperties(String uri) {
134:
135: try {
136: return WebDAVUtil.getAllProperties(this .repo
137: .getAbsoluteURI(uri));
138:
139: } catch (HttpException he) {
140: this .getLogger().error(
141: "HTTP Error getting properties for " + uri, he);
142:
143: } catch (IOException ioe) {
144: this .getLogger().error(
145: "IO Error getting properties for " + uri, ioe);
146: }
147:
148: return null;
149: }
150:
151: /* (non-Javadoc)
152: * @see org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper#setProperty(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
153: */
154: public boolean setProperty(String uri, String name,
155: String namespace, String value) {
156:
157: try {
158: WebDAVUtil.setProperty(this .repo.getAbsoluteURI(uri), name,
159: namespace, value);
160: return true;
161:
162: } catch (HttpException he) {
163: this .getLogger().error(
164: "HTTP Error setting property " + namespace + ":"
165: + name + " for " + uri, he);
166: } catch (IOException ioe) {
167: this .getLogger().error(
168: "IO Error setting property " + namespace + ":"
169: + name + " for " + uri, ioe);
170: }
171:
172: return false;
173: }
174:
175: /* (non-Javadoc)
176: * @see org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper#setProperty(java.lang.String, java.lang.String, java.lang.String, org.w3c.dom.Node)
177: */
178: public boolean setProperty(String uri, String name,
179: String namespace, Node value) {
180:
181: try {
182: Properties format = new Properties();
183: format.put(OutputKeys.METHOD, "xml");
184: format.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
185: return this .setProperty(uri, name, namespace, XMLUtils
186: .serializeNode(value, format));
187:
188: } catch (ProcessingException pe) {
189: this .getLogger().error("Error serializing node " + value,
190: pe);
191: }
192:
193: return false;
194: }
195:
196: /* (non-Javadoc)
197: * @see org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper#setProperties(java.lang.String, java.util.Map)
198: */
199: public boolean setProperties(final String uri, final Map properties) {
200:
201: try {
202: WebDAVUtil.setProperties(this .repo.getAbsoluteURI(uri),
203: properties);
204: return true;
205:
206: } catch (HttpException he) {
207: this .getLogger().error(
208: "HTTP Error setting properties for " + uri, he);
209: } catch (IOException ioe) {
210: this .getLogger().error(
211: "IO Error setting properties for " + uri, ioe);
212: }
213:
214: return false;
215: }
216:
217: }
|