Configuring an HTTP proxy in a Spring web app - web-services

I have been searching around for a proper way to configure an HTTP proxy in a Spring web application. Unfortunately, each time the results I get are about AOP proxies and not HTTP proxies.
Basically, one module of my application is running a webservice client configure in the Spring XML file with JAX-WS, giving something like :
<bean id="heartBeatWebservice" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="the.web.service.interface"/>
<property name="wsdlDocumentUrl" value="http://thehost:theport/theservicename.wsdl"/>
<property name="serviceName" value="TheServiceName"/>
<property name="namespaceUri" value="http://the.namespace/"/>
<property name="portName" value="TheWebServicePortName"/>
</bean>
But my app has to run behind an HTTP proxy for being able to call the web service, and I must acknowledge that I don't know how to do it properly within the Spring context.
I tried in some main class that I wrote to try out this code at first :
System.setProperty("http.proxyHost", "my.proxy.addr");
System.setProperty("http.proxyPort", "8080");
Unfortunately, it didn't work as expected. I assume there is a nice way to configure an HTTP proxy in a Spring context but can't find out how ...
Can you give me a hint ?

There isn't any Spring-specific HTTP proxy configuration required.
It should use the standard Java HTTP proxy settings, so you're going along the right lines.
Can you try running the main class using -Dhttp.proxyHost=my.proxy.host -Dhttp.proxyPort=8080 rather than using System.setProperty?

Related

How to Call External WebService using Proxy Host form JBOSS EAP 7.0.0

We have couple of external third party web services to send business data. But the constrain is that, those services need to be called from dedicated server (say 120.10.20.123 ).
Now any external service call need to be re directed through proxy server (120.10.20.123).
Can somebody please help me to understand how to achieve this functionality using JBOSS EAP 7.0.0
Thanks in advance.
Ajoy
You have to modify in
standalone.xml
at
the following:
<system-properties>
<property name="http.proxyHost" value="yourProxyIpAddress"/>
<property name="http.proxyPort" value="yourProxyPort"/>
<property name="http.nonProxyHosts" value="localhost"/>
</system-properties>
You can also do it programmatically but it's not related to question.
Or you could put a webserver to redirect the call to leave jboss clean.

WSO2 API Manager - Replace URLs in response body

I'm trying to setup a proxy for my RESTful API using WSO2 API Manager. My problem is that the responses from the backend API are left untouched so all the urls that connect to other endpoints still reference the backend server rather than the proxy. I need a way to replace those url values in the response body to point to the proxied api. I understand this can be accomplished via Mediation Extensions, using ESB Mediators.
I'm not familiar enough with them to pick the one better suited for the job. URLRewrite mediator looks pretty straightforward, but it doesn't seem to apply to the message body but the headers. Payload Factory seems to require a fixed structure for the message, which is not very convenient for me, since I need it to work on the different responses that my API provides (and I wouldn't want to maintain those structures in the mediator definition).
I've managed to solve it by setting the headers my application checks to build its urls:X-Forwarded-Host and X-Forwarded-Proto.
So I've created a Header Mediator that looks like:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In">
<header name="X-Forwarded-Host" expression="get-property('transport','Host')" scope="transport"/>
<header name="X-Forwarded-Proto" value="https" scope="transport"/>
</sequence>
And that did the trick.

cxf webservice over jms, how to get wsdl

I have a webservice configured like this.
<jaxws:endpoint
id="helloWorld"
xmlns:hello="http://service.test.com/"
serviceName="hello:MyServiceImplService"
endpointName="hello:MyServiceImplPort" address="jms://"
implementor="com.test.service.MyServiceImpl" >
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature" />
<bean class="org.apache.cxf.transport.jms.JMSConfigFeature">
<property name="jmsConfig" ref="jmsConfig"/>
</bean>
</jaxws:features>
</jaxws:endpoint>`
I want to have the cxf generate the wsdl for me. when the service is http, I can access the wsdl from a url like this http://localhost:8080/server/hello?wsdl, after I change the transport to jms, how do I access the wsdl? what's the jms message I need to send to get the wsdl. or I have to write the wsdl first and expose it through a regular http request. I am asking this because the client need to know the wsdl to get all the type and binding information to generate class. client should already know how to connect the queue.
You can use commandline java2ws tool (see http://cxf.apache.org/docs/java-to-ws.html) passing it your implementation class. There is also a sample invocation from Ant script. It will generate WSDL which your client can use to generate client code.

Accessing a password-protected WSDL in Weblogic when calling a web service

We're using Spring and JAXWS-generated client classes to access web services in a weblogic-deployed app. The WSDL defining the web service is remote and password-protected (basic http authentication). In a unit test it suffices to define a proxy in ~/.metro folder with the url and http password to use when accessing it. Is there a similar trick for Weblogic in some configuration file? Or is there some other common way of solvind this issue?
According to the documentation (Chapter 6. Using Spring Web Services on the Client):
6.2.1.1.1. HTTP transports
There are two implementations of the
WebServiceMessageSender interface for
sending messages via HTTP. The default
implementation is the
HttpUrlConnectionMessageSender, which
uses the facilities provided by Java
itself. The alternative is the
CommonsHttpMessageSender, which uses
the Jakarta Commons HttpClient. Use
the latter if you need more advanced
and easy-to-use functionality (such as
authentication, HTTP connection
pooling, and so forth).
(...)
The folowing example shows how
override the default configuration,
and to use Commons Http to
authenticate using HTTP
authentication:
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
<property name="credentials">
<bean class="org.apache.commons.httpclient.UsernamePasswordCredentials">
<constructor-arg value="john"/>
<constructor-arg value="secret"/>
</bean>
</property>
</bean>
</property>
<property name="defaultUri" value="http://example.com/WebService"/>
</bean>
Did you try this?
Update: Since you're using a JAX-WS client (which is not what I understood from "we're using Spring"), you can either:
Set a default Authenticator as described in HTTP basic authentication with JAX-WS (Client).
Use Spring and configure a http-conf:authorization for the conduit element. See Client HTTP Transport (including SSL support).
You can provide your own Authenticator. That way it will work if the WDSL itself is protected by basic HTTP authentication.
#WebServiceRef(wsdlLocation = "https://laka/sito?wsdl")
static XxxService service;
public static void main(String[] args) {
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
service = new XxxService();
Xxx port = service.getXxxPort();
// invoke webservice and print response
XxxResponse resp = port.foo();
System.out.println(resp.toString());
}

Usage of SoapActionEndpointMapping in Spring-ws

I'm trying to create a WS based on a WSDL that defines one Request and one Response. The incoming request should be mapped to an endpoint depending on the SOAPAction defined in the SOAP message. To achieve this I'm trying to use the SoapActionEndpointMapping in my servlet.xml config file and define the mappings, as described in the Spring documentation.
<bean id="endpointMapping" class="org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping">
<property name="mappings">
<props>
<prop key="http://myCompany/MyService/MyRequest/mySoapActionOne">myFirstEndpoint</prop>
<prop key="http://myCompany/MyService/MyRequest/mySoapActionTwo">mySecondEndpoint</prop>
</props>
</property>
My endpoint extends AbstractMarshallingPayloadEndpoint and should be able to handle the requests.
The problem is that when I try to send a request (with SoapUI) i get the following error in the log:
WARN [EndpointNotFound] No endpoint mapping found for [SaajSoapMessage {http://schemas.mycompany/MyService}MyRequest]
I have used the PayloadRootQNameEndpointMapping with great success earlier but can not this to work.
Any help is appreciated.
Regards.
Do you have a handler adapter bean defined also? You'll need one in order to use a MarshallingPayloadEndpoint, so that spring knows how to perform the marshalling. The adapter is called something like MarshallingEndpointHandlerAdapter, or similar.
In your SOAP client (SOAPUI), you'll need to add the SOAPAction header to your request, to supply spring with the SOAP action to use in its mapping.
E.g. SOAPAction=http://myCompany/MyService/MyRequest/mySoapActionOne
It shouldn't make any difference what type of Endpoint you're using, because currently, you're receiving a 404 response - your request isn't finding its way to any endpoint.