下面列出了org.apache.http.HttpStatus#SC_MULTI_STATUS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @inheritDoc
*/
@Override
public int updateFolder(String folderPath, Map<String, String> properties) throws IOException {
Set<PropertyValue> propertyValues = new HashSet<>();
if (properties != null) {
for (Map.Entry<String, String> entry : properties.entrySet()) {
propertyValues.add(Field.createPropertyValue(entry.getKey(), entry.getValue()));
}
}
ExchangePropPatchRequest propPatchRequest = new ExchangePropPatchRequest(URIUtil.encodePath(getFolderPath(folderPath)), propertyValues);
try (CloseableHttpResponse response = httpClientAdapter.execute(propPatchRequest)) {
propPatchRequest.handleResponse(response);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_MULTI_STATUS) {
try {
status = propPatchRequest.getResponseStatusCode();
} catch (HttpResponseException e) {
throw new IOException(e.getMessage(), e);
}
}
return status;
}
}
/**
* @inheritDoc
*/
@Override
public void updateMessage(ExchangeSession.Message message, Map<String, String> properties) throws IOException {
HttpProppatch patchMethod = new HttpProppatch(encodeAndFixUrl(message.permanentUrl), buildProperties(properties)) {
@Override
public MultiStatus getResponseBodyAsMultiStatus(HttpResponse response) {
// ignore response body, sometimes invalid with exchange mapi properties
throw new UnsupportedOperationException();
}
};
try (CloseableHttpResponse response = httpClientAdapter.execute(patchMethod)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
throw new DavMailException("EXCEPTION_UNABLE_TO_UPDATE_MESSAGE");
}
}
}
/**
* Send inbox response for request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendInbox(CaldavRequest request) throws IOException {
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
appendInbox(response, request, null);
// do not try to access inbox on shared calendar
if (!session.isSharedFolder(request.getFolderPath(null)) && request.getDepth() == 1
&& !request.isLightning()) {
try {
DavGatewayTray.debug(new BundleMessage("LOG_SEARCHING_CALENDAR_MESSAGES"));
List<ExchangeSession.Event> events = session.getEventMessages(request.getFolderPath());
DavGatewayTray.debug(new BundleMessage("LOG_FOUND_CALENDAR_MESSAGES", events.size()));
appendEventsResponses(response, request, events);
} catch (HttpResponseException e) {
// unauthorized access, probably an inbox on shared calendar
DavGatewayTray.debug(new BundleMessage("LOG_ACCESS_FORBIDDEN", request.getFolderPath(), e.getMessage()));
}
}
response.endMultistatus();
response.close();
}
/**
* Fake PROPPATCH response for request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void patchCalendar(CaldavRequest request) throws IOException {
String displayname = request.getProperty("displayname");
String folderPath = request.getFolderPath();
if (displayname != null) {
String targetPath = request.getParentFolderPath() + '/' + displayname;
if (!targetPath.equals(folderPath)) {
session.moveFolder(folderPath, targetPath);
}
}
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
// ical calendar folder proppatch
if (request.hasProperty("calendar-color") || request.hasProperty("calendar-order")) {
response.startPropstat();
if (request.hasProperty("calendar-color")) {
response.appendProperty("x1:calendar-color", "x1=\"http://apple.com/ns/ical/\"", null);
}
if (request.hasProperty("calendar-order")) {
response.appendProperty("x1:calendar-order", "x1=\"http://apple.com/ns/ical/\"", null);
}
response.endPropStatOK();
}
response.endMultistatus();
response.close();
}
/**
* @inheritDoc
*/
@Override
public int createFolder(String folderPath, String folderClass, Map<String, String> properties) throws IOException {
Set<PropertyValue> propertyValues = new HashSet<>();
if (properties != null) {
for (Map.Entry<String, String> entry : properties.entrySet()) {
propertyValues.add(Field.createPropertyValue(entry.getKey(), entry.getValue()));
}
}
propertyValues.add(Field.createPropertyValue("folderclass", folderClass));
// standard MkColMethod does not take properties, override ExchangePropPatchRequest instead
ExchangePropPatchRequest propPatchRequest = new ExchangePropPatchRequest(URIUtil.encodePath(getFolderPath(folderPath)), propertyValues) {
@Override
public String getMethod() {
return "MKCOL";
}
};
int status;
try (CloseableHttpResponse response = httpClientAdapter.execute(propPatchRequest)) {
propPatchRequest.handleResponse(response);
status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_MULTI_STATUS) {
status = propPatchRequest.getResponseStatusCode();
} else if (status == HttpStatus.SC_METHOD_NOT_ALLOWED) {
LOGGER.info("Folder " + folderPath + " already exists");
}
} catch (HttpResponseException e) {
throw new IOException(e.getMessage(), e);
}
LOGGER.debug("Create folder " + folderPath + " returned " + status);
return status;
}
/**
* Execute WebDav request
*
* @param request WebDav request
* @return multistatus response
* @throws IOException on error
*/
public MultiStatus executeDavRequest(BaseDavRequest request) throws IOException {
handleURI(request);
MultiStatus multiStatus = null;
try (CloseableHttpResponse response = execute(request)) {
request.checkSuccess(response);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_MULTI_STATUS) {
multiStatus = request.getResponseBodyAsMultiStatus(response);
}
} catch (DavException e) {
LOGGER.error(e.getMessage(), e);
throw new IOException(e.getErrorCode() + " " + e.getStatusPhrase(), e);
}
return multiStatus;
}
/**
* Send outbox response for request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendOutbox(CaldavRequest request) throws IOException {
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
appendOutbox(response, request, null);
response.endMultistatus();
response.close();
}
/**
* Send calendar response for request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendFolderOrItem(CaldavRequest request) throws IOException {
String folderPath = request.getFolderPath();
// process request before sending response to avoid sending headers twice on error
ExchangeSession.Folder folder = session.getFolder(folderPath);
List<ExchangeSession.Contact> contacts = null;
List<ExchangeSession.Event> events = null;
List<ExchangeSession.Folder> folderList = null;
if (request.getDepth() == 1) {
if (folder.isContact()) {
contacts = session.getAllContacts(folderPath, !isOldCardavClient(request));
} else if (folder.isCalendar() || folder.isTask()) {
events = session.getAllEvents(folderPath);
if (!folderPath.startsWith("/public")) {
folderList = session.getSubCalendarFolders(folderPath, false);
}
}
}
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
appendFolderOrItem(response, request, folder, null);
if (request.getDepth() == 1) {
if (folder.isContact()) {
appendContactsResponses(response, request, contacts);
} else if (folder.isCalendar() || folder.isTask()) {
appendEventsResponses(response, request, events);
// Send sub folders for multi-calendar support under iCal, except for public folders
if (folderList != null) {
for (ExchangeSession.Folder subFolder : folderList) {
appendFolderOrItem(response, request, subFolder, subFolder.folderPath.substring(subFolder.folderPath.indexOf('/') + 1));
}
}
}
}
response.endMultistatus();
response.close();
}
/**
* Send principals folder.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendPrincipalsFolder(CaldavRequest request) throws IOException {
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
response.startResponse(encodePath(request, request.getPath()));
response.startPropstat();
if (request.hasProperty("current-user-principal")) {
response.appendHrefProperty("D:current-user-principal", encodePath(request, "/principals/users/" + session.getEmail()));
}
response.endPropStatOK();
response.endResponse();
response.endMultistatus();
response.close();
}
/**
* Send user response for request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendUserRoot(CaldavRequest request) throws IOException {
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
response.startResponse(encodePath(request, request.getPath()));
response.startPropstat();
if (request.hasProperty("resourcetype")) {
response.appendProperty("D:resourcetype", "<D:collection/>");
}
if (request.hasProperty("displayname")) {
response.appendProperty("D:displayname", request.getLastPath());
}
if (request.hasProperty("getctag")) {
ExchangeSession.Folder rootFolder = session.getFolder("");
response.appendProperty("CS:getctag", "CS=\"http://calendarserver.org/ns/\"",
IOUtil.encodeBase64AsString(rootFolder.ctag));
}
response.endPropStatOK();
if (request.getDepth() == 1) {
appendInbox(response, request, "inbox");
appendOutbox(response, request, "outbox");
appendFolderOrItem(response, request, session.getFolder(request.getFolderPath("calendar")), "calendar");
appendFolderOrItem(response, request, session.getFolder(request.getFolderPath("contacts")), "contacts");
}
response.endResponse();
response.endMultistatus();
response.close();
}
/**
* Send caldav response for /directory/ request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendDirectory(CaldavRequest request) throws IOException {
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
response.startResponse("/directory/");
response.startPropstat();
if (request.hasProperty("current-user-privilege-set")) {
response.appendProperty("D:current-user-privilege-set", "<D:privilege><D:read/></D:privilege>");
}
response.endPropStatOK();
response.endResponse();
response.endMultistatus();
response.close();
}
@Override
public BackgroundException map(final SardineException failure) {
final StringBuilder buffer = new StringBuilder();
switch(failure.getStatusCode()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_MULTI_STATUS:
// HTTP method status
this.append(buffer, failure.getMessage());
// Failure unmarshalling XML response
return new InteroperabilityException(buffer.toString(), failure);
}
this.append(buffer, String.format("%s (%d %s)", failure.getReasonPhrase(), failure.getStatusCode(), failure.getResponsePhrase()));
return super.map(failure, buffer, failure.getStatusCode());
}
/**
* Send caldav response for / request.
*
* @param request Caldav request
* @throws IOException on error
*/
public void sendRoot(CaldavRequest request) throws IOException {
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
response.startResponse("/");
response.startPropstat();
if (request.hasProperty("principal-collection-set")) {
response.appendHrefProperty("D:principal-collection-set", "/principals/users/");
}
if (request.hasProperty("displayname")) {
response.appendProperty("D:displayname", "ROOT");
}
if (request.hasProperty("resourcetype")) {
response.appendProperty("D:resourcetype", "<D:collection/>");
}
if (request.hasProperty("current-user-principal")) {
response.appendHrefProperty("D:current-user-principal", encodePath(request, "/principals/users/" + session.getEmail()));
}
response.endPropStatOK();
response.endResponse();
if (request.depth == 1) {
// iPhone workaround: send calendar subfolder
response.startResponse("/users/" + session.getEmail() + "/calendar");
response.startPropstat();
if (request.hasProperty("resourcetype")) {
response.appendProperty("D:resourcetype", "<D:collection/>" +
"<C:calendar xmlns:C=\"urn:ietf:params:xml:ns:caldav\"/>");
}
if (request.hasProperty("displayname")) {
response.appendProperty("D:displayname", session.getEmail());
}
if (request.hasProperty("supported-calendar-component-set")) {
response.appendProperty("C:supported-calendar-component-set", "<C:comp name=\"VEVENT\"/>");
}
response.endPropStatOK();
response.endResponse();
response.startResponse("/users");
response.startPropstat();
if (request.hasProperty("displayname")) {
response.appendProperty("D:displayname", "users");
}
if (request.hasProperty("resourcetype")) {
response.appendProperty("D:resourcetype", "<D:collection/>");
}
response.endPropStatOK();
response.endResponse();
response.startResponse("/principals");
response.startPropstat();
if (request.hasProperty("displayname")) {
response.appendProperty("D:displayname", "principals");
}
if (request.hasProperty("resourcetype")) {
response.appendProperty("D:resourcetype", "<D:collection/>");
}
response.endPropStatOK();
response.endResponse();
}
response.endMultistatus();
response.close();
}
/**
* Send Caldav principal response.
*
* @param request Caldav request
* @param prefix principal prefix (users or public)
* @param principal principal name (email address for users)
* @throws IOException on error
*/
public void sendPrincipal(CaldavRequest request, String prefix, String principal) throws IOException {
// actual principal is email address
String actualPrincipal = principal;
if ("users".equals(prefix) &&
(principal.equalsIgnoreCase(session.getAlias()) || (principal.equalsIgnoreCase(session.getAliasFromLogin())))) {
actualPrincipal = session.getEmail();
}
CaldavResponse response = new CaldavResponse(HttpStatus.SC_MULTI_STATUS);
response.startMultistatus();
response.startResponse(encodePath(request, "/principals/" + prefix + '/' + principal));
response.startPropstat();
if (request.hasProperty("principal-URL") && request.isIcal5()) {
response.appendHrefProperty("D:principal-URL", encodePath(request, "/principals/" + prefix + '/' + actualPrincipal));
}
if (request.hasProperty("calendar-home-set")) {
if ("users".equals(prefix)) {
response.appendHrefProperty("C:calendar-home-set", encodePath(request, "/users/" + actualPrincipal + "/calendar/"));
} else {
response.appendHrefProperty("C:calendar-home-set", encodePath(request, '/' + prefix + '/' + actualPrincipal));
}
}
if (request.hasProperty("calendar-user-address-set") && "users".equals(prefix)) {
response.appendHrefProperty("C:calendar-user-address-set", "mailto:" + actualPrincipal);
}
if (request.hasProperty("addressbook-home-set")) {
if (request.isUserAgent("Address%20Book") || request.isUserAgent("Darwin")) {
response.appendHrefProperty("E:addressbook-home-set", encodePath(request, '/' + prefix + '/' + actualPrincipal + '/'));
} else if ("users".equals(prefix)) {
response.appendHrefProperty("E:addressbook-home-set", encodePath(request, "/users/" + actualPrincipal + "/contacts/"));
} else {
response.appendHrefProperty("E:addressbook-home-set", encodePath(request, '/' + prefix + '/' + actualPrincipal + '/'));
}
}
if ("users".equals(prefix)) {
if (request.hasProperty("schedule-inbox-URL")) {
response.appendHrefProperty("C:schedule-inbox-URL", encodePath(request, "/users/" + actualPrincipal + "/inbox/"));
}
if (request.hasProperty("schedule-outbox-URL")) {
response.appendHrefProperty("C:schedule-outbox-URL", encodePath(request, "/users/" + actualPrincipal + "/outbox/"));
}
} else {
// public calendar, send root href as inbox url (always empty) for Lightning
if (request.isLightning() && request.hasProperty("schedule-inbox-URL")) {
response.appendHrefProperty("C:schedule-inbox-URL", "/");
}
// send user outbox
if (request.hasProperty("schedule-outbox-URL")) {
response.appendHrefProperty("C:schedule-outbox-URL", encodePath(request, "/users/" + session.getEmail() + "/outbox/"));
}
}
if (request.hasProperty("displayname")) {
response.appendProperty("D:displayname", actualPrincipal);
}
if (request.hasProperty("resourcetype")) {
response.appendProperty("D:resourcetype", "<D:collection/><D:principal/>");
}
if (request.hasProperty("supported-report-set")) {
response.appendProperty("D:supported-report-set", "<D:supported-report><D:report><C:calendar-multiget/></D:report></D:supported-report>");
}
response.endPropStatOK();
response.endResponse();
response.endMultistatus();
response.close();
}
public static boolean isSuccess(final int statusCode) {
return statusCode >= HttpStatus.SC_OK && statusCode <= HttpStatus.SC_MULTI_STATUS;
}
private int computeStatus(int status) {
switch (status) {
case HttpStatus.SC_FORBIDDEN:
case HttpStatus.SC_METHOD_NOT_ALLOWED:
case HttpStatus.SC_BAD_REQUEST:
case HttpStatus.SC_UNAUTHORIZED:
case HttpStatus.SC_PAYMENT_REQUIRED:
case HttpStatus.SC_NOT_FOUND:
case HttpStatus.SC_NOT_ACCEPTABLE:
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
case HttpStatus.SC_REQUEST_TIMEOUT:
case HttpStatus.SC_CONFLICT:
case HttpStatus.SC_GONE:
case HttpStatus.SC_LENGTH_REQUIRED:
case HttpStatus.SC_PRECONDITION_FAILED:
case HttpStatus.SC_REQUEST_TOO_LONG:
case HttpStatus.SC_REQUEST_URI_TOO_LONG:
case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
case HttpStatus.SC_EXPECTATION_FAILED:
case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
case HttpStatus.SC_METHOD_FAILURE:
case HttpStatus.SC_UNPROCESSABLE_ENTITY:
case HttpStatus.SC_LOCKED:
case HttpStatus.SC_FAILED_DEPENDENCY:
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
case HttpStatus.SC_NOT_IMPLEMENTED:
case HttpStatus.SC_BAD_GATEWAY:
case HttpStatus.SC_SERVICE_UNAVAILABLE:
case HttpStatus.SC_GATEWAY_TIMEOUT:
case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
case HttpStatus.SC_INSUFFICIENT_STORAGE:
return 0;
case HttpStatus.SC_CONTINUE:
case HttpStatus.SC_SWITCHING_PROTOCOLS:
case HttpStatus.SC_PROCESSING:
case HttpStatus.SC_OK:
case HttpStatus.SC_CREATED:
case HttpStatus.SC_ACCEPTED:
case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
case HttpStatus.SC_NO_CONTENT:
case HttpStatus.SC_RESET_CONTENT:
case HttpStatus.SC_PARTIAL_CONTENT:
case HttpStatus.SC_MULTI_STATUS:
case HttpStatus.SC_MULTIPLE_CHOICES:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_NOT_MODIFIED:
case HttpStatus.SC_USE_PROXY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return 1;
default :
return 1;
}
}
public static boolean isSuccess(final int statusCode) {
return statusCode >= HttpStatus.SC_OK && statusCode <= HttpStatus.SC_MULTI_STATUS;
}