| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.gogrid.handlers; |
| 20 | |
| 21 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.gogrid.GoGridResponseException; |
| 26 | import org.jclouds.gogrid.domain.internal.ErrorResponse; |
| 27 | import org.jclouds.gogrid.functions.ParseErrorFromJsonResponse; |
| 28 | import org.jclouds.http.HttpCommand; |
| 29 | import org.jclouds.http.HttpErrorHandler; |
| 30 | import org.jclouds.http.HttpResponse; |
| 31 | import org.jclouds.http.HttpResponseException; |
| 32 | import org.jclouds.rest.AuthorizationException; |
| 33 | import org.jclouds.rest.ResourceNotFoundException; |
| 34 | |
| 35 | import com.google.common.collect.Iterables; |
| 36 | import com.google.inject.Inject; |
| 37 | |
| 38 | /** |
| 39 | * @author Oleksiy Yarmula |
| 40 | */ |
| 41 | public class GoGridErrorHandler implements HttpErrorHandler { |
| 42 | |
| 43 | private final ParseErrorFromJsonResponse errorParser; |
| 44 | |
| 45 | @Inject |
| 46 | public GoGridErrorHandler(ParseErrorFromJsonResponse errorParser) { |
| 47 | this.errorParser = errorParser; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void handleError(HttpCommand command, HttpResponse response) { |
| 52 | try { |
| 53 | Exception exception = new HttpResponseException(command, response); |
| 54 | Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response); |
| 55 | if (errors != null) |
| 56 | exception = new GoGridResponseException(command, response, errors); |
| 57 | switch (response.getStatusCode()) { |
| 58 | case 400: |
| 59 | if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) { |
| 60 | exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception); |
| 61 | break; |
| 62 | } |
| 63 | case 403: |
| 64 | exception = new AuthorizationException(exception.getMessage(), exception); |
| 65 | break; |
| 66 | } |
| 67 | command.setException(exception); |
| 68 | } finally { |
| 69 | releasePayload(response); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Set<ErrorResponse> parseErrorsFromContentOrNull(HttpResponse response) { |
| 74 | if (response.getPayload() != null) { |
| 75 | try { |
| 76 | return errorParser.apply(response); |
| 77 | } catch (/* Parsing */Exception e) { |
| 78 | return null; |
| 79 | } |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | } |