-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
342 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/org/codelibs/elasticsearch/runner/net/Curl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/codelibs/elasticsearch/runner/net/CurlException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
170
src/main/java/org/codelibs/elasticsearch/runner/net/CurlRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.