| 1 |
package org.astrogrid.samp; |
| 2 |
|
| 3 |
import java.util.Map; |
| 4 |
|
| 5 |
/** |
| 6 |
* Represents an encoded SAMP response. |
| 7 |
* |
| 8 |
* @author Mark Taylor |
| 9 |
* @since 14 Jul 2008 |
| 10 |
*/ |
| 11 |
public class Response extends SampMap { |
| 12 |
|
| 13 |
/** Key for response status. May take one of the values |
| 14 |
* {@link #OK_STATUS}, {@link #WARNING_STATUS} or {@link #ERROR_STATUS}. |
| 15 |
*/ |
| 16 |
public static final String STATUS_KEY = "samp.status"; |
| 17 |
|
| 18 |
/** Key for result map. This is a map of key-value pairs with semantics |
| 19 |
* defined by the original message's MType. |
| 20 |
* Only present in case of success (or warning). */ |
| 21 |
public static final String RESULT_KEY = "samp.result"; |
| 22 |
|
| 23 |
/** Key for error map. Only present in case of failure (or warning). */ |
| 24 |
public static final String ERROR_KEY = "samp.error"; |
| 25 |
|
| 26 |
private static final String[] KNOWN_KEYS = new String[] { |
| 27 |
STATUS_KEY, |
| 28 |
RESULT_KEY, |
| 29 |
ERROR_KEY, |
| 30 |
}; |
| 31 |
|
| 32 |
/** {@link #STATUS_KEY} value indicating success. */ |
| 33 |
public static final String OK_STATUS = "samp.ok"; |
| 34 |
|
| 35 |
/** {@link #STATUS_KEY} value indicating partial success. */ |
| 36 |
public static final String WARNING_STATUS = "samp.warning"; |
| 37 |
|
| 38 |
/** {@link #STATUS_KEY} value indicating failure. */ |
| 39 |
public static final String ERROR_STATUS = "samp.error"; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructs an empty response. |
| 43 |
*/ |
| 44 |
public Response() { |
| 45 |
super( KNOWN_KEYS ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructs a response based on an existing map. |
| 50 |
* |
| 51 |
* @param map map containing initial data for this object |
| 52 |
*/ |
| 53 |
public Response( Map map ) { |
| 54 |
this(); |
| 55 |
putAll( map ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Constructs a response with given status, result and error. |
| 60 |
* |
| 61 |
* @param status {@link #STATUS_KEY} value |
| 62 |
* @param result {@link #RESULT_KEY} value |
| 63 |
* @param errinfo {@link #ERROR_KEY} value |
| 64 |
*/ |
| 65 |
public Response( String status, Map result, ErrInfo errinfo ) { |
| 66 |
this(); |
| 67 |
put( STATUS_KEY, status ); |
| 68 |
if ( result != null ) { |
| 69 |
put( RESULT_KEY, result ); |
| 70 |
} |
| 71 |
if ( errinfo != null ) { |
| 72 |
put( ERROR_KEY, errinfo ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Sets the status value. |
| 78 |
* |
| 79 |
* @param status {@link #STATUS_KEY} value |
| 80 |
*/ |
| 81 |
public void setStatus( String status ) { |
| 82 |
put( STATUS_KEY, status ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the status value. |
| 87 |
* |
| 88 |
* @return {@link #STATUS_KEY} value |
| 89 |
*/ |
| 90 |
public String getStatus() { |
| 91 |
return getString( STATUS_KEY ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Sets the result map. |
| 96 |
* |
| 97 |
* @param result {@link #RESULT_KEY} value |
| 98 |
*/ |
| 99 |
public void setResult( Map result ) { |
| 100 |
put( RESULT_KEY, result ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns the result map. |
| 105 |
* |
| 106 |
* @return {@link #RESULT_KEY} value |
| 107 |
*/ |
| 108 |
public Map getResult() { |
| 109 |
return getMap( RESULT_KEY ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Sets the error object. |
| 114 |
* |
| 115 |
* @param errInfo {@link #ERROR_KEY} value |
| 116 |
* @see ErrInfo |
| 117 |
*/ |
| 118 |
public void setErrInfo( Map errInfo ) { |
| 119 |
put( ERROR_KEY, errInfo ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the error object. |
| 124 |
* |
| 125 |
* @return {@link #ERROR_KEY} value as an <code>ErrInfo</code> |
| 126 |
*/ |
| 127 |
public ErrInfo getErrInfo() { |
| 128 |
return ErrInfo.asErrInfo( getMap( ERROR_KEY ) ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Indicates whether the result was an unequivocal success. |
| 133 |
* |
| 134 |
* @return true iff <code>getStatus()==OK_STATUS</code> |
| 135 |
*/ |
| 136 |
public boolean isOK() { |
| 137 |
return OK_STATUS.equals( get( STATUS_KEY ) ); |
| 138 |
} |
| 139 |
|
| 140 |
public void check() { |
| 141 |
super.check(); |
| 142 |
checkHasKeys( new String[] { STATUS_KEY, } ); |
| 143 |
String status = getStatus(); |
| 144 |
if ( OK_STATUS.equals( status ) || WARNING_STATUS.equals( status ) ) { |
| 145 |
if ( ! containsKey( RESULT_KEY ) ) { |
| 146 |
throw new DataException( STATUS_KEY + "=" + status |
| 147 |
+ " but no " + RESULT_KEY ); |
| 148 |
} |
| 149 |
} |
| 150 |
if ( ERROR_STATUS.equals( status ) || |
| 151 |
WARNING_STATUS.equals( status ) ) { |
| 152 |
if ( ! containsKey( ERROR_KEY ) ) { |
| 153 |
throw new DataException( STATUS_KEY + "=" + status |
| 154 |
+ " but not " + ERROR_KEY ); |
| 155 |
} |
| 156 |
} |
| 157 |
if ( ! containsKey( RESULT_KEY ) && ! containsKey( ERROR_KEY ) ) { |
| 158 |
throw new DataException( "Neither " + RESULT_KEY + |
| 159 |
" nor " + ERROR_KEY + |
| 160 |
" keys present" ); |
| 161 |
} |
| 162 |
if ( containsKey( ERROR_KEY ) ) { |
| 163 |
ErrInfo.asErrInfo( getMap( ERROR_KEY ) ).check(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns a new response which is a success. |
| 169 |
* |
| 170 |
* @param result key-value map representing results of successful call |
| 171 |
* @return new success response |
| 172 |
*/ |
| 173 |
public static Response createSuccessResponse( Map result ) { |
| 174 |
return new Response( OK_STATUS, result, null ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns a new response which is an error. |
| 179 |
* |
| 180 |
* @param errinfo error information |
| 181 |
* @return new error response |
| 182 |
*/ |
| 183 |
public static Response createErrorResponse( ErrInfo errinfo ) { |
| 184 |
return new Response( ERROR_STATUS, null, errinfo ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns a map as a Response object. |
| 189 |
* |
| 190 |
* @param map map |
| 191 |
* @return response |
| 192 |
*/ |
| 193 |
public static Response asResponse( Map map ) { |
| 194 |
return ( map instanceof Response || map == null ) |
| 195 |
? (Response) map |
| 196 |
: new Response( map ); |
| 197 |
} |
| 198 |
} |