Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / AbstractExceptionMappingService.java
blob43745e03ebc588db3c80de9b2b99f7f2495d6b48
1 package ch.cyberduck.core;
3 /*
4 * Copyright (c) 2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import ch.cyberduck.core.exception.BackgroundException;
22 import ch.cyberduck.core.exception.ConnectionCanceledException;
23 import ch.cyberduck.core.exception.ConnectionRefusedException;
24 import ch.cyberduck.core.exception.ConnectionTimeoutException;
25 import ch.cyberduck.core.ssl.SSLExceptionMappingService;
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.commons.lang3.exception.ExceptionUtils;
29 import org.apache.http.NoHttpResponseException;
30 import org.apache.log4j.Logger;
32 import javax.net.ssl.SSLException;
33 import java.io.InterruptedIOException;
34 import java.net.SocketException;
35 import java.net.UnknownHostException;
36 import java.text.MessageFormat;
37 import java.util.concurrent.TimeoutException;
39 /**
40 * @version $Id$
42 public abstract class AbstractExceptionMappingService<T extends Exception> implements ExceptionMappingService<T> {
43 private static final Logger log = Logger.getLogger(AbstractExceptionMappingService.class);
45 public BackgroundException map(final String message, final T failure) {
46 final BackgroundException exception = this.map(failure);
47 final StringBuilder m = new StringBuilder();
48 this.append(m, StringUtils.chomp(LocaleFactory.localizedString(message, "Error")));
49 exception.setMessage(m.toString());
50 return exception;
53 public BackgroundException map(final String message, final T failure, final Path file) {
54 final BackgroundException exception = this.map(failure);
55 final StringBuilder m = new StringBuilder();
56 final String formatted = MessageFormat.format(StringUtils.chomp(
57 LocaleFactory.localizedString(message, "Error")), file.getName());
58 if(StringUtils.contains(formatted, String.format("%s ", file.getName()))
59 || StringUtils.contains(formatted, String.format(" %s", file.getName()))) {
60 this.append(m, formatted);
62 else {
63 this.append(m, String.format("%s (%s)",
64 MessageFormat.format(StringUtils.chomp(LocaleFactory.localizedString(message, "Error")), file.getName()),
65 file.getAbsolute()));
67 exception.setMessage(m.toString());
68 return exception;
71 /**
72 * @param exception Service error
73 * @return Mapped exception
75 @Override
76 public abstract BackgroundException map(T exception);
78 public StringBuilder append(final StringBuilder buffer, final String message) {
79 final StringAppender appender = new StringAppender(buffer);
80 appender.append(StringUtils.capitalize(message));
81 return buffer;
84 protected BackgroundException wrap(final T failure, final StringBuilder buffer) {
85 if(buffer.toString().isEmpty()) {
86 log.warn(String.format("No message for failure %s", failure));
87 this.append(buffer, LocaleFactory.localizedString("Interoperability failure", "Error"));
89 if(failure instanceof SSLException) {
90 return new SSLExceptionMappingService().map((SSLException) failure);
92 for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
93 if(cause instanceof InterruptedIOException) {
94 // Handling socket timeouts
95 return new ConnectionTimeoutException(buffer.toString(), failure);
97 if(cause instanceof TimeoutException) {
99 return new ConnectionTimeoutException(buffer.toString(), failure);
101 if(cause instanceof SocketException) {
102 if(StringUtils.equals(cause.getMessage(), "Software caused connection abort")) {
103 return new ConnectionCanceledException(failure);
105 if(StringUtils.equals(cause.getMessage(), "Socket closed")) {
106 return new ConnectionCanceledException(failure);
108 return new ConnectionRefusedException(buffer.toString(), failure);
110 if(cause instanceof UnknownHostException) {
111 return new ConnectionRefusedException(buffer.toString(), failure);
113 if(cause instanceof NoHttpResponseException) {
114 return new ConnectionRefusedException(buffer.toString(), failure);
117 return new BackgroundException(
118 LocaleFactory.localizedString("Connection failed", "Error"), buffer.toString(), failure);