public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 400: break; case 401: case 403: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; } command.setException(exception); } }
@Override public Object execute(CommandSession session) throws Exception { try { this.session = session; return doExecute(); } catch (AuthorizationException ex) { System.err.println(String.format("Authorization error: %s",ex.getMessage())); return null; } }
containerName, options); } catch (AuthorizationException ae) { if (ae.getCause() instanceof AccessDeniedException) { throw new S3Exception(S3ErrorCode.ACCESS_DENIED, "Could not create bucket", ae);
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AzureStorageError error, String message) { switch (response.getStatusCode()) { case 401: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; case 411: exception = new IllegalArgumentException(message); break; } return exception; }
@Override public void handleError(HttpCommand command, HttpResponse response) { try { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); Set<ErrorResponse> errors = parseErrorsFromContentOrNull(data); if (errors != null) exception = new GoGridResponseException(command, response, errors); switch (response.getStatusCode()) { case 400: if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) { exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception); break; } break; case 403: exception = new AuthorizationException(exception.getMessage(), exception); break; } command.setException(exception); } finally { releasePayload(response); } }
@In(required = true) private String username; @In Identity identity; public boolean authenticate() { //... throw new AuthorizationException("login failed"); //... return true; }
@Override public Object createOrPropagate(Throwable t) throws Exception { propagateIfRetryAfter.create(t); // if we pass here, we aren't a retry-after exception if (t instanceof HttpResponseException) { HttpResponseException responseException = HttpResponseException.class.cast(t); if (responseException.getResponse() != null) switch (responseException.getResponse().getStatusCode()) { case 401: throw new AuthorizationException(responseException); case 403: throw new AuthorizationException(responseException); case 404: throw new ResourceNotFoundException(responseException); case 409: throw new IllegalStateException(responseException); } } throw propagate(t); } }
private Exception refineException(AWSResponseException in) { int statusCode = in.getResponse().getStatusCode(); String errorCode = in.getError().getCode(); String message = in.getError().getMessage(); if (statusCode == 403 || "RequestExpired".equals(errorCode)) return new AuthorizationException(message, in); if (statusCode == 400) { if (ImmutableSet.of("InvalidAction", "AccessDenied").contains(errorCode)) return new UnsupportedOperationException(message, in); else if ("Throttling".equals(errorCode)) return new InsufficientResourcesException(message, in); else if (message.indexOf("not found") != -1) return new ResourceNotFoundException(message, in); return new IllegalArgumentException(message, in); } return in; } }
@Override public HttpRequest filter(HttpRequest request) throws HttpException { Credentials currentCreds = checkNotNull(creds.get(), "credential supplier returned null"); if (currentCreds.identity.indexOf(':') == -1) { throw new AuthorizationException(String.format("Identity %s does not match format tenantId:username", currentCreds.identity), null); } String tenantId = currentCreds.identity.substring(0, currentCreds.identity.indexOf(':')); String username = currentCreds.identity.substring(currentCreds.identity.indexOf(':') + 1); return request.toBuilder().replaceHeader("TID", tenantId) .replaceHeader(AUTHORIZATION, basic(username, currentCreds.credential)).build(); } }
@Test(expectedExceptions = AuthorizationException.class) public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Throwable { Exception e = new AuthorizationException(); propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e), ImmutableSet.<TypeToken<? extends Throwable>> of()); }
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AzureStorageError error, String message) { switch (response.getStatusCode()) { case 401: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; case 411: exception = new IllegalArgumentException(message); break; } return exception; }
@Override public String get() { throw new AuthorizationException(); } }, authException).get();
@Override public Object createOrPropagate(Throwable t) throws Exception { propagateIfRetryAfter.createOrPropagate(t); // if we pass here, we aren't a retry-after exception if (t instanceof HttpResponseException) { HttpResponseException responseException = HttpResponseException.class.cast(t); if (responseException.getResponse() != null) switch (responseException.getResponse().getStatusCode()) { case 401: throw new AuthorizationException(responseException); case 403: throw new AuthorizationException(responseException); case 404: throw new ResourceNotFoundException(responseException); case 409: throw new IllegalStateException(responseException); } } throw propagate(t); } }
public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 400: break; case 401: case 403: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; } command.setException(exception); } }
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AzureStorageError error, String message) { switch (response.getStatusCode()) { case 401: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; case 411: exception = new IllegalArgumentException(message); break; } return exception; }
public Void createOrPropagate(Throwable t) throws Exception { AuthorizationException ex = returnValueOnCodeOrNull(t, new AuthorizationException(t.getMessage(), t), new Predicate<Integer>() { @Override public boolean apply(Integer input) { return input >= 400 && input < 500; } }); throw ex != null ? ex : propagate(t); } }
@Override public Object createOrPropagate(Throwable t) throws Exception { propagateIfRetryAfter.createOrPropagate(t); // if we pass here, we aren't a retry-after exception if (t instanceof HttpResponseException) { HttpResponseException responseException = HttpResponseException.class.cast(t); if (responseException.getResponse() != null) switch (responseException.getResponse().getStatusCode()) { case 401: throw new AuthorizationException(responseException); case 403: throw new AuthorizationException(responseException); case 404: throw new ResourceNotFoundException(responseException); case 409: throw new IllegalStateException(responseException); } } throw propagate(t); } }
@Test(enabled = true, dependsOnMethods = "testReboot") public void testSuspendResume() throws Exception { try { // may fail because of lack of AdminActions extension or non-admin user, so log and continue super.testSuspendResume(); } catch (AuthorizationException e) { getAnonymousLogger().info("testSuspendResume() threw, probably due to lack of privileges: " + e.getMessage()); } catch (UnsupportedOperationException e) { getAnonymousLogger().info("testSuspendResume() threw, probably due to unavailable AdminActions extension: " + e.getMessage()); } }
containerName, options); } catch (AuthorizationException ae) { if (ae.getCause() instanceof AccessDeniedException) { throw new S3Exception(S3ErrorCode.ACCESS_DENIED, "Could not create bucket", ae);
public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 400: break; case 401: case 403: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; } command.setException(exception); } }