Skip to content

Commit

Permalink
ns
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Jun 28, 2019
1 parent 9522a68 commit 995874d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 72 deletions.
24 changes: 12 additions & 12 deletions src/org/jgroups/protocols/RACKSPACE_PING.java
Expand Up @@ -3,7 +3,6 @@
import org.jgroups.Address;
import org.jgroups.annotations.Property;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.util.Responses;
import org.jgroups.util.Util;

Expand All @@ -21,22 +20,19 @@
* @author Gustavo Fernandes
*/
public class RACKSPACE_PING extends FILE_PING {

protected static final Log log = LogFactory.getLog(RACKSPACE_PING.class);

private static final String UKService = "https://lon.auth.api.rackspacecloud.com/v1.0";
private static final String USService = "https://auth.api.rackspacecloud.com/v1.0";

protected RackspaceClient rackspaceClient = null;
protected RackspaceClient rackspaceClient;

@Property(description = "Rackspace username")
protected String username = null;
protected String username;

@Property(description = "Rackspace API access key",exposeAsManagedAttribute=false)
protected String apiKey = null;
protected String apiKey;

@Property(description = "Rackspace region, either UK or US")
protected String region = null;
protected String region;

@Property(description = "Name of the root container")
protected String container = "jgroups";
Expand All @@ -54,7 +50,7 @@ public void init() throws Exception {
}

URL authURL = new URL(region.equals("UK") ? UKService : USService);
rackspaceClient = new RackspaceClient(authURL, username, apiKey);
rackspaceClient = new RackspaceClient(authURL, username, apiKey).log(log);

super.init();

Expand Down Expand Up @@ -123,8 +119,7 @@ protected void removeAll(String clustername) {
/**
* A thread safe Rackspace ReST client
*/
static class RackspaceClient {

protected static class RackspaceClient {
private static final String ACCEPT_HEADER = "Accept";
private static final String AUTH_HEADER = "X-Auth-User";
private static final String AUTH_KEY_HEADER = "X-Auth-Key";
Expand All @@ -135,7 +130,8 @@ static class RackspaceClient {
private final String username;
private final String apiKey;

private volatile Credentials credentials = null;
private volatile Credentials credentials;
private Log log;

/**
* Constructor
Expand All @@ -150,6 +146,10 @@ public RackspaceClient(URL apiEndpoint, String username, String apiKey) {
this.apiKey = apiKey;
}

RackspaceClient log(Log l) {
this.log=l; return this;
}

/**
* Authenticate
*/
Expand Down
94 changes: 34 additions & 60 deletions src/org/jgroups/protocols/SWIFT_PING.java
Expand Up @@ -5,20 +5,15 @@
import org.jgroups.annotations.Experimental;
import org.jgroups.annotations.Property;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.util.Responses;
import org.jgroups.util.Util;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

import javax.script.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
Expand All @@ -32,24 +27,22 @@
@Experimental
public class SWIFT_PING extends FILE_PING {

private static final Log log = LogFactory.getLog(SWIFT_PING.class);

protected SwiftClient swiftClient = null;
protected SwiftClient swiftClient;

@Property(description = "Authentication url")
protected String auth_url = null;
protected String auth_url;

@Property(description = "Authentication type")
protected String auth_type = "keystone_v_2_0";

@Property(description = "Openstack Keystone tenant name")
protected String tenant = null;
protected String tenant;

@Property(description = "Username")
protected String username = null;
protected String username;

@Property(description = "Password",exposeAsManagedAttribute=false)
protected String password = null;
protected String password;

@Property(description = "Name of the root container")
protected String container = "jgroups";
Expand All @@ -65,7 +58,7 @@ public void init() throws Exception {
Authenticator authenticator = createAuthenticator();
authenticator.validateParams();

swiftClient = new SwiftClient(authenticator);
swiftClient = new SwiftClient(authenticator, log);
// Authenticate now to record credential
swiftClient.authenticate();

Expand Down Expand Up @@ -201,9 +194,9 @@ private enum AUTH_TYPE {
LOOKUP.put(type.configName, type);
}

private String configName;
private final String configName;

private AUTH_TYPE(String externalName) {
AUTH_TYPE(String externalName) {
this.configName = externalName;
}

Expand Down Expand Up @@ -262,7 +255,7 @@ private static class Keystone_V_2_0_Auth implements Authenticator {
"}" +
"result;";

private static Object scriptEngineLock = new Object();
private static final Object scriptEngineLock = new Object();
private static ScriptEngine scriptEngine;

private final String tenant;
Expand Down Expand Up @@ -303,20 +296,18 @@ public Credentials authenticate() throws Exception {
jsonBuilder.toString().getBytes(), true);

if (response.isSuccessCode()) {
Map<String, String> result = parseJsonResponse(new String(response.payload, "UTF-8"));
Map<String, String> result = parseJsonResponse(new String(response.payload, StandardCharsets.UTF_8));

String authToken = result.get("id");
String storageUrl = result.get("url");
if (authToken == null)
{
throw new IllegalStateException("Missing token id in authentication response");
}
if (storageUrl == null)
{
if (storageUrl == null) {
throw new IllegalStateException("Missing storage service URL in authentication response");
}

log.trace("Authentication successful");
return new Credentials(authToken, storageUrl);
} else {
throw new IllegalStateException(
Expand All @@ -325,7 +316,7 @@ public Credentials authenticate() throws Exception {
}
}

protected Map<String,String> parseJsonResponse(String json) throws ScriptException
protected static Map<String,String> parseJsonResponse(String json) throws ScriptException
{
synchronized (scriptEngineLock)
{
Expand All @@ -350,36 +341,22 @@ protected Map<String,String> parseJsonResponse(String json) throws ScriptExcepti
*/
private static class ConnBuilder {

private HttpURLConnection con;
private final HttpURLConnection con;

public ConnBuilder(URL url) {
try {
con = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
log.error(Util.getMessage("ErrorBuildingURL"), e);
}
public ConnBuilder(URL url) throws IOException {
con = (HttpURLConnection) url.openConnection();
}

public ConnBuilder(Credentials credentials, String container,
String object) {
try {
public ConnBuilder(Credentials credentials, String container, String object) throws IOException {
String url = credentials.storageUrl + "/" + container;
if (object != null) {
url = url + "/" + object;
}
con = (HttpURLConnection) new URL(url).openConnection();
} catch (IOException e) {
log.error(Util.getMessage("ErrorCreatingConnection"), e);
}

}

public ConnBuilder method(String method) {
try {
con.setRequestMethod(method);
} catch (ProtocolException e) {
log.error(Util.getMessage("ProtocolError"), e);
}
public ConnBuilder method(String method) throws ProtocolException {
con.setRequestMethod(method);
return this;
}

Expand Down Expand Up @@ -411,21 +388,17 @@ private static class HttpResponse {
this.payload = payload;
}

public List<String> payloadAsLines() {
public List<String> payloadAsLines() throws IOException {
List<String> lines = new ArrayList<>();
BufferedReader in;
try {
String line;
in = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(payload)));
String line;
in = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(payload)));

while ((line = in.readLine()) != null) {
lines.add(line);
}
in.close();
} catch (IOException e) {
log.error(Util.getMessage("ErrorReadingObjects"), e);
while ((line = in.readLine()) != null) {
lines.add(line);
}
in.close();
return lines;
}

Expand All @@ -445,15 +418,17 @@ protected static class SwiftClient {

private final Authenticator authenticator;

private volatile Credentials credentials = null;
private volatile Credentials credentials;
private final Log log;

/**
* Constructor
*
* @param authenticator Swift auth provider
*/
public SwiftClient(Authenticator authenticator) {
public SwiftClient(Authenticator authenticator, Log l) {
this.authenticator = authenticator;
this.log=l;
}

/**
Expand Down Expand Up @@ -605,9 +580,8 @@ public List<String> listObjects(String containerName) throws Exception {
return response.payloadAsLines();
}

private ConnBuilder getConnBuilder(String container, String object) {
ConnBuilder connBuilder = new ConnBuilder(credentials, container,
object);
private ConnBuilder getConnBuilder(String container, String object) throws IOException {
ConnBuilder connBuilder = new ConnBuilder(credentials, container, object);
connBuilder.addHeader(HttpHeaders.STORAGE_TOKEN_HEADER,
credentials.authToken);
connBuilder.addHeader(HttpHeaders.ACCEPT_HEADER, "*/*");
Expand All @@ -619,7 +593,7 @@ private ConnBuilder getConnBuilder(String container, String object) {
private static class Utils {

public static void validateNotEmpty(String arg, String argname) {
if (arg == null || arg.trim().length() == 0) {
if (arg == null || arg.trim().isEmpty()) {
throw new IllegalArgumentException("'" + argname
+ "' cannot be empty");
}
Expand Down

0 comments on commit 995874d

Please sign in to comment.