block.addStatement(Stmt.declareVariable("url", StringBuilder.class,
Stmt.newObject(StringBuilder.class, new StringStatement("getBaseUrl()"))));
// construct path using @PathParams and @MatrixParams
String path = resourceMethod.getPath();
ContextualStatementBuilder pathValue = Stmt.loadLiteral(path);
for (String pathParamName : JaxrsResourceMethodParameters.getPathParameterNames(path)) {
String pathParamId = pathParamName;
if (pathParamName.contains(":")) {
pathParamId = pathParamName.split(":")[0];
}
Statement pathParam = marshal(params.getPathParameter(pathParamId));
if (params.needsEncoding(pathParamId)) {
pathParam = encodePath(pathParam);
}
pathValue = pathValue.invoke("replace", "{" + pathParamName + "}", pathParam);
}
if (params.getMatrixParameters() != null) {
for (String matrixParamName : params.getMatrixParameters().keySet()) {
pathValue = pathValue.invoke("concat", ";" + matrixParamName + "=")
.invoke("concat", encodePath(marshal(params.getMatrixParameter(matrixParamName))));
}
}
ContextualStatementBuilder urlBuilder = Stmt.loadVariable("url").invoke(APPEND, pathValue);
block.addStatement(urlBuilder);
// construct query using @QueryParams
if (params.getQueryParameters() != null) {
urlBuilder = urlBuilder.invoke(APPEND, "?");
int i = 0;
for (String queryParamName : params.getQueryParameters().keySet()) {
for (Statement queryParam : params.getQueryParameters(queryParamName)) {
MetaClass queryParamType = queryParam.getType();
if (isListOrSet(queryParamType)) {
MetaClass paramType = assertValidCollectionParam(queryParamType, queryParamName, QueryParam.class);
ContextualStatementBuilder listParam = (queryParam instanceof Parameter) ?
Stmt.loadVariable(((Parameter) queryParam).getName()) : Stmt.nestedCall(queryParam);
block.addStatement(listParam.foreach("p")
.append(If.not(Stmt.loadVariable("url").invoke("toString").invoke("endsWith", "?"))
.append(Stmt.loadVariable("url").invoke(APPEND, "&")).finish())
.append(Stmt.loadVariable("url").invoke(APPEND, queryParamName).invoke(APPEND, "=")
.invoke(APPEND, encodeQuery(marshal(paramType, Stmt.loadVariable("p")))))
.finish()