Challenges performing basic HTTP authentication on RPC SOAP client - java

Been trying to follow some online examples as I need to do basic authentication on a webservice client.
I generated the stub classes of the project using wsimport and tried passing the authentication credentials using javax.xml.rpc.stub class but casting the proxy class throws a java.lang.ClassCastException:
com.sun.proxy.$Proxy29 cannot be cast to javax.xml.rpc.Stub.
please can anyone review this code and point me in the right direction if am doing something wrong.
public class WebClientTester
{
public static void main(String[] args)
{
doNameEnquiry("XXXXXXXXX");
}
public static void doNameEnquiry(String acct)
{
boolean txnOk = false;
try
{
String str = "http://XXX.XXX.XXX.XXX/GwHolderService.svc?wsdl";
URL url = new URL(str.substring(0, str.indexOf("?")));
QName qname = new QName("http://tempuri.org/", "GwHolderService");
Service service = Service.create(url, qname);
SInfoHolder port = (SInfoHolder) service.getPort(SInfoHolder.class);
((javax.xml.rpc.Stub) port)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "myUser");
((javax.xml.rpc.Stub) port)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "myPwd");
InfoHolderRequest request = new InfoHolderRequest();
request.setHolderAccountNumber(acct);
InfoHolderResponse response = port.infoHolder(request);
// System.out.println("authenticated: "+
// response.getRespMessageCode());
System.out.println("******************END RESPONSE***********");
System.out.println("responseCode: " + response.getCoderesp());
System.out.println(processResponseXML(response));
System.out.println("******************LIST DETAILS***********");
listDetails(processResponseXML(response));
}
catch (Exception ex)
{
ex.printStackTrace();
}
// return txnOk;
}
}

Related

How to use Holder<Payload> holder in SOAP web client

I am trying to figure out how to use a webservice which was generated from a wsdl file. In this project I am going send request by using the generated webservice client. The interface is shown below:
public interface IConnectService {
public void processMessage(
#WebParam(name = "payload", mode =
WebParam.Mode.INOUT)
Holder<Payload> payload);
}
The client is like this:
public class ConnectService
extends Service
{
private final static URL CONNECTSERVICE_WSDL_LOCATION;
private final static WebServiceException CONNECTSERVICE_EXCEPTION;
private final static QName CONNECTSERVICE_QNAME = new
QName("http://xxxxxx-asi.com/services", "ConnectService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost/wsdl/xxxxx");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
CONNECTSERVICE_WSDL_LOCATION = url;
CONNECTSERVICE_EXCEPTION = e;
}
public ConnectService() {
super(__getWsdlLocation(), CONNECTSERVICE_QNAME);
}
public ConnectService(WebServiceFeature... features) {
super(__getWsdlLocation(), CONNECTSERVICE_QNAME, features);
}
public ConnectService(URL wsdlLocation) {
super(wsdlLocation, CONNECTSERVICE_QNAME);
}
public ConnectService(URL wsdlLocation, WebServiceFeature...
features) {
super(wsdlLocation, CONNECTSERVICE_QNAME, features);
}
public ConnectService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public ConnectService(URL wsdlLocation, QName serviceName,
WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
#WebEndpoint(name = "BasicHttpBinding_IConnectService")
public IConnectService getBasicHttpBindingIConnectService() {
return super.getPort(new QName("http://xxxxxxx-
asi.com/services", "BasicHttpBinding_IConnectService"),
IConnectService.class);
}
#WebEndpoint(name = "BasicHttpBinding_IConnectService")
public IConnectService
getBasicHttpBindingIConnectService(WebServiceFeature... features) {
return super.getPort(new QName("http://xxxxxx-asi.com/services",
"BasicHttpBinding_IConnectService"), IConnectService.class,
features);
}
private static URL __getWsdlLocation() {
if (CONNECTSERVICE_EXCEPTION!= null) {
throw CONNECTSERVICE_EXCEPTION;
}
return CONNECTSERVICE_WSDL_LOCATION;
}
}
The soap request message structure is going to be like in the picture: enter image description here
So my question is how can I use the client to make call which includes my soapmessage using the interface method? To my understand, the Holder object can only take Payload object, which is an childelement of ProcessMessege (as shown in the structure), and ProcessMessage is a childelement of SOAP body. I need to put the security credentials in the SOAP header and I already did that. So right now if I use the webservice method, I only can pass the payload object, but the web server will not accept the request because no credentials inside of the payload part. Anybody can help out for this problem? I really appreciate your help!
I solved this problem by using soapmessage handler and handler resolver. The handler can insert the credentials in the soap header and modifier the soap body to satisfy all my requirements for the soap message.

Using ASP.NET Web API to call Java Web Service

I have an ASP.NET Web API which is supposed to call a java addition web service. When i run the java web service and type url http://localhost:8080/addition/9/6 i get {"firstNumber":9,"secondNumber":6,"sum":15}as the output data. Right now, i want to use the ASP.NET Web API to call and display that data when i run the ASP.NET Web API application. How do i go about doing that?
Here are my codes:
ASP.NET Web API Codes
RestfulClient.cs
public class RestfulClient
{
private string BASE_URL = "http://localhost:8080/addition/";
public Task<string> addition()
{
{
try
{
var client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("addition").Result;
return response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
HttpContext.Current.Server.Transfer("ErrorPage.html");
}
return null;
}
}
}
ApiController.cs
private RestfulClient restfulClient = new RestfulClient();
public ActionResult Index()
{
var Result1 = restfulClient.addition().Result;
return Content(Result1);
}
Java Web Service Codes
AdditionController.java
#RestController
public class AdditionController {
private static final String template = " %s";
private static int getSum;
#RequestMapping("/addition/{param1}/{param2}")
#ResponseBody
public Addition getSum
(#PathVariable("param1") int firstNumber,#PathVariable("param2") int secondNumber) {
return new Addition(
(String.format(template, firstNumber)), String.format(template, secondNumber));
}
}
Someone please help me. Thank you so much in advance.
According to the Java service, the URL you are calling from the client is not formatted correctly based on your base URL and the one used in the GetAsync.
public class RestfulClient {
private static HttpClient client;
private static string BASE_URL = "http://localhost:8080/";
static RestfulClient() {
client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<string> addition(int a, int b) {
try {
var endpoint = string.Format("addition/{0}/{1}", a, b);
var response = await client.GetAsync(endpoint);
return await response.Content.ReadAsStringAsync();
} catch (Exception e) {
HttpContext.Current.Server.Transfer("ErrorPage.html");
}
return null;
}
}
The controller would also need to be updated.
public async Task<ActionResult> Index() {
int a = 9;
int b = 6;
var result = await restfulClient.addition(a, b);
return Content(result);
}
Note the proper use of the HttpClient as suggested in the comments and as well as the use of async/await.

How to create Restful web service in core java without any container like tomcat/Jboss

I have core java project (swing module) but recently requirement come that to deploy one restful web service on core java without any container.
So Is it possible to deploy restful web service without any container?
I have searched many site using that I have got code as below:
public class JerseyEmbeddedHTTPServerCrunchify {
public static void main(String[] args) throws IOException {
System.out
.println("Starting Crunchify's Embedded Jersey HTTPServer...\n");
HttpServer crunchifyHTTPServer = createHttpServer();
crunchifyHTTPServer.start();
System.out.println(String.format(
"\nJersey Application Server started with WADL available at "
+ "%sapplication.wadl\n", getCrunchifyURI()));
System.out
.println("Started Crunchify's Embedded Jersey HTTPServer Successfully !!!");
}
private static HttpServer createHttpServer() throws IOException {
// ResourceConfig crunchifyResourceConfig = new
// PackagesResourceConfig("com.crunchify.tutorial");
ResourceConfig crunchifyResourceConfig = new ResourceConfig();
// This tutorial required and then enable below line:
// http://crunfy.me/1DZIui5
// crunchifyResourceConfig.getContainerResponseFilters().add(CrunchifyCORSFilter.class);
// return HttpServerFactory.create(getCrunchifyURI(),
// crunchifyResourceConfig);
System.out.println("URI : " + getCrunchifyURI());
return JdkHttpServerFactory.createHttpServer(getCrunchifyURI(),
crunchifyResourceConfig);
}
private static URI getCrunchifyURI() {
// return UriBuilder.fromUri("http://" + crunchifyGetHostName() +
// "/").port(18085).build();
return UriBuilder.fromUri("http://" + "localhost" + "/").port(18085)
.build();
}
private static String crunchifyGetHostName() {
String hostName = "localhost";
try {
hostName = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostName;
}
Maven Dependency:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.4.1</version>
</dependency>
In above code I got below exception:
Exception in thread "main" java.lang.IllegalStateException: server in wrong state
at sun.net.httpserver.ServerImpl.start(ServerImpl.java:139)
at sun.net.httpserver.HttpServerImpl.start(HttpServerImpl.java:58)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory$1.start(JdkHttpServerFactory.java:143)
You can try this
#RestController
public class WebServiceController
{
#RequestMapping(value="/getresultbyNumber/{number1}/{number2}", method={RequestMethod.POST,RequestMethod.GET})
public #ResponseBody Object getData(#PathVariable String number1, #PathVariable String number2 , HttpServletRequest request)
{
String URL="http://192.168.4.218:8081/DemoSpringMVC/getResultbyNumberdata/"+ number1 +"/"+number2;
HttpClient client= HttpClientBuilder.create().build();
HttpGet httpRequest=new HttpGet(URL);
String res =null;
try {
HttpResponse response=client.execute(httpRequest);
res = new BasicResponseHandler().handleResponse(response);
System.out.println("result===>"+res);
} catch (Exception e) {
e.printStackTrace();
}
return res ;
}
}

Soap Mismatch between Java model and WSDL model found

I'm developing an SOAP Service in Java but i encountered this error:
WARNING: Mismatch between Java model and WSDL model found, For wsdl operation
{http://database.unitn.it/}isLoginOkay,There is no matching wsdl fault with detail
QName {http://interfaces.database.unitn.it/}Exception
com.sun.xml.internal.ws.spi.db.DatabindingException: Unknown JAXBContext
implementation: class com.sun.xml.bind.v2.runtime.JAXBContextImpl
at com.sun.xml.internal.ws.spi.db.BindingContextFactory.getJAXBFactory(BindingContextFactory.j
ava:192)
at com.sun.xml.internal.ws.spi.db.BindingContextFactory.create(BindingContextFactory.java:134)
at com.sun.xml.internal.ws.message.jaxb.JAXBMessage.create(JAXBMessage.java:152)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createSOAPFaultMessage(SOAPFaultBuilder.java:241)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createSOAPFaultMessage(SOAPFaultBuilder.java:224)
at com.sun.xml.internal.ws.wsdl.PayloadQNameBasedOperationFinder.getWSDLOperationMapping(PayloadQNameBasedOperationFinder.java:143)
at com.sun.xml.internal.ws.wsdl.OperationDispatcher.getWSDLOperationMapping(OperationDispatcher.java:82)
at com.sun.xml.internal.ws.api.message.Packet.getWSDLOperationMapping(Packet.java:285)
at com.sun.xml.internal.ws.api.message.Message.getOperation(Message.java:284)
at com.sun.xml.internal.ws.api.message.Message.getOperation(Message.java:302)
at com.sun.xml.internal.ws.api.message.Message.isOneWay(Message.java:379)
...
I am working with java 1.8 and everything run on localhost. The wsdl seams to work fine.
I do not know what the problem could be. I tried a lot to solve the problem but I have not four no one thread that talk about this problem.
Please help.
EDIT:
Here the publisher:
private static final String mUrl = "http://localhost:" + Ports.MASTER_DATABASE_SERVICE + "/database";
public static void launch() throws Exception
{
System.out.println("----Starting on ..." + mUrl);
Endpoint.publish(mUrl, new Database());
System.out.println("----SOAP Service started!!!!");
}
Here the interface for the client part:
#WebService
#SOAPBinding(style = Style.RPC)
public interface DatabaseAPIsInterface
{
#WebMethod
public User isLoginOkay(String password, String email) throws Exception;
}
Here the implementation of the method isLoginOkay(...)
#Override
public User isLoginOkay(String password, String email) throws Exception
{
Database db = null;
try
{
db = fromConnectionPool();
Dao<User, Integer> dao = createPersonDAO(db);
throwIfSomeNull(password, email);
QueryBuilder<User, Integer> builder = dao.queryBuilder();
User user = builder.where().eq(User.FIELD_NAME_EMAIL, email).and().eq(User.FIELD_NAME_PASSWORD, password).queryForFirst();
if (user != null) return user;
else throw new FileNotFoundException("User does not exist");
} finally
{
close(db);
}
}
And here the client part:
String mUrl = "http://localhost:" + Ports.MASTER_DATABASE_SERVICE + "/database?wsdl";
URL url = new URL(mUrl);
QName qname = new QName("http://database.unitn.it/", "DatabaseService");
Service service = Service.create(url, qname);
DatabaseInterface database = service.getPort(new QName("http://database.unitn.it/", "DatabasePort"), DatabaseAPIsInterface.class);
User user = database.isLoginOkay(password, email);
System.out.println(user);

HTTP Request using Netty

I have just started netty and I am really disappointed with the documentation present on
their website.
I am trying to connect to an URL using Netty.. I took the time client example from their website and changed it as per my requirement..
Code :
public class NettyClient {
public static void main(String[] args) throws Exception {
String host = "myUrl.com/v1/parma?param1=value";
int port = 443;
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
#Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ClientHandler());
ch.pipeline().addLast("encoder", new HttpRequestEncoder());
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
}
But the problem is that that it expects only the url without the query parameters.. How can I pass query parameters with the URL?
and please provide me some link of a good documentation for Netty 4..
EDIT
Client code after referring the example mentioned in the answer :
URI uri = new URI("myUrl.com/v1/parma?param1=value");
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
String host = "myUrl.com";
int port = 443;
boolean ssl = "https".equalsIgnoreCase(scheme);
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new NettyClientInitializer(ssl));
// Make the connection attempt.
Channel ch = b.connect(host, port).sync().channel();
// Prepare the HTTP request.
HttpRequest request = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
request.headers().set(HttpHeaders.Names.HOST, host);
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
//request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
/*// Set some example cookies.
request.headers().set(
HttpHeaders.Names.COOKIE,
ClientCookieEncoder.encode(
new DefaultCookie("my-cookie", "foo"),
new DefaultCookie("another-cookie", "bar")));
*/
// Send the HTTP request.
ch.writeAndFlush(request);
// Wait for the server to close the connection.
ch.closeFuture().sync();
} finally {
// Shut down executor threads to exit.
group.shutdownGracefully();
}
handler code :
public class ClientHandler extends SimpleChannelInboundHandler<HttpObject> {
#Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
System.out.println("STATUS: " + response.getStatus());
System.out.println("VERSION: " + response.getProtocolVersion());
System.out.println();
if (!response.headers().isEmpty()) {
for (String name: response.headers().names()) {
for (String value: response.headers().getAll(name)) {
System.out.println("HEADER: " + name + " = " + value);
}
}
System.out.println();
}
if (HttpHeaders.isTransferEncodingChunked(response)) {
System.out.println("CHUNKED CONTENT {");
} else {
System.out.println("CONTENT {");
}
}
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
System.out.print(content.content().toString(CharsetUtil.UTF_8));
System.out.flush();
if (content instanceof LastHttpContent) {
System.out.println("} END OF CONTENT");
}
}
}
#Override
public void exceptionCaught(
ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
initializer code :
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {
private final boolean ssl;
public NettyClientInitializer(boolean ssl) {
this.ssl = ssl;
}
#Override
public void initChannel(SocketChannel ch) throws Exception {
// Create a default pipeline implementation.
ChannelPipeline p = ch.pipeline();
p.addLast("log", new LoggingHandler(LogLevel.INFO));
// Enable HTTPS if necessary.
/*
if (ssl) {
SSLEngine engine =
SecureChatSslContextFactory.getClientContext().createSSLEngine();
engine.setUseClientMode(true);
p.addLast("ssl", new SslHandler(engine));
}
*/
p.addLast("codec", new HttpClientCodec());
// Remove the following line if you don't want automatic content decompression.
// p.addLast("inflater", new HttpContentDecompressor());
// Uncomment the following line if you don't want to handle HttpChunks.
p.addLast("aggregator", new HttpObjectAggregator(1048576));
p.addLast("handler", new ClientHandler());
}
}
Your code only handles the low-level connection at the moment. Indeed at this level only the hostname and port can be used.
For the HTTP request You have to construct an HttpRequest object and send it over the channel. In this request object You define the query parameters and all such things.
There is a bunch of example code about HTTP client functionality on Netty website - have a a look!
In this example the problem lies with the constructor for the DefaultHttpRequest parameter of uri.getRawPath(). The invocation of this method does NOT return the query parameters. It works in this case as there were no query parameters in the Snoop example. By substituting uri.toASCIIString() returns the encoded uri complete with the query parameters. To prove this to yourself, rather than having a method invocation within a method invocation (a bad idea for just this reason, add the statement
String url = uri.getRawPath();
and look at the string url.
I had the exact same problem. I've done this natively in servlets for years but now was trying to do it in a Netty app.
Consequently the new code would be:
String path = uri.toASCIIString();
// Prepare the HTTP request.
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, path);
When you build the request, you need to add the query to the path. Instead of
uri.getRawPath()
use
uri.getRawPath() + "?" + uri.getRawQuery()

Categories

Resources