Search in sources :

Example 26 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-client by apache.

the class CachingExec method cacheAndReturnResponse.

ClassicHttpResponse cacheAndReturnResponse(final HttpHost target, final HttpRequest request, final ClassicHttpResponse backendResponse, final ExecChain.Scope scope, final Instant requestSent, final Instant responseReceived) throws IOException {
    LOG.debug("Caching backend response");
    final ByteArrayBuffer buf;
    final HttpEntity entity = backendResponse.getEntity();
    if (entity != null) {
        buf = new ByteArrayBuffer(1024);
        final InputStream inStream = entity.getContent();
        final byte[] tmp = new byte[2048];
        long total = 0;
        int l;
        while ((l = inStream.read(tmp)) != -1) {
            buf.append(tmp, 0, l);
            total += l;
            if (total > cacheConfig.getMaxObjectSize()) {
                LOG.debug("Backend response content length exceeds maximum");
                backendResponse.setEntity(new CombinedEntity(entity, buf));
                return backendResponse;
            }
        }
    } else {
        buf = null;
    }
    backendResponse.close();
    final HttpCacheEntry cacheEntry;
    if (cacheConfig.isFreshnessCheckEnabled()) {
        final HttpCacheEntry existingEntry = responseCache.getCacheEntry(target, request);
        if (DateSupport.isAfter(existingEntry, backendResponse, HttpHeaders.DATE)) {
            LOG.debug("Backend already contains fresher cache entry");
            cacheEntry = existingEntry;
        } else {
            cacheEntry = responseCache.createCacheEntry(target, request, backendResponse, buf, requestSent, responseReceived);
            LOG.debug("Backend response successfully cached");
        }
    } else {
        cacheEntry = responseCache.createCacheEntry(target, request, backendResponse, buf, requestSent, responseReceived);
        LOG.debug("Backend response successfully cached (freshness check skipped)");
    }
    return convert(responseGenerator.generateResponse(request, cacheEntry), scope);
}
Also used : HttpCacheEntry(org.apache.hc.client5.http.cache.HttpCacheEntry) HttpEntity(org.apache.hc.core5.http.HttpEntity) InputStream(java.io.InputStream) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer)

Example 27 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-client by apache.

the class BasicHttpCache method createCacheEntry.

@Override
public HttpCacheEntry createCacheEntry(final HttpHost host, final HttpRequest request, final HttpResponse originResponse, final ByteArrayBuffer content, final Instant requestSent, final Instant responseReceived) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create cache entry: {}; {}", host, new RequestLine(request));
    }
    final String cacheKey = cacheKeyGenerator.generateKey(host, request);
    try {
        final HttpCacheEntry entry = cacheUpdateHandler.createCacheEntry(request, originResponse, content, requestSent, responseReceived);
        storeInCache(cacheKey, host, request, entry);
        return entry;
    } catch (final ResourceIOException ex) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("I/O error creating cache entry with key {}", cacheKey);
        }
        return new HttpCacheEntry(requestSent, responseReceived, originResponse.getCode(), originResponse.getHeaders(), content != null ? HeapResourceFactory.INSTANCE.generate(null, content.array(), 0, content.length()) : null);
    }
}
Also used : RequestLine(org.apache.hc.core5.http.message.RequestLine) HttpCacheEntry(org.apache.hc.client5.http.cache.HttpCacheEntry) ResourceIOException(org.apache.hc.client5.http.cache.ResourceIOException)

Example 28 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-client by apache.

the class FileResource method get.

@Override
public byte[] get() throws ResourceIOException {
    final File file = this.fileRef.get();
    if (file == null) {
        throw new ResourceIOException("Resource already disposed");
    }
    try (final InputStream in = new FileInputStream(file)) {
        final ByteArrayBuffer buf = new ByteArrayBuffer(1024);
        final byte[] tmp = new byte[2048];
        int len;
        while ((len = in.read(tmp)) != -1) {
            buf.append(tmp, 0, len);
        }
        return buf.toByteArray();
    } catch (final IOException ex) {
        throw new ResourceIOException(ex.getMessage(), ex);
    }
}
Also used : ResourceIOException(org.apache.hc.client5.http.cache.ResourceIOException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResourceIOException(org.apache.hc.client5.http.cache.ResourceIOException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer)

Example 29 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testHuffmanEncoding.

@Test
public void testHuffmanEncoding() throws Exception {
    final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
    HPackEncoder.encodeHuffman(buffer, createByteBuffer("www.example.com", StandardCharsets.US_ASCII));
    final ByteBuffer expected = createByteBuffer(0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff);
    Assertions.assertArrayEquals(toArray(expected), buffer.toByteArray());
}
Also used : ByteBuffer(java.nio.ByteBuffer) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 30 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testResponseEncodingWithoutHuffmanRFC7541Examples.

@Test
public void testResponseEncodingWithoutHuffmanRFC7541Examples() throws Exception {
    final OutboundDynamicTable dynamicTable = new OutboundDynamicTable();
    dynamicTable.setMaxSize(256);
    final HPackEncoder encoder = new HPackEncoder(dynamicTable, StandardCharsets.US_ASCII);
    final ByteArrayBuffer buf = new ByteArrayBuffer(256);
    final List<Header> headers1 = Arrays.asList(new BasicHeader(":status", "302"), new BasicHeader("cache-control", "private"), new BasicHeader("date", "Mon, 21 Oct 2013 20:13:21 GMT"), new BasicHeader("location", "https://www.example.com"));
    encoder.encodeHeaders(buf, headers1, false, false);
    final byte[] expected1 = createByteArray(0x48, 0x03, 0x33, 0x30, 0x32, 0x58, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x31, 0x20, 0x47, 0x4d, 0x54, 0x6e, 0x17, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d);
    Assertions.assertArrayEquals(expected1, buf.toByteArray());
    Assertions.assertEquals(4, dynamicTable.dynamicLength());
    assertHeaderEquals(new BasicHeader("location", "https://www.example.com"), dynamicTable.getDynamicEntry(0));
    assertHeaderEquals(new BasicHeader("date", "Mon, 21 Oct 2013 20:13:21 GMT"), dynamicTable.getDynamicEntry(1));
    assertHeaderEquals(new BasicHeader("cache-control", "private"), dynamicTable.getDynamicEntry(2));
    assertHeaderEquals(new BasicHeader(":status", "302"), dynamicTable.getDynamicEntry(3));
    Assertions.assertEquals(222, dynamicTable.getCurrentSize());
    final List<Header> headers2 = Arrays.asList(new BasicHeader(":status", "307"), new BasicHeader("cache-control", "private"), new BasicHeader("date", "Mon, 21 Oct 2013 20:13:21 GMT"), new BasicHeader("location", "https://www.example.com"));
    buf.clear();
    encoder.encodeHeaders(buf, headers2, false, false);
    final byte[] expected2 = createByteArray(0x48, 0x03, 0x33, 0x30, 0x37, 0xc1, 0xc0, 0xbf);
    Assertions.assertArrayEquals(expected2, buf.toByteArray());
    Assertions.assertEquals(4, dynamicTable.dynamicLength());
    assertHeaderEquals(new BasicHeader(":status", "307"), dynamicTable.getDynamicEntry(0));
    assertHeaderEquals(new BasicHeader("location", "https://www.example.com"), dynamicTable.getDynamicEntry(1));
    assertHeaderEquals(new BasicHeader("date", "Mon, 21 Oct 2013 20:13:21 GMT"), dynamicTable.getDynamicEntry(2));
    assertHeaderEquals(new BasicHeader("cache-control", "private"), dynamicTable.getDynamicEntry(3));
    Assertions.assertEquals(222, dynamicTable.getCurrentSize());
    final List<Header> headers3 = Arrays.asList(new BasicHeader(":status", "200"), new BasicHeader("cache-control", "private"), new BasicHeader("date", "Mon, 21 Oct 2013 20:13:22 GMT"), new BasicHeader("location", "https://www.example.com"), new BasicHeader("content-encoding", "gzip"), new BasicHeader("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"));
    buf.clear();
    encoder.encodeHeaders(buf, headers3, false, false);
    final byte[] expected3 = createByteArray(0x88, 0xc1, 0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x32, 0x20, 0x47, 0x4d, 0x54, 0xc0, 0x5a, 0x04, 0x67, 0x7a, 0x69, 0x70, 0x77, 0x38, 0x66, 0x6f, 0x6f, 0x3d, 0x41, 0x53, 0x44, 0x4a, 0x4b, 0x48, 0x51, 0x4b, 0x42, 0x5a, 0x58, 0x4f, 0x51, 0x57, 0x45, 0x4f, 0x50, 0x49, 0x55, 0x41, 0x58, 0x51, 0x57, 0x45, 0x4f, 0x49, 0x55, 0x3b, 0x20, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65, 0x3d, 0x33, 0x36, 0x30, 0x30, 0x3b, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31);
    Assertions.assertArrayEquals(expected3, buf.toByteArray());
    Assertions.assertEquals(3, dynamicTable.dynamicLength());
    assertHeaderEquals(new BasicHeader("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"), dynamicTable.getDynamicEntry(0));
    assertHeaderEquals(new BasicHeader("content-encoding", "gzip"), dynamicTable.getDynamicEntry(1));
    assertHeaderEquals(new BasicHeader("date", "Mon, 21 Oct 2013 20:13:22 GMT"), dynamicTable.getDynamicEntry(2));
    Assertions.assertEquals(215, dynamicTable.getCurrentSize());
}
Also used : BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayBuffer (org.apache.hc.core5.util.ByteArrayBuffer)37 Test (org.junit.jupiter.api.Test)26 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)11 Header (org.apache.hc.core5.http.Header)10 ByteBuffer (java.nio.ByteBuffer)9 InputStream (java.io.InputStream)4 HttpCacheEntry (org.apache.hc.client5.http.cache.HttpCacheEntry)4 Charset (java.nio.charset.Charset)2 ResourceIOException (org.apache.hc.client5.http.cache.ResourceIOException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)2 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1