Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for @Consumes and absolute REST url #396

Merged
merged 2 commits into from
Jan 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +43,6 @@
*/
public class DefaultRestRequestBuilderFactory implements RestRequestBuilderFactory {
private static final Map<HttpMethod, Method> 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 {
Expand Down Expand Up @@ -75,7 +76,7 @@ public <A extends RestAction<?>> 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;
Expand Down Expand Up @@ -151,10 +152,14 @@ protected String serialize(Object object, String bodyType) {
return serialization.serialize(object, bodyType);
}

private void buildHeaders(RequestBuilder requestBuilder, String securityToken, List<RestParameter> customHeaders)
private void buildHeaders(RequestBuilder requestBuilder, String securityToken, String path,
List<RestParameter> 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);
Expand All @@ -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 <A extends RestAction<?>> void buildBody(RequestBuilder requestBuilder, A action) throws ActionException {
Expand All @@ -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<RestParameter> params) throws ActionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -174,18 +178,28 @@ private List<MethodCall> 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<MethodCall> getMethodCallsToAdd(List<AnnotatedMethodParameter> methodParameters,
private void addContentTypeHeaderMethodCall(List<MethodCall> 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<FromParamMethodCall> getMethodCallsToAdd(List<AnnotatedMethodParameter> methodParameters,
String methodName) {
List<MethodCall> methodCalls = new ArrayList<MethodCall>();
List<FromParamMethodCall> methodCalls = new ArrayList<FromParamMethodCall>();
for (AnnotatedMethodParameter methodParameter : methodParameters) {
methodCalls.add(new MethodCall(methodName, methodParameter.fieldName, methodParameter.parameter));
methodCalls.add(new FromParamMethodCall(methodName, methodParameter.fieldName, methodParameter.parameter));
}
return methodCalls;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down