Search in sources :

Example 1 with HttpFiltersAdapter

use of org.littleshoot.proxy.HttpFiltersAdapter in project spring-cloud-dataflow by spring-cloud.

the class ProxyTestServer method main.

public static void main(String[] args) throws IOException {
    final int port = 8077;
    final boolean withSecurity = true;
    final boolean withSsl = true;
    final String proxyServerUsername = "spring";
    final String proxyServerPassword = "cloud";
    HttpProxyServerBootstrap builder = DefaultHttpProxyServer.bootstrap().withFiltersSource(new HttpFiltersSourceAdapter() {

        public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
            System.out.println("Request comining in " + originalRequest.uri());
            return new HttpFiltersAdapter(originalRequest) {

                @Override
                public HttpResponse clientToProxyRequest(HttpObject httpObject) {
                    return null;
                }

                @Override
                public HttpObject serverToProxyResponse(HttpObject httpObject) {
                    return httpObject;
                }
            };
        }
    }).withPort(port);
    if (withSecurity) {
        builder.withProxyAuthenticator(new ProxyAuthenticator() {

            @Override
            public String getRealm() {
                return "dataflow";
            }

            @Override
            public boolean authenticate(String userName, String password) {
                if (proxyServerUsername.equals(userName) && proxyServerPassword.equals(password)) {
                    return true;
                } else {
                    return false;
                }
            }
        });
    }
    if (withSsl) {
        builder.withSslEngineSource(new SelfSignedSslEngineSource()).withAuthenticateSslClients(false);
    }
    builder.start();
    System.out.println(String.format("Started proxy server on port %s. With security? %s. Ssl enabled? %s", port, withSecurity, withSsl));
    if (withSecurity) {
        System.out.println(String.format("Proxy server username '%s' and password '%s'.", proxyServerUsername, proxyServerPassword));
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpFiltersAdapter(org.littleshoot.proxy.HttpFiltersAdapter) ProxyAuthenticator(org.littleshoot.proxy.ProxyAuthenticator) HttpObject(io.netty.handler.codec.http.HttpObject) HttpFiltersSourceAdapter(org.littleshoot.proxy.HttpFiltersSourceAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SelfSignedSslEngineSource(org.littleshoot.proxy.extras.SelfSignedSslEngineSource) HttpProxyServerBootstrap(org.littleshoot.proxy.HttpProxyServerBootstrap)

Example 2 with HttpFiltersAdapter

use of org.littleshoot.proxy.HttpFiltersAdapter in project phase4 by phax.

the class AS4eSENSCEFOneWayFuncTest method testEsens_TA11.

/**
 * Prerequisite:<br>
 * SMSH and RMSH are configured to exchange AS4 messages according to the
 * e-SENS profile (One-Way/Push MEP). Simulate the RMSH to not send receipts.
 * SMSH tries to send an AS4 User Message to the RMSH. Before a TIME_OUT is
 * reached network connection is restored (RMSH is able to send a
 * non-repudiation receipt).<br>
 * <br>
 * Predicate: <br>
 * The RMSH sends back an AS4 non-repudiation receipt to the SMSH and delivers
 * only one message to the consumer and the SMSH stops resending the original
 * AS4 User Message.
 *
 * @throws Exception
 *         In case of error
 */
@Test
public void testEsens_TA11() throws Exception {
    final int nProxyPort = 8001;
    final IStringMap aSettings = new StringMap();
    aSettings.putIn(SETTINGS_SERVER_PROXY_ENABLED, true);
    aSettings.putIn(SETTINGS_SERVER_PROXY_ADDRESS, "localhost");
    aSettings.putIn(SETTINGS_SERVER_PROXY_PORT, nProxyPort);
    try (final ScopedConfig aSC = ScopedConfig.createTestConfig(aSettings)) {
        // Simulating a timeout with Thread.sleep but before it entirely triggers
        // let the program continue as if the Connection is back up again
        final HttpProxyServer aProxyServer = DefaultHttpProxyServer.bootstrap().withPort(nProxyPort).withFiltersSource(new HttpFiltersSourceAdapter() {

            @Override
            public HttpFilters filterRequest(final HttpRequest originalRequest, final ChannelHandlerContext ctx) {
                return new HttpFiltersAdapter(originalRequest) {

                    @Override
                    public HttpResponse clientToProxyRequest(final HttpObject httpObject) {
                        ThreadHelper.sleep(500);
                        return null;
                    }

                    @Override
                    public HttpObject serverToProxyResponse(final HttpObject httpObject) {
                        LOGGER.error("Forcing a timeout from retryhandler ");
                        return httpObject;
                    }
                };
            }
        }).start();
        try {
            // send message
            final AS4MimeMessage aMsg = MimeMessageCreator.generateMimeMessage(m_eSoapVersion, createTestSignedUserMessage(m_eSoapVersion, m_aPayload, null, s_aResMgr), null);
            sendMimeMessage(new HttpMimeMessageEntity(aMsg), true, null);
        } finally {
            aProxyServer.stop();
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpFiltersAdapter(org.littleshoot.proxy.HttpFiltersAdapter) StringMap(com.helger.commons.collection.attr.StringMap) IStringMap(com.helger.commons.collection.attr.IStringMap) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) HttpObject(io.netty.handler.codec.http.HttpObject) AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) HttpFiltersSourceAdapter(org.littleshoot.proxy.HttpFiltersSourceAdapter) IStringMap(com.helger.commons.collection.attr.IStringMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) ScopedConfig(com.helger.phase4.ScopedConfig) Test(org.junit.Test)

Example 3 with HttpFiltersAdapter

use of org.littleshoot.proxy.HttpFiltersAdapter in project proxy by baloise.

the class FiltersSource407 method filterRequest.

@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
    return new HttpFiltersAdapter(originalRequest) {

        @Override
        public HttpObject proxyToClientResponse(HttpObject httpObject) {
            if (httpObject instanceof DefaultHttpResponse) {
                DefaultHttpResponse response = (DefaultHttpResponse) httpObject;
                int code = response.getStatus().code();
                if (code == 407) {
                    onError.run();
                }
            }
            return httpObject;
        }
    };
}
Also used : HttpFiltersAdapter(org.littleshoot.proxy.HttpFiltersAdapter) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse)

Example 4 with HttpFiltersAdapter

use of org.littleshoot.proxy.HttpFiltersAdapter in project gradle-download-task by michel-kraemer.

the class ProxyTest method startProxy.

/**
 * Runs a proxy server counting requests
 * @param authenticating true if the proxy should require authentication
 * @throws Exception if an error occurred
 */
private void startProxy(boolean authenticating) throws Exception {
    proxyPort = findPort();
    HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer.bootstrap().withPort(proxyPort).withFiltersSource(new HttpFiltersSourceAdapter() {

        public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
            return new HttpFiltersAdapter(originalRequest) {

                @Override
                public void proxyToServerRequestSent() {
                    proxyCounter++;
                }
            };
        }
    });
    if (authenticating) {
        bootstrap = bootstrap.withProxyAuthenticator(new ProxyAuthenticator() {

            @Override
            public boolean authenticate(String userName, String password) {
                return PROXY_USERNAME.equals(userName) && PROXY_PASSWORD.equals(password);
            }

            @Override
            public String getRealm() {
                return "gradle-download-task";
            }
        });
    }
    proxy = bootstrap.start();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpFiltersAdapter(org.littleshoot.proxy.HttpFiltersAdapter) ProxyAuthenticator(org.littleshoot.proxy.ProxyAuthenticator) HttpFiltersSourceAdapter(org.littleshoot.proxy.HttpFiltersSourceAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpFilters(org.littleshoot.proxy.HttpFilters) HttpProxyServerBootstrap(org.littleshoot.proxy.HttpProxyServerBootstrap)

Example 5 with HttpFiltersAdapter

use of org.littleshoot.proxy.HttpFiltersAdapter in project phase4 by phax.

the class AS4eSENSCEFOneWayFuncTest method testEsens_TA17.

/**
 * Prerequisite:<br>
 * eSENS_TA10. TIME_OUT for resending the messages is reached.<br>
 * <br>
 * Predicate: <br>
 * The SMSH reports an error to the message producer.
 *
 * @throws Exception
 *         In case of error
 */
@Test(expected = NoHttpResponseException.class)
public void testEsens_TA17() throws Exception {
    final int nProxyPort = 8001;
    final IStringMap aSettings = new StringMap();
    aSettings.putIn(SETTINGS_SERVER_PROXY_ENABLED, true);
    aSettings.putIn(SETTINGS_SERVER_PROXY_ADDRESS, "localhost");
    aSettings.putIn(SETTINGS_SERVER_PROXY_PORT, nProxyPort);
    try (final ScopedConfig aSC = ScopedConfig.createTestConfig(aSettings)) {
        // Forcing a Timeout from the retry handler
        final HttpProxyServer aProxyServer = DefaultHttpProxyServer.bootstrap().withPort(nProxyPort).withFiltersSource(new HttpFiltersSourceAdapter() {

            @Override
            public HttpFilters filterRequest(final HttpRequest originalRequest, final ChannelHandlerContext ctx) {
                return new HttpFiltersAdapter(originalRequest) {

                    @Override
                    public HttpResponse clientToProxyRequest(final HttpObject httpObject) {
                        return null;
                    }

                    @Override
                    public HttpObject serverToProxyResponse(final HttpObject httpObject) {
                        LOGGER.warn("Forcing a timeout from retryhandler for testing purposes.");
                        return null;
                    }
                };
            }
        }).start();
        try {
            // send message
            final AS4MimeMessage aMsg = MimeMessageCreator.generateMimeMessage(m_eSoapVersion, createTestSignedUserMessage(m_eSoapVersion, m_aPayload, null, s_aResMgr), null);
            sendMimeMessage(new HttpMimeMessageEntity(aMsg), false, EEbmsError.EBMS_OTHER.getErrorCode());
        } finally {
            aProxyServer.stop();
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpFiltersAdapter(org.littleshoot.proxy.HttpFiltersAdapter) StringMap(com.helger.commons.collection.attr.StringMap) IStringMap(com.helger.commons.collection.attr.IStringMap) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) HttpObject(io.netty.handler.codec.http.HttpObject) AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) HttpFiltersSourceAdapter(org.littleshoot.proxy.HttpFiltersSourceAdapter) IStringMap(com.helger.commons.collection.attr.IStringMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) ScopedConfig(com.helger.phase4.ScopedConfig) Test(org.junit.Test)

Aggregations

HttpFiltersAdapter (org.littleshoot.proxy.HttpFiltersAdapter)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpObject (io.netty.handler.codec.http.HttpObject)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 HttpFiltersSourceAdapter (org.littleshoot.proxy.HttpFiltersSourceAdapter)5 HttpProxyServer (org.littleshoot.proxy.HttpProxyServer)3 DefaultHttpProxyServer (org.littleshoot.proxy.impl.DefaultHttpProxyServer)3 IStringMap (com.helger.commons.collection.attr.IStringMap)2 StringMap (com.helger.commons.collection.attr.StringMap)2 ScopedConfig (com.helger.phase4.ScopedConfig)2 HttpMimeMessageEntity (com.helger.phase4.http.HttpMimeMessageEntity)2 AS4MimeMessage (com.helger.phase4.messaging.mime.AS4MimeMessage)2 Test (org.junit.Test)2 HttpProxyServerBootstrap (org.littleshoot.proxy.HttpProxyServerBootstrap)2 ProxyAuthenticator (org.littleshoot.proxy.ProxyAuthenticator)2 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 Nonnull (javax.annotation.Nonnull)1 HttpFilters (org.littleshoot.proxy.HttpFilters)1 SelfSignedSslEngineSource (org.littleshoot.proxy.extras.SelfSignedSslEngineSource)1