Skip to content
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

[JBIDE-26172] corrected request url when using api groups #337

Merged
merged 1 commit into from Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -17,7 +17,7 @@
<!-- Artifact Information -->
<groupId>com.openshift</groupId>
<artifactId>openshift-restclient-java</artifactId>
<version>6.1.1-SNAPSHOT</version>
<version>6.1.1.Final</version>
<packaging>jar</packaging>
<name>OpenShift Java REST Client</name>
<url>http://openshift.redhat.com</url>
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/openshift/internal/restclient/DefaultClient.java
Expand Up @@ -239,11 +239,20 @@ public <T> T execute(ITypeFactory factory, String method, String kind, String na
if (ResourceKind.LIST.equals(kind)) {
throw new UnsupportedOperationException("Generic create operation not supported for resource type 'List'");
}
final URL endpoint = new URLBuilder(this.baseUrl, typeMapper).kind(kind).name(name).namespace(namespace)
.subresource(subresource).subContext(subContext).addParameters(params).build();

final URL endpoint = new URLBuilder(this.baseUrl, typeMapper)
.apiVersion(getApiVersion(payload))
.kind(kind)
.name(name)
.namespace(namespace)
.subresource(subresource)
.subContext(subContext)
.addParameters(params)
.build();

try {
Request request = newRequestBuilderTo(endpoint.toString()).method(method, getPayload(method, payload))
Request request = newRequestBuilderTo(endpoint.toString())
.method(method, getPayload(method, payload))
.build();
LOGGER.debug("About to make {} request: {}", request.method(), request);
try (Response result = client.newCall(request).execute()) {
Expand All @@ -256,6 +265,14 @@ public <T> T execute(ITypeFactory factory, String method, String kind, String na
}
}

private String getApiVersion(JSONSerializeable payload) {
String apiVersion = null;
if (payload instanceof IResource) {
apiVersion = ((IResource) payload).getApiVersion();
}
return apiVersion;
}

private RequestBody getPayload(String method, JSONSerializeable payload) {
switch (method.toUpperCase()) {
case "GET":
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/openshift/internal/restclient/URLBuilder.java
Expand Up @@ -150,15 +150,8 @@ private void buildWithNamespaceInPath(StringBuilder url) {
}
url.append("/");
IVersionedApiResource apiResource = typeMappings.getEndpointFor(apiVersion, kind);
url.append(apiResource.getPrefix()).append("/").append(apiResource.getVersion());
if (namespace == null && apiResource.isNamespaced()) {
LOG.debug(
"The api endpoint for kind '{}' requires a namespace but none was provided. Will only work for priviledged user.",
kind);
}
if (!ResourceKind.PROJECT.equals(kind) && namespace != null) {
url.append("/namespaces/").append(namespace);
}
appendApiResource(url, apiResource);
appendNamespace(url);
url.append("/").append(apiResource.getName());
if (name != null) {
url.append("/").append(name);
Expand All @@ -177,6 +170,20 @@ private void buildWithNamespaceInPath(StringBuilder url) {
url = appendParameters(url);
}

private void appendApiResource(StringBuilder url, IVersionedApiResource apiResource) {
url.append(apiResource.getPrefix()).append("/");
if (!StringUtils.isEmpty(apiResource.getApiGroupName())) {
url.append(apiResource.getApiGroupName()).append("/");
}
url.append(apiResource.getVersion());
}

private void appendNamespace(StringBuilder url) {
if (!ResourceKind.PROJECT.equals(kind) && namespace != null) {
url.append("/namespaces/").append(namespace);
}
}

private StringBuilder appendParameters(StringBuilder url) {
if (!params.isEmpty()) {
url.append(IHttpConstants.QUESTION_MARK);
Expand Down