return e1.getPath().compareTo(e2.getPath());
}
});
Iterator<EndpointDescription> iter = sortedEndpoints.iterator();
while (iter.hasNext()) {
EndpointDescription ed = iter.next();
if (ed.getPath() != null && ed.getPath().contains("{path:.*}")) {
iter.remove();
}
}
return sortedEndpoints;
}
else if ("flatpackEndpoints".equals(propertyName)) {
List<EndpointDescription> sortedEndpoints = new ArrayList<EndpointDescription>(
apiDescription.getEndpoints());
Collections.sort(sortedEndpoints, new Comparator<EndpointDescription>() {
@Override
public int compare(EndpointDescription e1, EndpointDescription e2) {
return e1.getPath().compareTo(e2.getPath());
}
});
Iterator<EndpointDescription> iter = sortedEndpoints.iterator();
while (iter.hasNext()) {
if (!hasCustomRequestBuilderClass(iter.next())) {
iter.remove();
}
}
return sortedEndpoints;
}
else if ("requireNames".equals(propertyName)) {
return entityRequires;
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
group.registerModelAdaptor(EndpointDescription.class,
new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o,
Object property, String propertyName)
throws STNoSuchPropertyException {
EndpointDescription end = (EndpointDescription) o;
if ("docString".equals(propertyName)) {
return jsDocString(end.getDocString());
}
if ("methodName".equals(propertyName)) {
String path = end.getPath();
String[] parts = path.split(Pattern.quote("/"));
StringBuilder sb = new StringBuilder();
sb.append(end.getMethod().toLowerCase());
for (int i = 3, j = parts.length; i < j; i++) {
String part = parts[i];
if (part.length() == 0) continue;
if (!part.startsWith("{") && !part.endsWith("}")) {
if (part.contains(".")) {
String[] dotPart = part.split(Pattern.quote("."));
for (String dot : dotPart) {
sb.append(upcase(dot));
}
}
else {
sb.append(upcase(part));
}
}
else {
sb.append(upcase(part.substring(1, part.length() - 1)));
}
}
return sb.toString();
}
else if ("methodParameterList".equals(propertyName)) {
String path = end.getPath();
String[] parts = path.split(Pattern.quote("/"));
StringBuilder sb = new StringBuilder();
int paramCount = 0;
for (int i = 3, j = parts.length; i < j; i++) {
String part = parts[i];
if (part.length() == 0) continue;
if (part.startsWith("{") && part.endsWith("}")) {
String name = part.substring(1, part.length() - 1);
sb.append(name);
paramCount++;
if (end.getPathParameters() != null
&& paramCount < end.getPathParameters().size()) {
sb.append(", ");
}
}
}
if (end.getEntity() != null) {
if (paramCount > 0) {
sb.append(", ");
}
sb.append(getNameForType(end.getEntity()));
}
return sb.toString();
}
else if ("entityName".equals(propertyName)) {
return getNameForType(end.getEntity());
}
else if ("requestBuilderClassName".equals(propertyName)) {
if (hasCustomRequestBuilderClass(end)) {
return getBuilderReturnType(end);
}
else {
return "com.getperka.flatpack.client.JsonRequest";
}
}
else if ("requestBuilderBlockName".equals(propertyName)) {
return getBuilderReturnType(end) + "Block";
}
else if ("pathDecoded".equals(propertyName)) {
// URL-decode the path in the endpoint description
try {
String decoded = URLDecoder.decode(end.getPath(), "UTF8");
return decoded;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}