All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.route4me.sdk.RawHTTPDebugger Maven / Gradle / Ivy

Go to download

>This SDK makes it easier for you use the Route4Me API, which creates optimally sequenced driving routes for many drivers.

There is a newer version: 1.15.0
Show newest version
/*
 * The MIT License
 *
 * Copyright 2021 Route4Me.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.route4me.sdk;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.impl.ConnSupport;
import org.apache.http.impl.DefaultBHttpClientConnection;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.io.ChunkedOutputStream;
import org.apache.http.impl.io.ContentLengthOutputStream;
import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
import org.apache.http.impl.io.HttpTransportMetricsImpl;
import org.apache.http.impl.io.IdentityOutputStream;
import org.apache.http.impl.io.SessionOutputBufferImpl;
import org.apache.http.io.HttpMessageWriter;
import org.apache.http.io.SessionOutputBuffer;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;

/**
 *
 * @author juan
 */
public class RawHTTPDebugger {

    public static String toRawHttp(HttpUriRequest request) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HttpTransportMetricsImpl outTransportMetrics = new HttpTransportMetricsImpl();
        SessionOutputBufferImpl buffer = new SessionOutputBufferImpl(outTransportMetrics, 1);
        HttpMessageWriter defaultHttpRequestWriter = DefaultHttpRequestWriterFactory.INSTANCE.create(buffer);
        DefaultHttpRequestWriterFactory writerFactory = new DefaultHttpRequestWriterFactory(null) {
            @Override
            public HttpMessageWriter create(SessionOutputBuffer buffer) {
                return defaultHttpRequestWriter;
            }
        };
        buffer.bind(baos);

        ConnectionConfig config = ConnectionConfig.DEFAULT;
        DefaultBHttpClientConnection connection = new DefaultBHttpClientConnection(config.getBufferSize(),
                                                                                   config.getFragmentSizeHint(),
                                                                                   ConnSupport.createDecoder(config),
                                                                                   ConnSupport.createEncoder(config),
                                                                                   config.getMessageConstraints(),
                                                                                   null,
                                                                                   null,
                                                                                   writerFactory,
                                                                                   null) {
            @Override
            protected void ensureOpen() {
                //using writerFactory buffer instead of socket
            }

            @Override
            protected OutputStream createOutputStream(long len, SessionOutputBuffer outbuffer) {
                if (len == ContentLengthStrategy.CHUNKED) {
                    return new ChunkedOutputStream(2048, buffer);
                } else if (len == ContentLengthStrategy.IDENTITY) {
                    return new IdentityOutputStream(buffer);
                } else {
                    return new ContentLengthOutputStream(buffer, len);
                }
            }
        };

        CloseableHttpClient client = HttpClients.custom()
                                                .setRequestExecutor(new HttpRequestExecutor() {

                                                    @Override
                                                    protected HttpResponse doSendRequest(HttpRequest request, HttpClientConnection conn, HttpContext context) throws IOException, HttpException {
                                                        // inject fake connection
                                                        return super.doSendRequest(request, connection, context);
                                                    }

                                                    @Override
                                                    protected HttpResponse doReceiveResponse(HttpRequest request, HttpClientConnection conn, HttpContext context) {
                                                        return new BasicHttpResponse(request.getProtocolVersion(), 200, "OK");
                                                    }
                                                })
                                                .build();
        client.execute(request);
        return new String(baos.toByteArray());
    }    


    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy