-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from yvasyliev/dev
Dev
- Loading branch information
Showing
8 changed files
with
169 additions
and
33 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
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
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
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
116 changes: 116 additions & 0 deletions
116
src/main/java/api/longpoll/bots/http/impl/DefaultHttpClient.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,116 @@ | ||
package api.longpoll.bots.http.impl; | ||
|
||
import api.longpoll.bots.http.HttpClient; | ||
import api.longpoll.bots.http.HttpRequest; | ||
import api.longpoll.bots.http.HttpResponse; | ||
import api.longpoll.bots.http.MultipartFormData; | ||
|
||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Default implementation of HTTP client. | ||
*/ | ||
public class DefaultHttpClient implements HttpClient { | ||
/** | ||
* New line. | ||
*/ | ||
private static final String CRLF = "\r\n"; | ||
|
||
@Override | ||
public HttpResponse execute(HttpRequest httpRequest) throws IOException { | ||
HttpURLConnection httpURLConnection = null; | ||
try { | ||
httpURLConnection = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); | ||
httpURLConnection.setRequestMethod(httpRequest.getRequestMethod()); | ||
httpURLConnection.setDoOutput(true); | ||
|
||
if (httpRequest.hasParams()) { | ||
try (DataOutputStream output = new DataOutputStream(httpURLConnection.getOutputStream())) { | ||
output.writeBytes(toUrlParams(httpRequest.getParams())); | ||
output.flush(); | ||
} | ||
} else if (httpRequest.hasMultipartFormData()) { | ||
MultipartFormData multipartFormData = httpRequest.getMultipartFormData(); | ||
String boundary = UUID.randomUUID().toString(); | ||
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); | ||
|
||
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), StandardCharsets.UTF_8), true)) { | ||
writer.append("--").append(boundary).append(CRLF); | ||
writer.append("Content-Disposition: form-data; name=\"").append(multipartFormData.getKey()).append("\"; filename=\"").append(multipartFormData.getFilename()).append("\"").append(CRLF); | ||
writer.append("Content-Type: ").append(HttpURLConnection.guessContentTypeFromName(multipartFormData.getFilename())).append(CRLF); | ||
writer.append("Content-Transfer-Encoding: binary").append(CRLF); | ||
writer.append(CRLF); | ||
writer.flush(); | ||
|
||
copy(multipartFormData.getInputStream(), httpURLConnection.getOutputStream()); | ||
|
||
writer.append(CRLF); | ||
writer.append("--").append(boundary).append("--").append(CRLF); | ||
writer.flush(); | ||
} | ||
} | ||
|
||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) { | ||
return new HttpResponse( | ||
httpURLConnection.getResponseCode(), | ||
reader.lines().collect(Collectors.joining()) | ||
); | ||
} | ||
} finally { | ||
if (httpURLConnection != null) { | ||
httpURLConnection.disconnect(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Converts params Map to URL params. | ||
* | ||
* @param params params Map. | ||
* @return URL params. | ||
* @throws UnsupportedEncodingException when failed to encode params. | ||
*/ | ||
private String toUrlParams(Map<String, String> params) throws UnsupportedEncodingException { | ||
List<String> paramsList = new ArrayList<>(params.size()); | ||
for (Map.Entry<String, String> entry : params.entrySet()) { | ||
paramsList.add(encode(entry.getKey()) + "=" + encode(entry.getValue())); | ||
} | ||
return String.join("&", paramsList); | ||
} | ||
|
||
/** | ||
* Encodes string value with UTF-8 encoding. | ||
* | ||
* @param s string to encode. | ||
* @return encoded string. | ||
* @throws UnsupportedEncodingException when failed to encode. | ||
*/ | ||
private String encode(String s) throws UnsupportedEncodingException { | ||
return URLEncoder.encode(s, "UTF-8"); | ||
} | ||
|
||
/** | ||
* Reads data from {@link InputStream} and writes to {@link OutputStream}. | ||
* | ||
* @param from source. | ||
* @param to target. | ||
* @throws IOException when errors occur. | ||
*/ | ||
private void copy(InputStream from, OutputStream to) throws IOException { | ||
byte[] buffer = new byte[4096]; | ||
int bytesRead; | ||
while ((bytesRead = from.read(buffer)) != -1) { | ||
to.write(buffer, 0, bytesRead); | ||
} | ||
to.flush(); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="stdout" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="ALL"> | ||
<AppenderRef ref="stdout"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |