Skip to content

Commit

Permalink
[KARAF-5772] Add balancing policy in HTTP proxy service
Browse files Browse the repository at this point in the history
  • Loading branch information
jbonofre committed Jan 8, 2020
1 parent 5132238 commit 0375e28
Show file tree
Hide file tree
Showing 19 changed files with 490 additions and 108 deletions.
1 change: 1 addition & 0 deletions http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
org.apache.karaf.http.command.completers,
org.apache.karaf.http.core.internal,
org.apache.karaf.http.core.internal.osgi,
org.apache.karaf.http.core.internal.proxy,
org.apache.felix.utils.version,
org.apache.felix.utils.manifest;-split-package:=merge-first,
org.apache.http*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

Expand All @@ -36,9 +37,12 @@ public class ProxyAddCommand implements Action {
@Argument(index = 1, name = "proxyTo", description = "HTTP location to proxy on the prefix", required = true, multiValued = false)
String proxyTo;

@Option(name = "-b", aliases = { "--lb" }, description = "Define the filter to the balancing service to use", required = false, multiValued = false)
String balancingPolicy;

@Override
public Object execute() throws Exception {
proxyService.addProxy(url, proxyTo);
proxyService.addProxy(url, proxyTo, balancingPolicy);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.http.command;

import org.apache.karaf.http.core.ProxyService;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

@Command(scope = "http", name = "proxy-balancing-list", description = "List the available balancing policies")
@Service
public class ProxyBalancingListCommand implements Action {

@Reference
private ProxyService proxyService;

@Override
public Object execute() throws Exception {
for (String balancingPolicy : proxyService.getBalancingPolicies()) {
System.out.println(balancingPolicy);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.support.table.ShellTable;

@Command(scope = "http", name = "proxies", description = "List the HTTP proxies")
@Command(scope = "http", name = "proxy-list", description = "List the HTTP proxies")
@Service
public class ProxyListCommand implements Action {

Expand All @@ -35,8 +35,9 @@ public Object execute() throws Exception {
ShellTable table = new ShellTable();
table.column("URL");
table.column("ProxyTo");
table.column("Balancing Policy");
for (String url : proxyService.getProxies().keySet()) {
table.addRow().addContent(url, proxyService.getProxies().get(url));
table.addRow().addContent(url, proxyService.getProxies().get(url).getProxyTo(), proxyService.getProxies().get(url).getBalancingPolicy());
}
table.print(System.out);
return null;
Expand Down
29 changes: 29 additions & 0 deletions http/src/main/java/org/apache/karaf/http/core/BalancingPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.http.core;

public interface BalancingPolicy {

/**
* Select a target proxy host in the given list.
*
* @param targets the list of hosts.
* @return the selected host in the list.
*/
String selectHost(String[] targets);

}
10 changes: 8 additions & 2 deletions http/src/main/java/org/apache/karaf/http/core/HttpMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.management.MBeanException;
import javax.management.openmbean.TabularData;
import java.util.Collection;
import java.util.Map;

/**
Expand All @@ -36,12 +37,17 @@ public interface HttpMBean {
/**
* List configured HTTP proxies.
*/
Map<String, String> getProxies() throws MBeanException;
Map<String, Proxy> getProxies() throws MBeanException;

/**
* List the available balancing policies.
*/
Collection<String> getProxyBalancingPolicies() throws MBeanException;

/**
* Add a new HTTP proxy using URL, proxyTo and prefix.
*/
void addProxy(String url, String proxyTo) throws MBeanException;
void addProxy(String url, String proxyTo, String balancingPolicy) throws MBeanException;

/**
* Remove an existing HTTP proxy identified by URL.
Expand Down
58 changes: 58 additions & 0 deletions http/src/main/java/org/apache/karaf/http/core/Proxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.karaf.http.core;

/**
* POJO describing proxy definition.
*/
public class Proxy {

private String url;
private String proxyTo;
private String balancingPolicy;

public Proxy(String url, String proxyTo, String balancingPolicy) {
this.url = url;
this.proxyTo = proxyTo;
this.balancingPolicy = balancingPolicy;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getProxyTo() {
return proxyTo;
}

public void setProxyTo(String proxyTo) {
this.proxyTo = proxyTo;
}

public String getBalancingPolicy() {
return balancingPolicy;
}

public void setBalancingPolicy(String balancingPolicy) {
this.balancingPolicy = balancingPolicy;
}

}
10 changes: 7 additions & 3 deletions http/src/main/java/org/apache/karaf/http/core/ProxyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
*/
package org.apache.karaf.http.core;

import java.util.Collection;
import java.util.Dictionary;
import java.util.Map;

public interface ProxyService {

Map<String, String> getProxies();
Map<String, Proxy> getProxies();

void addProxy(String url, String proxyTo) throws Exception;
Collection<String> getBalancingPolicies() throws Exception;

void addProxy(String url, String proxyTo, String balancingProxy) throws Exception;

void removeProxy(String url) throws Exception;

void initProxies();
void update(Dictionary<String, ?> properties);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.karaf.http.core.internal;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -71,14 +72,23 @@ public TabularData getServlets() throws MBeanException {
}

@Override
public Map<String, String> getProxies() throws MBeanException {
public Map<String, Proxy> getProxies() throws MBeanException {
return proxyService.getProxies();
}

@Override
public void addProxy(String url, String proxyTo) throws MBeanException {
public Collection<String> getProxyBalancingPolicies() throws MBeanException {
try {
proxyService.addProxy(url, proxyTo);
return proxyService.getBalancingPolicies();
} catch (Exception e) {
throw new MBeanException(null, e.toString());
}
}

@Override
public void addProxy(String url, String proxyTo, String balancingPolicy) throws MBeanException {
try {
proxyService.addProxy(url, proxyTo, balancingPolicy);
} catch (Exception e) {
throw new MBeanException(null, e.toString());
}
Expand Down
Loading

0 comments on commit 0375e28

Please sign in to comment.