|
| 1 | +/** |
| 2 | + * Copyright (c) 2018-2019, Mihai Emil Andronache |
| 3 | + * All rights reserved. |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are met: |
| 6 | + * 1)Redistributions of source code must retain the above copyright notice, |
| 7 | + * this list of conditions and the following disclaimer. |
| 8 | + * 2)Redistributions in binary form must reproduce the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer in the documentation |
| 10 | + * and/or other materials provided with the distribution. |
| 11 | + * 3)Neither the name of docker-java-api nor the names of its |
| 12 | + * contributors may be used to endorse or promote products derived from |
| 13 | + * this software without specific prior written permission. |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | + * POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | +package com.amihaiemil.docker; |
| 27 | + |
| 28 | +import org.apache.http.HttpEntity; |
| 29 | +import org.apache.http.HttpResponse; |
| 30 | +import org.apache.http.client.ResponseHandler; |
| 31 | +import org.apache.http.entity.ContentType; |
| 32 | +import org.apache.http.protocol.HTTP; |
| 33 | +import org.apache.http.util.Args; |
| 34 | +import org.apache.http.util.CharArrayBuffer; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.InputStream; |
| 38 | +import java.io.InputStreamReader; |
| 39 | +import java.io.Reader; |
| 40 | +import java.nio.charset.Charset; |
| 41 | + |
| 42 | +/** |
| 43 | + * Handler that returns the response content as a String. |
| 44 | + * @author Morozov Evgeniy ([email protected]) |
| 45 | + * @version $Id$ |
| 46 | + * @since 0.0.2 |
| 47 | + */ |
| 48 | +final class ReadLogString implements ResponseHandler<String> { |
| 49 | + |
| 50 | + /** |
| 51 | + * Handlers to be executed before actually reading the array. |
| 52 | + */ |
| 53 | + private final ResponseHandler<HttpResponse> other; |
| 54 | + |
| 55 | + /** |
| 56 | + * Ctor. |
| 57 | + * @param other Handlers to be executed before actually reading the array. |
| 58 | + */ |
| 59 | + ReadLogString(final ResponseHandler<HttpResponse> other) { |
| 60 | + this.other = other; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String handleResponse(final HttpResponse httpResponse) |
| 65 | + throws IOException { |
| 66 | + final HttpResponse resp = this.other.handleResponse(httpResponse); |
| 67 | + return this.toString(resp.getEntity()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Docker logs contains header |
| 72 | + * [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4} |
| 73 | + * STREAM_TYPE |
| 74 | + * 0: stdin (is written on stdout) |
| 75 | + * 1: stdout |
| 76 | + * 2: stderr |
| 77 | + * |
| 78 | + * SIZE1, SIZE2, SIZE3, SIZE4 are the four bytes of the uint32 size |
| 79 | + * encoded as big endian. |
| 80 | + * |
| 81 | + * This method do: |
| 82 | + * |
| 83 | + * 1) Read 8 bytes. |
| 84 | + * 2) Choose stdout or stderr depending on the first byte. |
| 85 | + * 3) Extract the frame size from the last four bytes. |
| 86 | + * 4) Read the extracted size and output it on the correct output. |
| 87 | + * 5) Goto 1. |
| 88 | + * |
| 89 | + * @param entity HttpEntity for read message. |
| 90 | + * @return Logs from container in String. |
| 91 | + * @throws IOException if the entity cannot be read |
| 92 | + */ |
| 93 | + private String toString(final HttpEntity entity) throws IOException { |
| 94 | + final InputStream instream = entity.getContent(); |
| 95 | + final CharArrayBuffer buffer = new CharArrayBuffer( |
| 96 | + this.getCapacity(entity) |
| 97 | + ); |
| 98 | + if (instream != null) { |
| 99 | + try { |
| 100 | + final Reader reader = new InputStreamReader( |
| 101 | + instream, |
| 102 | + this.getCharset(ContentType.get(entity)) |
| 103 | + ); |
| 104 | + this.read(buffer, reader); |
| 105 | + } finally { |
| 106 | + instream.close(); |
| 107 | + } |
| 108 | + } |
| 109 | + return buffer.toString(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Docker logs contains header |
| 114 | + * [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4} |
| 115 | + * STREAM_TYPE |
| 116 | + * 0: stdin (is written on stdout) |
| 117 | + * 1: stdout |
| 118 | + * 2: stderr |
| 119 | + * |
| 120 | + * SIZE1, SIZE2, SIZE3, SIZE4 are the four bytes of the uint32 size |
| 121 | + * encoded as big endian. |
| 122 | + * |
| 123 | + * 1) Read 8 bytes from reader. |
| 124 | + * 2) Choose not stdin(0) depending on the first byte. |
| 125 | + * 3) Extract the frame size from the last four bytes. |
| 126 | + * 4) Read the extracted size from reader and save it in buffer in circle. |
| 127 | + * 5) Goto 1. |
| 128 | + * |
| 129 | + * @param buffer Buffer for save message. |
| 130 | + * @param reader Reader for read message. |
| 131 | + * @throws IOException if the entity cannot be read |
| 132 | + */ |
| 133 | + private void read(final CharArrayBuffer buffer, |
| 134 | + final Reader reader) throws IOException { |
| 135 | + char[] controlChars = new char[8]; |
| 136 | + int len; |
| 137 | + while (reader.read(controlChars) != -1) { |
| 138 | + if (controlChars[0] != 0) { |
| 139 | + long byteInLine = this.getUInt(controlChars); |
| 140 | + char[] stdout; |
| 141 | + if (byteInLine > 1024) { |
| 142 | + stdout = new char[1024]; |
| 143 | + } else { |
| 144 | + stdout = new char[(int) byteInLine]; |
| 145 | + } |
| 146 | + while (byteInLine > 0) { |
| 147 | + len = reader.read(stdout); |
| 148 | + byteInLine -= len; |
| 149 | + if (len != -1) { |
| 150 | + buffer.append(stdout, 0, len); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Check that content length less then Integer.MAX_VALUE. |
| 159 | + * Try to get content length from entity |
| 160 | + * If length less then zero return 4096 |
| 161 | + * |
| 162 | + * @param entity HttpEntity for get capacity. |
| 163 | + * @return Capacity. |
| 164 | + */ |
| 165 | + private int getCapacity(final HttpEntity entity) { |
| 166 | + Args.check(entity.getContentLength() <= Integer.MAX_VALUE, |
| 167 | + "HTTP entity too large to be buffered in memory"); |
| 168 | + int capacity = (int) entity.getContentLength(); |
| 169 | + if (capacity < 0) { |
| 170 | + capacity = 4096; |
| 171 | + } |
| 172 | + return capacity; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Try to get charset from content type. |
| 177 | + * If charset not set, try get default charset by mime type |
| 178 | + * If not set return ISO-8859-1 |
| 179 | + * |
| 180 | + * @param contentType Content type. |
| 181 | + * @return Charset. |
| 182 | + */ |
| 183 | + private Charset getCharset(final ContentType contentType) { |
| 184 | + Charset charset = null; |
| 185 | + if (contentType != null) { |
| 186 | + charset = contentType.getCharset(); |
| 187 | + if (charset == null) { |
| 188 | + ContentType defaultContentType = |
| 189 | + ContentType.getByMimeType(contentType.getMimeType()); |
| 190 | + if (defaultContentType != null) { |
| 191 | + charset = defaultContentType.getCharset(); |
| 192 | + } else { |
| 193 | + charset = null; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (charset == null) { |
| 199 | + charset = HTTP.DEF_CONTENT_CHARSET; |
| 200 | + } |
| 201 | + return charset; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Convert byte to long. |
| 206 | + * |
| 207 | + * @param byteForConvert Byte for convert. |
| 208 | + * @return Long Converted byte. |
| 209 | + */ |
| 210 | + private long byteAsULong(final char byteForConvert) { |
| 211 | + return ((long) byteForConvert) & 0x00000000000000FFL; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Convert byte from control byte to uint32. |
| 216 | + * |
| 217 | + * @param bytes Array of byte. |
| 218 | + * @return Long uint32. |
| 219 | + */ |
| 220 | + private long getUInt(final char[] bytes) { |
| 221 | + return this.byteAsULong(bytes[7]) |
| 222 | + | (this.byteAsULong(bytes[6]) << 8) |
| 223 | + | (this.byteAsULong(bytes[5]) << 16) |
| 224 | + | (this.byteAsULong(bytes[4]) << 24); |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | +} |
0 commit comments