Skip to content

Commit 19aca65

Browse files
committed
Merge pull request #396 from ArcBees/mmc_rest_dispatch
Added support for @consumes and absolute REST url
2 parents 1927cdb + d592302 commit 19aca65

File tree

8 files changed

+147
-49
lines changed

8 files changed

+147
-49
lines changed

gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/client/DefaultRestRequestBuilderFactory.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Map;
2121

2222
import javax.inject.Inject;
23+
import javax.ws.rs.core.HttpHeaders;
24+
import javax.ws.rs.core.MediaType;
2325

2426
import com.github.nmorel.gwtjackson.client.exception.JsonMappingException;
2527
import com.google.common.base.Strings;
@@ -41,7 +43,6 @@
4143
*/
4244
public class DefaultRestRequestBuilderFactory implements RestRequestBuilderFactory {
4345
private static final Map<HttpMethod, Method> HTTP_METHODS = Maps.newEnumMap(HttpMethod.class);
44-
private static final String CONTENT_TYPE = "Content-Type";
4546
private static final String JSON_UTF8 = "application/json; charset=utf-8";
4647

4748
static {
@@ -75,7 +76,7 @@ public <A extends RestAction<?>> RequestBuilder build(A action, String securityT
7576

7677
RequestBuilder requestBuilder = new RequestBuilder(httpMethod, url);
7778

78-
buildHeaders(requestBuilder, securityToken, action.getHeaderParams());
79+
buildHeaders(requestBuilder, securityToken, action.getPath(), action.getHeaderParams());
7980
buildBody(requestBuilder, action);
8081

8182
return requestBuilder;
@@ -151,10 +152,14 @@ protected String serialize(Object object, String bodyType) {
151152
return serialization.serialize(object, bodyType);
152153
}
153154

154-
private void buildHeaders(RequestBuilder requestBuilder, String securityToken, List<RestParameter> customHeaders)
155+
private void buildHeaders(RequestBuilder requestBuilder, String securityToken, String path,
156+
List<RestParameter> customHeaders)
155157
throws ActionException {
156-
requestBuilder.setHeader(CONTENT_TYPE, JSON_UTF8);
157-
requestBuilder.setHeader(MODULE_BASE_HEADER, baseUrl);
158+
requestBuilder.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
159+
160+
if (!isAbsoluteUrl(path)) {
161+
requestBuilder.setHeader(MODULE_BASE_HEADER, baseUrl);
162+
}
158163

159164
if (!Strings.isNullOrEmpty(securityToken)) {
160165
requestBuilder.setHeader(securityHeaderName, securityToken);
@@ -163,6 +168,10 @@ private void buildHeaders(RequestBuilder requestBuilder, String securityToken, L
163168
for (RestParameter param : customHeaders) {
164169
requestBuilder.setHeader(param.getName(), encodeHeaderParam(param));
165170
}
171+
172+
if (requestBuilder.getHeader(HttpHeaders.CONTENT_TYPE) == null) {
173+
requestBuilder.setHeader(HttpHeaders.CONTENT_TYPE, JSON_UTF8);
174+
}
166175
}
167176

168177
private <A extends RestAction<?>> void buildBody(RequestBuilder requestBuilder, A action) throws ActionException {
@@ -182,7 +191,15 @@ private String buildUrl(RestAction<?> restAction) throws ActionException {
182191

183192
String path = buildPath(restAction.getPath(), restAction.getPathParams());
184193

185-
return baseUrl + path + queryString;
194+
if (isAbsoluteUrl(path)) {
195+
return path + queryString;
196+
} else {
197+
return baseUrl + path + queryString;
198+
}
199+
}
200+
201+
private boolean isAbsoluteUrl(String path) {
202+
return path.startsWith("http://") || path.startsWith("https://");
186203
}
187204

188205
private String buildPath(String rawPath, List<RestParameter> params) throws ActionException {

gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/AbstractVelocityGenerator.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import org.apache.velocity.VelocityContext;
2424
import org.apache.velocity.app.VelocityEngine;
2525

26-
import com.google.common.base.Strings;
2726
import com.google.gwt.core.ext.UnableToCompleteException;
28-
import com.google.gwt.core.ext.typeinfo.JClassType;
2927
import com.google.gwt.core.ext.typeinfo.TypeOracle;
3028
import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil;
3129

@@ -95,19 +93,10 @@ protected String concatenatePath(String prefix, String suffix) {
9593

9694
protected String normalizePath(String path) {
9795
String normalizedPath = path;
98-
if (!path.isEmpty() && !path.startsWith("/")) {
96+
if (!path.isEmpty() && !path.startsWith("/") && !path.startsWith("http://") && !path.startsWith("https://")) {
9997
normalizedPath = "/" + path;
10098
}
10199

102100
return normalizedPath;
103101
}
104-
105-
private String createName(JClassType type, String name, String suffix) {
106-
String newName = name + suffix;
107-
String alternativeName = type.getSimpleSourceName() + suffix;
108-
109-
newName = Strings.isNullOrEmpty(newName) ? alternativeName : newName;
110-
111-
return newName;
112-
}
113102
}

gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/RestActionGenerator.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import javax.inject.Inject;
2727
import javax.inject.Provider;
28+
import javax.ws.rs.Consumes;
2829
import javax.ws.rs.DELETE;
2930
import javax.ws.rs.FormParam;
3031
import javax.ws.rs.GET;
@@ -35,6 +36,7 @@
3536
import javax.ws.rs.Path;
3637
import javax.ws.rs.PathParam;
3738
import javax.ws.rs.QueryParam;
39+
import javax.ws.rs.core.HttpHeaders;
3840

3941
import org.apache.velocity.VelocityContext;
4042
import org.apache.velocity.app.VelocityEngine;
@@ -52,7 +54,9 @@
5254
import com.gwtplatform.dispatch.rest.rebind.event.RegisterMetadataEvent;
5355
import com.gwtplatform.dispatch.rest.rebind.event.RegisterSerializableTypeEvent;
5456
import com.gwtplatform.dispatch.rest.rebind.type.ActionBinding;
57+
import com.gwtplatform.dispatch.rest.rebind.type.FromParamMethodCall;
5558
import com.gwtplatform.dispatch.rest.rebind.type.MethodCall;
59+
import com.gwtplatform.dispatch.rest.rebind.type.NoParamMethodCall;
5660
import com.gwtplatform.dispatch.rest.rebind.util.AnnotationValueResolver;
5761
import com.gwtplatform.dispatch.rest.rebind.util.FormParamValueResolver;
5862
import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil;
@@ -174,18 +178,28 @@ private List<MethodCall> getMethodCallsToAdd() {
174178
methodCalls.addAll(getMethodCallsToAdd(queryParams, ADD_QUERY_PARAM));
175179
methodCalls.addAll(getMethodCallsToAdd(formParams, ADD_FORM_PARAM));
176180

181+
addContentTypeHeaderMethodCall(methodCalls);
182+
177183
if (bodyParam != null) {
178-
methodCalls.add(new MethodCall(SET_BODY_PARAM, null, bodyParam));
184+
methodCalls.add(new FromParamMethodCall(SET_BODY_PARAM, null, bodyParam));
179185
}
180186

181187
return methodCalls;
182188
}
183189

184-
private List<MethodCall> getMethodCallsToAdd(List<AnnotatedMethodParameter> methodParameters,
190+
private void addContentTypeHeaderMethodCall(List<MethodCall> methodCalls) {
191+
Consumes consumes = actionMethod.getAnnotation(Consumes.class);
192+
193+
if (consumes != null && consumes.value().length > 0) {
194+
methodCalls.add(new NoParamMethodCall(ADD_HEADER_PARAM, HttpHeaders.CONTENT_TYPE, consumes.value()[0]));
195+
}
196+
}
197+
198+
private List<FromParamMethodCall> getMethodCallsToAdd(List<AnnotatedMethodParameter> methodParameters,
185199
String methodName) {
186-
List<MethodCall> methodCalls = new ArrayList<MethodCall>();
200+
List<FromParamMethodCall> methodCalls = new ArrayList<FromParamMethodCall>();
187201
for (AnnotatedMethodParameter methodParameter : methodParameters) {
188-
methodCalls.add(new MethodCall(methodName, methodParameter.fieldName, methodParameter.parameter));
202+
methodCalls.add(new FromParamMethodCall(methodName, methodParameter.fieldName, methodParameter.parameter));
189203
}
190204
return methodCalls;
191205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2013 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.rest.rebind.type;
18+
19+
import com.google.gwt.core.ext.typeinfo.JParameter;
20+
21+
public class FromParamMethodCall implements MethodCall {
22+
private final String methodName;
23+
private final String fieldName;
24+
private final JParameter parameter;
25+
26+
public FromParamMethodCall(String methodName, String fieldName, JParameter parameter) {
27+
this.methodName = methodName;
28+
this.fieldName = fieldName;
29+
this.parameter = parameter;
30+
}
31+
32+
@Override
33+
public boolean isFromParam() {
34+
return true;
35+
}
36+
37+
public String getMethodName() {
38+
return methodName;
39+
}
40+
41+
public JParameter getParameter() {
42+
return parameter;
43+
}
44+
45+
public String getFieldName() {
46+
return fieldName;
47+
}
48+
}

gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/MethodCall.java

+2-24
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,6 @@
1616

1717
package com.gwtplatform.dispatch.rest.rebind.type;
1818

19-
import com.google.gwt.core.ext.typeinfo.JParameter;
20-
21-
public class MethodCall {
22-
private final String methodName;
23-
private String fieldName;
24-
private final JParameter parameter;
25-
26-
public MethodCall(String methodName, String fieldName, JParameter parameter) {
27-
this.methodName = methodName;
28-
this.fieldName = fieldName;
29-
this.parameter = parameter;
30-
}
31-
32-
public String getMethodName() {
33-
return methodName;
34-
}
35-
36-
public JParameter getParameter() {
37-
return parameter;
38-
}
39-
40-
public String getFieldName() {
41-
return fieldName;
42-
}
19+
public interface MethodCall {
20+
boolean isFromParam();
4321
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2013 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.rest.rebind.type;
18+
19+
public class NoParamMethodCall implements MethodCall {
20+
private final String methodName;
21+
private final String fieldName;
22+
private final String value;
23+
24+
public NoParamMethodCall(String methodName, String fieldName, String value) {
25+
this.methodName = methodName;
26+
this.fieldName = fieldName;
27+
this.value = value;
28+
}
29+
30+
@Override
31+
public boolean isFromParam() {
32+
return false;
33+
}
34+
35+
public String getMethodName() {
36+
return methodName;
37+
}
38+
39+
public String getFieldName() {
40+
return fieldName;
41+
}
42+
43+
public String getValue() {
44+
return value;
45+
}
46+
}

gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/shared/RestParameter.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public RestParameter(String name, Object object) {
3535
this.name = name;
3636
if (object instanceof Collection) {
3737
stringValue = Joiner.on(',').join((Collection) object);
38-
} else {
38+
} else if (object != null) {
3939
this.stringValue = object.toString();
40+
} else {
41+
stringValue = "";
4042
}
4143
}
4244

gwtp-core/gwtp-dispatch-rest/src/main/resources/com/gwtplatform/dispatch/rest/rebind/RestAction.vm

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ public class ${implName} extends AbstractRestAction<${resultClass.parameterizedQ
77
public ${implName}(#foreach (${param} in ${ctorParams})${param.type.qualifiedSourceName} ${param.name}#commaIfNeeded($ctorParams)#end) {
88
super(HttpMethod.${httpMethod}, "${restPath}");
99
#foreach ($methodCall in $methodCalls)
10-
#if ($methodCall.fieldName)
10+
#if ($methodCall.fromParam)
11+
#if ($methodCall.fieldName)
1112
${methodCall.methodName}("${methodCall.fieldName}", ${methodCall.parameter.name});
12-
#else
13+
#else
1314
${methodCall.methodName}(${methodCall.parameter.name});
15+
#end
16+
#else
17+
${methodCall.methodName}("${methodCall.fieldName}", "${methodCall.value}");
1418
#end
1519
#end
1620
}

0 commit comments

Comments
 (0)