Skip to content

Commit

Permalink
Add autodetection of text for @produces annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Armageddon- committed Aug 14, 2015
1 parent 296abeb commit 15da62f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2009-2012 the original author or authors.
* Copyright (C) 2009-2015 the original author or authors.
* See the notice.md file distributed with this work for additional
* information regarding copyright ownership.
*
Expand Down Expand Up @@ -219,8 +219,12 @@ private Logger getLogger() {
}
return this.logger;
}

public Object send(final TextCallback callback) {
return send((MethodCallback<String>) callback);
}

public Object send(final MethodCallback<String> callback) {
defaultAcceptType(Resource.CONTENT_TYPE_TEXT);
try {
return send(new AbstractRequestCallback<String>(this, callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@
import com.google.gwt.core.client.JsArrayInteger;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.core.ext.BadPropertyValueException;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.PropertyOracle;
import com.google.gwt.core.ext.SelectionProperty;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.HasAnnotations;
Expand All @@ -97,6 +100,7 @@
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.xml.client.Document;

/**
*
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
Expand All @@ -109,6 +113,8 @@ public class RestServiceClassCreator extends BaseSourceCreator {

private static final String REST_SERVICE_PROXY_SUFFIX = "_Generated_RestServiceProxy_";

private static final String PLAIN_TEXT_AUTODETECTION_CONFIGURATION_PROPERTY_NAME = "restygwt.autodetect.plaintText";

private static final String METHOD_CLASS = Method.class.getName();
private static final String RESOURCE_CLASS = Resource.class.getName();
private static final String DISPATCHER_CLASS = Dispatcher.class.getName();
Expand Down Expand Up @@ -166,6 +172,8 @@ public class RestServiceClassCreator extends BaseSourceCreator {
private JClassType REST_SERVICE_TYPE;
private JsonEncoderDecoderInstanceLocator locator;

private boolean autodetectTypeForStrings;

public RestServiceClassCreator(TreeLogger logger, GeneratorContext context, JClassType source) {
super(logger, context, source, REST_SERVICE_PROXY_SUFFIX);
}
Expand Down Expand Up @@ -206,6 +214,8 @@ protected void generate() throws UnableToCompleteException {
throw new UnableToCompleteException();
}

autodetectTypeForStrings = shouldAutodetectPlainTextForStrings(getLogger(), context.getPropertyOracle());

locator = new JsonEncoderDecoderInstanceLocator(context, getLogger());

this.XML_CALLBACK_TYPE = find(XmlCallback.class, getLogger(), context);
Expand Down Expand Up @@ -294,6 +304,20 @@ protected void generate() throws UnableToCompleteException {
}
}

/**
* Returns {@code true} if plain text autodetection for strings should be used.
*/
static boolean shouldAutodetectPlainTextForStrings(TreeLogger logger, PropertyOracle propertyOracle) {
try {
SelectionProperty prop = propertyOracle.getSelectionProperty(logger, PLAIN_TEXT_AUTODETECTION_CONFIGURATION_PROPERTY_NAME);
String propVal = prop.getCurrentValue();
return Boolean.parseBoolean(propVal);
} catch (BadPropertyValueException e) {
// use default "false"
}
return false;
}

private static String getPathFromSource(HasAnnotations annotatedType) {
String path = null;

Expand Down Expand Up @@ -572,22 +596,28 @@ private void writeMethodImpl(JMethod method, Options classOptions) throws Unable

writeOptions(options, classOptions);

if(jsonpAnnotation == null) {
if (jsonpAnnotation == null) {
final String acceptHeader;
Produces producesAnnotation = findAnnotationOnMethodOrEnclosingType(method, Produces.class);
if (producesAnnotation != null) {
p("__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, "+wrap(producesAnnotation.value()[0])+");");
// Do not use autodetection, if accept type already set
if (acceptTypeBuiltIn == null && autodetectTypeForStrings && producesAnnotation.value()[0].startsWith("text/")) {
acceptTypeBuiltIn = "CONTENT_TYPE_TEXT";
}
acceptHeader = wrap(producesAnnotation.value()[0]);
} else {
// set the default accept header....
// set the default accept header value ...
if (acceptTypeBuiltIn != null) {
p("__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + RESOURCE_CLASS + "." + acceptTypeBuiltIn + ");");
acceptHeader = RESOURCE_CLASS + "." + acceptTypeBuiltIn;
} else {
p("__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + RESOURCE_CLASS + ".CONTENT_TYPE_JSON);");
acceptHeader = RESOURCE_CLASS + ".CONTENT_TYPE_JSON";
}
}
p("__method.header(" + RESOURCE_CLASS + ".HEADER_ACCEPT, " + acceptHeader + ");");

Consumes consumesAnnotation = findAnnotationOnMethodOrEnclosingType(method, Consumes.class);
if (consumesAnnotation != null) {
p("__method.header(" + RESOURCE_CLASS + ".HEADER_CONTENT_TYPE, "+wrap(consumesAnnotation.value()[0])+");");
p("__method.header(" + RESOURCE_CLASS + ".HEADER_CONTENT_TYPE, " + wrap(consumesAnnotation.value()[0]) + ");");
}

// and set the explicit headers now (could override the accept header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<module>
<inherits name='org.fusesource.restygwt.StringEncoderDecoderTestGwt' />

<!-- <set-property name="restygwt.autodetect.plaintText" value="true" /> -->
<set-property name="restygwt.autodetect.plaintText" value="true" />

</module>

0 comments on commit 15da62f

Please sign in to comment.