-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathRequestImpl.java
More file actions
195 lines (172 loc) · 5.46 KB
/
RequestImpl.java
File metadata and controls
195 lines (172 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright (c) restSQL Project Contributors. Licensed under MIT. */
package org.restsql.core.impl;
import java.util.List;
import org.restsql.core.Factory;
import org.restsql.core.HttpRequestAttributes;
import org.restsql.core.InvalidRequestException;
import org.restsql.core.Request;
import org.restsql.core.RequestLogger;
import org.restsql.core.RequestValue;
/**
* Represents a restSQL request.
*
* @author Mark Sawers
*/
public class RequestImpl implements Request {
private final List<List<RequestValue>> childrenParams;
private HttpRequestAttributes httpAttributes;
private List<RequestValue> params;
private Request parent;
private final RequestLogger requestLogger;
private final List<RequestValue> resourceIdentifiers;
private Integer selectLimit, selectOffset;
private final String sqlResource;
private final Request.Type type;
/** Constructs object. */
public RequestImpl(final HttpRequestAttributes httpAttributes, final Request.Type type,
final String sqlResource, final List<RequestValue> resourceIdentifiers,
final List<RequestValue> params, final List<List<RequestValue>> childrenParams,
final RequestLogger requestLogger) {
this.type = type;
this.resourceIdentifiers = resourceIdentifiers;
this.sqlResource = sqlResource;
this.childrenParams = childrenParams;
this.requestLogger = requestLogger;
this.params = params;
if (httpAttributes != null) {
this.httpAttributes = httpAttributes;
} else {
this.httpAttributes = Factory.getHttpRequestAttributes("?", "?", "?", null, null, null);
}
if (requestLogger != null) {
requestLogger.setRequest(this);
}
}
@Override
public List<List<RequestValue>> getChildrenParameters() {
return childrenParams;
}
@Override
public HttpRequestAttributes getHttpRequestAttributes() {
return httpAttributes;
}
@Override
public RequestLogger getLogger() {
return requestLogger;
}
@Override
public List<RequestValue> getParameters() {
return params;
}
@Override
public Request getParent() {
return parent;
}
@Override
public List<RequestValue> getResourceIdentifiers() {
return resourceIdentifiers;
}
@Override
public Integer getSelectLimit() {
return selectLimit;
}
@Override
public Integer getSelectOffset() {
return selectOffset;
}
@Override
public String getSqlResource() {
return sqlResource;
}
@Override
public Request.Type getType() {
return type;
}
@Override
public boolean hasParameter(final String name) {
for (final RequestValue param : params) {
if (name.equals(param.getName())) {
return true;
}
}
return false;
}
/** Sets parameters for request. Used for cloning requests on child objects. Does not scan for output param. */
@Override
public void setParameters(final List<RequestValue> params) {
this.params = params;
}
@Override
public void setParent(final Request parent) {
this.parent = parent;
}
@Override
public void setSelectLimit(final Integer selectLimit) {
this.selectLimit = selectLimit;
}
@Override
public void setSelectOffset(final Integer selectOffset) {
this.selectOffset = selectOffset;
}
/**
* Returns string representation, using HttpRequestAttributes string if present. of resource identifiers and params
*/
@Override
public String toString() {
if (httpAttributes != null) {
return httpAttributes.toString();
} else {
return type + " " + sqlResource;
}
}
@Override
public void extractParameters() throws InvalidRequestException {
if (params != null && params.size() > 0) {
RequestValue selectLimitRequestValue = null, selectOffsetRequestValue = null;
for (final RequestValue requestValue : params) {
// Extract limit and offset
if (requestValue.getName().equalsIgnoreCase(Request.PARAM_NAME_LIMIT)) {
selectLimitRequestValue = setSelectLimitOrOffset(Request.PARAM_NAME_LIMIT, requestValue);
} else if (requestValue.getName().equalsIgnoreCase(Request.PARAM_NAME_OFFSET)) {
selectOffsetRequestValue = setSelectLimitOrOffset(Request.PARAM_NAME_OFFSET, requestValue);
}
}
// Validate both limit and offset provided
if (type == Type.SELECT) {
if (selectLimit != null && selectOffset == null) {
throw new InvalidRequestException(InvalidRequestException.MESSAGE_OFFSET_REQUIRED);
} else if (selectOffset != null && selectLimit == null) {
throw new InvalidRequestException(InvalidRequestException.MESSAGE_LIMIT_REQUIRED);
} else if (selectLimit != null && selectOffset != null) {
params.remove(selectLimitRequestValue);
params.remove(selectOffsetRequestValue);
}
}
}
}
private RequestValue setSelectLimitOrOffset(final String paramName, final RequestValue requestValue)
throws InvalidRequestException {
if (requestValue.getValue() instanceof Integer) {
if (paramName.equals(Request.PARAM_NAME_LIMIT)) {
selectLimit = (Integer) requestValue.getValue();
} else {
selectOffset = (Integer) requestValue.getValue();
}
} else if (requestValue.getValue() instanceof String) {
try {
if (paramName.equals(Request.PARAM_NAME_LIMIT)) {
selectLimit = Integer.valueOf((String) requestValue.getValue());
} else {
selectOffset = Integer.valueOf((String) requestValue.getValue());
}
} catch (final NumberFormatException exception) {
throw new InvalidRequestException(paramName + " value " + requestValue.getValue()
+ " is not a number");
}
} else {
throw new InvalidRequestException(paramName + " value " + requestValue.getValue()
+ " is not an Integer");
}
return requestValue;
}
}