Announcement Announcement Module
Collapse
No announcement yet.
Spring WS 2.1 + 4.0 HttpClient + Proxy -> Content-Length header already present Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring WS 2.1 + 4.0 HttpClient + Proxy -> Content-Length header already present

    Hi I am trying to write spring ws client which connect via proxy.

    This is mine configuration:
    Code:
      <bean id="sms-message-factory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <!--<property name="soapVersion">-->
          <!--<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>-->
        <!--</property>-->
      </bean>
    
      <bean id="sms-webservice-uri-factory" class="com.consol.cmas.daimler.core.util.ValidConfigurationValueFactory">
        <constructor-arg name="value" value="${daimler-integration-sms.ws.sms.uri}"/>
        <constructor-arg name="defaultValue" value=""/>
      </bean>
    
      <bean id="sms-webservice-uri" factory-method="getSafeValue" factory-bean="sms-webservice-uri-factory"/>
    
      <bean id="sms-abstract-client" abstract="true">
        <constructor-arg ref="sms-message-factory"/>
        <property name="defaultUri" ref="sms-webservice-uri"/>
        <property name="messageSenders">
          <list>
            <ref bean="sms-http-sender"/>
          </list>
        </property>
        <property name="marshaller" ref="sms-marshaller"/>
        <property name="unmarshaller" ref="sms-marshaller"/>
      </bean>
    
      <bean id="sms-http-sender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
        <constructor-arg ref="sms-http-client"/>
      </bean>
    
      <bean id="sms-http-client" class="org.apache.http.impl.client.DefaultHttpClient">
        <property name="params" ref="sms-http-params"/>
      </bean>
    
      <bean id="sms-http-params-factory" class="com.consol.cmas.daimler.integration.sms.ws.SmsWsProxyParamsFactory">
        <constructor-arg ref="sms-http-proxy"/>
      </bean>
    
      <bean id="sms-http-params" factory-bean="sms-http-params-factory" factory-method="get"/>
    
      <bean id="sms-http-proxy" class="org.apache.http.HttpHost">
        <constructor-arg name="hostname" value="${daimler-integration-sms.ws.sms.proxy.host}"/>
        <constructor-arg name="port" value="${daimler-integration-sms.ws.sms.proxy.port}"/>
      </bean>
    
      <bean id="sms-marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="org.csapi.schema.sms"/>
      </bean>
    
      <bean id="sms-send-request-getaway" parent="sms-abstract-client"
            class="com.consol.cmas.daimler.integration.sms.ws.WebServiceSmsDelivery">
        <qualifier type="com.consol.cmas.daimler.integration.sms.SmsWebService"/>
        <property name="webServiceSmsRequestConfiguration" ref="sms-send-request-configuration"/>
        <property name="phoneUri">
          <bean class="com.consol.cmas.daimler.integration.sms.ws.WsSmsPhoneUri"/>
        </property>
      </bean>
    
      <bean id="sms-send-request-configuration" class="com.consol.cmas.daimler.integration.sms.ws.WebServiceSmsRequestConfiguration">
        <property name="applicationId" value="${daimler-integration-sms.ws.sms.application.id}"/>
        <property name="extendCode" value="${daimler-integration-sms.ws.sms.extend.code}"/>
        <property name="messageFormat" value="${daimler-integration-sms.ws.sms.message.format}"/>
        <property name="deliveryResultRequest" value="${daimler-integration-sms.ws.sms.delivery.result.request}"/>
        <property name="sendMethod" value="${daimler-integration-sms.ws.sms.send.method}"/>
      </bean>
    Unfortunatelly I am getting following error:
    Code:
    org.springframework.ws.client.WebServiceIOException: I/O error: null; nested exception is org.apache.http.client.ClientProtocolException
    	at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:543)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:384)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:378)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:370)
    	at com.consol.cmas.daimler.integration.sms.ws.WebServiceSmsDelivery.deliver(WebServiceSmsDelivery.java:69)
    	at com.consol.cmas.daimler.integration.sms.ws.WebServiceSmsDeliveryTest.testSendingRealSms(WebServiceSmsDeliveryTest.java:45)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
    	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    Caused by: org.apache.http.client.ClientProtocolException
    	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909)
    	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
    	at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:119)
    	at org.springframework.ws.transport.AbstractWebServiceConnection.send(AbstractWebServiceConnection.java:47)
    	at org.springframework.ws.client.core.WebServiceTemplate.sendRequest(WebServiceTemplate.java:622)
    	at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:585)
    	at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
    	... 32 more
    Caused by: org.apache.http.ProtocolException: Content-Length header already present
    	at org.apache.http.protocol.RequestContent.process(RequestContent.java:96)
    	at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:109)
    	at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:176)
    	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:516)
    	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    	... 39 more
    Could someone give me a hint how to solve this ?

  • #2
    Use an interceptor

    Hi,

    you can take a look at
    Code:
    org.springframework.ws.transport.http.HttpComponentsMessageSender
    they are using an interceptor:
    Code:
    DefaultHttpClient defaultClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);
    to remove the content-length:
    Code:
    private static class RemoveSoapHeadersInterceptor implements HttpRequestInterceptor {
    
            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                if (request instanceof HttpEntityEnclosingRequest) {
                    if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
                        request.removeHeaders(HTTP.TRANSFER_ENCODING);
                    }
                    if (request.containsHeader(HTTP.CONTENT_LEN)) {
                        request.removeHeaders(HTTP.CONTENT_LEN);
                    }
                }
            }
        }

    Comment


    • #3
      Originally posted by de_tom View Post
      Hi,

      you can take a look at
      Code:
      org.springframework.ws.transport.http.HttpComponentsMessageSender
      they are using an interceptor:
      Code:
      DefaultHttpClient defaultClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
      defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);
      to remove the content-length:
      Code:
      private static class RemoveSoapHeadersInterceptor implements HttpRequestInterceptor {
      
              public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                  if (request instanceof HttpEntityEnclosingRequest) {
                      if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
                          request.removeHeaders(HTTP.TRANSFER_ENCODING);
                      }
                      if (request.containsHeader(HTTP.CONTENT_LEN)) {
                          request.removeHeaders(HTTP.CONTENT_LEN);
                      }
                  }
              }
          }
      Thanks for a hint!
      As a workaround, I have subclassed the DefaultHttpClient like this:

      public class MyDefaultHttpClient extends DefaultHttpClient{

      protected BasicHttpProcessor createHttpProcessor() {
      BasicHttpProcessor processor = super.createHttpProcessor();
      processor.addInterceptor(new RemoveHttpHeadersInterceptor(), 0);
      return processor;
      }

      public MyDefaultHttpClient(org.apache.http.impl.conn.Pool ingClientConnectionManager connManager){
      super(connManager);
      }
      }

      And RemoveHttpHeadersInterceptor is:
      public class RemoveHttpHeadersInterceptor implements HttpRequestInterceptor{

      public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
      if (request instanceof HttpEntityEnclosingRequest) {
      if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
      request.removeHeaders(HTTP.TRANSFER_ENCODING);
      }
      if (request.containsHeader(HTTP.CONTENT_LEN)) {
      request.removeHeaders(HTTP.CONTENT_LEN);
      }
      }
      }
      }

      and in my configuration I made change:
      <!-- http client definition -->
      <bean id="httpClient" class="ru.chernykh.MyDefaultHttpClient">
      <constructor-arg>
      <ref bean="connmanager"/>
      </constructor-arg>
      <property name="params" ref="httpParams" />
      </bean>

      Thanks to this, I can successfully run the ws client.

      Comment


      • #4
        yayy! I was having the exact same issue.. Thanks for that hint de_tom

        Comment


        • #5
          I was having the same issue...thank you very much!

          Comment

          Working...
          X