Skip to content
Permalink
Browse files

Merge pull request #17 from jenkinsci/JENKINS-41502_select_provider_w…

…ith_parameter

[JENKINS-41502] Select provider using a paramater
  • Loading branch information...
imeredith committed Nov 27, 2017
2 parents d69340a + c32fd09 commit 4c68a345a7b691937fe11f40a495461cf23c98ed
@@ -16,6 +16,11 @@ public String getDisplayName() {
return Messages.classic_name();
}

@Override
public String getName() {
return "classic";
}

@Override
public String getRunURL(Run<?, ?> run) {
return getRoot() + Util.encode(run.getUrl());
@@ -53,6 +53,11 @@ public String getDisplayName() {
return this.getClass().getSimpleName();
}

/** Name of provider to be used as an id. Do not use i18n */
public String getName() {
return this.getClass().getSimpleName();
}

/** Fully qualified URL for a Run */
public abstract String getRunURL(Run<?, ?> run);

@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.displayurlapi.actions;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import hudson.model.Action;
@@ -11,6 +12,7 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@@ -34,13 +36,36 @@ public String getUrlName() {
}

public final Object doRedirect(StaplerRequest req, StaplerResponse rsp) throws IOException {
DisplayURLProvider provider = lookupProvider();
DisplayURLProvider provider = lookupProvider(req);
rsp.sendRedirect(HttpServletResponse.SC_MOVED_TEMPORARILY, getRedirectURL(provider));
return null;
}

protected abstract String getRedirectURL(DisplayURLProvider provider);

DisplayURLProvider lookupProvider(StaplerRequest req) {
final String providerName = req.getParameter("provider");
if(providerName != null && !providerName.isEmpty()) {
Iterable<DisplayURLProvider> providers = DisplayURLProvider.all();
Iterable<DisplayURLProvider> filtered = Iterables.filter(providers, new Predicate<DisplayURLProvider>() {
@Override
public boolean apply(@Nullable DisplayURLProvider displayURLProvider) {
if(displayURLProvider == null) {
return false;
}

return displayURLProvider.getName().equals(providerName);
}
});

DisplayURLProvider provider = Iterables.getFirst(filtered, null);
if(provider != null) {
return provider;
}
}

return lookupProvider();
}
DisplayURLProvider lookupProvider() {
PreferredProviderUserProperty prefProperty = getUserPreferredProviderProperty();

@@ -44,6 +44,16 @@ public void testRedirectForChangesURL() throws Exception {
.header("Location", getRedirectedProvider().getChangesURL(run));
}

@Test
public void testRedirectForYetAnotherProviderParameter() throws Exception {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
.when().get(provider.getChangesURL(run) + "&provider=YetAnotherDisplayURLProvider").then()
.statusCode(HttpServletResponse.SC_MOVED_TEMPORARILY)
.header("Location", getYetAnotherRedirectedProvider().getChangesURL(run));
}

@Test
public void testUrls() throws Exception {
String root = DisplayURLProvider.get().getRoot();
@@ -59,6 +69,10 @@ protected DisplayURLProvider getRedirectedProvider() {
return Iterables.find(DisplayURLProvider.all(), Predicates.instanceOf(AnotherDisplayURLProvider.class));
}

protected DisplayURLProvider getYetAnotherRedirectedProvider() {
return Iterables.find(DisplayURLProvider.all(), Predicates.instanceOf(YetAnotherDisplayURLProvider.class));
}

@TestExtension
public static class AnotherDisplayURLProvider extends DisplayURLProvider {

@@ -79,4 +93,25 @@ public String getJobURL(Job<?, ?> project) {
return DisplayURLProvider.getDefault().getJobURL(project) + EXTRA_CONTENT_IN_URL;
}
}

@TestExtension
public static class YetAnotherDisplayURLProvider extends DisplayURLProvider {

public static final String EXTRA_CONTENT_IN_URL = "yetanother";

@Override
public String getRunURL(Run<?, ?> run) {
return DisplayURLProvider.getDefault().getRunURL(run) + EXTRA_CONTENT_IN_URL;
}

@Override
public String getChangesURL(Run<?, ?> run) {
return DisplayURLProvider.getDefault().getChangesURL(run) + EXTRA_CONTENT_IN_URL;
}

@Override
public String getJobURL(Job<?, ?> project) {
return DisplayURLProvider.getDefault().getJobURL(project) + EXTRA_CONTENT_IN_URL;
}
}
}

0 comments on commit 4c68a34

Please sign in to comment.
You can’t perform that action at this time.