camel jetty header 过大问题解决办法

在camel 中使用jetty组件可以完成http接口的调用与监听,但调用时候如果header参数值过大,会抛出异常信息,默认jetty组件header值的大小最大为 8k,可以通过如下代码进行修改

package com.xiaoka.camel.springboot.example.component;

import org.apache.camel.RuntimeCamelException;
import org.apache.camel.component.jetty.CamelHttpClient;
import org.apache.camel.component.jetty.JettyHttpComponent;
import org.apache.camel.component.jetty.JettyHttpEndpoint;
import org.apache.camel.component.jetty9.CamelHttpClient9;
import org.apache.camel.component.jetty9.JettyHttpEndpoint9;
import org.apache.camel.support.IntrospectionSupport;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuchengbiao
 * @date 2019-06-03 14:56
 */

public class JettyComponentDemo extends JettyHttpComponent {

    private static final Logger LOG = LoggerFactory.getLogger(JettyComponentDemo.class);

    protected CamelHttpClient createCamelHttpClient(HttpClientTransport transport, SslContextFactory sslContextFactory) {
        return new CamelHttpClient9(transport, sslContextFactory);
    }

    protected JettyHttpEndpoint createEndpoint(URI endpointUri, URI httpUri) throws URISyntaxException {
        return new JettyHttpEndpoint9(this, endpointUri.toString(), httpUri);
    }

    protected AbstractConnector createConnectorJettyInternal(Server server,
                                                             JettyHttpEndpoint endpoint,
                                                             SslContextFactory sslcf) {
        try {
            String host = endpoint.getHttpUri().getHost();
            int port = endpoint.getPort();
            org.eclipse.jetty.server.HttpConfiguration httpConfig = new org.eclipse.jetty.server.HttpConfiguration();
            httpConfig.setSendServerVersion(endpoint.isSendServerVersion());
            httpConfig.setSendDateHeader(endpoint.isSendDateHeader());
            System.out.println("初始化");
            if (requestBufferSize != null) {
                // Does not work
                //httpConfig.setRequestBufferSize(requestBufferSize);
            }
            if (requestHeaderSize != null) {
                httpConfig.setRequestHeaderSize(requestHeaderSize);
            }
            if (responseBufferSize != null) {
                httpConfig.setOutputBufferSize(responseBufferSize);
            }
            int size = 1024 * 1024 * 10;
            httpConfig.setRequestHeaderSize(size);
            httpConfig.setResponseHeaderSize(size);

            if (responseHeaderSize != null) {
                httpConfig.setResponseHeaderSize(responseHeaderSize);
            }
            if (useXForwardedForHeader) {
                httpConfig.addCustomizer(new ForwardedRequestCustomizer());
            }
            HttpConnectionFactory httpFactory = new org.eclipse.jetty.server.HttpConnectionFactory(httpConfig);

            ArrayList connectionFactories = new ArrayList<>();
            ServerConnector result = new org.eclipse.jetty.server.ServerConnector(server);
            if (sslcf != null) {
                httpConfig.addCustomizer(new org.eclipse.jetty.server.SecureRequestCustomizer());
                SslConnectionFactory scf = new org.eclipse.jetty.server.SslConnectionFactory(sslcf, "HTTP/1.1");
                connectionFactories.add(scf);
                // The protocol name can be "SSL" or "SSL-HTTP/1.1" depending on the version of Jetty
                result.setDefaultProtocol(scf.getProtocol());
            }
            connectionFactories.add(httpFactory);
            for (ConnectionFactory cf : connectionFactories) {
                result.addConnectionFactory(cf);
            }
            result.setPort(port);
            if (host != null) {
                result.setHost(host);
            }
            if (sslcf != null) {
                if (getSslSocketConnectorProperties() != null && "https".equals(endpoint.getProtocol())) {
                    // must copy the map otherwise it will be deleted
                    Map properties = new HashMap<>(getSslSocketConnectorProperties());
                    IntrospectionSupport.setProperties(sslcf, properties);
                    if (properties.size() > 0) {
                        throw new IllegalArgumentException("There are " + properties.size()
                                + " parameters that couldn't be set on the SocketConnector."
                                + " Check the uri if the parameters are spelt correctly and that they are properties of the SelectChannelConnector."
                                + " Unknown parameters=[" + properties + "]");
                    }
                }

                LOG.info("Connector on port: {} is using includeCipherSuites: {} excludeCipherSuites: {} includeProtocols: {} excludeProtocols: {}",
                        port, sslcf.getIncludeCipherSuites(), sslcf.getExcludeCipherSuites(), sslcf.getIncludeProtocols(), sslcf.getExcludeProtocols());
            }

            return result;
        } catch (Exception e) {
            throw RuntimeCamelException.wrapRuntimeCamelException(e);
        }
    }

}

 

@Bean("jetty")
public JettyComponentDemo nothing() {
    JettyComponentDemo jetty = new JettyComponentDemo();
    int size = 1024 * 1024 * 10;
    jetty.setRequestHeaderSize(size);
    jetty.setResponseHeaderSize(size);
    return jetty;
}

 

通过上述修改后最大header值可传入 10M 

你可能感兴趣的:(Camel)