2010-07-24 05:45:32 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2010 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
package davmail.exchange.dav;
|
|
|
|
|
2010-07-27 15:28:27 -04:00
|
|
|
import davmail.exchange.XMLStreamUtil;
|
2010-07-24 05:45:32 -04:00
|
|
|
import org.apache.commons.httpclient.*;
|
2010-07-24 08:38:10 -04:00
|
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
2010-07-24 05:45:32 -04:00
|
|
|
import org.apache.commons.httpclient.methods.RequestEntity;
|
|
|
|
import org.apache.jackrabbit.webdav.MultiStatusResponse;
|
|
|
|
import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
|
|
|
|
import org.apache.jackrabbit.webdav.xml.Namespace;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
|
|
import javax.xml.stream.XMLStreamException;
|
|
|
|
import javax.xml.stream.XMLStreamReader;
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom Exchange PROPPATCH method.
|
|
|
|
* Supports extended property update with type.
|
|
|
|
*/
|
2010-07-24 08:38:10 -04:00
|
|
|
public class ExchangePropPatchMethod extends PostMethod {
|
2010-12-17 08:06:11 -05:00
|
|
|
protected static final Logger LOGGER = Logger.getLogger(ExchangePropPatchMethod.class);
|
2010-07-24 05:45:32 -04:00
|
|
|
|
2010-07-24 06:54:27 -04:00
|
|
|
static final String TYPE_NAMESPACE = "urn:schemas-microsoft-com:datatypes";
|
2010-07-24 05:45:32 -04:00
|
|
|
final Set<PropertyValue> propertyValues;
|
|
|
|
|
2010-07-24 06:11:58 -04:00
|
|
|
/**
|
|
|
|
* Create PROPPATCH method.
|
|
|
|
*
|
|
|
|
* @param path path
|
|
|
|
* @param propertyValues property values
|
|
|
|
*/
|
2010-07-24 05:45:32 -04:00
|
|
|
public ExchangePropPatchMethod(String path, Set<PropertyValue> propertyValues) {
|
|
|
|
super(path);
|
|
|
|
this.propertyValues = propertyValues;
|
|
|
|
setRequestEntity(new RequestEntity() {
|
|
|
|
byte[] content;
|
|
|
|
|
|
|
|
public boolean isRepeatable() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeRequest(OutputStream outputStream) throws IOException {
|
|
|
|
if (content == null) {
|
|
|
|
content = generateRequestContent();
|
|
|
|
}
|
|
|
|
outputStream.write(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getContentLength() {
|
|
|
|
if (content == null) {
|
|
|
|
content = generateRequestContent();
|
|
|
|
}
|
|
|
|
return content.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getContentType() {
|
|
|
|
return "text/xml;charset=UTF-8";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-07-24 06:11:58 -04:00
|
|
|
protected byte[] generateRequestContent() {
|
2010-07-24 05:45:32 -04:00
|
|
|
try {
|
|
|
|
// build namespace map
|
|
|
|
int currentChar = 'e';
|
2010-07-24 06:54:27 -04:00
|
|
|
final Map<String, Integer> nameSpaceMap = new HashMap<String, Integer>();
|
2010-07-24 05:45:32 -04:00
|
|
|
final Set<PropertyValue> setPropertyValues = new HashSet<PropertyValue>();
|
|
|
|
final Set<PropertyValue> deletePropertyValues = new HashSet<PropertyValue>();
|
|
|
|
for (PropertyValue propertyValue : propertyValues) {
|
|
|
|
// data type namespace
|
|
|
|
if (!nameSpaceMap.containsKey(TYPE_NAMESPACE) && propertyValue.getType() != null) {
|
|
|
|
nameSpaceMap.put(TYPE_NAMESPACE, currentChar++);
|
|
|
|
}
|
|
|
|
// property namespace
|
2010-07-24 06:54:27 -04:00
|
|
|
String namespaceUri = propertyValue.getNamespaceUri();
|
|
|
|
if (!nameSpaceMap.containsKey(namespaceUri)) {
|
|
|
|
nameSpaceMap.put(namespaceUri, currentChar++);
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
2010-07-24 06:54:27 -04:00
|
|
|
if (propertyValue.getXmlEncodedValue() == null) {
|
2010-07-24 05:45:32 -04:00
|
|
|
deletePropertyValues.add(propertyValue);
|
|
|
|
} else {
|
|
|
|
setPropertyValues.add(propertyValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
OutputStreamWriter writer = new OutputStreamWriter(baos, "UTF-8");
|
|
|
|
writer.write("<D:propertyupdate xmlns:D=\"DAV:\"");
|
2010-07-24 06:54:27 -04:00
|
|
|
for (Map.Entry<String, Integer> mapEntry : nameSpaceMap.entrySet()) {
|
2010-07-24 05:45:32 -04:00
|
|
|
writer.write(" xmlns:");
|
|
|
|
writer.write((char) mapEntry.getValue().intValue());
|
|
|
|
writer.write("=\"");
|
2010-07-24 06:54:27 -04:00
|
|
|
writer.write(mapEntry.getKey());
|
2010-07-24 05:45:32 -04:00
|
|
|
writer.write("\"");
|
|
|
|
}
|
|
|
|
writer.write(">");
|
|
|
|
if (!setPropertyValues.isEmpty()) {
|
|
|
|
writer.write("<D:set><D:prop>");
|
|
|
|
for (PropertyValue propertyValue : setPropertyValues) {
|
|
|
|
PropertyType propertyType = propertyValue.getType();
|
2010-07-24 06:54:27 -04:00
|
|
|
char nameSpaceChar = (char) nameSpaceMap.get(propertyValue.getNamespaceUri()).intValue();
|
2010-07-24 05:45:32 -04:00
|
|
|
writer.write('<');
|
|
|
|
writer.write(nameSpaceChar);
|
|
|
|
writer.write(':');
|
|
|
|
writer.write(propertyValue.getName());
|
|
|
|
if (propertyType != null) {
|
|
|
|
writer.write(' ');
|
|
|
|
writer.write(nameSpaceMap.get(TYPE_NAMESPACE));
|
|
|
|
writer.write(":dt=\"");
|
|
|
|
writer.write(propertyType.toString().toLowerCase());
|
|
|
|
writer.write("\"");
|
|
|
|
}
|
|
|
|
writer.write('>');
|
2010-07-24 06:54:27 -04:00
|
|
|
writer.write(propertyValue.getXmlEncodedValue());
|
2010-07-24 05:45:32 -04:00
|
|
|
writer.write("</");
|
|
|
|
writer.write(nameSpaceChar);
|
|
|
|
writer.write(':');
|
|
|
|
writer.write(propertyValue.getName());
|
|
|
|
writer.write('>');
|
|
|
|
}
|
|
|
|
writer.write("</D:prop></D:set>");
|
|
|
|
}
|
|
|
|
if (!deletePropertyValues.isEmpty()) {
|
2010-07-24 06:11:58 -04:00
|
|
|
writer.write("<D:remove><D:prop>");
|
2010-07-24 05:45:32 -04:00
|
|
|
for (PropertyValue propertyValue : deletePropertyValues) {
|
2010-07-24 06:54:27 -04:00
|
|
|
char nameSpaceChar = (char) nameSpaceMap.get(propertyValue.getNamespaceUri()).intValue();
|
2010-07-24 05:45:32 -04:00
|
|
|
writer.write('<');
|
|
|
|
writer.write(nameSpaceChar);
|
|
|
|
writer.write(':');
|
|
|
|
writer.write(propertyValue.getName());
|
|
|
|
writer.write("/>");
|
|
|
|
}
|
2010-07-24 06:11:58 -04:00
|
|
|
writer.write("</D:prop></D:remove>");
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
|
|
|
writer.write("</D:propertyupdate>");
|
|
|
|
writer.close();
|
|
|
|
return baos.toByteArray();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "PROPPATCH";
|
|
|
|
}
|
|
|
|
|
|
|
|
List<MultiStatusResponse> responses;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
|
|
|
|
Header contentTypeHeader = getResponseHeader("Content-Type");
|
|
|
|
if (contentTypeHeader != null && "text/xml".equals(contentTypeHeader.getValue())) {
|
|
|
|
responses = new ArrayList<MultiStatusResponse>();
|
|
|
|
XMLStreamReader reader;
|
|
|
|
try {
|
2010-08-28 16:21:31 -04:00
|
|
|
reader = XMLStreamUtil.createXMLStreamReader(new FilterInputStream(getResponseBodyAsStream()) {
|
2010-07-24 08:38:10 -04:00
|
|
|
final byte[] lastbytes = new byte[3];
|
|
|
|
|
2010-07-24 05:45:32 -04:00
|
|
|
@Override
|
2010-07-24 09:11:22 -04:00
|
|
|
public int read(byte[] bytes, int off, int len) throws IOException {
|
|
|
|
int count = in.read(bytes, off, len);
|
2010-07-24 08:38:10 -04:00
|
|
|
// patch invalid element name
|
|
|
|
for (int i = 0; i < count; i++) {
|
2010-07-24 09:11:22 -04:00
|
|
|
byte currentByte = bytes[off + i];
|
2010-07-24 08:38:10 -04:00
|
|
|
if ((lastbytes[0] == '<') && (currentByte >= '0' && currentByte <= '9')) {
|
|
|
|
// move invalid first tag char to valid range
|
2010-07-24 09:11:22 -04:00
|
|
|
bytes[off + i] = (byte) (currentByte + 49);
|
2010-07-24 08:38:10 -04:00
|
|
|
}
|
|
|
|
lastbytes[0] = lastbytes[1];
|
|
|
|
lastbytes[1] = lastbytes[2];
|
|
|
|
lastbytes[2] = currentByte;
|
|
|
|
}
|
|
|
|
return count;
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
while (reader.hasNext()) {
|
2010-08-28 16:31:17 -04:00
|
|
|
reader.next();
|
2010-08-28 16:21:31 -04:00
|
|
|
if (XMLStreamUtil.isStartTag(reader, "response")) {
|
2010-07-24 05:45:32 -04:00
|
|
|
handleResponse(reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
2010-12-17 08:06:11 -05:00
|
|
|
LOGGER.error("Error while parsing soap response: " + e, e);
|
2010-07-24 05:45:32 -04:00
|
|
|
} catch (XMLStreamException e) {
|
2010-12-17 08:06:11 -05:00
|
|
|
LOGGER.error("Error while parsing soap response: " + e, e);
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void handleResponse(XMLStreamReader reader) throws XMLStreamException {
|
2010-07-24 06:54:27 -04:00
|
|
|
String href = null;
|
2010-07-24 08:38:10 -04:00
|
|
|
String responseStatus = "";
|
2010-08-28 16:21:31 -04:00
|
|
|
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "response")) {
|
2010-09-01 10:56:08 -04:00
|
|
|
reader.next();
|
2010-08-28 16:21:31 -04:00
|
|
|
if (XMLStreamUtil.isStartTag(reader)) {
|
2010-07-24 05:45:32 -04:00
|
|
|
String tagLocalName = reader.getLocalName();
|
|
|
|
if ("href".equals(tagLocalName)) {
|
2010-07-24 06:54:27 -04:00
|
|
|
href = reader.getElementText();
|
2010-07-24 05:45:32 -04:00
|
|
|
} else if ("status".equals(tagLocalName)) {
|
2010-07-24 08:38:10 -04:00
|
|
|
responseStatus = reader.getElementText();
|
2010-07-24 06:54:27 -04:00
|
|
|
} else if ("propstat".equals(tagLocalName)) {
|
2010-07-24 08:38:10 -04:00
|
|
|
MultiStatusResponse multiStatusResponse = new MultiStatusResponse(href, responseStatus);
|
2010-07-24 06:54:27 -04:00
|
|
|
handlePropstat(reader, multiStatusResponse);
|
2010-07-24 08:38:10 -04:00
|
|
|
responses.add(multiStatusResponse);
|
2010-07-24 06:54:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void handlePropstat(XMLStreamReader reader, MultiStatusResponse multiStatusResponse) throws XMLStreamException {
|
|
|
|
int propstatStatus = 0;
|
2010-08-28 16:21:31 -04:00
|
|
|
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "propstat")) {
|
2010-09-01 10:56:08 -04:00
|
|
|
reader.next();
|
2010-08-28 16:21:31 -04:00
|
|
|
if (XMLStreamUtil.isStartTag(reader)) {
|
2010-07-24 06:54:27 -04:00
|
|
|
String tagLocalName = reader.getLocalName();
|
|
|
|
if ("status".equals(tagLocalName)) {
|
2010-07-24 05:45:32 -04:00
|
|
|
if ("HTTP/1.1 200 OK".equals(reader.getElementText())) {
|
2010-07-24 06:54:27 -04:00
|
|
|
propstatStatus = HttpStatus.SC_OK;
|
2010-07-24 08:38:10 -04:00
|
|
|
} else {
|
|
|
|
propstatStatus = 0;
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
2010-07-24 06:54:27 -04:00
|
|
|
} else if ("prop".equals(tagLocalName) && propstatStatus == HttpStatus.SC_OK) {
|
2010-07-24 06:11:58 -04:00
|
|
|
handleProperty(reader, multiStatusResponse);
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-24 06:54:27 -04:00
|
|
|
|
2010-07-24 06:11:58 -04:00
|
|
|
protected void handleProperty(XMLStreamReader reader, MultiStatusResponse multiStatusResponse) throws XMLStreamException {
|
2010-09-24 04:44:13 -04:00
|
|
|
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "prop")) {
|
2010-09-01 10:56:08 -04:00
|
|
|
reader.next();
|
2010-08-28 16:21:31 -04:00
|
|
|
if (XMLStreamUtil.isStartTag(reader)) {
|
2010-07-24 08:38:10 -04:00
|
|
|
String tagLocalName = reader.getLocalName();
|
|
|
|
Namespace namespace = Namespace.getNamespace(reader.getNamespaceURI());
|
|
|
|
multiStatusResponse.add(new DefaultDavProperty(tagLocalName, reader.getElementText(), namespace));
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-24 06:11:58 -04:00
|
|
|
/**
|
|
|
|
* Get Multistatus responses.
|
|
|
|
*
|
|
|
|
* @return responses
|
|
|
|
* @throws HttpException on error
|
|
|
|
*/
|
2010-07-24 05:45:32 -04:00
|
|
|
public List<MultiStatusResponse> getResponses() throws HttpException {
|
|
|
|
if (responses == null) {
|
|
|
|
throw new HttpException(getStatusLine().toString());
|
|
|
|
}
|
|
|
|
return responses;
|
|
|
|
}
|
|
|
|
|
2010-07-24 09:11:22 -04:00
|
|
|
/**
|
|
|
|
* Get single Multistatus response.
|
|
|
|
*
|
|
|
|
* @return response
|
|
|
|
* @throws HttpException on error
|
|
|
|
*/
|
|
|
|
public MultiStatusResponse getResponse() throws HttpException {
|
|
|
|
if (responses == null || responses.size() != 1) {
|
|
|
|
throw new HttpException(getStatusLine().toString());
|
|
|
|
}
|
|
|
|
return responses.get(0);
|
|
|
|
}
|
|
|
|
|
2010-09-24 04:44:13 -04:00
|
|
|
/**
|
|
|
|
* Return method http status code.
|
|
|
|
*
|
|
|
|
* @return http status code
|
|
|
|
* @throws HttpException on error
|
|
|
|
*/
|
2010-07-24 09:11:22 -04:00
|
|
|
public int getResponseStatusCode() throws HttpException {
|
|
|
|
String responseDescription = getResponse().getResponseDescription();
|
|
|
|
if ("HTTP/1.1 201 Created".equals(responseDescription)) {
|
|
|
|
return HttpStatus.SC_CREATED;
|
|
|
|
} else {
|
|
|
|
return HttpStatus.SC_OK;
|
|
|
|
}
|
|
|
|
}
|
2010-07-24 05:45:32 -04:00
|
|
|
}
|