Skip to content

Commit

Permalink
fix #10
Browse files Browse the repository at this point in the history
  • Loading branch information
marevol committed Sep 20, 2014
1 parent e48f9bd commit e59e8bc
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private FileVisitResult checkIfExist(final Path path)
print(e.getMessage() + " Retring to delete it.");
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
} catch (final InterruptedException ignore) {
// ignore
}
}
Expand Down Expand Up @@ -349,8 +349,8 @@ protected Settings buildNodeSettings(final int number) {
.toAbsolutePath().toString());

final String nodeName = "Node " + number;
final int transportPort = getTransportPort(number);
final int httpPort = getHttpPort(number);
final int transportPort = getAvailableTransportPort(number);
final int httpPort = getAvailableHttpPort(number);
putIfAbsent(settingsBuilder, "cluster.name", clusterName);
putIfAbsent(settingsBuilder, "node.name", nodeName);
putIfAbsent(settingsBuilder, "node.master", String.valueOf(true));
Expand All @@ -373,7 +373,7 @@ protected Settings buildNodeSettings(final int number) {
return settings;
}

protected int getHttpPort(final int number) {
protected int getAvailableHttpPort(final int number) {
int httpPort = baseHttpPort + number;
if (maxHttpPort < 0) {
return httpPort;
Expand All @@ -392,7 +392,7 @@ protected int getHttpPort(final int number) {
+ " is unavailable.");
}

protected int getTransportPort(final int number) {
protected int getAvailableTransportPort(final int number) {
int transportPort = baseTransportPort + number;
if (maxTransportPort < 0) {
return transportPort;
Expand Down Expand Up @@ -428,7 +428,7 @@ public void setMaxTransportPort(final int maxTransportPort) {

/**
* Return a node by the node index.
*
*
* @param i A node index
* @return null if the node is not found
*/
Expand All @@ -441,8 +441,8 @@ public Node getNode(final int i) {

/**
* Start a closed node.
*
* @param i
*
* @param i
* @return true if the node is started.
*/
public boolean startNode(final int i) {
Expand All @@ -460,7 +460,7 @@ public boolean startNode(final int i) {

/**
* Return a node by the name.
*
*
* @param name A node name
* @return null if the node is not found by the name
*/
Expand All @@ -478,7 +478,7 @@ public Node getNode(final String name) {

/**
* Return a node index.
*
*
* @param node
* @return -1 if the node does not exist.
*/
Expand All @@ -493,7 +493,7 @@ public int getNodeIndex(final Node node) {

/**
* Return the number of nodes.
*
*
* @return the number of nodes
*/
public int getNodeSize() {
Expand Down Expand Up @@ -521,7 +521,7 @@ protected void createDir(final Path path) {

/**
* Return an available node.
*
*
* @return
*/
public Node node() {
Expand All @@ -535,7 +535,7 @@ public Node node() {

/**
* Return a master node.
*
*
* @return
*/
public synchronized Node masterNode() {
Expand All @@ -547,7 +547,7 @@ public synchronized Node masterNode() {

/**
* Return a non-master node.
*
*
* @return
*/
public synchronized Node nonMasterNode() {
Expand All @@ -564,7 +564,7 @@ public synchronized Node nonMasterNode() {

/**
* Return an elasticsearch client.
*
*
* @return
*/
public Client client() {
Expand All @@ -573,7 +573,7 @@ public Client client() {

/**
* Return an elasticsearch admin client.
*
*
* @return
*/
public AdminClient admin() {
Expand All @@ -582,7 +582,7 @@ public AdminClient admin() {

/**
* Wait for green state of a cluster.
*
*
* @param indices
* @return
*/
Expand All @@ -606,7 +606,7 @@ public ClusterHealthStatus ensureGreen(final String... indices) {

/**
* Wait for yellow state of a cluster.
*
*
* @param indices
* @return
*/
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/codelibs/elasticsearch/runner/net/Curl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.codelibs.elasticsearch.runner.net;

import java.net.HttpURLConnection;

import org.elasticsearch.node.Node;

public class Curl {

protected Curl() {
// nothing
}

public static CurlRequest get(Node node, String path) {
return new CurlRequest(Method.GET, node, path);
}

public static CurlRequest post(Node node, String path) {
return new CurlRequest(Method.POST, node, path);
}

public static CurlRequest put(Node node, String path) {
return new CurlRequest(Method.PUT, node, path);
}

public static CurlRequest delete(Node node, String path) {
return new CurlRequest(Method.DELETE, node, path);
}

public static CurlRequest get(final String url) {
return new CurlRequest(Method.GET, url);
}

public static CurlRequest post(final String url) {
return new CurlRequest(Method.POST, url);
}

public static CurlRequest put(final String url) {
return new CurlRequest(Method.PUT, url);
}

public static CurlRequest delete(final String url) {
return new CurlRequest(Method.DELETE, url);
}

public enum Method {
GET, POST, PUT, DELETE;
}

public interface ResponseListener {
public void onResponse(HttpURLConnection con);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.codelibs.elasticsearch.runner.net;

public class CurlException extends RuntimeException {

private static final long serialVersionUID = 1L;

public CurlException(final String message, final Throwable cause) {
super(message, cause);
}

public CurlException(final String message) {
super(message);
}

}
170 changes: 170 additions & 0 deletions src/main/java/org/codelibs/elasticsearch/runner/net/CurlRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package org.codelibs.elasticsearch.runner.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

import org.codelibs.elasticsearch.runner.net.Curl.Method;
import org.codelibs.elasticsearch.runner.net.Curl.ResponseListener;
import org.elasticsearch.node.Node;

public class CurlRequest {
protected String url;

protected String encoding = "UTF-8";

protected Method method;

protected List<String> paramList;

protected String body;

public CurlRequest(final Method method, final String url) {
this.method = method;
this.url = url;
}

public CurlRequest(Method method, Node node, String path) {
this.method = method;
StringBuilder urlBuf = new StringBuilder(200);
urlBuf.append("http://localhost:").append(
node.settings().get("http.port"));
if (path.startsWith("/")) {
urlBuf.append(path);
} else {
urlBuf.append('/').append(path);
}
this.url = urlBuf.toString();
}

public CurlRequest encoding(final String encoding) {
if (paramList != null) {
throw new CurlException(
"This method must be called before param method.");
}
this.encoding = encoding;
return this;
}

public CurlRequest body(final String body) {
this.body = body;
return this;
}

public CurlRequest param(final String key, final String value) {
if (paramList == null) {
paramList = new ArrayList<>();
}
paramList.add(encode(key) + "=" + encode(value));
return this;
}

public void execute(final ResponseListener listener) {
if (paramList != null) {
char sp;
if (url.indexOf('?') == -1) {
sp = '?';
} else {
sp = '&';
}
final StringBuilder urlBuf = new StringBuilder(url.length() + 100);
for (final String param : paramList) {
urlBuf.append(sp).append(param);
if (sp == '?') {
sp = '&';
}
}
if (Method.GET == method || body != null) {
url = url + urlBuf.toString();
} else {
body = urlBuf.substring(1);
}
}

HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod(method.toString());
if (body != null) {
connection.setDoOutput(true);
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(connection.getOutputStream(),
encoding))) {
writer.write(body);
writer.flush();
}
}
listener.onResponse(connection);
} catch (final Exception e) {
throw new CurlException("Failed to access to " + url, e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

public CurlResponse execute() {
final CurlResponse response = new CurlResponse();
execute(new ResponseListener() {
@Override
public void onResponse(final HttpURLConnection con) {
try {
response.setHttpStatusCode(con.getResponseCode());
response.setEncoding(encoding);
final Path tempFile = Files.createTempFile("esrunner-",
".tmp");
try (BufferedInputStream bis = new BufferedInputStream(
con.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(
Files.newOutputStream(tempFile,
StandardOpenOption.WRITE))) {
byte[] bytes = new byte[4096];
try {
int length = bis.read(bytes);
while (length != -1) {
if (length != 0) {
bos.write(bytes, 0, length);
}
length = bis.read(bytes);
}
} finally {
bytes = null;
}
bos.flush();
response.setContentFile(tempFile);
} catch (Exception e) {
response.setContentException(e);
try {
Files.deleteIfExists(tempFile);
} catch (Exception ignore) {
// ignore
}
}
} catch (final Exception e) {
throw new CurlException("Failed to access the response.", e);
}
}
});
return response;
}

protected String encode(final String value) {
try {
return URLEncoder.encode(value, encoding);
} catch (final UnsupportedEncodingException e) {
throw new CurlException("Invalid encoding: " + encoding, e);
}
}

}
Loading

0 comments on commit e59e8bc

Please sign in to comment.