Skip to content

Commit

Permalink
fix: specifying api vers in base url works with form auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Jun 9, 2016
1 parent 4ae811f commit 72ff1a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
44 changes: 37 additions & 7 deletions src/main/java/org/rundeck/client/Rundeck.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by greg on 3/28/16.
*/
public class Rundeck {
public static final int API_VERS = 16;
public static final Pattern API_VERS_PATTERN = Pattern.compile("^(.*)(/api/\\d+/?)$");

/**
* Create a client using the specified, or default version
Expand All @@ -29,7 +32,12 @@ public class Rundeck {
*
* @return
*/
public static Client<RundeckApi> client(String baseUrl, final String token, final boolean debugHttp) {
public static Client<RundeckApi> client(
String baseUrl,
final String token,
final boolean debugHttp
)
{
return client(baseUrl, API_VERS, token, debugHttp);
}

Expand Down Expand Up @@ -73,7 +81,7 @@ public static Client<RundeckApi> client(
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

logging.setLevel(HttpLoggingInterceptor.Level.BODY);
String base = buildBaseUrlForVersion(baseUrl, apiVers);
String base = buildApiUrlForVersion(baseUrl, apiVers);

OkHttpClient.Builder callFactory = new OkHttpClient.Builder()
.addInterceptor(new StaticHeaderInterceptor("X-Rundeck-Auth-Token", authToken));
Expand Down Expand Up @@ -116,7 +124,8 @@ public static Client<RundeckApi> client(
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
String base = buildBaseUrlForVersion(baseUrl, apiVers);
String appBaseUrl = buildBaseAppUrlForVersion(baseUrl);
String apiBaseUrl = buildApiUrlForVersion(baseUrl, apiVers);

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
Expand All @@ -134,7 +143,7 @@ public static Client<RundeckApi> client(
callFactory.addInterceptor(new FormAuthInterceptor(
username,
password,
baseUrl,
appBaseUrl,
postUrl,
System.getProperty(
"rundeck.client.j_username",
Expand All @@ -158,7 +167,7 @@ public static Client<RundeckApi> client(
callFactory.cookieJar(new JavaNetCookieJar(cookieManager));

Retrofit build = new Retrofit.Builder()
.baseUrl(base)
.baseUrl(apiBaseUrl)
.callFactory(callFactory.build())
.addConverterFactory(new QualifiedTypeConverterFactory(
JacksonConverterFactory.create(),
Expand All @@ -170,9 +179,30 @@ public static Client<RundeckApi> client(
return new Client<>(build.create(RundeckApi.class), build);
}

private static String buildBaseUrlForVersion(String baseUrl, final int apiVers) {
/**
* @param baseUrl input url
* @param apiVers api VERSION to append if /api/VERS is not present
*
* @return URL for API by appending /api/VERS if it is not present
*/
private static String buildApiUrlForVersion(String baseUrl, final int apiVers) {
if (!baseUrl.matches("^.*/api/\\d+/?$")) {
return baseUrl + "/api/" + apiVers + "/";
return baseUrl + "/api/" + (apiVers) + "/";
} else if (!baseUrl.matches(".*/$")) {
return baseUrl + "/";
}
return baseUrl;
}

/**
* @param baseUrl input url
*
* @return the base part of the API url without the /api/VERS part
*/
private static String buildBaseAppUrlForVersion(String baseUrl) {
Matcher matcher = API_VERS_PATTERN.matcher(baseUrl);
if (matcher.matches()) {
return matcher.group(1);
} else if (!baseUrl.matches(".*/$")) {
return baseUrl + "/";
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/rundeck/client/tool/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public static Tool tool(final String name) {


public static Client<RundeckApi> createClient() {
String baseUrl = requireEnv("RUNDECK_URL", "Please specify the Rundeck URL");
String baseUrl = requireEnv(
"RUNDECK_URL",
"Please specify the Rundeck base URL, e.g. http://host:port or http://host:port/api/14"
);
if (System.getenv("RUNDECK_TOKEN") == null
&& System.getenv("RUNDECK_USER") == null
&& System.getenv("RUNDECK_PASSWORD") == null) {
Expand Down

0 comments on commit 72ff1a8

Please sign in to comment.