001/**
002 * Copyright (C) 2006-2021 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.junit.http.internal.impl;
017
018import static java.util.Collections.emptyList;
019import static java.util.stream.Collectors.toMap;
020
021import java.io.ByteArrayInputStream;
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.nio.charset.StandardCharsets;
026import java.util.List;
027import java.util.Map;
028import java.util.zip.GZIPInputStream;
029
030import org.talend.sdk.component.junit.http.api.HttpApiHandler;
031import org.talend.sdk.component.junit.http.api.Response;
032
033import io.netty.handler.codec.http.FullHttpRequest;
034
035import lombok.extern.slf4j.Slf4j;
036
037@Slf4j
038public class DefaultResponseLocatorCapturingHandler extends PassthroughHandler {
039
040    public DefaultResponseLocatorCapturingHandler(final HttpApiHandler api) {
041        super(api);
042    }
043
044    @Override
045    protected void beforeResponse(final String requestUri, final FullHttpRequest request, final Response resp,
046            final Map<String, List<String>> responseHeaderFields) {
047        final DefaultResponseLocator.RequestModel requestModel = new DefaultResponseLocator.RequestModel();
048        requestModel.setMethod(request.method().name().toString());
049        requestModel.setUri(requestUri);
050        requestModel
051                .setHeaders(filterHeaders(
052                        request.headers().entries().stream().collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));
053        final DefaultResponseLocator.Model model = new DefaultResponseLocator.Model();
054        model.setRequest(requestModel);
055
056        final DefaultResponseLocator.ResponseModel responseModel = new DefaultResponseLocator.ResponseModel();
057        responseModel.setStatus(resp.status());
058        responseModel.setHeaders(filterHeaders(resp.headers()));
059        // todo: support as byte[] for not text responses
060        if (resp.payload() != null) {
061            if (responseHeaderFields
062                    .getOrDefault("Content-Encoding", emptyList())
063                    .stream()
064                    .anyMatch(it -> it.contains("gzip"))) {
065                responseModel.setPayload(new String(degzip(resp.payload()), StandardCharsets.UTF_8));
066            } else {
067                responseModel.setPayload(new String(resp.payload(), StandardCharsets.UTF_8));
068            }
069        }
070        model.setResponse(responseModel);
071
072        if (DefaultResponseLocator.class.isInstance(api.getResponseLocator())) {
073            DefaultResponseLocator.class.cast(api.getResponseLocator()).addModel(model);
074        }
075    }
076
077    private byte[] degzip(final byte[] payloadBytes) {
078        final ByteArrayOutputStream out = new ByteArrayOutputStream();
079        final byte[] bytes = new byte[1024 * 8];
080        int read;
081        try (final InputStream stream = new GZIPInputStream(new ByteArrayInputStream(payloadBytes))) {
082            while ((read = stream.read(bytes)) >= 0) {
083                if (read == 0) {
084                    continue;
085                }
086                out.write(bytes, 0, read);
087            }
088        } catch (final IOException e) {
089            throw new IllegalStateException(e);
090        }
091        return out.toByteArray();
092    }
093
094    private Map<String, String> filterHeaders(final Map<String, String> headers) {
095        return headers
096                .entrySet()
097                .stream()
098                .filter(h -> !api.getHeaderFilter().test(h.getKey()))
099                .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
100    }
101}