-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Fixes for JAXRS-CXF Codegen #2256
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
modules/swagger-codegen/src/main/resources/JavaJaxRS/cxf/CXF2InterfaceComparator.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package {{package}}; | ||
|
||
|
||
import java.lang.reflect.Method; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.ws.rs.HttpMethod; | ||
import javax.ws.rs.Path; | ||
|
||
import org.apache.cxf.jaxrs.ext.ResourceComparator; | ||
import org.apache.cxf.jaxrs.model.ClassResourceInfo; | ||
import org.apache.cxf.jaxrs.model.OperationResourceInfo; | ||
import org.apache.cxf.message.Message; | ||
|
||
/** | ||
* This class only help CXF to decide which resource interface is more suitable. It used Java reflexion to iterate over Java methods but it *DO NOT* select the target method. | ||
*/ | ||
public class CXFInterfaceComparator implements ResourceComparator { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CXFInterfaceComparator.class); | ||
|
||
@Override | ||
public int compare(ClassResourceInfo cri1, ClassResourceInfo cri2, Message message) { | ||
String requestVerb = (String) message.get(Message.HTTP_REQUEST_METHOD); | ||
String requestURI = (String) message.get(Message.REQUEST_URI); | ||
String requestPath = requestURI.replace((String) message.get(Message.BASE_PATH), ""); | ||
|
||
// remove "/"at the end of requestPath if present | ||
if (requestPath.endsWith("/")){ | ||
requestPath = requestPath.substring(0, requestPath.length()-1); | ||
} | ||
|
||
if (analyseInterface(cri1, requestPath, requestVerb)) { | ||
return -1; // Indicate that 'cri1' interface should be preferred | ||
} else if (analyseInterface(cri2, requestPath, requestVerb)) { | ||
return 1; // Indicate that 'cri2' interface should be preferred | ||
} else { | ||
return 0; // Nothing match, leave CXF decision | ||
} | ||
} | ||
|
||
/** | ||
* Analyse each methods provided to check if there is a match with the | ||
* message request path and request HTTP verb. | ||
* | ||
* @param cri | ||
* the interface to be analysed | ||
* @param requestPath | ||
* the path of the request. Do not contains the host and base | ||
* path | ||
* @return true if a method match the request, false otherwise | ||
*/ | ||
private static boolean analyseInterface(ClassResourceInfo cri, String requestPath, String requestVerb) { | ||
assert cri.getServiceClass() != null; | ||
assert cri.getServiceClass().getInterfaces() != null; | ||
assert cri.getServiceClass().getInterfaces()[0] != null; | ||
assert cri.getServiceClass().getInterfaces()[0].getMethods().length > 0; | ||
|
||
Method[] methods = cri.getServiceClass().getInterfaces()[0].getMethods(); | ||
// Java reflexion. Check all the methods of an interface. | ||
for (Method method : methods) { | ||
Path pathAnnotation = method.getAnnotation(javax.ws.rs.Path.class); | ||
if (pathAnnotation != null && pathAnnotation.value() != null) { | ||
String pathValue = pathAnnotation.value(); | ||
String methodHttpVerb = getMethodHttpVerb(method); | ||
|
||
// Always authorize OPTIONS request if the path is matching a method declaration | ||
if (requestVerb.equals(HttpMethod.OPTIONS) && match(pathValue,requestPath)) { | ||
return true; | ||
} | ||
// Also check the HTTP verb since a single path can be match do multiple request, depending of the HTTP request verb. | ||
if (requestVerb.equals(methodHttpVerb) && match(pathValue, requestPath)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static String getMethodHttpVerb(Method method) { | ||
if (method.getAnnotation(javax.ws.rs.POST.class) != null) { | ||
return HttpMethod.POST; | ||
} else if (method.getAnnotation(javax.ws.rs.GET.class) != null) { | ||
return HttpMethod.GET; | ||
} else if (method.getAnnotation(javax.ws.rs.PUT.class) != null) { | ||
return HttpMethod.PUT; | ||
} else if (method.getAnnotation(javax.ws.rs.OPTIONS.class) != null) { | ||
return HttpMethod.OPTIONS; | ||
} else if (method.getAnnotation(javax.ws.rs.DELETE.class) != null) { | ||
return HttpMethod.DELETE; | ||
} else if (method.getAnnotation(javax.ws.rs.HEAD.class) != null) { | ||
return HttpMethod.HEAD; | ||
} | ||
assert false; | ||
return null; | ||
} | ||
|
||
/** | ||
* Check whether if the pathValue match with the requestPath parameter. | ||
* Every path params are considered to be declared as '{param}'. The tokens to start and close path params declaration are '{' and '}'. | ||
* | ||
* @param valueFromAnnotation | ||
* @param valueFromRequest | ||
* @return true if there is a match, false otherwise | ||
*/ | ||
private static boolean match(String valueFromAnnotation, String valueFromRequest) { | ||
String patternFinal = valueFromAnnotation.replaceAll("\\{(.*?)\\}", "([^/]*)").replace("/", "\\/"); | ||
Matcher matcher = Pattern.compile(patternFinal).matcher(valueFromRequest); | ||
if (matcher.matches()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int compare(OperationResourceInfo ori1, OperationResourceInfo ori2, Message message) { | ||
return 0; // Leave CXF decision | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
modules/swagger-codegen/src/main/resources/JavaJaxRS/cxf/entityModel.mustache
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
modules/swagger-codegen/src/main/resources/JavaJaxRS/cxf/enumClass.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import javax.xml.bind.annotation.XmlEnum; | ||
import javax.xml.bind.annotation.XmlType; | ||
|
||
@XmlType(name="{{classname}}") | ||
@XmlEnum | ||
public enum {{classname}} { | ||
{{#allowableValues}}{{.}}{{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/allowableValues}} | ||
|
||
public String value() { | ||
return name(); | ||
} | ||
|
||
public static {{classname}} fromValue(String v) { | ||
return valueOf(v); | ||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
modules/swagger-codegen/src/main/resources/JavaJaxRS/cxf/enumOuterClass.mustache
This file was deleted.
Oops, something went wrong.
5 changes: 2 additions & 3 deletions
5
modules/swagger-codegen/src/main/resources/JavaJaxRS/cxf/model.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package {{package}}; | ||
|
||
{{#imports}}import{{import}}; | ||
{{#imports}}import {{import}}; | ||
{{/imports}} | ||
|
||
{{#serializableModel}}import java.io.Serializable;{{/serializableModel}} | ||
{{#models}} | ||
{{#model}}{{#description}} | ||
/** | ||
* {{description}} | ||
**/{{/description}} | ||
{{#isEnum}}{{>enumOuterClass}}{{/isEnum}} | ||
{{#isEnum}}{{>enumClass}}{{/isEnum}} | ||
{{^isEnum}}{{>pojo}}{{/isEnum}} | ||
{{/model}} | ||
{{/models}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found some grammatical mistakes and make an update in another commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK thank you. I also updated the file to remove my company name in the 'CXFInterfaceComparator' package. Pease, include it.