Skip to content

Commit

Permalink
Add media type handling on per method level.
Browse files Browse the repository at this point in the history
  • Loading branch information
pilhuhn committed Feb 20, 2015
1 parent a4edb93 commit 27eeac3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ private void processMethod(PMethod method, String outerPath) throws IOException
lf();
}

handleMediaTypes(method.produces, "Produces:");
handleMediaTypes(method.consumes, "Consumes:");

if (method.returnType != null) {
write("Return type: ");
if (method.returnType.typeId.startsWith("...")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
Expand Down Expand Up @@ -237,22 +238,30 @@ private void processClass(PApi pApi, TypeElement classElementIn) {
pClass.basePath = api.basePath();
}

Produces produces = classElementIn.getAnnotation(Produces.class);
if (produces != null) {
String[] types = produces.value();
Collections.addAll(pClass.produces, types);
}
Consumes consumes = classElementIn.getAnnotation(Consumes.class);
if (consumes != null) {
String[] types = consumes.value();
Collections.addAll(pClass.consumes, types);
}
processProduces(classElementIn, pClass.produces);
processConsumes(classElementIn, pClass.consumes);

pApi.classes.add(pClass);

// Loop over the methods on this class
for (ExecutableElement executableElement : ElementFilter.methodsIn(classElementIn.getEnclosedElements())) {
processMethods(pClass, executableElement);
processMethod(pClass, executableElement);
}
}

private void processProduces(Element elementIn, List<String> out) {
Produces produces = elementIn.getAnnotation(Produces.class);
if (produces != null) {
String[] types = produces.value();
Collections.addAll(out, types);
}
}

private void processConsumes(Element elementIn, List<String> out) {
Consumes produces = elementIn.getAnnotation(Consumes.class);
if (produces != null) {
String[] types = produces.value();
Collections.addAll(out, types);
}
}

Expand All @@ -263,7 +272,7 @@ private void processClass(PApi pApi, TypeElement classElementIn) {
* @param pClass The class to add the method to
* @param td One Type element for the method
*/
private void processMethods(PClass pClass, ExecutableElement td) {
private void processMethod(PClass pClass, ExecutableElement td) {

log.fine(" Looking at method " + td.getSimpleName().toString());

Expand Down Expand Up @@ -308,9 +317,10 @@ private void processMethods(PClass pClass, ExecutableElement td) {

// Loop over the parameters
processParams(pMethod, td);

processErrors(pMethod, td);

processProduces(td, pMethod.produces);
processConsumes(td, pMethod.consumes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ private void handleMethod(Document doc, Element methodElement, PMethod method) {
addOptionalAttribute(methodElement, "returnType", method.returnType.typeString);
handleParams(doc, methodElement, method.params);
handleErrors(doc, methodElement, method.errors);
handleMediaTypes(doc, methodElement, method.consumes,"consumes");
handleMediaTypes(doc, methodElement, method.produces,"produces");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class PMethod {
public String notes;
public List<ErrorCode> errors = new ArrayList<>();
public List<PParam> params = new ArrayList<>();
public List<String> produces = new ArrayList<>();
public List<String> consumes = new ArrayList<>();
public String path;
public boolean gzip = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.wordnik.swagger.annotations.ApiError;
import com.wordnik.swagger.annotations.ApiErrors;
import com.wordnik.swagger.annotations.ApiOperation;
import org.jboss.resteasy.annotations.GZIP;

import javax.ejb.Local;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -59,12 +60,15 @@ public interface SomeEjbInterface {
@Path("/hello")
public String helloWorld();

@ApiOperation(value = "Returns hello world", notes = "<xml><simpara>This is XML</simpara></xml><xml>Bla</xml>")
@GET
@ApiOperation(value = "Returns hello bogus world", notes = "<xml><simpara>This is " +
"XML</simpara></xml><xml>Bla</xml>")
@GET @GZIP
@Path("/hello2")
public String helloBogusWorld();

@ApiOperation(value = "Returns hello world", notes = "<simpara>This is XML</simpara>")
@Consumes("text/plain")
@Produces("application/vnd+hawkluar-hello-world;v2")
@ApiOperation(value = "Returns hello bogus world2", notes = "<simpara>This is XML</simpara>")
@GET
@Path("/hello3")
public String helloBogusWorld2();
Expand Down

0 comments on commit 27eeac3

Please sign in to comment.