|
19 | 19 |
|
20 | 20 | package org.elasticsearch.rest;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.ElasticSearchIllegalArgumentException; |
| 23 | +import org.elasticsearch.common.Booleans; |
| 24 | +import org.elasticsearch.common.Nullable; |
| 25 | +import org.elasticsearch.common.Strings; |
22 | 26 | import org.elasticsearch.common.bytes.BytesReference;
|
23 | 27 | import org.elasticsearch.common.unit.ByteSizeValue;
|
24 | 28 | import org.elasticsearch.common.unit.TimeValue;
|
25 | 29 | import org.elasticsearch.common.xcontent.ToXContent;
|
| 30 | +import org.elasticsearch.rest.support.RestUtils; |
26 | 31 |
|
| 32 | +import java.net.SocketAddress; |
27 | 33 | import java.util.Map;
|
28 | 34 |
|
| 35 | +import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue; |
| 36 | +import static org.elasticsearch.common.unit.TimeValue.parseTimeValue; |
| 37 | + |
29 | 38 | /**
|
30 | 39 | *
|
31 | 40 | */
|
32 |
| -public interface RestRequest extends ToXContent.Params { |
| 41 | +public abstract class RestRequest implements ToXContent.Params { |
33 | 42 |
|
34 |
| - enum Method { |
| 43 | + public enum Method { |
35 | 44 | GET, POST, PUT, DELETE, OPTIONS, HEAD
|
36 | 45 | }
|
37 | 46 |
|
38 |
| - Method method(); |
| 47 | + public abstract Method method(); |
39 | 48 |
|
40 | 49 | /**
|
41 | 50 | * The uri of the rest request, with the query string.
|
42 | 51 | */
|
43 |
| - String uri(); |
| 52 | + public abstract String uri(); |
44 | 53 |
|
45 | 54 | /**
|
46 | 55 | * The non decoded, raw path provided.
|
47 | 56 | */
|
48 |
| - String rawPath(); |
| 57 | + public abstract String rawPath(); |
49 | 58 |
|
50 | 59 | /**
|
51 | 60 | * The path part of the URI (without the query string), decoded.
|
52 | 61 | */
|
53 |
| - String path(); |
| 62 | + public final String path() { |
| 63 | + return RestUtils.decodeComponent(rawPath()); |
| 64 | + } |
54 | 65 |
|
55 |
| - boolean hasContent(); |
| 66 | + public abstract boolean hasContent(); |
56 | 67 |
|
57 | 68 | /**
|
58 | 69 | * Is the byte array content safe or unsafe for usage on other threads
|
59 | 70 | */
|
60 |
| - boolean contentUnsafe(); |
| 71 | + public abstract boolean contentUnsafe(); |
61 | 72 |
|
62 |
| - BytesReference content(); |
| 73 | + public abstract BytesReference content(); |
63 | 74 |
|
64 |
| - String header(String name); |
| 75 | + public abstract String header(String name); |
65 | 76 |
|
66 |
| - boolean hasParam(String key); |
| 77 | + public abstract Iterable<Map.Entry<String, String>> headers(); |
67 | 78 |
|
68 |
| - String param(String key); |
| 79 | + @Nullable |
| 80 | + public SocketAddress getRemoteAddress() { |
| 81 | + return null; |
| 82 | + } |
69 | 83 |
|
70 |
| - String[] paramAsStringArray(String key, String[] defaultValue); |
| 84 | + @Nullable |
| 85 | + public SocketAddress getLocalAddress() { |
| 86 | + return null; |
| 87 | + } |
71 | 88 |
|
72 |
| - float paramAsFloat(String key, float defaultValue); |
| 89 | + public abstract boolean hasParam(String key); |
73 | 90 |
|
74 |
| - int paramAsInt(String key, int defaultValue); |
| 91 | + @Override |
| 92 | + public abstract String param(String key); |
| 93 | + |
| 94 | + public abstract Map<String, String> params(); |
| 95 | + |
| 96 | + public float paramAsFloat(String key, float defaultValue) { |
| 97 | + String sValue = param(key); |
| 98 | + if (sValue == null) { |
| 99 | + return defaultValue; |
| 100 | + } |
| 101 | + try { |
| 102 | + return Float.parseFloat(sValue); |
| 103 | + } catch (NumberFormatException e) { |
| 104 | + throw new ElasticSearchIllegalArgumentException("Failed to parse float parameter [" + key + "] with value [" + sValue + "]", e); |
| 105 | + } |
| 106 | + } |
75 | 107 |
|
76 |
| - long paramAsLong(String key, long defaultValue); |
| 108 | + public int paramAsInt(String key, int defaultValue) { |
| 109 | + String sValue = param(key); |
| 110 | + if (sValue == null) { |
| 111 | + return defaultValue; |
| 112 | + } |
| 113 | + try { |
| 114 | + return Integer.parseInt(sValue); |
| 115 | + } catch (NumberFormatException e) { |
| 116 | + throw new ElasticSearchIllegalArgumentException("Failed to parse int parameter [" + key + "] with value [" + sValue + "]", e); |
| 117 | + } |
| 118 | + } |
77 | 119 |
|
78 |
| - boolean paramAsBoolean(String key, boolean defaultValue); |
| 120 | + public long paramAsLong(String key, long defaultValue) { |
| 121 | + String sValue = param(key); |
| 122 | + if (sValue == null) { |
| 123 | + return defaultValue; |
| 124 | + } |
| 125 | + try { |
| 126 | + return Long.parseLong(sValue); |
| 127 | + } catch (NumberFormatException e) { |
| 128 | + throw new ElasticSearchIllegalArgumentException("Failed to parse int parameter [" + key + "] with value [" + sValue + "]", e); |
| 129 | + } |
| 130 | + } |
79 | 131 |
|
80 |
| - Boolean paramAsBooleanOptional(String key, Boolean defaultValue); |
| 132 | + @Override |
| 133 | + public boolean paramAsBoolean(String key, boolean defaultValue) { |
| 134 | + return Booleans.parseBoolean(param(key), defaultValue); |
| 135 | + } |
81 | 136 |
|
82 |
| - TimeValue paramAsTime(String key, TimeValue defaultValue); |
| 137 | + @Override |
| 138 | + public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) { |
| 139 | + String sValue = param(key); |
| 140 | + if (sValue == null) { |
| 141 | + return defaultValue; |
| 142 | + } |
| 143 | + return !(sValue.equals("false") || sValue.equals("0") || sValue.equals("off")); |
| 144 | + } |
83 | 145 |
|
84 |
| - ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue); |
| 146 | + public TimeValue paramAsTime(String key, TimeValue defaultValue) { |
| 147 | + return parseTimeValue(param(key), defaultValue); |
| 148 | + } |
85 | 149 |
|
86 |
| - Map<String, String> params(); |
| 150 | + public ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue) { |
| 151 | + return parseBytesSizeValue(param(key), defaultValue); |
| 152 | + } |
87 | 153 |
|
88 |
| - Iterable<Map.Entry<String, String>> headers(); |
| 154 | + public String[] paramAsStringArray(String key, String[] defaultValue) { |
| 155 | + String value = param(key); |
| 156 | + if (value == null) { |
| 157 | + return defaultValue; |
| 158 | + } |
| 159 | + return Strings.splitStringByCommaToArray(value); |
| 160 | + } |
89 | 161 | }
|
0 commit comments