Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6313a06d1341db0b95c597ceeea9a3456e56a84c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************************
 * Copyright (c) 2009 Cloudsmith Inc, and other.
 * The code, documentation and other materials contained herein have been
 * licensed under the Eclipse Public License - v 1.0 by the individual
 * copyright holders listed above, as Initial Contributors under such license.
 * The text of such license is available at www.eclipse.org.
 * Contributors:
 * 	Cloudsmith Inc. - Initial API and implementation
 *  IBM Corporation - Original Implementation of checkPermissionDenied
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.repository;

import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.*;
import org.eclipse.ecf.filetransfer.IncomingFileTransferException;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.osgi.util.NLS;

/**
 * RepositoryStatusHelper is a utility class for processing of exceptions and status.
 * @author thomas.hallgren@cloudsmith.com
 * @author henrik.lindberg@cloudsmith.com - adaption to java 1.4, and Repository helper methods
 */
public abstract class RepositoryStatusHelper {

	private static final long serialVersionUID = 1L;
	protected static final String SERVER_REDIRECT = "Server redirected too many times"; //$NON-NLS-1$

	public static IStatus createStatus(String nlsMessage, Object arg) {
		return createExceptionStatus(null, nlsMessage, new Object[] {arg});
	}

	public static IStatus createStatus(String nlsMessage, Object arg1, Object arg2) {
		return createExceptionStatus(null, nlsMessage, new Object[] {arg1, arg2});
	}

	public static IStatus createStatus(String nlsMessage, Object arg1, Object arg2, Object arg3) {
		return createExceptionStatus(null, nlsMessage, new Object[] {arg1, arg2, arg3});
	}

	public static IStatus createStatus(String nlsMessage, Object[] args) {
		return createExceptionStatus(null, nlsMessage, args);
	}

	public static IStatus createStatus(String nlsMessage) {
		return createExceptionStatus(null, nlsMessage, new Object[] {});
	}

	public static IStatus createExceptionStatus(Throwable cause) {
		return (cause instanceof CoreException) ? ((CoreException) cause).getStatus() : new Status(IStatus.ERROR, Activator.ID, IStatus.OK, cause.getMessage(), cause);
	}

	public static IStatus createExceptionStatus(Throwable cause, String nlsMessage, Object[] args) {
		if (args != null && args.length > 0)
			nlsMessage = NLS.bind(nlsMessage, args);
		return new Status(IStatus.ERROR, Activator.ID, IStatus.OK, nlsMessage, cause);
	}

	public static IStatus createExceptionStatus(Throwable cause, String nlsMessage, Object arg1, Object arg2, Object arg3) {
		return createExceptionStatus(cause, nlsMessage, new Object[] {arg1, arg2, arg3});
	}

	public static IStatus createExceptionStatus(Throwable cause, String nlsMessage, Object arg1, Object arg2) {
		return createExceptionStatus(cause, nlsMessage, new Object[] {arg1, arg2});
	}

	public static IStatus createExceptionStatus(Throwable cause, String nlsMessage, Object arg1) {
		return createExceptionStatus(cause, nlsMessage, new Object[] {arg1});
	}

	public static IStatus createExceptionStatus(Throwable cause, String nlsMessage) {
		return createExceptionStatus(cause, nlsMessage, new Object[] {});
	}

	public static void deeplyPrint(Throwable e, PrintStream strm, boolean stackTrace) {
		deeplyPrint(e, strm, stackTrace, 0);
	}

	public static CoreException fromMessage(String nlsMessage, Object[] args) {
		return fromExceptionMessage(null, nlsMessage, args);
	}

	public static CoreException fromMessage(String nlsMessage, Object arg1) {
		return fromExceptionMessage(null, nlsMessage, new Object[] {arg1});
	}

	public static CoreException fromMessage(String nlsMessage, Object arg1, Object arg2) {
		return fromExceptionMessage(null, nlsMessage, new Object[] {arg1, arg2});
	}

	public static CoreException fromMessage(String nlsMessage, Object arg1, Object arg2, Object arg3) {
		return fromExceptionMessage(null, nlsMessage, new Object[] {arg1, arg2, arg3});
	}

	public static CoreException fromMessage(String nlsMessage) {
		return fromExceptionMessage(null, nlsMessage, new Object[] {});
	}

	public static CoreException fromExceptionMessage(Throwable cause, String nlsMessage, Object[] args) {
		CoreException ce = new CoreException(createExceptionStatus(cause, nlsMessage, args));
		if (cause != null)
			ce.initCause(cause);
		return ce;
	}

	public static CoreException fromExceptionMessage(Throwable cause, String nlsMessage, Object arg1, Object arg2, Object arg3) {
		return fromExceptionMessage(cause, nlsMessage, new Object[] {arg1, arg2, arg3});
	}

	public static CoreException fromExceptionMessage(Throwable cause, String nlsMessage, Object arg1, Object arg2) {
		return fromExceptionMessage(cause, nlsMessage, new Object[] {arg1, arg2});
	}

	public static CoreException fromExceptionMessage(Throwable cause, String nlsMessage, Object arg1) {
		return fromExceptionMessage(cause, nlsMessage, new Object[] {arg1});
	}

	public static CoreException fromExceptionMessage(Throwable cause, String nlsMessage) {
		return fromExceptionMessage(cause, nlsMessage, new Object[] {});
	}

	public static Throwable unwind(Throwable t) {
		for (;;) {
			Class tc = t.getClass();

			// We don't use instanceof operator since we want
			// the explicit class, not subclasses.
			//
			if (tc != RuntimeException.class && tc != InvocationTargetException.class && tc != IOException.class)
				break;

			Throwable cause = t.getCause();
			if (cause == null)
				break;

			String msg = t.getMessage();
			if (msg != null && !msg.equals(cause.toString()))
				break;

			t = cause;
		}
		return t;
	}

	public static CoreException unwindCoreException(CoreException exception) {
		IStatus status = exception.getStatus();
		while (status != null && status.getException() instanceof CoreException) {
			exception = (CoreException) status.getException();
			status = exception.getStatus();
		}
		return exception;
	}

	public static CoreException wrap(IStatus status) {
		CoreException e = new CoreException(status);
		Throwable t = status.getException();
		if (t != null)
			e.initCause(t);
		return e;
	}

	public static CoreException wrap(Throwable t) {
		t = unwind(t);
		if (t instanceof CoreException)
			return unwindCoreException((CoreException) t);

		if (t instanceof OperationCanceledException || t instanceof InterruptedException)
			return new CoreException(Status.CANCEL_STATUS);

		String msg = t.toString();
		return fromExceptionMessage(t, msg);
	}

	private static void appendLevelString(PrintStream strm, int level) {
		if (level > 0) {
			strm.print("[0"); //$NON-NLS-1$
			for (int idx = 1; idx < level; ++idx) {
				strm.print('.');
				strm.print(level);
			}
			strm.print(']');
		}
	}

	private static void deeplyPrint(CoreException ce, PrintStream strm, boolean stackTrace, int level) {
		appendLevelString(strm, level);
		if (stackTrace)
			ce.printStackTrace(strm);
		deeplyPrint(ce.getStatus(), strm, stackTrace, level);
	}

	private static void deeplyPrint(IStatus status, PrintStream strm, boolean stackTrace, int level) {
		appendLevelString(strm, level);
		String msg = status.getMessage();
		strm.println(msg);
		Throwable cause = status.getException();
		if (cause != null) {
			strm.print("Caused by: "); //$NON-NLS-1$
			if (stackTrace || !(msg.equals(cause.getMessage()) || msg.equals(cause.toString())))
				deeplyPrint(cause, strm, stackTrace, level);
		}

		if (status.isMultiStatus()) {
			IStatus[] children = status.getChildren();
			for (int i = 0; i < children.length; i++)
				deeplyPrint(children[i], strm, stackTrace, level + 1);
		}
	}

	private static void deeplyPrint(Throwable t, PrintStream strm, boolean stackTrace, int level) {
		if (t instanceof CoreException)
			deeplyPrint((CoreException) t, strm, stackTrace, level);
		else {
			appendLevelString(strm, level);
			if (stackTrace)
				t.printStackTrace(strm);
			else {
				strm.println(t.toString());
				Throwable cause = t.getCause();
				if (cause != null) {
					strm.print("Caused by: "); //$NON-NLS-1$
					deeplyPrint(cause, strm, stackTrace, level);
				}
			}
		}
	}

	/**
	 * Check if the given exception represents a permission failure (401 for HTTP),
	 * and throw a AuthenticationFailedException if a permission failure was encountered.
	 */
	public static void checkPermissionDenied(Throwable t) throws AuthenticationFailedException {
		if (t instanceof IncomingFileTransferException) {
			if (((IncomingFileTransferException) t).getErrorCode() == 401)
				throw new AuthenticationFailedException();
			IStatus status = ((IncomingFileTransferException) t).getStatus();
			t = status.getException();
		}

		if (t == null || !(t instanceof IOException))
			return;

		// try to figure out if we have a 401 by parsing the exception message
		// There is unfortunately no specific exception for "redirected too many times" - which is commonly
		// caused by a failed login.
		//
		String m = t.getMessage();
		if (m != null && (m.indexOf(" 401 ") != -1 || m.indexOf(SERVER_REDIRECT) != -1)) //$NON-NLS-1$
			throw new AuthenticationFailedException();

	}

	/**
	 * Get default "InternalError" ProvisionException.
	 * @param t
	 * @return a default "InternalError"
	 */
	public static ProvisionException internalError(Throwable t) {
		return new ProvisionException(new Status(IStatus.ERROR, Activator.ID, //
				ProvisionException.INTERNAL_ERROR, Messages.repoMan_internalError, t));
	}

	public static IStatus malformedAddressStatus(String address, Throwable t) {
		return new Status(IStatus.ERROR, Activator.ID, //
				ProvisionException.REPOSITORY_INVALID_LOCATION, NLS.bind(Messages.exception_malformedRepoURI, address), t);

	}
}

Back to the top