A general purpose, low level webdav transformer. Sends http requests defined in xml
directly to the server and returns the response to the processing stream.
For a more high level approach, use WebDAVSource (GET/PUT/PROPPATCH) and DASLTransformer (SEARCH).
| Method from org.apache.cocoon.transformation.WebDAVTransformer Detail: |
public void dispose() {
recycle();
manager = null;
}
|
public void endElement(String uri,
String name,
String raw) throws SAXException {
if (name.equals(REQUEST_TAG) && uri.equals(NS_URI)) {
try {
HttpURL url = new HttpURL(m_target);
if(url.getUser() != null && !"".equals(url.getUser())) {
m_state.setCredentials(null, new UsernamePasswordCredentials(
url.getUser(),
url.getPassword()));
}
m_target = url.getURI();
if (m_validity != null) {
m_validity.add(makeWebdavEventValidity(url));
}
} catch (Exception e) {
//ignore
}
// create method
WebDAVRequestMethod method = new WebDAVRequestMethod(m_target, m_method);
try {
// add request headers
Iterator headers = m_headers.entrySet().iterator();
while (headers.hasNext()) {
Map.Entry header = (Map.Entry) headers.next();
method.addRequestHeader((String) header.getKey(), (String) header.getValue());
}
Properties props = XMLUtils.createPropertiesForXML(false);
props.put(OutputKeys.ENCODING, "ISO-8859-1");
String body = XMLUtils.serializeNode(m_requestdocument, props);
// set request body
method.setRequestBody(body.getBytes("ISO-8859-1"));
// execute the request
executeRequest(method);
} catch (ProcessingException e) {
if(getLogger().isErrorEnabled()) {
getLogger().debug("Couldn't read request from sax stream",e);
}
throw new SAXException("Couldn't read request from sax stream",e);
} catch (UnsupportedEncodingException e) {
if(getLogger().isErrorEnabled()) {
getLogger().debug("ISO-8859-1 encoding not present",e);
}
throw new SAXException("ISO-8859-1 encoding not present",e);
}
finally {
method.releaseConnection();
m_headers = null;
}
}
else if (name.equals(HEADER_TAG) && uri.equals(NS_URI)) {
// dont do anything
}
else if (name.equals(BODY_TAG) && uri.equals(NS_URI)) {
m_requestdocument = super.endRecording();
}
else {
super.endElement(uri, name, raw);
}
}
|
public Serializable getKey() {
if (m_state == null) {
return "WebDAVTransformer";
}
final StringBuffer key = new StringBuffer();
// get the credentials
final Credentials credentials = m_state.getCredentials(null, null);
if (credentials != null) {
if (credentials instanceof UsernamePasswordCredentials) {
key.append(((UsernamePasswordCredentials) credentials).getUserName());
}
else {
key.append(credentials.toString());
}
}
return key.toString();
}
|
public SourceValidity getValidity() {
// dont do any caching when no event caching is set up
if (m_eventfactory == null) {
return null;
}
if (m_validity == null) {
m_validity = new AggregatedValidity();
}
return m_validity;
}
|
public void recycle() {
super.recycle();
m_method = null;
m_target = null;
m_validity = null;
m_requestdocument = null;
}
|
public void setup(SourceResolver resolver,
Map objectModel,
String src,
Parameters par) throws IOException, SAXException, ProcessingException {
super.setup(resolver, objectModel, src, par);
m_state = new HttpState();
if(null != par.getParameter("username", null)) {
m_state.setCredentials(null, null, new UsernamePasswordCredentials(
par.getParameter("username", ""),
par.getParameter("password", ""))
);
}
if(m_eventfactory == null) {
try {
m_eventfactory = (WebDAVEventFactory)manager.lookup(WebDAVEventFactory.ROLE);
} catch (ServiceException e) {
// ignore, no eventcaching configured
m_eventfactory = null;
}
}
}
|
public void startElement(String uri,
String name,
String raw,
Attributes atts) throws SAXException {
if (name.equals(REQUEST_TAG) && uri.equals(NS_URI)) {
m_headers = new HashMap();
if ((m_method = atts.getValue(METHOD_ATTR)) == null) {
final String msg = "The < request > element must contain a \"method\" attribute";
throw new IllegalStateException(msg);
}
if ((m_target = atts.getValue(TARGET_ATTR)) == null) {
throw new IllegalStateException("The < request > element must contain a \"target\" attribute");
}
if (m_target.startsWith(WEBDAV_SCHEME)) {
m_target = HTTP_SCHEME + m_target.substring(WEBDAV_SCHEME.length());
}
else {
throw new SAXException("Illegal value for target, must be an http:// or webdav:// URL");
}
}
else if (name.equals(HEADER_TAG) && uri.equals(NS_URI)) {
final String hname = atts.getValue(NAME_ATTR);
if (hname == null) {
throw new SAXException("The < header > element requires a \"name\" attribute");
}
final String value = atts.getValue(VALUE_ATTR);
if (value == null) {
throw new SAXException("The < header > element requires a \"value\" attribute");
}
m_headers.put(hname, value);
}
else if (name.equals(BODY_TAG) && uri.equals(NS_URI)) {
startRecording();
}
else {
super.startElement(uri, name, raw, atts);
}
}
|