embulk-input-http 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +188 -0
- data/build.gradle +76 -0
- data/classpath/embulk-input-http-0.0.15.jar +0 -0
- data/config/checkstyle/checkstyle.xml +128 -0
- data/config/checkstyle/default.xml +108 -0
- data/example/json-example.yml +21 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +164 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/input/http.rb +3 -0
- data/settings.gradle +1 -0
- data/src/main/java/org/embulk/input/http/BasicAuthConfig.java +39 -0
- data/src/main/java/org/embulk/input/http/HttpFileInputPlugin.java +333 -0
- data/src/main/java/org/embulk/input/http/PagerConfig.java +86 -0
- data/src/main/java/org/embulk/input/http/ParamsConfig.java +82 -0
- data/src/main/java/org/embulk/input/http/QueryConfig.java +171 -0
- data/src/main/java/org/embulk/input/http/RetryableHandler.java +90 -0
- data/src/test/java/org/embulk/input/http/TestHttpInputPlugin.java +5 -0
- data/src/test/java/org/embulk/input/http/TestPagerConfig.java +78 -0
- data/src/test/java/org/embulk/input/http/TestParamsConfig.java +105 -0
- data/src/test/java/org/embulk/input/http/TestQueryConfig.java +125 -0
- metadata +36 -13
- data/classpath/embulk-input-http-0.0.14.jar +0 -0
data/gradlew
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
##############################################################################
|
4
|
+
##
|
5
|
+
## Gradle start up script for UN*X
|
6
|
+
##
|
7
|
+
##############################################################################
|
8
|
+
|
9
|
+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
10
|
+
DEFAULT_JVM_OPTS=""
|
11
|
+
|
12
|
+
APP_NAME="Gradle"
|
13
|
+
APP_BASE_NAME=`basename "$0"`
|
14
|
+
|
15
|
+
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
16
|
+
MAX_FD="maximum"
|
17
|
+
|
18
|
+
warn ( ) {
|
19
|
+
echo "$*"
|
20
|
+
}
|
21
|
+
|
22
|
+
die ( ) {
|
23
|
+
echo
|
24
|
+
echo "$*"
|
25
|
+
echo
|
26
|
+
exit 1
|
27
|
+
}
|
28
|
+
|
29
|
+
# OS specific support (must be 'true' or 'false').
|
30
|
+
cygwin=false
|
31
|
+
msys=false
|
32
|
+
darwin=false
|
33
|
+
case "`uname`" in
|
34
|
+
CYGWIN* )
|
35
|
+
cygwin=true
|
36
|
+
;;
|
37
|
+
Darwin* )
|
38
|
+
darwin=true
|
39
|
+
;;
|
40
|
+
MINGW* )
|
41
|
+
msys=true
|
42
|
+
;;
|
43
|
+
esac
|
44
|
+
|
45
|
+
# For Cygwin, ensure paths are in UNIX format before anything is touched.
|
46
|
+
if $cygwin ; then
|
47
|
+
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
48
|
+
fi
|
49
|
+
|
50
|
+
# Attempt to set APP_HOME
|
51
|
+
# Resolve links: $0 may be a link
|
52
|
+
PRG="$0"
|
53
|
+
# Need this for relative symlinks.
|
54
|
+
while [ -h "$PRG" ] ; do
|
55
|
+
ls=`ls -ld "$PRG"`
|
56
|
+
link=`expr "$ls" : '.*-> \(.*\)$'`
|
57
|
+
if expr "$link" : '/.*' > /dev/null; then
|
58
|
+
PRG="$link"
|
59
|
+
else
|
60
|
+
PRG=`dirname "$PRG"`"/$link"
|
61
|
+
fi
|
62
|
+
done
|
63
|
+
SAVED="`pwd`"
|
64
|
+
cd "`dirname \"$PRG\"`/" >&-
|
65
|
+
APP_HOME="`pwd -P`"
|
66
|
+
cd "$SAVED" >&-
|
67
|
+
|
68
|
+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
69
|
+
|
70
|
+
# Determine the Java command to use to start the JVM.
|
71
|
+
if [ -n "$JAVA_HOME" ] ; then
|
72
|
+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
73
|
+
# IBM's JDK on AIX uses strange locations for the executables
|
74
|
+
JAVACMD="$JAVA_HOME/jre/sh/java"
|
75
|
+
else
|
76
|
+
JAVACMD="$JAVA_HOME/bin/java"
|
77
|
+
fi
|
78
|
+
if [ ! -x "$JAVACMD" ] ; then
|
79
|
+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
80
|
+
|
81
|
+
Please set the JAVA_HOME variable in your environment to match the
|
82
|
+
location of your Java installation."
|
83
|
+
fi
|
84
|
+
else
|
85
|
+
JAVACMD="java"
|
86
|
+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
87
|
+
|
88
|
+
Please set the JAVA_HOME variable in your environment to match the
|
89
|
+
location of your Java installation."
|
90
|
+
fi
|
91
|
+
|
92
|
+
# Increase the maximum file descriptors if we can.
|
93
|
+
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
|
94
|
+
MAX_FD_LIMIT=`ulimit -H -n`
|
95
|
+
if [ $? -eq 0 ] ; then
|
96
|
+
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
97
|
+
MAX_FD="$MAX_FD_LIMIT"
|
98
|
+
fi
|
99
|
+
ulimit -n $MAX_FD
|
100
|
+
if [ $? -ne 0 ] ; then
|
101
|
+
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
102
|
+
fi
|
103
|
+
else
|
104
|
+
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
105
|
+
fi
|
106
|
+
fi
|
107
|
+
|
108
|
+
# For Darwin, add options to specify how the application appears in the dock
|
109
|
+
if $darwin; then
|
110
|
+
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
111
|
+
fi
|
112
|
+
|
113
|
+
# For Cygwin, switch paths to Windows format before running java
|
114
|
+
if $cygwin ; then
|
115
|
+
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
116
|
+
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
117
|
+
|
118
|
+
# We build the pattern for arguments to be converted via cygpath
|
119
|
+
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
120
|
+
SEP=""
|
121
|
+
for dir in $ROOTDIRSRAW ; do
|
122
|
+
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
123
|
+
SEP="|"
|
124
|
+
done
|
125
|
+
OURCYGPATTERN="(^($ROOTDIRS))"
|
126
|
+
# Add a user-defined pattern to the cygpath arguments
|
127
|
+
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
128
|
+
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
129
|
+
fi
|
130
|
+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
131
|
+
i=0
|
132
|
+
for arg in "$@" ; do
|
133
|
+
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
134
|
+
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
135
|
+
|
136
|
+
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
137
|
+
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
138
|
+
else
|
139
|
+
eval `echo args$i`="\"$arg\""
|
140
|
+
fi
|
141
|
+
i=$((i+1))
|
142
|
+
done
|
143
|
+
case $i in
|
144
|
+
(0) set -- ;;
|
145
|
+
(1) set -- "$args0" ;;
|
146
|
+
(2) set -- "$args0" "$args1" ;;
|
147
|
+
(3) set -- "$args0" "$args1" "$args2" ;;
|
148
|
+
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
149
|
+
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
150
|
+
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
151
|
+
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
152
|
+
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
153
|
+
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
154
|
+
esac
|
155
|
+
fi
|
156
|
+
|
157
|
+
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
158
|
+
function splitJvmOpts() {
|
159
|
+
JVM_OPTS=("$@")
|
160
|
+
}
|
161
|
+
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
162
|
+
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
163
|
+
|
164
|
+
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
data/gradlew.bat
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
@if "%DEBUG%" == "" @echo off
|
2
|
+
@rem ##########################################################################
|
3
|
+
@rem
|
4
|
+
@rem Gradle startup script for Windows
|
5
|
+
@rem
|
6
|
+
@rem ##########################################################################
|
7
|
+
|
8
|
+
@rem Set local scope for the variables with windows NT shell
|
9
|
+
if "%OS%"=="Windows_NT" setlocal
|
10
|
+
|
11
|
+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
12
|
+
set DEFAULT_JVM_OPTS=
|
13
|
+
|
14
|
+
set DIRNAME=%~dp0
|
15
|
+
if "%DIRNAME%" == "" set DIRNAME=.
|
16
|
+
set APP_BASE_NAME=%~n0
|
17
|
+
set APP_HOME=%DIRNAME%
|
18
|
+
|
19
|
+
@rem Find java.exe
|
20
|
+
if defined JAVA_HOME goto findJavaFromJavaHome
|
21
|
+
|
22
|
+
set JAVA_EXE=java.exe
|
23
|
+
%JAVA_EXE% -version >NUL 2>&1
|
24
|
+
if "%ERRORLEVEL%" == "0" goto init
|
25
|
+
|
26
|
+
echo.
|
27
|
+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
28
|
+
echo.
|
29
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
30
|
+
echo location of your Java installation.
|
31
|
+
|
32
|
+
goto fail
|
33
|
+
|
34
|
+
:findJavaFromJavaHome
|
35
|
+
set JAVA_HOME=%JAVA_HOME:"=%
|
36
|
+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
37
|
+
|
38
|
+
if exist "%JAVA_EXE%" goto init
|
39
|
+
|
40
|
+
echo.
|
41
|
+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
42
|
+
echo.
|
43
|
+
echo Please set the JAVA_HOME variable in your environment to match the
|
44
|
+
echo location of your Java installation.
|
45
|
+
|
46
|
+
goto fail
|
47
|
+
|
48
|
+
:init
|
49
|
+
@rem Get command-line arguments, handling Windowz variants
|
50
|
+
|
51
|
+
if not "%OS%" == "Windows_NT" goto win9xME_args
|
52
|
+
if "%@eval[2+2]" == "4" goto 4NT_args
|
53
|
+
|
54
|
+
:win9xME_args
|
55
|
+
@rem Slurp the command line arguments.
|
56
|
+
set CMD_LINE_ARGS=
|
57
|
+
set _SKIP=2
|
58
|
+
|
59
|
+
:win9xME_args_slurp
|
60
|
+
if "x%~1" == "x" goto execute
|
61
|
+
|
62
|
+
set CMD_LINE_ARGS=%*
|
63
|
+
goto execute
|
64
|
+
|
65
|
+
:4NT_args
|
66
|
+
@rem Get arguments from the 4NT Shell from JP Software
|
67
|
+
set CMD_LINE_ARGS=%$
|
68
|
+
|
69
|
+
:execute
|
70
|
+
@rem Setup the command line
|
71
|
+
|
72
|
+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
73
|
+
|
74
|
+
@rem Execute Gradle
|
75
|
+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
76
|
+
|
77
|
+
:end
|
78
|
+
@rem End local scope for the variables with windows NT shell
|
79
|
+
if "%ERRORLEVEL%"=="0" goto mainEnd
|
80
|
+
|
81
|
+
:fail
|
82
|
+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
83
|
+
rem the _cmd.exe /c_ return code!
|
84
|
+
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
85
|
+
exit /b 1
|
86
|
+
|
87
|
+
:mainEnd
|
88
|
+
if "%OS%"=="Windows_NT" endlocal
|
89
|
+
|
90
|
+
:omega
|
data/settings.gradle
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rootProject.name = 'embulk-input-http'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
package org.embulk.input.http;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.annotation.JsonCreator;
|
4
|
+
import com.fasterxml.jackson.annotation.JsonProperty;
|
5
|
+
import com.google.common.base.Objects;
|
6
|
+
|
7
|
+
public class BasicAuthConfig {
|
8
|
+
|
9
|
+
private final String user;
|
10
|
+
private final String password;
|
11
|
+
|
12
|
+
@JsonCreator
|
13
|
+
public BasicAuthConfig(
|
14
|
+
@JsonProperty("user") String user,
|
15
|
+
@JsonProperty("password") String password) {
|
16
|
+
this.user = user;
|
17
|
+
this.password = password;
|
18
|
+
}
|
19
|
+
|
20
|
+
@JsonProperty("user")
|
21
|
+
public String getUser() {
|
22
|
+
return user;
|
23
|
+
}
|
24
|
+
|
25
|
+
@JsonProperty("password")
|
26
|
+
public String getPassword() {
|
27
|
+
return password;
|
28
|
+
}
|
29
|
+
|
30
|
+
@Override
|
31
|
+
public int hashCode() {
|
32
|
+
return Objects.hashCode(user, password);
|
33
|
+
}
|
34
|
+
|
35
|
+
@Override
|
36
|
+
public String toString() {
|
37
|
+
return String.format("BasicAuthConfig[%s, %s]", getUser(), getPassword());
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,333 @@
|
|
1
|
+
package org.embulk.input.http;
|
2
|
+
|
3
|
+
import com.google.common.base.Optional;
|
4
|
+
import com.google.common.base.Throwables;
|
5
|
+
import com.google.common.collect.Lists;
|
6
|
+
import org.apache.http.Header;
|
7
|
+
import org.apache.http.NameValuePair;
|
8
|
+
import org.apache.http.auth.AuthScope;
|
9
|
+
import org.apache.http.auth.UsernamePasswordCredentials;
|
10
|
+
import org.apache.http.client.CredentialsProvider;
|
11
|
+
import org.apache.http.client.HttpClient;
|
12
|
+
import org.apache.http.client.config.RequestConfig;
|
13
|
+
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
14
|
+
import org.apache.http.client.methods.HttpGet;
|
15
|
+
import org.apache.http.client.methods.HttpPost;
|
16
|
+
import org.apache.http.client.methods.HttpRequestBase;
|
17
|
+
import org.apache.http.client.utils.URIBuilder;
|
18
|
+
import org.apache.http.impl.client.BasicCredentialsProvider;
|
19
|
+
import org.apache.http.impl.client.HttpClientBuilder;
|
20
|
+
import org.apache.http.message.BasicHeader;
|
21
|
+
import org.apache.http.message.BasicNameValuePair;
|
22
|
+
import org.embulk.config.*;
|
23
|
+
import org.embulk.spi.BufferAllocator;
|
24
|
+
import org.embulk.spi.Exec;
|
25
|
+
import org.embulk.spi.FileInputPlugin;
|
26
|
+
import org.embulk.spi.TransactionalFileInput;
|
27
|
+
import org.embulk.spi.util.InputStreamFileInput;
|
28
|
+
import org.embulk.spi.util.RetryExecutor;
|
29
|
+
import org.slf4j.Logger;
|
30
|
+
|
31
|
+
import java.io.IOException;
|
32
|
+
import java.io.InputStream;
|
33
|
+
import java.io.UnsupportedEncodingException;
|
34
|
+
import java.net.URISyntaxException;
|
35
|
+
import java.util.ArrayList;
|
36
|
+
import java.util.List;
|
37
|
+
import java.util.Map;
|
38
|
+
|
39
|
+
public class HttpFileInputPlugin implements FileInputPlugin {
|
40
|
+
|
41
|
+
private final Logger logger = Exec.getLogger(getClass());
|
42
|
+
|
43
|
+
public interface PluginTask extends Task {
|
44
|
+
@Config("url")
|
45
|
+
public String getUrl();
|
46
|
+
|
47
|
+
@Config("charset")
|
48
|
+
@ConfigDefault("\"utf-8\"")
|
49
|
+
public String getCharset();
|
50
|
+
|
51
|
+
@Config("method")
|
52
|
+
@ConfigDefault("\"get\"")
|
53
|
+
public String getMethod();
|
54
|
+
|
55
|
+
@Config("user_agent")
|
56
|
+
@ConfigDefault("\"Embulk::Input::HttpFileInputPlugin\"")
|
57
|
+
public String getUserAgent();
|
58
|
+
|
59
|
+
@Config("open_timeout")
|
60
|
+
@ConfigDefault("2000")
|
61
|
+
public int getOpenTimeout();
|
62
|
+
|
63
|
+
@Config("read_timeout")
|
64
|
+
@ConfigDefault("10000")
|
65
|
+
public int getReadTimeout();
|
66
|
+
|
67
|
+
@Config("max_retries")
|
68
|
+
@ConfigDefault("5")
|
69
|
+
public int getMaxRetries();
|
70
|
+
|
71
|
+
@Config("retry_interval")
|
72
|
+
@ConfigDefault("10000")
|
73
|
+
public int getRetryInterval();
|
74
|
+
|
75
|
+
@Config("request_interval")
|
76
|
+
@ConfigDefault("0")
|
77
|
+
public int getRequestInterval();
|
78
|
+
|
79
|
+
public void setRequestInterval(int requestInterval);
|
80
|
+
|
81
|
+
@Config("interval_includes_response_time")
|
82
|
+
@ConfigDefault("null")
|
83
|
+
public boolean getIntervalIncludesResponseTime();
|
84
|
+
|
85
|
+
@Config("params")
|
86
|
+
@ConfigDefault("null")
|
87
|
+
public Optional<ParamsConfig> getParams();
|
88
|
+
|
89
|
+
@Config("basic_auth")
|
90
|
+
@ConfigDefault("null")
|
91
|
+
public Optional<BasicAuthConfig> getBasicAuth();
|
92
|
+
|
93
|
+
@Config("pager")
|
94
|
+
@ConfigDefault("null")
|
95
|
+
public Optional<PagerConfig> getPager();
|
96
|
+
|
97
|
+
@Config("request_headers")
|
98
|
+
@ConfigDefault("{}")
|
99
|
+
public Map<String, String> getRequestHeaders();
|
100
|
+
|
101
|
+
@ConfigInject
|
102
|
+
public BufferAllocator getBufferAllocator();
|
103
|
+
|
104
|
+
public List<List<QueryConfig.Query>> getQueries();
|
105
|
+
|
106
|
+
public void setQueries(List<List<QueryConfig.Query>> queries);
|
107
|
+
|
108
|
+
public HttpMethod getHttpMethod();
|
109
|
+
|
110
|
+
public void setHttpMethod(HttpMethod httpMethod);
|
111
|
+
}
|
112
|
+
|
113
|
+
public enum HttpMethod {
|
114
|
+
POST,
|
115
|
+
GET
|
116
|
+
}
|
117
|
+
|
118
|
+
@Override
|
119
|
+
public ConfigDiff transaction(ConfigSource config, FileInputPlugin.Control control) {
|
120
|
+
PluginTask task = config.loadConfig(PluginTask.class);
|
121
|
+
|
122
|
+
final int tasks;
|
123
|
+
if (task.getParams().isPresent()) {
|
124
|
+
List<List<QueryConfig.Query>> queries = task.getParams().get().generateQueries(task.getPager());
|
125
|
+
task.setQueries(queries);
|
126
|
+
tasks = queries.size();
|
127
|
+
} else if (task.getPager().isPresent()) {
|
128
|
+
List<List<QueryConfig.Query>> queries = task.getPager().get().expand();
|
129
|
+
task.setQueries(queries);
|
130
|
+
tasks = queries.size();
|
131
|
+
} else {
|
132
|
+
task.setQueries(Lists.<List<QueryConfig.Query>>newArrayList());
|
133
|
+
task.setRequestInterval(0);
|
134
|
+
tasks = 1;
|
135
|
+
}
|
136
|
+
|
137
|
+
task.setHttpMethod(HttpMethod.valueOf(task.getMethod().toUpperCase()));
|
138
|
+
|
139
|
+
return resume(task.dump(), tasks, control);
|
140
|
+
}
|
141
|
+
|
142
|
+
@Override
|
143
|
+
public ConfigDiff resume(TaskSource taskSource,
|
144
|
+
int taskCount,
|
145
|
+
FileInputPlugin.Control control) {
|
146
|
+
control.run(taskSource, taskCount);
|
147
|
+
return Exec.newConfigDiff();
|
148
|
+
}
|
149
|
+
|
150
|
+
@Override
|
151
|
+
public void cleanup(TaskSource taskSource, int taskCount, List<TaskReport> successTaskReports) {
|
152
|
+
}
|
153
|
+
|
154
|
+
@Override
|
155
|
+
public TransactionalFileInput open(TaskSource taskSource, int taskIndex) {
|
156
|
+
PluginTask task = taskSource.loadTask(PluginTask.class);
|
157
|
+
|
158
|
+
HttpRequestBase request;
|
159
|
+
try {
|
160
|
+
request = makeRequest(task, taskIndex);
|
161
|
+
} catch (URISyntaxException | UnsupportedEncodingException e) {
|
162
|
+
throw Throwables.propagate(e);
|
163
|
+
}
|
164
|
+
|
165
|
+
HttpClientBuilder builder = HttpClientBuilder.create()
|
166
|
+
.disableAutomaticRetries()
|
167
|
+
.setDefaultRequestConfig(makeRequestConfig(task))
|
168
|
+
.setDefaultHeaders(makeHeaders(task));
|
169
|
+
|
170
|
+
if (task.getBasicAuth().isPresent()) {
|
171
|
+
builder.setDefaultCredentialsProvider(makeCredentialsProvider(task.getBasicAuth().get(), request));
|
172
|
+
}
|
173
|
+
|
174
|
+
HttpClient client = builder.build();
|
175
|
+
|
176
|
+
logger.info(String.format("%s \"%s\"", task.getMethod().toUpperCase(),
|
177
|
+
request.getURI().toString()));
|
178
|
+
|
179
|
+
RetryableHandler retryable = new RetryableHandler(client, request);
|
180
|
+
long startTimeMills = System.currentTimeMillis();
|
181
|
+
try {
|
182
|
+
RetryExecutor.retryExecutor().
|
183
|
+
withRetryLimit(task.getMaxRetries()).
|
184
|
+
withInitialRetryWait(task.getRetryInterval()).
|
185
|
+
withMaxRetryWait(30 * 60 * 1000).
|
186
|
+
runInterruptible(retryable);
|
187
|
+
InputStream stream = retryable.getResponse().getEntity().getContent();
|
188
|
+
PluginFileInput input = new PluginFileInput(task, stream, startTimeMills);
|
189
|
+
stream = null;
|
190
|
+
return input;
|
191
|
+
} catch (Exception e) {
|
192
|
+
throw Throwables.propagate(e);
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
private CredentialsProvider makeCredentialsProvider(BasicAuthConfig config, HttpRequestBase scopeRequest) {
|
197
|
+
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
198
|
+
final AuthScope authScope = new AuthScope(scopeRequest.getURI().getHost(),
|
199
|
+
scopeRequest.getURI().getPort());
|
200
|
+
credentialsProvider.setCredentials(authScope,
|
201
|
+
new UsernamePasswordCredentials(config.getUser(), config.getPassword()));
|
202
|
+
return credentialsProvider;
|
203
|
+
}
|
204
|
+
|
205
|
+
private HttpRequestBase makeRequest(PluginTask task, int taskIndex)
|
206
|
+
throws URISyntaxException, UnsupportedEncodingException {
|
207
|
+
final List<QueryConfig.Query> queries = (task.getQueries().isEmpty()) ?
|
208
|
+
null : task.getQueries().get(taskIndex);
|
209
|
+
if (task.getHttpMethod() == HttpMethod.GET) {
|
210
|
+
HttpGet request = new HttpGet(task.getUrl());
|
211
|
+
if (queries != null) {
|
212
|
+
URIBuilder builder = new URIBuilder(request.getURI());
|
213
|
+
for (QueryConfig.Query q : queries) {
|
214
|
+
for (String v : q.getValues()) {
|
215
|
+
builder.addParameter(q.getName(), v);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
request.setURI(builder.build());
|
219
|
+
}
|
220
|
+
return request;
|
221
|
+
} else if (task.getHttpMethod() == HttpMethod.POST) {
|
222
|
+
HttpPost request = new HttpPost(task.getUrl());
|
223
|
+
if (queries != null) {
|
224
|
+
List<NameValuePair> pairs = new ArrayList<>();
|
225
|
+
for (QueryConfig.Query q : queries) {
|
226
|
+
for (String v : q.getValues()) {
|
227
|
+
pairs.add(new BasicNameValuePair(q.getName(), v));
|
228
|
+
}
|
229
|
+
}
|
230
|
+
request.setEntity(new UrlEncodedFormEntity(pairs));
|
231
|
+
}
|
232
|
+
return request;
|
233
|
+
}
|
234
|
+
throw new IllegalArgumentException(String.format("Unsupported http method %s", task.getMethod()));
|
235
|
+
}
|
236
|
+
|
237
|
+
private List<Header> makeHeaders(PluginTask task) {
|
238
|
+
List<Header> headers = new ArrayList<>();
|
239
|
+
headers.add(new BasicHeader("Accept", "*/*"));
|
240
|
+
headers.add(new BasicHeader("Accept-Charset", task.getCharset()));
|
241
|
+
headers.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
|
242
|
+
headers.add(new BasicHeader("Accept-Language", "en-us,en;q=0.5"));
|
243
|
+
headers.add(new BasicHeader("User-Agent", task.getUserAgent()));
|
244
|
+
for (Map.Entry<String, String> entry : task.getRequestHeaders().entrySet()) {
|
245
|
+
headers.add(new BasicHeader(entry.getKey(), entry.getValue()));
|
246
|
+
}
|
247
|
+
return headers;
|
248
|
+
}
|
249
|
+
|
250
|
+
private RequestConfig makeRequestConfig(PluginTask task) {
|
251
|
+
return RequestConfig.custom()
|
252
|
+
.setCircularRedirectsAllowed(true)
|
253
|
+
.setMaxRedirects(10)
|
254
|
+
.setRedirectsEnabled(true)
|
255
|
+
.setConnectTimeout(task.getOpenTimeout())
|
256
|
+
.setSocketTimeout(task.getReadTimeout())
|
257
|
+
.build();
|
258
|
+
}
|
259
|
+
|
260
|
+
public static class PluginFileInput extends InputStreamFileInput
|
261
|
+
implements TransactionalFileInput {
|
262
|
+
|
263
|
+
private final Logger logger = Exec.getLogger(getClass());
|
264
|
+
|
265
|
+
private final long startTimeMills;
|
266
|
+
private final PluginTask task;
|
267
|
+
|
268
|
+
public PluginFileInput(PluginTask task, InputStream stream, long startTimeMills) {
|
269
|
+
super(task.getBufferAllocator(), new SingleFileProvider(stream));
|
270
|
+
this.startTimeMills = startTimeMills;
|
271
|
+
this.task = task;
|
272
|
+
}
|
273
|
+
|
274
|
+
public TaskReport commit() {
|
275
|
+
return Exec.newTaskReport();
|
276
|
+
}
|
277
|
+
|
278
|
+
@Override
|
279
|
+
public void close() {
|
280
|
+
super.close();
|
281
|
+
handleInterval();
|
282
|
+
}
|
283
|
+
|
284
|
+
@Override
|
285
|
+
public void abort() {
|
286
|
+
}
|
287
|
+
|
288
|
+
protected void handleInterval() {
|
289
|
+
if (task.getRequestInterval() <= 0) {
|
290
|
+
return;
|
291
|
+
}
|
292
|
+
long interval = task.getRequestInterval();
|
293
|
+
if (task.getIntervalIncludesResponseTime()) {
|
294
|
+
interval = interval - (System.currentTimeMillis() - startTimeMills);
|
295
|
+
}
|
296
|
+
if (interval > 0) {
|
297
|
+
logger.info(String.format("waiting %d msec ...", interval));
|
298
|
+
try {
|
299
|
+
Thread.sleep(interval);
|
300
|
+
} catch (InterruptedException e) {
|
301
|
+
Throwables.propagate(e);
|
302
|
+
}
|
303
|
+
}
|
304
|
+
}
|
305
|
+
|
306
|
+
private static class SingleFileProvider
|
307
|
+
implements InputStreamFileInput.Provider {
|
308
|
+
|
309
|
+
private final InputStream stream;
|
310
|
+
private boolean opened = false;
|
311
|
+
|
312
|
+
public SingleFileProvider(InputStream stream) {
|
313
|
+
this.stream = stream;
|
314
|
+
}
|
315
|
+
|
316
|
+
@Override
|
317
|
+
public InputStream openNext() throws IOException {
|
318
|
+
if (opened) {
|
319
|
+
return null;
|
320
|
+
}
|
321
|
+
opened = true;
|
322
|
+
return stream;
|
323
|
+
}
|
324
|
+
|
325
|
+
@Override
|
326
|
+
public void close() throws IOException {
|
327
|
+
if (!opened) {
|
328
|
+
stream.close();
|
329
|
+
}
|
330
|
+
}
|
331
|
+
}
|
332
|
+
}
|
333
|
+
}
|