Changeset 11250 in josm for trunk/src


Ignore:
Timestamp:
2016-11-13T13:12:47+01:00 (8 years ago)
Author:
wiktorn
Message:

Add response debug printing on HTTP Status code >= 300

Closes: #13961

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r10420 r11250  
    66import java.io.BufferedOutputStream;
    77import java.io.BufferedReader;
     8import java.io.ByteArrayInputStream;
    89import java.io.IOException;
    910import java.io.InputStream;
     
    190191        private boolean uncompress;
    191192        private boolean uncompressAccordingToContentDisposition;
     193        private String responseData = null;
    192194
    193195        private Response(HttpURLConnection connection, ProgressMonitor monitor) throws IOException {
     
    198200            this.responseCode = connection.getResponseCode();
    199201            this.responseMessage = connection.getResponseMessage();
     202            if (this.responseCode >= 300) {
     203                String contentType = getContentType();
     204                if (contentType == null || (
     205                        contentType.contains("text") ||
     206                        contentType.contains("html") ||
     207                        contentType.contains("xml"))
     208                        ) {
     209                    String content = this.fetchContent();
     210                    if (content == null || content.isEmpty()) {
     211                        Main.debug("Server did not return any body");
     212                    } else {
     213                        Main.debug("Response body: ");
     214                        Main.debug(this.fetchContent());
     215                    }
     216                } else {
     217                    Main.debug("Server returned content: {0} of length: {1}. Not printing.", contentType, this.getContentLength());
     218                }
     219            }
    200220        }
    201221
     
    264284                Main.debug(ioe);
    265285                in = connection.getErrorStream();
     286                if (in == null) {
     287                    in = new ByteArrayInputStream(new byte[]{});
     288                }
    266289            }
    267290            if (in != null) {
     
    306329         * @throws IOException if any I/O error occurs
    307330         */
    308         @SuppressWarnings("resource")
    309         public String fetchContent() throws IOException {
    310             try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) {
    311                 return scanner.hasNext() ? scanner.next() : "";
    312             }
     331        public synchronized String fetchContent() throws IOException {
     332            if (responseData == null) {
     333                try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) { // \A - beginning of input
     334                    responseData = scanner.hasNext() ? scanner.next() : "";
     335                }
     336            }
     337            return responseData;
    313338        }
    314339
Note: See TracChangeset for help on using the changeset viewer.