I develop a code to access a SOAP-Server via proxy and regarding to the description here I can set a global Proxy. Although my question seems Naive but I have not find any guide how to set Username and Password for this proxy setting in my java code?
you can at runtime get the System's properties and set all what you need to configurate the proxy...
Example:
System.getProperties().put("http.proxyHost", "myProxyURL");
System.getProperties().put("http.proxyPort", "myProxyPort");
System.getProperties().put("http.proxyUser", "myUserName");
System.getProperties().put("http.proxyPassword", "myPassword");
After some days I found the solution in my case and I try to explain it here.
It is important to know which kind of SOAP Client service you have wrote. In my case I used CXF 3.1.7 to generate Java code. To be more explicit I had a WSDL file and Generated the code via wsdl2java plugin in maven with the mentioned version.
In the level of the WebService the follwoing can be done in code to enter the proxy Setting
private void setProxySetting(EventPortType port) {
try{
Client client = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getClient().setProxyServer("***host***");
http.getClient().setProxyServerPort(80);
http.getProxyAuthorization().setUserName("***username***");
http.getProxyAuthorization().setPassword("***password***");
}catch (Exception e) {
logger.error("Please Enter your proxy setting in MyClass class", e);
}
}
The port is comming from the Service Level that I got like this
EventService es = new EventService();
EventPortType port = es.getEventPort();
setProxySetting();
Related
I have a JSF web page.This Web page is calling a .net web service from back end. I want to use this web service on different host(webservice is same just host -wsdllocation- different) but i don't want do recall wsdl from host to my JSF project. Now, im importing wsdl this command line:
$ wsimport -keep -verbose "wsdl url"
and this line have "wsdlurl" but when i upload my webservice to different webserver i must recall webservice with new "wsdlurl" after i must write new code about that. In .net this way very easy if i want to use same wsdl from different host i can just add a wsdl location line to webconfig.xml is there any way in java about this? How can i call same wsdl from different server without writing code?
You can set the URL when creating the service object
By default when you import wsdl files in client and you try to open port to that service, it test the connection to a default url referenced in imported wsdl. For avoid errors first I create Service Object with the URL of the phisical file pointing to wsdl imported.
URL baseUrl = <classname>.class.getClassLoader().getResource("wsdl/Service.wsdl");
ServiceExtended service = new ServiceExtended(baseUrl);
In addition I use an intermediary class that extend from the original ServiceStub class, it's the way that I use for override connection-timeout and request-timeout properties for a future connection, and also you could add some layer of security using Handlers, etc.
public class ServiceExtended extends ServiceOriginal {
...
public ServiceExtended(URL wsdlLocation) {
super(wsdlLocation);
}
public ProductRepository getPersonalizedPort(URL wsdlLocation, int connectTimeout, int requestTimeout) {
ProductRepository port = super.getProductRepositoryPort();
((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsdlLocation.toString());
//JAXS-WS for compatibility
((BindingProvider) port).getRequestContext().put("com.sun.xml.ws.connect.timeout", connectTimeout);
((BindingProvider) port).getRequestContext().put("com.sun.xml.ws.request.timeout", requestTimeout);
//JAXS-WS new
((BindingProvider) port).getRequestContext().put("com.sun.xml.internal.ws.connect.timeout", connectTimeout);
((BindingProvider) port).getRequestContext().put("com.sun.xml.internal.ws.request.timeout", requestTimeout);
return port;
}
...
}
this is an example because I save the .wsdl files using maven in "src/main/resources/wsdl/" beside META-INF, feel free to put that files where do you want. Using this way the creation of Service object will doesn't fail because you're pointing to real path, in this case to a file that exists.
After that you cant create the port to a Service pointing to any URL you want.
Would be for example:
url1 = http://server1.com/appname/Service?wsdl
url2 = http://server2.com/appname/Service?wsdl
ServicePort port = service.getPersonalizedPort(url1, 10000, 30000);
o
ServicePort port = service.getPersonalizedPort(url2, 10000, 30000);
This is an example of how you could connect to webservice.
Hope this help.
I'm trying to access an FTP server through an FTP SITE Proxy to bypass a firewall using it.sauronsoftware.ftp4j.FTPClient I know my username/password is correct because I can connect using FileZilla. I tried using Authenticator, but it has no use. Code:
import java.net.Authenticator;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.connectors.FTPProxyConnector;
...
FTPClient client = new FTPClient();
FTPProxyConnector connector = new FTPProxyConnector(String "proxyHost", int proxyPort);
client.setConnector(connector);
Authenticator.setDefault(new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("proxyUser", "proxyPass".toCharArray());
}});
System.setProperty("ftp.proxyHost", "proxyHost");
System.setProperty("ftp.proxyPort", "proxyPort");
System.setProperty("ftp.proxyUser", "proxyUser");
System.setProperty("ftp.proxyPass", "proxyPass");
System.out.println("Proxy Accessed");
client.connect("ftpHost");
client.login("ftpUser", "ftpPass");
Gives me this error: java.io.IOException: Proxy authentication failed
Things I have tried:
Using the alternate constructor (String, int, String, String).
Removing Authenticator
Using just Authenticator, without the FTPProxyConnector
Authenticating before setting the connector, and vice versa.
However, when I am JUST using the Authenticator, I get a different error saying Connection timed out.
Both errors occur on line client.connect("ftpHost");
ANY help would be appreciated.
Note: The FTP Proxy Connector
EDIT: I found out that the proxy is used to bypass a Firewall-1 Checkpoint -- if this helps.
Check password property name. It's name is ftp.proxyPassword, and not ftp.proxyPass.
System.setProperty("ftp.proxyUser", "proxyUser");
System.setProperty("ftp.proxyPassword", "proxyPass");
Try it and let us know your results!
Check password property name. It's name is ftp.proxyPassword, and not ftp.proxyPass.
System.setProperty("ftp.proxyUser", "proxyUser");
System.setProperty("ftp.proxyPassword", "proxyPass");
Try it and let us know your results!
I found the solution...
I discovered that the FTP client was responding with a different response code:
200-User <username> authenticated by FireWall-1 authentication
In the source code of FTPProxyConnector, a response code of anything other than the regular
230-Connected to server. Logging in...
will throw an error.
I had to decompile the class file for FTPProxyConnector and then modify the source code, then recompile and save it back to the jar. Worked like a charm.
I'm trying to call the web service here: http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL
I've generated proxy classes using wsimport with JDK1.6.0_29. My wsimport command line is:
wsimport.exe" -keep -B-XautoNameResolution -d E:\mapov\mapov-dev\shared\hotel_info\ http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL
I'm using the following code to attempt to call the service:
QName qName = new QName("http://webservices.hotel.de/V2_8", "FreeHotelSearchWebService");
FreeHotelSearchWebService service = new FreeHotelSearchWebService(new URL("http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc"), qName);
IFreeHotelSearchWebService sws = service.getBasicHttpBindingIFreeHotelSearchWebService();
String version = sws.getWebservicesVersion();
System.out.println("Hotel.info web service version: " + version);
However I get the following exception:
Exception in thread "main" javax.xml.ws.WebServiceException:
Unsupported endpoint address: at
com.sun.xml.ws.api.pipe.TransportTubeFactory.create(TransportTubeFactory.java:148)
at
com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:134)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:641) at
com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:600) at
com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:585) at
com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:482) at
com.sun.xml.ws.client.Stub.process(Stub.java:323) at
com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:161) at
com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:113)
at
com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:93)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:144) at
$Proxy42.getWebservicesVersion(Unknown Source)
In most examples I've seen the generated code includes a getPort() method but that hasn't been generated for this class. Is my code wrong or do I need to run wsimport differently? I've also tried calling the FreeHotelWebService constructor without the parameters which yields the same exception.
Reanimating an answerless question basing on Justin's and Tug's Blog:
JAX-WS: How to configure the service end point at runtime?
When deploying your Web Service client you often need to change the endpoint of the service that has been set during the code generation. This short post explains how you can set change it at runtime in the client code.
You have two approaches to do that:
set the endpoint in the Port using the BindingProvider;
get the endpoint URL from the WSDL itself at runtime;
Use the Binding Provider to set the endpoint URL
The first approach is to change the BindingProvider.ENDPOINT_ADDRESS_PROPERTY property value of the BindingProvider (Port) using the following code:
try {
EmployeeServiceService service = new EmployeeServiceService();
EmployeeService port = service.getEmployeeServicePort();
BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://server1.grallandco.com:8282/HumanRessources/EmployeeServiceService");
Employee emp = port.getEmployee(123);
System.out.println("Result = "+ emp);
} catch (Exception ex) {...
Use the WSDL to get the endpoint URL
Another part is to set the WSDL when you are creating the Service. The service will be using the value that is located in the WSDL port -SOAP Endpoint-. This is simply done using the following code:
try {
EmployeeServiceService service =
new org.demo.service.EmployeeServiceService(
new URL(
"http://server1.grallandco.com:8282/HumanRessources/" +
"EmployeeServiceService?wsdl"),
new QName(
"http://service.demo.org/",
"EmployeeServiceService"));
EmployeeService port = service.getEmployeeServicePort();
Employee emp = port.getEmployee(123);
System.out.println("Result = "+ emp);
} catch (Exception ex) {...}
Note that, in Glassfish, like lot of Web Service environments the WSDL can generate dynamically the Endpoint URL based on the URL used to get the WSDL. With this approach you can also dynamically change the Soap endpoint (if compatible with the network configuration of the production environment.)
HI everyone. I've just started to play a little with XMPP in java, both server and client side.
On the server side I'm using Apache Vysper 0.7 and on client side I'm using Ignite Smack 3.1.0
I'm using a small XMPP embedded server from the apache vysper demo page using a TLS certificate that comes with the source code:
XMPPServer server = new XMPPServer("localhost");
StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry();
AccountManagement accountManagement = (AccountManagement) providerRegistry.retrieve(AccountManagement.class);
Entity user = EntityImpl.parseUnchecked("user#localhost");
accountManagement.addUser(user, "password");
server.setStorageProviderRegistry(providerRegistry);
server.addEndpoint(new TCPEndpoint());
server.setTLSCertificateInfo(new File("bogus_mina_tls.cert"), "boguspw");
server.start();
System.out.println("Vysper server is running...");
The problem is that this is not a correct/valid certificate. If I test my server using pidgin an alert window pops up and tells me the certificate is invalid and a button in case I want to add an exception for this.
What I want is to do the same thing with the Smack api, but I don't know how.
on my smack api I'm using something like this:
ConnectionConfiguration config = new ConnectionConfiguration("localhost",5222, "localhost");
config.setSASLAuthenticationEnabled(false);
connection = new XMPPConnection(config);
connection.connect();
connection.login(userName, password);
So here it is. What do I need to do to accept or decline invalid certificates ?
Thanks for your help.
In the integration tests in Apache Vysper, we use something like:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("localhost", 5222);
connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
connectionConfiguration.setSASLAuthenticationEnabled(true);
connectionConfiguration.setKeystorePath("src/main/resources/bogus_mina_tls.cert");
connectionConfiguration.setTruststorePath("src/main/resources/bogus_mina_tls.cert");
connectionConfiguration.setTruststorePassword("boguspw");
See for example: https://svn.apache.org/repos/asf/mina/vysper/trunk/server/core-inttest/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java
I think you are looking for
config.setSelfSignedCertificateEnabled(true)
I am using HttpClient to connect to a host which requires BasicAUTH. But the proxy doesn't require any authentication. I have set it up as follows:
private final HttpClient httpClient; // Spring injected
Setting Basic auth:
private void setBasicAuth(final String username, final String password) {
httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
httpClient.getParams().setAuthenticationPreemptive(true);
}
Setting proxy:
private void setProxy(final String proxyHost, final int proxyPort) {
hostConfiguration hostConfiguration = httpClient.getHostConfiguration();
hostConfiguration.setProxy(proxyHost, proxyPort);
}
But I get the following warnings when running the code. Everything works, but I want to get rid of the warnings as well (or at least understand why they appears)
WARN o.a.c.httpclient.HttpMethodDirector - Required proxy credentials not available for BASIC <any realm>#proxy.XXXXXX.no:3128
WARN o.a.c.httpclient.HttpMethodDirector - Preemptive authentication requested but no default proxy credentials available
Any ideas?
Here's an example from the Apache site for a proxy w/o credentials:
http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java
(From http://hc.apache.org/httpcomponents-client-ga/examples.html)
You are getting the error because you are passing in a username/password and don't need to.
The WARN messages are coming from the logger (http://hc.apache.org/httpcomponents-client-ga/logging.html) - depending on how you have your logger set up you could just ignore that.
Having spent WAY too much time dealing with trying to make a Java application deal with proxy servers, I can tell you that using a tool such as Proxifier ( http://www.proxifier.com/ for Mac OS X and Windows) or CNTLM ( http://cntlm.sourceforge.net/) was much easier, more flexible, easier to debug, and kept the code clean.