diff --git a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java index 726045da1d12..8a2e7f41a7d2 100644 --- a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java +++ b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java @@ -72,40 +72,31 @@ public final class HttpRequests { private HttpRequests() { } public interface Request { - @NotNull - String getURL(); + @NotNull String getURL(); - @NotNull - URLConnection getConnection() throws IOException; + @NotNull URLConnection getConnection() throws IOException; - @NotNull - InputStream getInputStream() throws IOException; + @NotNull InputStream getInputStream() throws IOException; - @NotNull - BufferedReader getReader() throws IOException; + @NotNull BufferedReader getReader() throws IOException; - @NotNull - BufferedReader getReader(@Nullable ProgressIndicator indicator) throws IOException; + @NotNull BufferedReader getReader(@Nullable ProgressIndicator indicator) throws IOException; /** @deprecated Called automatically on open connection. Use {@link RequestBuilder#tryConnect()} to get response code */ @Deprecated boolean isSuccessful() throws IOException; - @NotNull - File saveToFile(@NotNull File file, @Nullable ProgressIndicator indicator) throws IOException; + @NotNull File saveToFile(@NotNull File file, @Nullable ProgressIndicator indicator) throws IOException; byte @NotNull [] readBytes(@Nullable ProgressIndicator indicator) throws IOException; - @NotNull - String readString(@Nullable ProgressIndicator indicator) throws IOException; + @NotNull String readString(@Nullable ProgressIndicator indicator) throws IOException; - @NotNull - default String readString() throws IOException { + default @NotNull String readString() throws IOException { return readString(null); } - @NotNull - CharSequence readChars(@Nullable ProgressIndicator indicator) throws IOException; + @NotNull CharSequence readChars(@Nullable ProgressIndicator indicator) throws IOException; default void write(@NotNull String data) throws IOException { write(data.getBytes(StandardCharsets.UTF_8)); @@ -142,8 +133,7 @@ public final class HttpRequests { return myStatusCode; } - @NotNull - public String getUrl() { + public @NotNull String getUrl() { return myUrl; } @@ -153,37 +143,30 @@ public final class HttpRequests { } } - @NotNull public static RequestBuilder request(@NotNull Url url) { return request(url.toExternalForm()); } - @NotNull public static RequestBuilder request(@NotNull String url) { return new RequestBuilderImpl(url, null); } - @NotNull public static RequestBuilder head(@NotNull String url) { return new RequestBuilderImpl(url, connection -> ((HttpURLConnection)connection).setRequestMethod("HEAD")); } - @NotNull public static RequestBuilder delete(@NotNull String url) { return new RequestBuilderImpl(url, connection -> ((HttpURLConnection)connection).setRequestMethod("DELETE")); } - @NotNull public static RequestBuilder delete(@NotNull String url, @Nullable String contentType) { return requestWithBody(url, "DELETE", contentType, null); } - @NotNull public static RequestBuilder post(@NotNull String url, @Nullable String contentType) { return requestWithBody(url, "POST", contentType, null); } - @NotNull public static RequestBuilder put(@NotNull String url, @Nullable String contentType) { return requestWithBody(url, "PUT", contentType, null); } @@ -194,36 +177,25 @@ public final class HttpRequests { *
* TODO: either fiddle with reflection or patch JDK to avoid server reliance */ - @NotNull public static RequestBuilder patch(@NotNull String url, @Nullable String contentType) { - return requestWithBody(url, "POST", contentType, - connection -> connection.setRequestProperty("X-HTTP-Method-Override", "PATCH")); + return requestWithBody(url, "POST", contentType, connection -> connection.setRequestProperty("X-HTTP-Method-Override", "PATCH")); } - @NotNull - private static RequestBuilder requestWithBody(@NotNull String url, - @NotNull String requestMethod, - @Nullable String contentType, - @Nullable ConnectionTuner tuner) { + public static RequestBuilder requestWithRange(@NotNull String url, @NotNull String bytes) { + return requestWithBody(url, "GET", null, connection -> connection.setRequestProperty("Range", "bytes=" + bytes)); + } + + private static RequestBuilder requestWithBody(String url, String requestMethod, @Nullable String contentType, @Nullable ConnectionTuner tuner) { return new RequestBuilderImpl(url, rawConnection -> { HttpURLConnection connection = (HttpURLConnection)rawConnection; connection.setRequestMethod(requestMethod); connection.setDoOutput(true); - if (contentType != null) { - connection.setRequestProperty("Content-Type", contentType); - } + if (contentType != null) connection.setRequestProperty("Content-Type", contentType); if (tuner != null) tuner.tune(connection); }); } - public static RequestBuilder requestWithRange(@NotNull String url, - @NotNull String bytes){ - return requestWithBody(url, "GET", null, - connection -> connection.setRequestProperty("Range", "bytes="+bytes)); - } - - @NotNull - public static String createErrorMessage(@NotNull IOException e, @NotNull Request request, boolean includeHeaders) { + public static @NotNull String createErrorMessage(@NotNull IOException e, @NotNull Request request, boolean includeHeaders) { StringBuilder builder = new StringBuilder(); builder.append("Cannot download '").append(request.getURL()).append("': ").append(e.getMessage()); @@ -343,9 +315,8 @@ public final class HttpRequests { return this; } - @NotNull @Override - public RequestBuilder throwStatusCodeException(boolean shouldThrow) { + public @NotNull RequestBuilder throwStatusCodeException(boolean shouldThrow) { myThrowStatusCodeException = shouldThrow; return this; } @@ -368,24 +339,21 @@ public final class HttpRequests { myUrl = myBuilder.myUrl; } - @NotNull @Override - public String getURL() { + public @NotNull String getURL() { return myUrl; } - @NotNull @Override - public URLConnection getConnection() throws IOException { + public @NotNull URLConnection getConnection() throws IOException { if (myConnection == null) { myConnection = openConnection(myBuilder, this); } return myConnection; } - @NotNull @Override - public InputStream getInputStream() throws IOException { + public @NotNull InputStream getInputStream() throws IOException { if (myInputStream == null) { URLConnection connection = getConnection(); myInputStream = unzipStreamIfNeeded(connection, connection.getInputStream()); @@ -404,23 +372,20 @@ public final class HttpRequests { return unzipStreamIfNeeded(connection, errorStream); } - @NotNull - private InputStream unzipStreamIfNeeded(@NotNull URLConnection connection, @NotNull InputStream stream) throws IOException { + private @NotNull InputStream unzipStreamIfNeeded(@NotNull URLConnection connection, @NotNull InputStream stream) throws IOException { if (myBuilder.myGzip && "gzip".equalsIgnoreCase(connection.getContentEncoding())) { return CountingGZIPInputStream.create(stream); } return stream; } - @NotNull @Override - public BufferedReader getReader() throws IOException { + public @NotNull BufferedReader getReader() throws IOException { return getReader(null); } - @NotNull @Override - public BufferedReader getReader(@Nullable ProgressIndicator indicator) throws IOException { + public @NotNull BufferedReader getReader(@Nullable ProgressIndicator indicator) throws IOException { if (myReader == null) { InputStream inputStream = getInputStream(); if (indicator != null) { @@ -434,8 +399,7 @@ public final class HttpRequests { return myReader; } - @NotNull - private Charset getCharset() throws IOException { + private @NotNull Charset getCharset() throws IOException { return HttpUrlConnectionUtil.getCharset(getConnection()); } @@ -450,20 +414,17 @@ public final class HttpRequests { return doReadBytes(indicator).toByteArray(); } - @NotNull - private BufferExposingByteArrayOutputStream doReadBytes(@Nullable ProgressIndicator indicator) throws IOException { + private @NotNull BufferExposingByteArrayOutputStream doReadBytes(@Nullable ProgressIndicator indicator) throws IOException { return HttpUrlConnectionUtil.readBytes(getInputStream(), getConnection(), indicator); } - @NotNull @Override - public String readString(@Nullable ProgressIndicator indicator) throws IOException { + public @NotNull String readString(@Nullable ProgressIndicator indicator) throws IOException { return HttpUrlConnectionUtil.readString(getInputStream(), getConnection(), indicator); } - @NotNull @Override - public CharSequence readChars(@Nullable ProgressIndicator indicator) throws IOException { + public @NotNull CharSequence readChars(@Nullable ProgressIndicator indicator) throws IOException { BufferExposingByteArrayOutputStream byteStream = doReadBytes(indicator); if (byteStream.size() == 0) { return Strings.EMPTY_CHAR_SEQUENCE; @@ -474,8 +435,7 @@ public final class HttpRequests { } @Override - @NotNull - public File saveToFile(@NotNull File file, @Nullable ProgressIndicator indicator) throws IOException { + public @NotNull File saveToFile(@NotNull File file, @Nullable ProgressIndicator indicator) throws IOException { FileUtilRt.createParentDirs(file); boolean deleteFile = true; @@ -707,4 +667,4 @@ public final class HttpRequests { } } } -} \ No newline at end of file +}