diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/client/DefaultRestRequestBuilderFactory.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/client/DefaultRestRequestBuilderFactory.java index 03c912c9b1..dec7b6e8cb 100644 --- a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/client/DefaultRestRequestBuilderFactory.java +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/client/DefaultRestRequestBuilderFactory.java @@ -20,6 +20,8 @@ import java.util.Map; import javax.inject.Inject; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import com.github.nmorel.gwtjackson.client.exception.JsonMappingException; import com.google.common.base.Strings; @@ -41,7 +43,6 @@ */ public class DefaultRestRequestBuilderFactory implements RestRequestBuilderFactory { private static final Map HTTP_METHODS = Maps.newEnumMap(HttpMethod.class); - private static final String CONTENT_TYPE = "Content-Type"; private static final String JSON_UTF8 = "application/json; charset=utf-8"; static { @@ -75,7 +76,7 @@ public > RequestBuilder build(A action, String securityT RequestBuilder requestBuilder = new RequestBuilder(httpMethod, url); - buildHeaders(requestBuilder, securityToken, action.getHeaderParams()); + buildHeaders(requestBuilder, securityToken, action.getPath(), action.getHeaderParams()); buildBody(requestBuilder, action); return requestBuilder; @@ -151,10 +152,14 @@ protected String serialize(Object object, String bodyType) { return serialization.serialize(object, bodyType); } - private void buildHeaders(RequestBuilder requestBuilder, String securityToken, List customHeaders) + private void buildHeaders(RequestBuilder requestBuilder, String securityToken, String path, + List customHeaders) throws ActionException { - requestBuilder.setHeader(CONTENT_TYPE, JSON_UTF8); - requestBuilder.setHeader(MODULE_BASE_HEADER, baseUrl); + requestBuilder.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); + + if (!isAbsoluteUrl(path)) { + requestBuilder.setHeader(MODULE_BASE_HEADER, baseUrl); + } if (!Strings.isNullOrEmpty(securityToken)) { requestBuilder.setHeader(securityHeaderName, securityToken); @@ -163,6 +168,10 @@ private void buildHeaders(RequestBuilder requestBuilder, String securityToken, L for (RestParameter param : customHeaders) { requestBuilder.setHeader(param.getName(), encodeHeaderParam(param)); } + + if (requestBuilder.getHeader(HttpHeaders.CONTENT_TYPE) == null) { + requestBuilder.setHeader(HttpHeaders.CONTENT_TYPE, JSON_UTF8); + } } private > void buildBody(RequestBuilder requestBuilder, A action) throws ActionException { @@ -182,7 +191,15 @@ private String buildUrl(RestAction restAction) throws ActionException { String path = buildPath(restAction.getPath(), restAction.getPathParams()); - return baseUrl + path + queryString; + if (isAbsoluteUrl(path)) { + return path + queryString; + } else { + return baseUrl + path + queryString; + } + } + + private boolean isAbsoluteUrl(String path) { + return path.startsWith("http://") || path.startsWith("https://"); } private String buildPath(String rawPath, List params) throws ActionException { diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/AbstractVelocityGenerator.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/AbstractVelocityGenerator.java index 10afb16883..c55b9b068e 100644 --- a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/AbstractVelocityGenerator.java +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/AbstractVelocityGenerator.java @@ -23,9 +23,7 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; -import com.google.common.base.Strings; import com.google.gwt.core.ext.UnableToCompleteException; -import com.google.gwt.core.ext.typeinfo.JClassType; import com.google.gwt.core.ext.typeinfo.TypeOracle; import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil; @@ -95,19 +93,10 @@ protected String concatenatePath(String prefix, String suffix) { protected String normalizePath(String path) { String normalizedPath = path; - if (!path.isEmpty() && !path.startsWith("/")) { + if (!path.isEmpty() && !path.startsWith("/") && !path.startsWith("http://") && !path.startsWith("https://")) { normalizedPath = "/" + path; } return normalizedPath; } - - private String createName(JClassType type, String name, String suffix) { - String newName = name + suffix; - String alternativeName = type.getSimpleSourceName() + suffix; - - newName = Strings.isNullOrEmpty(newName) ? alternativeName : newName; - - return newName; - } } diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/RestActionGenerator.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/RestActionGenerator.java index 9530c529eb..2fb6709304 100644 --- a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/RestActionGenerator.java +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/RestActionGenerator.java @@ -25,6 +25,7 @@ import javax.inject.Inject; import javax.inject.Provider; +import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.FormParam; import javax.ws.rs.GET; @@ -35,6 +36,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.HttpHeaders; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; @@ -52,7 +54,9 @@ import com.gwtplatform.dispatch.rest.rebind.event.RegisterMetadataEvent; import com.gwtplatform.dispatch.rest.rebind.event.RegisterSerializableTypeEvent; import com.gwtplatform.dispatch.rest.rebind.type.ActionBinding; +import com.gwtplatform.dispatch.rest.rebind.type.FromParamMethodCall; import com.gwtplatform.dispatch.rest.rebind.type.MethodCall; +import com.gwtplatform.dispatch.rest.rebind.type.NoParamMethodCall; import com.gwtplatform.dispatch.rest.rebind.util.AnnotationValueResolver; import com.gwtplatform.dispatch.rest.rebind.util.FormParamValueResolver; import com.gwtplatform.dispatch.rest.rebind.util.GeneratorUtil; @@ -174,18 +178,28 @@ private List getMethodCallsToAdd() { methodCalls.addAll(getMethodCallsToAdd(queryParams, ADD_QUERY_PARAM)); methodCalls.addAll(getMethodCallsToAdd(formParams, ADD_FORM_PARAM)); + addContentTypeHeaderMethodCall(methodCalls); + if (bodyParam != null) { - methodCalls.add(new MethodCall(SET_BODY_PARAM, null, bodyParam)); + methodCalls.add(new FromParamMethodCall(SET_BODY_PARAM, null, bodyParam)); } return methodCalls; } - private List getMethodCallsToAdd(List methodParameters, + private void addContentTypeHeaderMethodCall(List methodCalls) { + Consumes consumes = actionMethod.getAnnotation(Consumes.class); + + if (consumes != null && consumes.value().length > 0) { + methodCalls.add(new NoParamMethodCall(ADD_HEADER_PARAM, HttpHeaders.CONTENT_TYPE, consumes.value()[0])); + } + } + + private List getMethodCallsToAdd(List methodParameters, String methodName) { - List methodCalls = new ArrayList(); + List methodCalls = new ArrayList(); for (AnnotatedMethodParameter methodParameter : methodParameters) { - methodCalls.add(new MethodCall(methodName, methodParameter.fieldName, methodParameter.parameter)); + methodCalls.add(new FromParamMethodCall(methodName, methodParameter.fieldName, methodParameter.parameter)); } return methodCalls; } diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/FromParamMethodCall.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/FromParamMethodCall.java new file mode 100644 index 0000000000..93e3641f0c --- /dev/null +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/FromParamMethodCall.java @@ -0,0 +1,48 @@ +/** + * Copyright 2013 ArcBees Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.gwtplatform.dispatch.rest.rebind.type; + +import com.google.gwt.core.ext.typeinfo.JParameter; + +public class FromParamMethodCall implements MethodCall { + private final String methodName; + private final String fieldName; + private final JParameter parameter; + + public FromParamMethodCall(String methodName, String fieldName, JParameter parameter) { + this.methodName = methodName; + this.fieldName = fieldName; + this.parameter = parameter; + } + + @Override + public boolean isFromParam() { + return true; + } + + public String getMethodName() { + return methodName; + } + + public JParameter getParameter() { + return parameter; + } + + public String getFieldName() { + return fieldName; + } +} diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/MethodCall.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/MethodCall.java index 061b85c82c..dbf89b6ae5 100644 --- a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/MethodCall.java +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/MethodCall.java @@ -16,28 +16,6 @@ package com.gwtplatform.dispatch.rest.rebind.type; -import com.google.gwt.core.ext.typeinfo.JParameter; - -public class MethodCall { - private final String methodName; - private String fieldName; - private final JParameter parameter; - - public MethodCall(String methodName, String fieldName, JParameter parameter) { - this.methodName = methodName; - this.fieldName = fieldName; - this.parameter = parameter; - } - - public String getMethodName() { - return methodName; - } - - public JParameter getParameter() { - return parameter; - } - - public String getFieldName() { - return fieldName; - } +public interface MethodCall { + boolean isFromParam(); } diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/NoParamMethodCall.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/NoParamMethodCall.java new file mode 100644 index 0000000000..5497c9d645 --- /dev/null +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/rebind/type/NoParamMethodCall.java @@ -0,0 +1,46 @@ +/** + * Copyright 2013 ArcBees Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.gwtplatform.dispatch.rest.rebind.type; + +public class NoParamMethodCall implements MethodCall { + private final String methodName; + private final String fieldName; + private final String value; + + public NoParamMethodCall(String methodName, String fieldName, String value) { + this.methodName = methodName; + this.fieldName = fieldName; + this.value = value; + } + + @Override + public boolean isFromParam() { + return false; + } + + public String getMethodName() { + return methodName; + } + + public String getFieldName() { + return fieldName; + } + + public String getValue() { + return value; + } +} diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/shared/RestParameter.java b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/shared/RestParameter.java index 5a7fd297c5..452c424685 100644 --- a/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/shared/RestParameter.java +++ b/gwtp-core/gwtp-dispatch-rest/src/main/java/com/gwtplatform/dispatch/rest/shared/RestParameter.java @@ -35,8 +35,10 @@ public RestParameter(String name, Object object) { this.name = name; if (object instanceof Collection) { stringValue = Joiner.on(',').join((Collection) object); - } else { + } else if (object != null) { this.stringValue = object.toString(); + } else { + stringValue = ""; } } diff --git a/gwtp-core/gwtp-dispatch-rest/src/main/resources/com/gwtplatform/dispatch/rest/rebind/RestAction.vm b/gwtp-core/gwtp-dispatch-rest/src/main/resources/com/gwtplatform/dispatch/rest/rebind/RestAction.vm index 116b3838be..1c6e900b1f 100644 --- a/gwtp-core/gwtp-dispatch-rest/src/main/resources/com/gwtplatform/dispatch/rest/rebind/RestAction.vm +++ b/gwtp-core/gwtp-dispatch-rest/src/main/resources/com/gwtplatform/dispatch/rest/rebind/RestAction.vm @@ -7,10 +7,14 @@ public class ${implName} extends AbstractRestAction<${resultClass.parameterizedQ public ${implName}(#foreach (${param} in ${ctorParams})${param.type.qualifiedSourceName} ${param.name}#commaIfNeeded($ctorParams)#end) { super(HttpMethod.${httpMethod}, "${restPath}"); #foreach ($methodCall in $methodCalls) -#if ($methodCall.fieldName) +#if ($methodCall.fromParam) + #if ($methodCall.fieldName) ${methodCall.methodName}("${methodCall.fieldName}", ${methodCall.parameter.name}); -#else + #else ${methodCall.methodName}(${methodCall.parameter.name}); + #end +#else + ${methodCall.methodName}("${methodCall.fieldName}", "${methodCall.value}"); #end #end }