andrewsbrown | 43b9630 | 2015-03-17 21:51:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * jndn-management |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
| 16 | import com.intel.jndn.management.types.ControlResponse; |
| 17 | import net.named_data.jndn.encoding.EncodingException; |
| 18 | import net.named_data.jndn.util.Blob; |
| 19 | |
| 20 | /** |
| 21 | * Represent a failure to correctly manage the NDN Forwarding Daemon (NFD). |
| 22 | * Inspect this object with getCause() to see why the management operation |
| 23 | * failed. |
| 24 | * |
| 25 | * @author Andrew Brown <andrew.brown@intel.com> |
| 26 | */ |
| 27 | public class ManagementException extends Exception { |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @param message |
| 32 | */ |
| 33 | public ManagementException(String message) { |
| 34 | super(message); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | * @param message |
| 40 | * @param cause |
| 41 | */ |
| 42 | public ManagementException(String message, Throwable cause) { |
| 43 | super(message, cause); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Parse an NFD response to create a ManagementException. |
| 48 | * |
| 49 | * @param forwarderResponse |
| 50 | * @return |
| 51 | */ |
| 52 | public static ManagementException fromResponse(Blob forwarderResponse) { |
| 53 | ControlResponse response = new ControlResponse(); |
| 54 | try { |
| 55 | response.wireDecode(forwarderResponse.buf()); |
| 56 | String message = "Action failed, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText(); |
| 57 | return new ManagementException(message); |
| 58 | } catch (EncodingException e) { |
| 59 | return new ManagementException("Action failed and forwarder response was unparseable.", e); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Parse an NFD response to create a ManagementException. |
| 65 | * |
| 66 | * @param forwarderResponse |
| 67 | * @return |
| 68 | */ |
| 69 | public static ManagementException fromResponse(ControlResponse response) { |
| 70 | String message = "Action failed, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText(); |
| 71 | return new ManagementException(message); |
| 72 | } |
| 73 | } |