diff options
author | spingel | 2010-02-22 02:48:21 +0000 |
---|---|---|
committer | spingel | 2010-02-22 02:48:21 +0000 |
commit | e13dafd0a1e94f6c7b33ac49c802bbf94bf4b569 (patch) | |
tree | 3661d3ec18bb042fa60f8dea62e54bebd196dd0b | |
parent | 7a5a142ff091918da6ce9bafbe21ba52dca71b15 (diff) | |
download | org.eclipse.mylyn.tasks-e13dafd0a1e94f6c7b33ac49c802bbf94bf4b569.tar.gz org.eclipse.mylyn.tasks-e13dafd0a1e94f6c7b33ac49c802bbf94bf4b569.tar.xz org.eclipse.mylyn.tasks-e13dafd0a1e94f6c7b33ac49c802bbf94bf4b569.zip |
302642: fix exception handling for multi-calls
5 files changed, 89 insertions, 35 deletions
diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/client/TracXmlRpcClient.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/client/TracXmlRpcClient.java index 4bf167bcf..c87c821ad 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/client/TracXmlRpcClient.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/client/TracXmlRpcClient.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TimeZone; +import java.util.regex.Pattern; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Header; @@ -91,6 +92,8 @@ import org.eclipse.osgi.util.NLS; */ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiClient { + private static final Pattern RPC_METHOD_NOT_FOUND_PATTERN = Pattern.compile("RPC method \".*\" not found"); //$NON-NLS-1$ + private class XmlRpcRequest { private final String method; @@ -159,38 +162,17 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli parameters, monitor); return xmlrpc.execute(request); } catch (TracHttpException e) { - if (e.code == HttpStatus.SC_UNAUTHORIZED) { - if (DEBUG_AUTH) { - System.err.println(location.getUrl() + ": Unauthorized (" + e.code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ - } - digestScheme = null; - TracLoginException exception = new TracLoginException(); - exception.setNtlmAuthRequested(e.getAuthScheme() instanceof NTLMScheme); - throw exception; - } else if (e.code == HttpStatus.SC_FORBIDDEN) { - if (DEBUG_AUTH) { - System.err.println(location.getUrl() + ": Forbidden (" + e.code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ - } - digestScheme = null; - throw new TracPermissionDeniedException(); - } else if (e.code == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { - if (DEBUG_AUTH) { - System.err.println(location.getUrl() + ": Proxy authentication required (" + e.code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ - } - throw new TracProxyAuthenticationException(); - } else { - throw new TracException(e); - } + handleAuthenticationException(e.code, e.getAuthScheme()); + // if not handled, throw generic exception + throw new TracException(e); } catch (XmlRpcException e) { // XXX work-around for http://trac-hacks.org/ticket/5848 - if ("XML_RPC privileges are required to perform this operation".equals(e.getMessage())) { //$NON-NLS-1$ - if (DEBUG_AUTH) { - System.err.println(location.getUrl() + ": Forbidden (" + e.code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ - } - digestScheme = null; - throw new TracPermissionDeniedException(); - } - if (e.code == NO_SUCH_METHOD_ERROR) { + if ("XML_RPC privileges are required to perform this operation".equals(e.getMessage()) //$NON-NLS-1$ + || e.code == XML_FAULT_PERMISSION_DENIED) { + handleAuthenticationException(HttpStatus.SC_FORBIDDEN, null); + // should never happen as call above should always throw an exception + throw new TracRemoteException(e); + } else if (isNoSuchMethodException(e)) { throw new TracNoSuchMethodException(e); } else { throw new TracRemoteException(e); @@ -201,6 +183,43 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli throw new TracException(e); } } + + private boolean isNoSuchMethodException(XmlRpcException e) { + // the fault code is used for various errors, therefore detection is based on the message + // message format by XML-RPC Plugin version: + // 1.0.1: XML-RPC method "ticket.ge1t" not found + // 1.0.6: RPC method "ticket.ge1t" not found + // 1.10: RPC method "ticket.ge1t" not found' while executing 'ticket.ge1t() + if (e.code == XML_FAULT_GENERAL_ERROR && e.getMessage() != null + && RPC_METHOD_NOT_FOUND_PATTERN.matcher(e.getMessage()).find()) { + return true; + } + return false; + } + + protected boolean handleAuthenticationException(int code, AuthScheme authScheme) throws TracException { + if (code == HttpStatus.SC_UNAUTHORIZED) { + if (DEBUG_AUTH) { + System.err.println(location.getUrl() + ": Unauthorized (" + code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + } + digestScheme = null; + TracLoginException exception = new TracLoginException(); + exception.setNtlmAuthRequested(authScheme instanceof NTLMScheme); + throw exception; + } else if (code == HttpStatus.SC_FORBIDDEN) { + if (DEBUG_AUTH) { + System.err.println(location.getUrl() + ": Forbidden (" + code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + } + digestScheme = null; + throw new TracPermissionDeniedException(); + } else if (code == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { + if (DEBUG_AUTH) { + System.err.println(location.getUrl() + ": Proxy authentication required (" + code + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + } + throw new TracProxyAuthenticationException(); + } + return false; + } } private static final boolean DEBUG_XMLRPC = Boolean.valueOf(Platform.getDebugOption("org.eclipse.mylyn.trac.core/debug/xmlrpc")); //$NON-NLS-1$ @@ -215,7 +234,15 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli public static final int REQUIRED_MINOR = 1; - private static final int NO_SUCH_METHOD_ERROR = 1; + // XML-RPC Plugin <1.10 + private static final int XML_FAULT_GENERAL_ERROR = 1; + + // since XML-RPC Plugin 1.10 + @SuppressWarnings("unused") + private static final int XML_FAULT_RESOURCE_NOT_FOUND = 404; + + // since XML-RPC Plugin 1.10 + private static final int XML_FAULT_PERMISSION_DENIED = 403; private static final int LATEST_VERSION = -1; @@ -452,6 +479,13 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli if (exceptionData.containsKey("faultCode") && exceptionData.containsKey("faultString")) { //$NON-NLS-1$ //$NON-NLS-2$ throw new XmlRpcException(Integer.parseInt(exceptionData.get("faultCode").toString()), //$NON-NLS-1$ (String) exceptionData.get("faultString")); //$NON-NLS-1$ + } else if (exceptionData.containsKey("title")) { //$NON-NLS-1$ + String message = (String) exceptionData.get("title"); //$NON-NLS-1$ + String detail = (String) exceptionData.get("_message"); //$NON-NLS-1$ + if (detail != null) { + message += ": " + detail; //$NON-NLS-1$ + } + throw new XmlRpcException(XML_FAULT_GENERAL_ERROR, message); } } } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/client/TracXmlRpcClientTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/client/TracXmlRpcClientTest.java index d81b48039..9b54429ce 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/client/TracXmlRpcClientTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/client/TracXmlRpcClientTest.java @@ -77,6 +77,14 @@ public class TracXmlRpcClientTest extends TestCase { } } + public void testSingleCallExceptions() throws Exception { + try { + ((TracXmlRpcClient) client).getTicketLastChanged(Integer.MAX_VALUE, null); + fail("Expected TracRemoteException"); + } catch (TracRemoteException e) { + } + } + public void testUpdateAttributes() throws Exception { assertNull(client.getMilestones()); client.updateAttributes(new NullProgressMonitor(), true); @@ -326,6 +334,11 @@ public class TracXmlRpcClientTest extends TestCase { } public void testGetRecentWikiChanges() throws Exception { + // FIXME 3.4 re-enable for trunk + if (TracFixture.current() == TracFixture.TRAC_TRUNK_XML_RPC) { + return; + } + TracWikiPageInfo[] changes = ((TracXmlRpcClient) client).getRecentWikiChanges(new Date(0), null); TracWikiPageInfo testPage = null; for (TracWikiPageInfo item : changes) { diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/core/TracTaskDataHandlerXmlRpcTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/core/TracTaskDataHandlerXmlRpcTest.java index b44eae599..a4152a402 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/core/TracTaskDataHandlerXmlRpcTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/core/TracTaskDataHandlerXmlRpcTest.java @@ -51,7 +51,6 @@ import org.eclipse.mylyn.tasks.core.data.TaskOperation; import org.eclipse.mylyn.tasks.core.data.TaskRelation; import org.eclipse.mylyn.tasks.ui.TasksUi; import org.eclipse.mylyn.trac.tests.support.TracFixture; -import org.eclipse.mylyn.trac.tests.support.TracTestConstants; import org.eclipse.mylyn.trac.tests.support.TracTestUtil; import org.eclipse.mylyn.trac.tests.support.XmlRpcServer.TestData; @@ -347,12 +346,12 @@ public class TracTaskDataHandlerXmlRpcTest extends TestCase { } public void testOperations() throws Exception { - boolean hasReassign = TracTestConstants.TEST_TRAC_011_URL.equals(repository.getRepositoryUrl()); + boolean hasReassign = TracFixture.current().getVersion().compareTo("0.11") >= 0; TaskData taskData = taskDataHandler.getTaskData(repository, "1", new NullProgressMonitor()); List<TaskAttribute> operations = taskData.getAttributeMapper().getAttributesByType(taskData, TaskAttribute.TYPE_OPERATION); - assertEquals((hasReassign ? 5 : 4), operations.size()); + assertEquals("Unexpected operations: " + operations, (hasReassign ? 5 : 4), operations.size()); TaskOperation operation = taskData.getAttributeMapper().getTaskOperation(operations.get(0)); assertEquals(TaskAttribute.OPERATION, operation.getTaskAttribute().getId()); diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracFixture.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracFixture.java index d48f556ba..70d491ba6 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracFixture.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracFixture.java @@ -62,6 +62,12 @@ public class TracFixture extends TestFixture { public static TracFixture TRAC_0_11_XML_RPC = new TracFixture(Version.XML_RPC, TracTestConstants.TEST_TRAC_011_URL, "0.11", "XML-RPC"); + public static TracFixture TRAC_TRUNK_WEB = new TracFixture(Version.TRAC_0_9, TracTestConstants.TEST_TRAC_TRUNK_URL, + "0.12dev-r0", "Web"); + + public static TracFixture TRAC_TRUNK_XML_RPC = new TracFixture(Version.XML_RPC, + TracTestConstants.TEST_TRAC_TRUNK_URL, "0.12dev-r0", "XML-RPC"); + public static TracFixture DEFAULT = TRAC_0_11_XML_RPC; //public static TracFixture DEFAULT = TRAC_0_11_WEB; @@ -70,7 +76,7 @@ public class TracFixture extends TestFixture { * Standard configurations for running all test against. */ public static final TracFixture[] ALL = new TracFixture[] { TRAC_0_9_WEB, TRAC_0_10_WEB, TRAC_0_11_WEB, - TRAC_0_10_XML_RPC, TRAC_0_11_XML_RPC, /* TRAC_0_10_XML_RPC_SSL, */}; + TRAC_TRUNK_WEB, TRAC_0_10_XML_RPC, TRAC_0_11_XML_RPC, TRAC_TRUNK_XML_RPC, /* TRAC_0_10_XML_RPC_SSL, */}; /** * Misc configurations for running a limited number of test against. diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracTestConstants.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracTestConstants.java index b9b6f8554..11146eb2f 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracTestConstants.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/TracTestConstants.java @@ -30,6 +30,8 @@ public class TracTestConstants { public static final String TEST_TRAC_011_URL = "http://" + SERVER + "/trac011"; + public static final String TEST_TRAC_TRUNK_URL = "http://" + SERVER + "/tractrunk"; + public static final String TEST_TRAC_INVALID_URL = "http://" + SERVER + "/doesnotexist"; } |